This library is designed to generate files and read PCAP format. Generation of UDP packets is also supported
Functions:
The function creates a file and returns either NULL on error or a pointer to PCAPFILE
Function for filling a file created through lpcap_create, returns 0 on error. At the input she receives:
File closing function It receives as input:
The function of generating an ethernet-ip-udp data packet ethernet_data_t based on data from network_packet_frame_t It receives as input:
File opening function. If the file does not contain signs of PCAP format, 0 is returned, as in other error situations
Function to read a file header from a PCAPFILE descriptor * pfl of an already open file in pcap_hdr_t * phdr phdr must point to an existing memory area. Returns 0 on error
Function to read a file data frame from a PCAPFILE handle * pfl of an already open file in pcaprec_hdr_and_data_t * phdr phdr must point to an existing memory area. Returns 0 on error
Function for moving the position pointer to the frame number (i.e. record) record_num.
Example of use with simple packet generation
int i=0;
const int PKTS_COUNT = 212000;
int PKTS_LEN = 540;
static ethernet_data_t eda;
eda.len = PKTS_LEN;
PCAPFILE * pfl = lpcap_create("./pcaplibtestfile.pcap");
for( i=0;i< PKTS_COUNT;i++ )
{
/* TODO: fill data memcpy(eda.data , YOUR_DATA_BUF,SIZE_YOUR_DATA_BUF );
eda.len = SIZE_YOUR_DATA_BUF;
*/
lpcap_write_data( pfl , &eda , i, 0 );
}
lpcap_close_file( pfl );
Example with generating UDP packets
#include "pcap_file_generator.h"
#include "ethernet.h"
.......
int i=0;
const int PKTS_COUNT = 2000100;
const int udp_data_sz = 1440;// udp data size
ethernet_data_t eda;
eda.len = udp_data_sz +(sizeof(eth_frame_t)+sizeof(ip_packet_t))+8;//34 - headers len
uint8_t eth_data[eda.len];
eth_frame_t * eth_f = (eth_frame_t *) eth_data;
network_packet_frame_t npf;
uint8_t m_addr[] = {0xef,0xab,0x03, 0xdc,0xee,0x11};
memcpy(npf.dst_mac ,m_addr , sizeof(m_addr));
//change mac
m_addr[4] = 0x44;
m_addr[5] = 0x88;
memcpy(npf.src_mac ,m_addr , sizeof(m_addr));
npf.src_port = 4567;
npf.dst_port = 4568;
strcpy(npf.src_ip, "192.168.23.100");
strcpy(npf.dst_ip, "192.168.22.105");
uint8_t tdata[ udp_data_sz ];
npf.data = tdata;
npf.data_len = sizeof(tdata);
build_udp_frame(eth_f , &npf ); // convert network_packet_frame_t to eth_frame_t
eda.data = (void *) eth_f;
PCAPFILE * pfl = lpcap_create("./pcaplibtestfile.pcap");
for( i=0;i< PKTS_COUNT;i++ )
{
lpcap_write_data( pfl , &eda , i, 0 );
}
lpcap_close_file( pfl );
Example of reading packages from a file
PCAPFILE * pfr = lpcap_open("./pcaplibtestfile.pcap");
pcap_hdr_t phdr;
if( lpcap_read_header( pfr, &phdr ))
{
int rese_rec_read = 0 ;
pcaprec_hdr_and_data_t p_rec_data;
do{
rese_rec_read = lpcap_read_frame_record( pfr , &p_rec_data);
}while(rese_rec_read>0);