#pac_ana.h
#ifndef _PAC_ANA_H
#define _PAC_ANA_H
#ifdef _MSC_VER
/*
* we do not want the warnings about the old deprecated and unsecure CRT functions
* since these examples can be compiled under *nix as well
*/
#define _CRT_SECURE_NO_WARNINGS
#endif
/*set the environment head files*/
#define WIN32
#pragma comment (lib, "ws2_32.lib") //load ws2_32.dll
/*set the C++ head files*/
#include <iostream>
#include <stdio.h>
#include <map>
#include <string>
#include <iomanip>
#include <sstream>
/*set the wpcap head files*/
#include <pcap.h>
#include <WinSock2.h>
#define DIVISION "--------------------"
#define B_DIVISION "==================="
/* 4 bytes IP address */
typedef struct ip_v4_address ip_v4_address;
/* 16 bytes IP address */
typedef struct ip_v6_address ip_v6_address;
/*8 bytes MAC addresss*/
typedef struct mac_address mac_address;
/*ethernet header*/
typedef struct ethernet_header ethernet_header;
/* IPv4 header */
typedef struct ip_v4_header ip_v4_header;
/*IPv6 header*/
typedef struct ip_v6_header ip_v6_header;
/*arp header*/
typedef struct arp_header arp_header;
/*TCP header*/
typedef struct tcp_header tcp_header;
/* UDP header*/
typedef struct udp_header udp_header;
/*ICMP header*/
typedef struct icmp_header icmp_header;
/* prototype of the packet handler */
// 回调函数
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
/*analysis the ethernet packet*/
//void packet_handler(u_char* packets,const struct pcap_pkthdr *header,const u_char *data)
void ethernet_package_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
/*analysis the IPv4 packet*/
void ip_v4_package_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
/*analysis the IPv6 packet*/
void ip_v6_package_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
/*analysis the arp packet*/
void arp_package_handler(u_char* param, const struct pcap_pkthdr *header, const u_char *pkt_data);
/*analysis the udp packet*/
void udp_package_handler(u_char* param, const struct pcap_pkthdr *header, const u_char *pkt_data);
/*analysis the tcp packet*/
void tcp_package_handler(u_char* param, const struct pcap_pkthdr *header, const u_char *pkt_data);
/*analysis the icmp packet*/
void icmp_package_handler(u_char* param, const struct pcap_pkthdr *header, const u_char *pkt_data);
/*count the package with c++ std::map*/
void add_to_map(std::map<std::string, int> &counter, ip_v4_address ip);
void add_to_map(std::map<std::string, int> &counter, ip_v6_address ip);
/*print the map info*/
void print_map(std::map<std::string, int> counter);
class ethernet_package_handler;
class packet_handler;
class ip_v4_package_handler;
class tcp_package_handler;
#endif // !_PAC_ANA_H
#winpcap.cpp
#include "pac_ana.h"
#pragma comment(lib,"wpcap.lib")
#pragma comment(lib,"packet.lib")
#pragma comment(lib,"ws2_32.lib")
using namespace std;
/*ip counter*/
map<string, int> counter;
/*header structure*/
struct ip_v4_address
{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
};
struct ip_v6_address
{
u_short part1;
u_short part2;
u_short part3;
u_short part4;
u_short part5;
u_short part6;
u_short part7;
u_short part8;
};
struct mac_address
{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
u_char byte5;
u_char byte6;
};
struct ethernet_header
{
mac_address des_mac_addr;
mac_address src_mac_addr;
u_short type;
};
struct ip_v4_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 checksum; // Header checksum
ip_v4_address src_ip_addr; // Source address
ip_v4_address des_ip_addr; // Destination address
u_int op_pad; // Option + Padding
};
struct ip