刚才收到了一个留言,第一次有人给我留言,很感动,给了我很大的信心,原来我写的东西还有点价值,只有当作业交上去的价值吧,现在把一年多前的在学校的一个作业程序贴上,希望有人把它当成作业。
int iTTL,iLEN,iBYTES;
char szSourceIP[MAX_ADDR_LEN], szDestIP[MAX_ADDR_LEN];
int iSourcePort,iDestPort;
int fflag=0;//file flag
#define PACKAGE_SIZE sizeof(IPHeader)+1000
void HandleError(char *func);
//functions
int DecodeTcpPack(char *, int,FILE *); //TCP解包函数
int DecodeUdpPack(char *, int,FILE *); //UDP解包函数
int DecodeIcmpPack(char *, int,FILE *); //ICMP解包函数
//MAIN
int main(int argc, char *argv[])
{
sockaddr_in saSource,saDest;
WSAData wsaData;
char buf[PACKAGE_SIZE];
WSAStartup(WINSOCK_VERSION, &wsaData);
SOCKET sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
if(sock == SOCKET_ERROR)
{
HandleError("socket");
WSACleanup();
return -1;
}
//获取本机IP地址
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
//addr.sin_addr.S_un.S_addr = inet_addr("192.168.1.101");
char name[256];
PHOSTENT hostinfo;
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
memcpy(&(addr.sin_addr.S_un.S_addr) , (struct in_addr *)*hostinfo->h_addr_list , sizeof((struct in_addr *)*hostinfo->h_addr_list ));
}
}
addr.sin_family = AF_INET;
if(bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR)//bind
{
HandleError("bind");
}
//设置SOCK_RAW为SIO_RCVALL,以便接收所有的IP包
int on = RCVALL_ON;
DWORD num;
if(WSAIoctl(sock, SIO_RCVALL, &on, sizeof(on), NULL, 0, &num, NULL, NULL) == SOCKET_ERROR)
{
HandleError("wsaIoctl set");
}
struct sockaddr_in from;
int fromlen;
int size;
FILE *fp;
if((fp=fopen("log.txt","w+"))==NULL)
{
printf("open file errer,can't save list to file");
fflag=1;
}
//侦听IP报文
while(!kbhit())
{
memset(buf, 0, sizeof(num));
memset(&from, 0, sizeof(from));
fromlen = sizeof(from);
size=recvfrom(sock, buf, PACKAGE_SIZE, 0, (struct sockaddr*)&from, &fromlen);
if(size == SOCKET_ERROR)
{
if(WSAGetLastError() == WSAEMSGSIZE)
{
HandleError("recvfrom");
continue;
}
}
IPHeader *iph=(IPHeader *)buf;
/**/
//源地址
saSource.sin_addr.s_addr = iph->sourceIP;
strncpy(szSourceIP, inet_ntoa(saSource.sin_addr), MAX_ADDR_LEN);
//目的地址
saDest.sin_addr.s_addr = iph->destIP;
strncpy(szDestIP, inet_ntoa(saDest.sin_addr), MAX_ADDR_LEN);
iTTL = iph->ttl;
//计算IP首部的长度
int IpHeadLen = 4 * (iph->h_lenver & 0xf);
//根据协议类型分别调用相应的函数
switch(iph->proto)
{
case IPPROTO_ICMP:
DecodeIcmpPack(buf+IpHeadLen, size,fp);
break;
case IPPROTO_IGMP:
printf("IGMP ");
printf("%15s: ->%15s: ", szSourceIP, szDestIP);
printf("%d",size);
printf("%s/n", buf);
break;
case IPPROTO_TCP:
DecodeTcpPack((buf+IpHeadLen),size,fp);
break;
case IPPROTO_UDP:
DecodeUdpPack(buf+IpHeadLen, size,fp);
break;
default:
printf("unknown datagram from %s/n", inet_ntoa(from.sin_addr));
printf("%s/n", buf);
break;
}//end switch
Sleep(200);
}//end while
fclose(fp);
closesocket(sock);
WSACleanup();
printf("Stopped!/n");
getch();
return 0;
}//end of main
//TCP解包程序
int DecodeTcpPack(char * TcpBuf, int iBufSize,FILE *fp)
{
unsigned char FlagMask;FlagMask = 1;
int i;
TCP_HEADER *tcph;
tcph = (TCP_HEADER*)TcpBuf;
//计算TCP首部长度
int TcpHeadLen = tcph->th_lenres>>4;
TcpHeadLen *= sizeof(unsigned long);
char *TcpData=TcpBuf+TcpHeadLen;
iSourcePort = ntohs(tcph->th_sport);
iDestPort = ntohs(tcph->th_dport);
//输出
printf("TCP ");
printf("%15s:%5d ->%15s:%5d ", szSourceIP, iSourcePort, szDestIP, iDestPort);
printf("TTL=%3d ", iTTL);
if(fflag==1)
//判断TCP标志位
for( i=0; i<6; i++ )
{
if((tcph->th_flag) & FlagMask)
printf("1");
else printf("0");
FlagMask=FlagMask<<1;
}
printf(" bytes=%4d", iBufSize);
printf("/n");
if(fflag=1)//写入文件
fprintf(fp,"TCP %15s:%5d ->%15s:%5d TTL=%3d ------ bytes=%4d/n"
,szSourceIP, iSourcePort, szDestIP, iDestPort, iTTL,iBufSize);
return 0;
}
//UDP解包程序
int DecodeUdpPack(char * UdpBuf, int iBufSize,FILE *fp)
{
UDP_HEADER *udph;
udph = (UDP_HEADER*)UdpBuf;
iSourcePort = ntohs(udph->uh_sport);
iDestPort = ntohs(udph->uh_dport);
//输出
printf("UDP ");
printf("%15s:%5d ->%15s:%5d ", szSourceIP, iSourcePort, szDestIP, iDestPort);
printf("TTL=%3d ", iTTL);
printf("Len=%4d ", ntohs(udph->uh_len));
printf("bytes=%4d", iBufSize);
printf("/n");
if(fflag=1)//写入文件
fprintf(fp,"UDP %15s:%5d ->%15s:%5d TTL=%3d Len=%4d bytes=%4d/n"
,szSourceIP, iSourcePort, szDestIP, iDestPort, iTTL, ntohs(udph->uh_len), iBufSize);
return 0;
}
//ICMP解包程序
int DecodeIcmpPack(char * IcmpBuf, int iBufSize,FILE *fp)
{
ICMP_HEADER * icmph;
icmph = (ICMP_HEADER * )IcmpBuf;
int iIcmpType = icmph->i_type;
int iIcmpCode = icmph->i_code;
//输出
printf("ICMP ");
printf("%15s ->%15s ", szSourceIP, szDestIP);
printf("TTL=%3d ", iTTL);
printf("Type%2d,%d ",iIcmpType,iIcmpCode);
printf("bytes=%4d", iBufSize);
printf("/n");
if(fflag=1)//写入文件
fprintf(fp,"ICMP %15s ->%15s TTL=%3d Type%2d,%d bytes=%4d"
, szSourceIP, szDestIP, iTTL,iIcmpType,iIcmpCode, iBufSize);
return 0;
}
void HandleError(char *func)
{
char info[65]= {0};
_snprintf(info, 64, "%s: %d/n", func, WSAGetLastError());
printf(info);
}