资源描述:
《用c 实现网卡侦听》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、物联网工程学院实验报告课程名称《计算机网络》实验名称网卡侦听实验日期2012-5-1班级计科姓名学号0304实验报告要求1.实验名称2.实验要求3.实验环境4.实验步骤(写明实验结果)5.实验体会实验2编程实现网卡侦听程序代码:#include#include#include#include"mstcpip.h"#pragmacomment(lib,"ws2_32.lib")#defineSTATUS_FAILED0xFFFF//定义异常出错代码#defineM
2、AX_PACK_LEN65535//接收的最大IP报文#defineMAX_ADDR_LEN16//点分十进制地址的最大长度#defineMAX_PROTO_TEXT_LEN16//子协议名称(如"TCP")最大长度#defineMAX_PROTO_NUM12//子协议数量#defineMAX_HOSTNAME_LAN255//最大主机名长度#defineCMD_PARAM_HELPtruetypedefstruct_iphdr//定义IP头部{unsignedcharh_lenver;//4位首部长度+4位IP版本号un
3、signedchartos;//8位服务类型TOSunsignedshorttotal_len;//16位总长度(字节)unsignedshortident;//16位标识unsignedshortfrag_and_flags;//3位标志位unsignedcharttl;//8位生存时间TTLunsignedcharproto;//8位协议(TCP,UDP或其他)unsignedshortchecksum;//16位IP首部校验和unsignedintsourceIP;//32位源IP地址unsignedintdestI
4、P;//32位目的IP地址}IP_HEADER;typedefstruct_tcphdr//定义TCP首部{USHORTth_sport;//16位源端口USHORTth_dport;//16位目的端口unsignedintth_seq;//32位序列号unsignedintth_ack;//32位确认号unsignedcharth_lenres;//4位首部长度/6位保留字unsignedcharth_flag;//6位标志位USHORTth_win;//16位窗口大小USHORTth_sum;//16位校验和USHOR
5、Tth_urp;//16位紧急数据偏移量}TCP_HEADER;typedefstruct_udphdr//定义UDP首部{unsignedshortuh_sport;//16位源端口unsignedshortuh_dport;//16位目的端口unsignedshortuh_len;//16位长度unsignedshortun_sum;//16位校验和}UDP_HEADER;typedefstruct_icmphdr//定义ICMP首部{BYTEi_type;//8位类型BYTEi_code;//8位代码USHORTi_
6、cksum;//16位校验和USHORTi_id;//识别号(一般用进程号作为识别号)USHORTi_seq;//报文序列号ULONGtimestamp;//时间戳}ICMP_HEADER;typedefstruct_protomap//定义子协议映射表{intProtoNum;charProtoText[MAX_PROTO_TEXT_LEN];}PROTOMAP;PROTOMAPProtoMap[MAX_PROTO_NUM]={//为子协议映射表赋值{IPPROTO_IP,"IP"},{IPPROTO_ICMP,"ICM
7、P"},{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,""}};SOCKETSockRaw;charTcpFlag[6]={'F','S','R','P','A','U'};//定义TCP标志位bool
8、ParamTcp=false;//关注TCP报文boolParamUdp=false;//关注UDP报文boolParamIcmp=false;//关注ICMP报文boolParamDecode=true;//对协议进行解码char*strFromIpFilter=NULL;//源IP地址过滤char*