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

📄 get_packet_code.c

📁 网络完全开发包 书 所带的、光盘 里面有书上的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* 文件名字:get_packet_code.c 刘文涛编写 */
#include "pcap.h"
/*
-----------------------------------------------------------------------------------------------------------------------
Libpcap的头文件 ;
下面是以太网协议格式的定义
-----------------------------------------------------------------------------------------------------------------------
 */
struct ether_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 in_addr
{
    in_addr_t s_addr;
};
/*
-----------------------------------------------------------------------------------------------------------------------
下面是ARP协议格式的定义
-----------------------------------------------------------------------------------------------------------------------
 */
struct arp_header
{
    u_int16_t arp_hardware_type;
    /* 硬件类型 */
    u_int16_t arp_protocol_type;
    /* 协议类型 */
    u_int8_t arp_hardware_length;
    /* 硬件地址长度 */
    u_int8_t arp_protocol_length;
    /* 协议地址长度 */
    u_int16_t arp_operation_code;
    /* ARP操作码 */
    u_int8_t arp_source_ethernet_address[6];
    /* 源以太网地址 */
    u_int8_t arp_source_ip_address[4];
    /* 源IP地址 */
    u_int8_t arp_destination_ethernet_address[6];
    /* 目的以太网地址 */
    u_int8_t arp_destination_ip_address[4];
    /* 目的IP地址 */
};
/*
-----------------------------------------------------------------------------------------------------------------------
下面是IP协议格式的定义
-----------------------------------------------------------------------------------------------------------------------
 */
struct ip_header
{
    #if defined(WORDS_BIGENDIAN)
        u_int8_t ip_version: 4,
        /* 版本 */
        ip_header_length: 4;
        /* 首部长度 */
    #else
        u_int8_t ip_header_length: 4,
        /* 首部长度 */
        ip_version: 4;
        /* 版本 */
    #endif
    u_int8_t ip_tos;
    /* 服务质量 */
    u_int16_t ip_length;
    /* 总长度 */
    u_int16_t ip_id;
    /* 标识 */
    u_int16_t ip_off;
    /* 偏移 */
    u_int8_t ip_ttl;
    /* 生存时间 */
    u_int8_t ip_protocol;
    /* 协议类型 */
    u_int16_t ip_checksum;
    /* 校验和 */
    struct in_addr ip_souce_address;
    /* 源IP地址 */
    struct in_addr ip_destination_address;
    /* 目的IP地址 */
};
/*
-----------------------------------------------------------------------------------------------------------------------
下面是UDP协议格式的定义
-----------------------------------------------------------------------------------------------------------------------
 */
struct udp_header_liuwentao
{
    u_int16_t udp_source_port;
    /* 源端口号 */
    u_int16_t udp_destination_port;
    /* 目的端口号 */
    u_int16_t udp_length;
    /* 长度 */
    u_int16_t udp_checksum;
    /* 校验和 */
};
/*
-----------------------------------------------------------------------------------------------------------------------
下面是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;
    /* 紧急指针 */
};
/*
-----------------------------------------------------------------------------------------------------------------------
下面是ICMP协议格式的定义
-----------------------------------------------------------------------------------------------------------------------
 */
struct icmp_header
{
    u_int8_t icmp_type;
    /* ICMP类型 */
    u_int8_t icmp_code;
    /* ICMP代码 */
    u_int16_t icmp_checksum;
    /* 校验和 */
    u_int16_t icmp_id;
    /* 标识 */
    u_int16_t icmp_sequence;
    /* 序列号 */
};
/*
=======================================================================================================================
下面是分析TCP协议的函数定义
=======================================================================================================================
 */
