//////////////////////////////////////////////////////////////////////////
// //
// ipdump for win2k by shotgun //
// //
// released: [2001.4] //
// author: [shotgun] //
// homepage: //
// [http://it.xici.net] //
// [http://www.patching.net] //
// //
//////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#include <mstcpip.h>
#define status_failed 0xffff //定义异常出错代码
#define max_pack_len 65535 //接收的最大ip报文
#define max_addr_len 16 //点分十进制地址的最大长度
#define max_proto_text_len 16 //子协议名称(如"tcp")最大长度
#define max_proto_num 12 //子协议数量
#define max_hostname_lan 255 //最大主机名长度
#define cmd_param_help true
typedef struct _iphdr
{
unsigned char h_lenver; //4位首部长度+4位ip版本号
unsigned char tos; //8位服务类型tos
unsigned short total_len; //16位总长度(字节)
unsigned short ident; //16位标识
unsigned short frag_and_flags; //3位标志位
unsigned char ttl; //8位生存时间 ttl
unsigned char proto; //8位协议 (tcp, udp 或其他)
unsigned short checksum; //16位ip首部校验和
unsigned int sourceip; //32位源ip地址
unsigned int destip; //32位目的ip地址
}ip_header;
typedef struct _tcphdr //定义tcp首部
{
ushort th_sport; //16位源端口
ushort th_dport; //16位目的端口
unsigned int th_seq; //32位序列号
unsigned int th_ack; //32位确认号
unsigned char th_lenres; //4位首部长度/6位保留字
unsigned char th_flag; //6位标志位
ushort th_win; //16位窗口大小
ushort th_sum; //16位校验和
ushort th_urp; //16位紧急数据偏移量
}tcp_header;
typedef struct _udphdr //定义udp首部
{
unsigned short uh_sport; //16位源端口
unsigned short uh_dport; //16位目的端口
unsigned short uh_len; //16位长度
unsigned short uh_sum; //16位校验和
} udp_header;
typedef struct _icmphdr //定义icmp首部
{
byte i_type; //8位类型
byte i_code; //8位代码
ushort i_cksum; //16位校验和
ushort i_id; //识别号(一般用进程号作为识别号)
ushort i_seq; //报文序列号
ulong timestamp; //时间戳
}icmp_header;
typedef struct _protomap //定义子协议映射表
{
int protonum;
char prototext[max_proto_text_len];
}protomap;
protomap protomap[max_proto_num]={ //为子协议映射表赋值
{ ipproto_ip , "ip " },
{ ipproto_icmp , "icmp" },
{ ipproto_igmp , "igmp" },
{ ipproto_ggp , "ggp " },
{ ipproto_tcp , "tcp " },
{ ipproto_pup , "pup " },
{ ipproto_udp , "udp " },
{ ipproto_idp , "idp " },
{ ipproto_nd , "np " },
{ ipproto_raw , "raw " },
{ ipproto_max , "max " },
{ null , "" } };
socket sockraw;
char tcpflag[6]={'f','s','r','p','a','u'}; //定义tcp标志位
bool paramtcp =false; // -t关注tcp 报文
bool paramudp =false; // -u关注udp 报文
bool paramicmp =false; // -i关注icmp报文
bool paramdecode=true; // -d对协议进行解码
char *strfromipfilter=null; // 源ip地址过滤
char *strdestipfilter=null; // 目的地址过滤
int decodeippack(char *,int);
int decodetcppack(char *);
int decodeudppack(char *);
int decodeicmppack(char *);
void checksockerror(int,char*);
char * checkprotocol(int);
void usage(void);
bool getcmdline(int, char **);
void main(int argc, char ** argv)
{
int ierrorcode;
char recvbuf[max_pack_len] = {0};
usage();
if(getcmdline(argc, argv)==cmd_param_help) exit(0);
//初始化socket
wsadata wsadata;
ierrorcode = wsastartup(makeword(2,1),&wsadata);
checksockerror(ierrorcode, "wsastartup");
sockraw = socket(af_inet , sock_raw , ipproto_ip);
checksockerror(sockraw, "socket");
//获取本机ip地址
char far name[max_hostname_lan];
ierrorcode = gethostname(name, max_hostname_lan);
checksockerror(ierrorcode, "gethostname");
struct hostent far * phostent;
phostent = (struct hostent * )malloc(sizeof(struct hostent));
phostent = gethostbyname(name);
sockaddr_in sa;
sa.sin_family = af_inet;
sa.sin_port = htons(6000);
memcpy(&sa.sin_addr.s_un.s_addr, phostent->h_addr_list[0], phostent->h_length);
ierrorcode = bind(sockraw, (psockaddr)&sa, sizeof(sa));
checksockerror(ierrorcode, "bind");
//设置sock_raw为sio_rcvall,以便接收所有的ip包
dword dwbufferlen[10] ;
dword dwbufferinlen = 1 ;
dword dwbytesreturned = 0 ;
ierrorcode=wsaioctl(sockraw, sio_rcvall,&dwbufferinlen, sizeof(dwbufferinlen),
&dwbufferlen, sizeof(dwbufferlen),&dwbytesreturned , null , null );
checksockerror(ierrorcode, "ioctl");
//侦听ip报文
while(1)
{
memset(recvbuf, 0, sizeof(recvbuf));
ierrorcode = recv(sockraw, recvbuf, sizeof(recvbuf), 0);
checksockerror(ierrorcode, "recv");
ierrorcode = decodeippack(recvbuf, ierrorcode);
checksockerror(ierrorcode, "decode");
}
}
//ip解包程序
int decodeippack(char *buf, int ibufsize)
{
ip_header *pipheader;
int iprotocol, ittl;
char szprotocol[max_proto_text_len];
char szsourceip[max_addr_len], szdestip[max_addr_len];