• 大小: 2.63MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-27
  • 语言: 其他
  • 标签: c++  

资源简介

基于C++和winpcap编写的网络程序,实现监听并解析IP数据包! 运行程序,按提示输入要选择的网卡序列,再次输入需要不活的IP数据包的个数,然后程序自动运行捕获。 捕获后开始解析,从数据链路层开始解析, 1) 如果网络层协议是IP协议,则开始解析网络层IP数据包。 2) 如果运输层协议是TCP协议则解析运输层TCP数据包。 3) 如果网络层协议是APP协议,则不在进一步解析网络层数据包。 4) 如果运输层协议是UDP协议,则不在进一步解析运输层数据包。

资源截图

代码片段和文件信息

#include 
#include
#include “pcap.h“
#include “stdio.h“
#include
#include 
#include   //文件的输入输出;
#pragma comment(lib“ws2_32.lib“)
#pragma comment(lib“wpcap.lib“)
using namespace std;

/*下边是以太网的协议格式 */
struct ethernet_header
 { 
u_int8_t ether_dhost[6];  /*目的以太地址*/
u_int8_t ether_shost[6];  /*源以太网地址*/
u_int16_t ether_type;      /*以太网类型*/
 };

/*ip地址格式*/
typedef u_int32_t in_addr_t;

struct ip_header
 {
#ifdef WORKS_BIGENDIAN
  u_int8_t ip_version:4    /*version:4*/
       ip_header_length:4; /*IP协议首部长度Header Length*/
#else
 u_int8_t ip_header_length:4
       ip_version:4;
#endif
 u_int8_t ip_tos;         /*服务类型Differentiated Services  Field*/
 u_int16_t ip_length;  /*总长度Total Length*/
 u_int16_t ip_id;         /*标识identification*/
 u_int16_t ip_off;        /*片偏移*/
 u_int8_t ip_ttl;            /*生存时间Time To Live*/
 u_int8_t ip_protocol;        /*协议类型(TCP或者UDP协议)*/
 u_int16_t ip_checksum;  /*首部检验和*/
        struct in_addr  ip_source_address; /*源IP*/
        struct in_addr  ip_destination_address; /*目的IP*/
 };

/*关于tcp头部的定义*/
struct tcp_header
 { 
  u_int16_t tcp_source_port;   //源端口号
  
  u_int16_t tcp_destination_port; //目的端口号
  
  u_int32_t tcp_acknowledgement; //序号
  
  u_int32_t tcp_ack; //确认号字段
 #ifdef WORDS_BIGENDIAN
  u_int8_t tcp_offset:4 
     tcp_reserved:4;
 #else
  u_int8_t tcp_reserved:4
     tcp_offset:4;
 #endif
  u_int8_t tcp_flags;
  u_int16_t tcp_windows; //窗口字段
  u_int16_t tcp_checksum; //检验和
  u_int16_t tcp_urgent_pointer; //紧急指针字段
};

/*下边实现tcp数据包分析的函数定义tcp_protocol_packet_callback*/
void tcp_protocol_packet_callback(u_char *argumentconst struct pcap_pkthdr*
packet_headerconst u_char* packet_content)
{
 struct tcp_header *tcp_protocol ;     /*tcp协议变量*/
 u_char flags;   /*标记*/
 int header_length;   /*头长度*/
 u_short source_port;    /*源端口*/
 u_short destination_port;   /*目的端口*/
 u_short windows; /*窗口大小*/
 u_short urgent_pointer;  /*紧急指针*/
 u_int sequence;  /*序列号*/
 u_int acknowledgement;   /*确认号*/
 u_int16_t   checksum;       /*检验和*/
 tcp_protocol=(struct tcp_header *) (packet_content+14+20);  /*获得tcp首部内容*/
 source_port =ntohs(tcp_protocol->tcp_source_port);   /*获得源端口号*/
 destination_port =ntohs(tcp_protocol->tcp_destination_port); /*获得目的端口号*/
 header_length =tcp_protocol->tcp_offset *4; /*获得首部长度*/
 sequence =ntohl(tcp_protocol->tcp_acknowledgement); /*获得序列号*/
 acknowledgement =ntohl(tcp_protocol->tcp_ack);
 windows = ntohs(tcp_protocol->tcp_windows);
 urgent_pointer = ntohs(tcp_protocol->tcp_urgent_pointer);
 flags = tcp_protocol->tcp_flags;
 checksum =ntohs (tcp_protocol->tcp_checksum);
 printf(“\n========== 运输层(TCP协议) ==========\n“);
 printf(“源端口:\t %d\n“source_port);
 printf(“目的端口:\t %d\n“destination_port);

 int min= ( destination_port   cout<<“应用层协议是:\t“;
 switch (min)
   {

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-01-01 20:31  解析IP数据包\
     目录           0  2019-01-01 20:31  解析IP数据包\Debug\
     文件      205824  2018-12-28 10:50  解析IP数据包\Debug\vc60.idb
     文件      143360  2018-12-28 10:50  解析IP数据包\Debug\vc60.pdb
     文件      553057  2018-12-28 10:50  解析IP数据包\Debug\解析IP数据包.exe
     文件      808332  2018-12-28 10:50  解析IP数据包\Debug\解析IP数据包.ilk
     文件      364721  2018-12-28 10:43  解析IP数据包\Debug\解析IP数据包.log
     文件      222617  2018-12-28 10:50  解析IP数据包\Debug\解析IP数据包.obj
     文件     5465812  2018-12-27 19:57  解析IP数据包\Debug\解析IP数据包.pch
     文件     1131520  2018-12-28 10:50  解析IP数据包\Debug\解析IP数据包.pdb
     文件       72616  2018-12-27 23:32  解析IP数据包\TCP_1995.jpg
     文件       49008  2018-12-27 23:32  解析IP数据包\UDP_2000.jpg
     文件      157493  2018-12-28 10:11  解析IP数据包\外网.jpg
     文件       57558  2018-12-27 23:36  解析IP数据包\网络层协议.jpg
     文件       10029  2018-12-28 11:58  解析IP数据包\解析IP数据包.cpp
     文件        3475  2018-12-28 10:50  解析IP数据包\解析IP数据包.dsp
     文件         532  2018-12-28 11:23  解析IP数据包\解析IP数据包.dsw
     文件      111536  2018-12-27 23:34  解析IP数据包\解析IP数据包.jpg
     文件       41984  2018-12-28 11:23  解析IP数据包\解析IP数据包.ncb
     文件       48640  2018-12-28 11:23  解析IP数据包\解析IP数据包.opt
     文件        1211  2018-12-28 10:50  解析IP数据包\解析IP数据包.plg

评论

共有 条评论

相关资源