In the previous article, we briefly understood the pcap file. Now let's take a look at the general structure of the IP package.
Before starting the IP package, we recommend a very useful rfcview tool. With this tool, we only need to enter RFC (request for comments, the basic Internet communication protocols are described in the RFC file in detail.) You can view various RFC documents. The data structure of the IP package is defined in rfc791. Here we will give a general introduction: 0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 4 5 6 7 8 9 0 1
+- +-+
| Version | IHL | type of service | total length |
+- +-+
| Identification | flags | Fragment Offset |
+- +-+
| Time to live | Protocol | header checksum |
+- +-+
| Source address |
+- +-+
| Destination Address |
+- +-+
| Options | padding |
+- +-+
Version: Version 4-bit IHL: Header Length. IHL does not directly indicate the length occupied by the header, but how many 32-bit 4-bit types occupied by the header: service type, A brief description of a specific network type for data transmission. 8-digit
Total Length: total length 16-bit Identification: The identifier 16-bit used to assemble a data packet
Flags: three-digit Control
Bit 0: reserved, must be zero
Bit 1: (DF) 0 = may fragment, 1 = Don't fragment.
Bit 2: (MF) 0 = last fragment, 1 = more fragments.
Fragment offset: 13-Bit Data offset
Time to live: 8-bit Lifecycle
Protocol: protocol type. For more information, see rfc790.
8-digit
Header
Checksum: Header verification code 16 bits
Source
Address: 32-bit Source Address
Destination
Address: 32-bit Destination Address
Location of IP data in the pcap File
| Pcap Header |
| Data Header |
| Destination MAC, source MAC, type (14 bytes) |
| IP data |
| ...... |
| Data Header |
| Destination MAC, source MAC, type (14 bytes) |
| IP data |
| ...... |
According to the above table ...... In each IP packet, the starting position of IP data = the starting position of the packet + Header Length + 14. The IP address data parsing program is used to export sctp protocol data from the PACP file.
Pcap_header.h
# Pragma pack (push, 1) // to ensure normal compilation in Windows and Linux, discard int64 or _ int_64typedef unsigned short _ int16; typedef unsigned long _ int32; typedef unsigned char byte; typedef int bool; # define false 1 # define true 0 // pcap File Header struct _ file_header {_ int32imagic; _ int16imaversion; _ int16imiversion; _ int32itimezone; _ struct; _ int32isnaplen; _ int32ilinktype;}; typedef struct _ file_header _ pcap_header; // data packet header struct _ pkthdr {_ int32itimesecond; _ int32itimess; _ int32iplength; _ identifier ;}; typedef struct _ pkthdr _ pk_header; struct _ iphdr {byte byteversions: 4; byte bytehdlength: 4; byte bytesertype; _ int16 itotallength; _ int16 Iidentification; union {_ int16 iflags; _ int16 ifragmentoffset;}; byte bytetimetolive; byte byteprotocol; _ int16 iheaderchecksum; _ int32 isourceaddress; _ int32 idestinationaddress ;}; // flags # define ip_df 0X4000 # define ip_mf 0x2000 // fragmentoffset mask # define ip_offmask 0x1ffftypedef struct _ iphdr _ ip_header; # pragma pack (POP)
Pcap_adapter.h
# Include <stdio. h> # include "pcap_header.h" // open the pcap file bool openpcapfile (char * strpath); // obtain the pcap File Header void getpcapheader (_ pcap_header * pheader ); // The returned value of the current package is the buffer length. The buffer does not contain header information. After the information is obtained, the system automatically jumps to the next packet header int getpacketandmovenext (_ pk_header * ppacket, byte ** pbuffer ); // call the bool movefirst () function before reading data from the first package; // determine whether the function has reached the end of the file bool ieeof (); // obtain the IP address information void getipdata (/* out */_ ip_header * pipdata,/* In */byte * pdatabuffer );
Pcap_adapter.c
# Include "pcap_adapter.h" # include "value_checker.h" # define null 0 # define checkfile if (m_gppcapfile = NULL) \ {\ printf ("file not opened"); \ return; \}# define checkfileex (x) if (m_gppcapfile = NULL) \{\ printf ("file not opened"); \ return x ;\}# define ntetohost_short (X) X = t_ntohs (x) # define ntetohost_long (x) x = t_ntohl (x) file * m_gppcapfile = NULL; int m_gifilelength = 0; bool openpcapfile (char * strpath) {m_gppcapfile = fopen (strpath, "rb"); checkfileex (false); fseek (m_gppcapfile, 0, seek_end); separator = ftell (m_gppcapfile); fseek (m_gppcapfile, 0, seek_set); Return true;} void getpcapheader (_ pcap_header * pheader) {checkfile; // Save the current cursor position int inowpos = ftell (m_gppcapfile); fseek (m_gppcapfile, 0, seek_set); fread (void *) pheader, sizeof (_ pcap_header), 1, m_gppcapfile); fseek (m_gppcapfile, inowpos, seek_set);} bool movefirst () {checkfileex (false); Return fseek (m_gppcapfile, sizeof (_ pcap_header), seek_set) = 0;} int getpacketandmovenext (_ pk_header * ppacket, byte * pbuffer) {checkfileex (0); fread (void *) ppacket, sizeof (_ pk_header), 1, m_gppcapfile); * pbuffer = (byte *) malloc (ppacket-> ilength); fread (void *) * pbuffer, ppacket-> ilength, 1, m_gppcapfile); Return ppacket-> ilength;} bool ieeof () {checkfileex (false); int inowpos = ftell (m_gppcapfile); Return inowpos> = m_gifilelength;} void getipdata (/* out */_ ip_header * pipdata, /* In */byte * pdatabuffer) {memcpy (void *) pipdata, pdatabuffer + 14, sizeof (_ ip_header); ntetohost_short (pipdata-> itotallength ); ntetohost_short (pipdata> Iidentification); ntetohost_short (pipdata-> ifragmentoffset); ntetohost_short (pipdata-> iheaderchecksum); ntetohost_long (pipdata-> isourceaddress ); ntetohost_long (pipdata-> idestinationaddress );}
Main. c
# Include "pcap_adapter.h" int main () {If (openpcapfile ("zsrnc3_iups_inerface3.pcap") {printf ("failed to open the pcap file"); Return 0 ;}_ _ pcap_header header; getpcapheader (& header); int ino = 1; movefirst (); file * pwfile = fopen ("export-file.pcap", "WB"); fwrite (void *) & header, sizeof (_ pcap_header), 1, pwfile); While (! Ieeof () {__ pk_header data ;__ ip_header ipdata; byte * pbuffer; getpacketandmovenext (& Data, & pbuffer); getipdata (& ipdata, pbuffer ); // sctp = 132 If (ipdata. byteprotocol = 132) {fwrite (void *) (& data), sizeof (struct _ pkthdr), 1, pwfile); fwrite (void *) pbuffer, Data. ilength, 1, pwfile);} Free (pbuffer);} fclose (pwfile); printf ("export over"); return 1 ;}