⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ipaddrip.h

📁 可以一键自动检测局域网内的网关和未被占用的IP地址
💻 H
字号:
#include "pcap.h"
#include <vector>

using namespace std;

/////////////////////////////////////////////////////////////////////////////
//
#ifndef ipAddrIp
#define ipAddrIp

/* 4 bytes IP address */
typedef struct ip_address{
    u_char byte1;
    u_char byte2;
    u_char byte3;
    u_char byte4;

	ip_address(){
		byte1 = 0;
		byte2 = 0;
		byte3 = 0;
		byte4 = 0;
	}

	ip_address(const ip_address& add)
	{
		byte1 = add.byte1;
		byte2 = add.byte2;
		byte3 = add.byte3;
		byte4 = add.byte4;
	}
}ip_address;

typedef struct mac{
	u_char b1;
	u_char b2;
	u_char b3;
	u_char b4;
	u_char b5;
	u_char b6;
} mac;

/* Ethernet Packet Header */
typedef struct ethernet_header{
	UCHAR eth_dest[6]; // Destination address 
	UCHAR eth_src[6]; // Source address
	union { 
		USHORT eth_len; // 802.3 length field. 
		USHORT eth_type; // Ethernet type field. 
	};
}ethernet_header; 

/* IPv4 header */
typedef struct ip_header{
    u_char  ver_ihl;        // Version (4 bits) + Internet header length (4 bits)
    u_char  tos;            // Type of service 
    u_short tlen;           // Total length 
    u_short identification; // Identification
    u_short flags_fo;       // Flags (3 bits) + Fragment offset (13 bits)
    u_char  ttl;            // Time to live
    u_char  proto;          // Protocol
    u_short crc;            // Header checksum
    ip_address  saddr;      // Source address
    ip_address  daddr;      // Destination address
    u_int   op_pad;         // Option + Padding
}ip_header;

/* TCP header */ 
typedef struct tcp_header{
	USHORT tcph_src; // Source Port Number 
	USHORT tcph_dest; // Destination Port Number 
	ULONG tcph_seq; // Sequence Number 
	ULONG tcph_ack_seq; // Acknowlegdement Number 
	USHORT tcph_flags; // Flags 
	USHORT tcph_window; // Window 
	USHORT tcph_check; // Checksum 
	USHORT tcph_urgent; // Urgent pointer 
	UCHAR GetDataOffset() 
	{ 
		USHORT nLen=(tcph_flags & 0x00f0);// 例程中无此处理,造成标志字段高 4 位为全零(如 //tcph_flags=0x0270 )时, TCP 包头长度计算出错。 // 虽标志字段高 4 位为全零在正常收发封包过程中不 // 会出现,但在通过“三次握手”建立 TCP 连接和结 // 束 TCP 连接过程中,必然出现高 4 位为全零情况, // 进而造成在此过程中 TCP 包头长度计算错。一但 //TCP 包头长度计算错,可能引发进一步的异常。 r
		return (nLen >> 4)*4; 
	}
}tcp_header;

/* UDP header */ 
typedef struct udp_header {
	u_short sport;   /* Source port */
	u_short dport;   /* Destination port */
	u_short len;     /* Datagram length */
	u_short crc;     /* Checksum */
}udp_header;


// ARP Frame
typedef struct arp_frame                      
{
           unsigned short      HW_Type;            /* hardware address */
           unsigned short      Prot_Type;              /* protocol address */
           unsigned char       HW_Addr_Len;        /* length of hardware address */
           unsigned char       Prot_Addr_Len;          /* length of protocol address */
           unsigned short      Opcode;                 /* ARP/RARP */
           unsigned char       Send_HW_Addr[6];      /* sender hardware address */
           unsigned long       Send_Prot_Addr;       /* sender protocol address */
           unsigned char       Targ_HW_Addr[6];      /* target hardware address */
           unsigned long       Targ_Prot_Addr;       /* target protocol address */
     //      unsigned char       padding[18];
} arp_frame;

typedef struct ARPPacket                
{
     ethernet_header	ethernetHeader;
     arp_frame			arpFrame;
} ARPPacket;

/* Prototype of the pachet handler */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
/*the common variables*/
ip_address srcIP; 
ip_address dstIP; 
vector<ip_address> repIP_List;
vector<mac> repMAC_List;

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -