library/src/network/PCAP/BP-PacketCapture.cc
/*
 * This is a default pcap callback function which can be used with BP_StartPacketCapture if no
 * capture is set.
 */
This is a default pcap callback function which can be used with BP_StartPacketCapture if no
capture is set.
This is a default pcap callback function which can be used with BP_StartPacketCapture if no capture is set .
void BP_CaptureFilter(u_char *userData, const struct pcap_pkthdr* pkthdr, const u_char*  packet)
{
	if(pkthdr->len == 0 || pkthdr->len == sizeof(BP_ETHERNET_HEADER)){
		memset((void*) packet, 0x00, pkthdr->len);
		return;
	}
	P_BP_PACKET_CAPTURE_FILTER captureFilter = (P_BP_PACKET_CAPTURE_FILTER) userData;
	if(captureFilter->logFilePath && !captureFilter->logFile)
	{
		captureFilter->logFile = fopen(captureFilter->logFilePath, "w+");
	}
	if(captureFilter->logFile && !captureFilter->interactiveMode)
	{
		fwrite(pkthdr, sizeof(struct pcap_pkthdr), 1, captureFilter->logFile);
		fwrite(packet, pkthdr->len, 1, captureFilter->logFile);
	}
	if(captureFilter->interactiveMode)
	{
		fflush(stdin);
		fflush(stdout);
		P_BP_ETHERNET_HEADER ethernetHeader = (P_BP_ETHERNET_HEADER) packet;
		P_BP_IP_HEADER  ipHeader  = (P_BP_IP_HEADER) &packet[sizeof(BP_ETHERNET_HEADER)];
		P_BP_TCP_HEADER tcpHeader = (P_BP_TCP_HEADER) &packet[sizeof(BP_IP_HEADER)];
		BP_PrintIPHeader(ipHeader);
		fprintf(stdout, "\nCaptured Packet: (k)eep,(d)isgard,e(x)it:");
		switch(fgetc(stdin))
		{
			case 'k':
				fwrite(pkthdr, sizeof(struct pcap_pkthdr), 1, captureFilter->logFile);
				fwrite(packet, pkthdr->len, 1, captureFilter->logFile);
				break;
			case 'd':
				printf("\n");
				break;
			case 'e':
				pcap_breakloop(captureFilter->captureConf->descr);
				break;
			default:
				break;
		}
		memset((void*) packet, 0x00, pkthdr->len);
	}
}
void BP_CaptureFilter ( u_char * userData , const struct pcap_pkthdr * pkthdr , const u_char * packet ) { if ( pkthdr -> len == 0 || pkthdr -> len == sizeof ( BP_ETHERNET_HEADER ) ) { memset ( ( void * ) packet , 0x00 , pkthdr -> len ) ; return ; } P_BP_PACKET_CAPTURE_FILTER captureFilter = ( P_BP_PACKET_CAPTURE_FILTER ) userData ; if ( captureFilter -> logFilePath && ! captureFilter -> logFile ) { captureFilter -> logFile = fopen ( captureFilter -> logFilePath , " " ) ; } if ( captureFilter -> logFile && ! captureFilter -> interactiveMode ) { fwrite ( pkthdr , sizeof ( struct pcap_pkthdr ) , 1 , captureFilter -> logFile ) ; fwrite ( packet , pkthdr -> len , 1 , captureFilter -> logFile ) ; } if ( captureFilter -> interactiveMode ) { fflush ( stdin ) ; fflush ( stdout ) ; P_BP_ETHERNET_HEADER ethernetHeader = ( P_BP_ETHERNET_HEADER ) packet ; P_BP_IP_HEADER ipHeader = ( P_BP_IP_HEADER ) & packet [ sizeof ( BP_ETHERNET_HEADER ) ] ; P_BP_TCP_HEADER tcpHeader = ( P_BP_TCP_HEADER ) & packet [ sizeof ( BP_IP_HEADER ) ] ; BP_PrintIPHeader ( ipHeader ) ; fprintf ( stdout , " \n " ) ; switch ( fgetc ( stdin ) ) { case ' ' : fwrite ( pkthdr , sizeof ( struct pcap_pkthdr ) , 1 , captureFilter -> logFile ) ; fwrite ( packet , pkthdr -> len , 1 , captureFilter -> logFile ) ; break ; case ' ' : printf ( " \n " ) ; break ; case ' ' : pcap_breakloop ( captureFilter -> captureConf -> descr ) ; break ; default : break ; } memset ( ( void * ) packet , 0x00 , pkthdr -> len ) ; } }
This is a default pcap callback function which can be used with BP_StartPacketCapture if no
 capture is set.
This is a default pcap callback function which can be used with BP_StartPacketCapture if no capture is set .
// set capture filter // if the log file path is set try to open/lock the file // open the file for appending // if the log file is non-null, we can write the file // write the header to the file // write the packet to the file // Interactive:  keep/disgard/exit_pcap // flush out standard input // set ethernet header // set ip header in packet // set tcp header in packet // display ip header // print indicator message // get character choice from user // keep // write the header to the file // write the packet to the file // disgard (ie do nothing) // exit pcap looping // default switch