void tcp_protocol_packet_callback(u_char *argument, const struct pcap_pkthdr *packet_header, const 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协议数据内容,跳过以太网协议和IP协议部分 */
    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("-------  TCP Protocol  (Transport Layer)  -------\n");
    printf("Source Port:%d\n", source_port);
    printf("Destination Port:%d\n", destination_port);
    switch (destination_port)
    /* 根据端口号判断应用层协议类型 */
    {
        case 80:
            printf("HTTP protocol\n");
            break;
            /* 上层协议为HTTP协议,可以在此调用分析HTTP协议的函数,读者可以自己尝试实现 */
        case 21:
            printf("FTP protocol\n");
            break;
            /* 上层协议为FTP协议,可以在此调用分析HTTP协议的函数 */
        case 23:
            printf("TELNET protocol\n");
            break;
            /* 上层协议为TELNET协议,可以在此调用分析HTTP协议的函数 */
        case 25:
            printf("SMTP protocol\n");
            break;
            /* 上层协议为SMTP协议,可以在此调用分析HTTP协议的函数 */
        case 110:
            printf("POP3 protocol\n");
            break;
            /* 上层协议为POP3协议,可以在此调用分析HTTP协议的函数 */
        default:
            break; /* 其它的端口号在这里没有分析,读者可以在此分析其它端口号代表的应用层协议 */
    }
    printf("Sequence Number:%u\n", sequence);
    printf("Acknowledgement Number:%u\n", acknowledgement);
    printf("Header Length:%d\n", header_length);
    printf("Reserved:%d\n", tcp_protocol->tcp_reserved);
    printf("Flags:");
    /* 判断标记的种类 */
    if (flags &0x08)
        printf("PSH ");
    if (flags &0x10)
        printf("ACK ");
    if (flags &0x02)
        printf("SYN ");
    if (flags &0x20)
        printf("URG ");
    if (flags &0x01)
        printf("FIN ");
    if (flags &0x04)
        printf("RST ");
    printf("\n");
    printf("Window Size:%d\n", windows);
    printf("Checksum:%d\n", checksum);
    printf("Urgent pointer:%d\n", urgent_pointer);
}
/*
=======================================================================================================================
下面是实现分析UDP协议的函数定义
=======================================================================================================================
 */
void udp_protocol_packet_callback(u_char *argument, const struct pcap_pkthdr *packet_header, const u_char *packet_content)
{
    struct udp_header_liuwentao *udp_protocol;
    /* UDP协议变量 */
    u_short source_port;
    /* 源端口号 */
    u_short destination_port;
    /* 目的端口号 */
    u_short length;
    /* 长度 */
    udp_protocol = (struct udp_header_liuwentao*)(packet_content + 14+20);
    /* 获得UDP协议数据内容,跳过以太网协议和IP协议部分 */
    source_port = ntohs(udp_protocol->udp_source_port);
    /* 获得源端口号 */
    destination_port = ntohs(udp_protocol->udp_destination_port);
    /* 获得目的端口号 */
    length = ntohs(udp_protocol->udp_length);
    /* 获得长度 */
    printf("----------  UDP Protocol  (Transport  Layer)  ----------\n");
    printf("Source port:%d\n", source_port);
    printf("Destination port:%d\n", destination_port);
    switch (destination_port)
    /* 根据端口号来判断应用层协议类型 */
    {
        case 138:
            printf("NETBIOS Datagram Service\n");
            break;
            /*
             * 端口号是138,表示上层协议为NETBIOS
             * 数据报服务,在此可以调用分析此协议的函数,读者自己可以试着实现。
             */
        case 137:
            printf("NETBIOS Name Service\n");
            break;
            /* 端口号是137,表示上层协议为NETBIOS 名字服务,在此可以调用分析此协议的函数 */
        case 139:
            printf("NETBIOS session service\n");
            break;
            /* 端口号是139,表示上层协议为NETBIOS 会话服务,在此可以调用分析此协议的函数。 */
        case 53:
            printf("name-domain server \n");
            break;
            /* 端口号是53,表示上层协议为域名服务,在此可以调用分析此协议的函数。 */
        default:
            break; /* 其他的端口号在此没有分析,读者可以在此进一步分析 */
    }
    printf("Length:%d\n", length);
    printf("Checksum:%d\n", ntohs(udp_protocol->udp_checksum));
    /* 获得校验和 */
}
/*
=======================================================================================================================
下面是实现分析ICMP协议的函数的定义
=======================================================================================================================

⌨️ 快捷键说明

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