网络嗅探器设计(三)

用到的libnet中定义的协议首部,以及类型。
按照posix标准,一般整形对应的*_t类型为:
1字节     uint8_t
2字节     uint16_t
4字节     uint32_t
8字节     uint64_t

转换:
ntohs       ==> 16位网络转主机
htons       ==> 是将整型变量从主机字节顺序转变成网络字节顺序
inet_ntoa   ==> 返回点分十进制的字符串在静态内存中的指针


libnet.h中封装好的首部

struct libnet_ethernet_hdr                      //以太网层  6+6+2 = 14
{
    uint8_t  ether_dhost[ETHER_ADDR_LEN];
    uint8_t  ether_shost[ETHER_ADDR_LEN];
    uint16_t ether_type;                 
};

struct libnet_ipv4_hdr                          //IP层  20
{
    uint8_t ip_hl:4,ip_v:4;          /* header length and version */
    uint8_t ip_tos;                  /* type of service */
    uint16_t ip_len;                 /* total length */
    uint16_t ip_id;                  /* identification */
    uint16_t ip_off;
    uint8_t ip_ttl;                  /* time to live */
    uint8_t ip_p;                    /* protocol */
    uint16_t ip_sum;                 /* checksum */
    struct in_addr ip_src, ip_dst;   /* source and dest address */
    //struct in_addr表示一个32位的IPv4地址
};


struct libnet_tcp_hdr                                   //TCP 20
{
    uint16_t th_sport;                  /* source port */
    uint16_t th_dport;                  /* destination port */
    uint32_t th_seq;                    /* sequence number */
    uint32_t th_ack;                    /* acknowledgement number */
    uint8_t  th_x2:4,th_off:4;          /* data offset */
    uint8_t  th_flags;                  /* control flags */
    uint16_t th_win;                    /* window */
    uint16_t th_sum;                    /* checksum */
    uint16_t th_urp;                    /* urgent pointer */
};


struct libnet_icmpv6_hdr                //一共8字节
{
    uint8_t icmp_type;       /* ICMP type */
    uint16_t icmp_sum;       /* ICMP Checksum */
    uint8_t icmp_code;       /* 
    uint16_t id;             /* ICMP id (unused, for backwards compatibility) */
    uint16_t seq;            /* ICMP sequence number (unused, for backwards compatibility) */
};


struct servent
{
   char *s_name;        //这个服务的名称
   char **s_aliases;    //这个服务可能的别名
   int s_port;          //这个服务可能的端口
   char *s_proto;       //这个服务可能使用的协议
};



libnet.h中的一些宏定义:
LIBNET_IPV4_H;              ==>0x14  = 20

为了详细记录抓包过程,状态,以及分析情况,提供日志文件打印功能

#ifndef LOG_H
#define LOG_H
#include<string>
#include<unistd.h>
#include<fcntl.h>


using namespace std;
class Logger
{
    private:
        Logger(const string &filename);
        string get_current_time();//获取系统当前时间 
        int fd;
        static  Logger *log;

    public:
        static  Logger *get_log(const string &filename);
        void    notice (const string &message);
        void    error  (const string &message);
        void    warn   (const string &message);
        ~Logger();
};
#endif

猜你喜欢

转载自blog.csdn.net/m18706819671/article/details/80612404
今日推荐