http://gamedev.uhome.net/GUNiffer.zip
2000下使用,shotgun写的
2000下使用,shotgun写的
解决方案 »
- VS2010不能使用Skin++吗?
- listview中checkbox的问题,求助(SDK)
- 多个协议的套接字可以绑定到相同端口吗
- 有关Tabcontrol的一个疑问
- 类的成员变量没法在定义的时候初始化吗?
- 请教这样的界面是怎么实现的?
- 谁知道Combox的右面向下按钮的点击事件
- 求助阿,哪里可以找到中英文对照的资料,最好是数据库开发方面的DAO的,大家帮帮忙啊!!!
- 初学者问题:用odbc连接&打开oracle数据库问题。
- 如何获取对方的MAC地址呢?
- 大哥们,帮帮忙吧!!谁有bmp向矢量图转化的资料,能不能与小弟一起分享,忙着写毕业论文急呀!!!
- Access中新建表时,导入表的功能怎样在vc中实现?
注意:不要做坏事喔!
你有好东东的话就拿出来给大家分享嘛! 大家说是不是?
哦,对了,http://netgroutp-serve.polito.it 我也上不了!
我认为要使丢包率降低,至少应该是多服务器模式,如果是在lunux上,呵呵,那就更有文章可作了。事实上我就是那么做的。至于程序,sorry,我不能给上,请谅解。
我认为要使丢包率降低,至少应该是多服务器模式,如果是在linux上,呵呵,那就更有文章可作了。事实上我就是那么做的。至于程序,sorry,我不能给上,请谅解[email protected] :)
//sniffer.c Power by flying#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/ether.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <linux/if.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <bits/sockaddr.h>
#include <netdb.h>
#include <string.h>
/*****************Define global marocs************************/
#define SIG_EXIT 23
#define MAXSIZE 8000
#define SCKERROR -1
#define GETIOSTERROR -2
#define SETIOSTERROR -3
#define TCPFIN "[==FIN==]"
#define TCPSYN "[==SYN==]"
#define TCPRST "[==RST==]"
#define TCPACK "[==ACK==]"//Buffer that store data from socket
typedef struct DataPacket {
struct ethhdr eth;
struct iphdr ip;
struct tcphdr tcp;
char DataBuffer[MAXSIZE];
} DataBuf;//Global values
DataBuf EthDataPck;
struct iphdr * ip;
struct tcphdr * tcp;
int SnifferSocket;
//log files
FILE * pid_log;
FILE * http_log;
FILE * pop_log;
FILE * smtp_log;
FILE * ftp_log;struct ifreq old_status;
int IsWriteData;
int IsWriteHead;//Functions here
int InitSocket(char *);
int ResetDev(void);
void DoSnif(int);
void DoRecordBuf(void);
int BeginSniffer(void);
void ShowUsageMsg(void);
void StartUp(void);
void ShutDown(void);
void WritePidToLog(int);
int ReadPidFromLog(void);
void DoExit(void);
void OpenLogFiles(void);
void CloseLogFiles(void);
void WriteDataToFile(FILE *);
void WriteHeadToFile(FILE *);
char * GetHostName(unsigned long int);/***********Write information head to correct log file************/
void WriteHeadToFile(FILE * f) {
if (IsWriteHead == 0)
return;
IsWriteHead = 0;
fprintf(f,"From : %s\t",GetHostName(ip->saddr));
fprintf(f,"To : %s\nPort : [%d]\n",GetHostName(ip->daddr),
ntohs(tcp->dest));
}
/***********Write information body to correct log file************/
void WriteDataToFile(FILE * f) {
char * msg;
int loop,len;
if (IsWriteData == 0) {
return;
}
IsWriteData = 0;
msg = EthDataPck.DataBuffer - 2;
if (ntohs(tcp->dest) == 25) {
len = htons(ip->tot_len)-sizeof(EthDataPck.
ip)-sizeof(EthDataPck.tcp);
} else if (ntohs(tcp->dest) == 80) {
len = strlen(msg);
} else if (ntohs(tcp->dest) == 110) {
len = htons(ip->tot_len)-sizeof(EthDataPck.
ip)-sizeof(EthDataPck.tcp);
} else if (ntohs(tcp->dest) == 21) {
len = htons(ip->tot_len)-sizeof(EthDataPck.
ip)-sizeof(EthDataPck.tcp);
} else {
return;
}
for (loop = 0; loop < len; loop++) {
if (msg[loop] == 13) {
fprintf(f,"\n");
} else {
fprintf(f,"%c",msg[loop]);
}
}
fprintf(f,"\n");
}//Init socket and device status
int InitSocket(char * dev) {
struct ifreq ifr;
int s;
int sck;
sck = socket(AF_INET,SOCK_PACKET,htons(0x800));
if (sck < 0) {
return SCKERROR;
}
strcpy(ifr.ifr_name,dev);
s = ioctl(sck,SIOCGIFFLAGS,&ifr);
if (s < 0) {
close(sck);
return GETIOSTERROR;
}
//remember what status it used to be
memcpy(&old_status,&ifr,sizeof(struct ifreq));
ifr.ifr_flags |= IFF_PROMISC;
s = ioctl(sck,SIOCSIFFLAGS,&ifr);
if (s < 0) {
close(sck);
return SETIOSTERROR;
}
return sck;
}/**************Begin to get data from device******************/
void DoSnif(int sck) {
int datasize;
while (1) {
datasize = read(sck,(struct etherpacket *)&EthDataPck,
sizeof(DataBuf));
if (datasize > 1) {
DoRecordBuf();
}
}
}/************Do with data and log correct
information*****************/
void DoRecordBuf() {
int tcpport;
FILE * f;
struct in_addr inaddr;
char strIP[11];
if (ip->protocol != 6)
return;
tcpport = ntohs(tcp->dest);
switch (tcpport) {
case 110:
f = pop_log;
break;
case 25:
f = smtp_log;
break;
case 80:
f = http_log;
break;
case 21:
f = ftp_log;
break;
default:
return;
break;
}
if (tcp->fin == 1) {
fprintf(f,"Status : %s\n",TCPFIN);
IsWriteData = 0;
IsWriteHead = 1;
} else if (tcp->fin == 0) {
if (tcp->rst == 1) {
IsWriteHead = 1;
if (tcp->syn == 0) {
fprintf(f,"Status : %s\n",TCPRST);
IsWriteData = 0;
} else {
if (tcp->ack == 0) {
fprintf(f,"Status : %s\t%s\n",TCPRST,TCPSYN);
IsWriteData = 1;
} else {
fprintf(f,"Status : %s\t%s\t%s\n",TCPRST,TCPSYN,
TCPACK);
IsWriteData = 0;
}
}
} else if (tcp->rst == 0) {
if (tcp->syn == 1) {
IsWriteHead = 1;
if (tcp->ack == 0) {
fprintf(f,"Status : %s\n",TCPSYN);
IsWriteData = 1;
} else {
fprintf(f,"Status : %s\t%s\n",TCPSYN,TCPACK);
IsWriteData = 0;
}
}
}
}
inaddr.s_addr = ip->saddr;
strncpy(strIP,inet_ntoa(inaddr),10);
strIP[10] = '\0';
if (!strcmp(strIP,"192.168.1.")) {
if (IsWriteHead == 1) {
WriteHeadToFile(f);
}
if (IsWriteData == 1) {
WriteDataToFile(f);
}
}
}/****************Tell others how to use this
software******************/
void ShowUsageMsg() {
printf("FSnif usage:{start|stop}\n");
exit(0);
}/****************Begin to do with sniffer************************/
int BeginSniffer() {
int pid;
setpgrp();
pid = getpid();
WritePidToLog(pid);
signal(SIG_EXIT,DoExit);
ip = (struct iphdr *)(((unsigned long)&EthDataPck.ip)-2);
tcp = (struct tcphdr *)(((unsigned long)&EthDataPck.tcp)-2);
IsWriteData = 0;
IsWriteHead = 0;
OpenLogFiles();
SnifferSocket = InitSocket("eth0");
DoSnif(SnifferSocket);
return 0;
}/*************If the user decide to start it,do this**************/
void StartUp() {
int pid;
pid = fork();
switch (pid) {
case 0:
BeginSniffer();
break;
case -1:
printf("Cannot fork,please retry!\n");
exit(0);
break;
default:
exit(0);
break;
}
}/**************Save the process ID to file************/
void WritePidToLog(int pid) {
pid_log = fopen("pid/pid","w");
fprintf(pid_log,"%d",pid);
fclose(pid_log);
}
int ReadPidFromLog() {
int pid,log,len;
char chrpid[10];
log = open("pid/pid",O_RDONLY);
len = read(log,chrpid,10);
chrpid[len] = 0;
pid = atoi(chrpid);
close(pid_log);
return pid;
}/*****************Do this so that the sniffer can stop
working********/
void ShutDown() {
int pid;
pid = ReadPidFromLog();
kill(pid,SIG_EXIT);
}
void DoExit() {
while (!(ResetDev() == 0));
close(SnifferSocket);
CloseLogFiles();
exit(0);
}
int ResetDev() {
int s;
s = ioctl(SnifferSocket,SIOCSIFFLAGS,&old_status);
if (s < 0)
return (-1);
}
void OpenLogFiles() {
http_log = fopen("log/http.log","at");
smtp_log = fopen("log/smtp.log","at");
pop_log = fopen("log/pop.log","at");
ftp_log = fopen("log/ftp.log","at");
}
void CloseLogFiles() {
fclose(http_log);
fclose(smtp_log);
fclose(pop_log);
fclose(ftp_log);
}
char * GetHostName(unsigned long int in) {
static char hostname[1024];
struct in_addr i;
struct hostent * remotehost;
i.s_addr=in;
remotehost = gethostbyaddr((char *)&i, sizeof(struct in_addr),
AF_INET);
if(remotehost == NULL)
strcpy(hostname, inet_ntoa(i));
else
strcpy(hostname, remotehost->h_name);
return hostname;
}
int main(int argc,char ** argv) {
switch (argc) {
case 1:
ShowUsageMsg();
break;
case 2:
if (strcmp(argv[1],"start") == 0) {
StartUp();
} else if (strcmp(argv[1],"stop") == 0) {
ShutDown();
} else {
ShowUsageMsg();
}
break;
default:
ShowUsageMsg();
break;
}
}