?? get_packet_code.c
字號:
/* 文件名字: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 + -