library/src/network/PCAP/BP-PacketCapture.cc
/*
 * This function starts a packet capture given a device, a berkley packet filter
 * and a filter callback function.  The callback function should be in the following
 * format.  Populate userData with datastructure of your choice in a custom filter, or
 * use P_BP_CAPTURE_FILTER for the default BP_CaptureFilter function.
 *
 * Example Callback: (provided in library)
 *
 * 		void BP_CaptureFilter(u_char *userData, const struct pcap_pkthdr* pkthdr, const u_char*  packet);
 *
 *
 * When packet is captured, that function will be called and packet will be processed by passing all parameters
 * to the Pfilter.
 *
 * Example Usage:
 *
 *		BP_StartPacketCapture("eth0", "not port 22", 150, BP_CaptureFilter,
 *
 */
This function starts a packet capture given a device, a berkley packet filter
and a filter callback function.  The callback function should be in the following
format.  Populate userData with datastructure of your choice in a custom filter, or
use P_BP_CAPTURE_FILTER for the default BP_CaptureFilter function.

Example Callback: (provided in library)



When packet is captured, that function will be called and packet will be processed by passing all parameters
to the Pfilter.

Example Usage.


This function starts a packet capture given a device a berkley packet filter and a filter callback function . The callback function should be in the following format . Populate userData with datastructure of your choice in a custom filter or use P_BP_CAPTURE_FILTER for the default BP_CaptureFilter function . Example Callback : ( provided in library ) When packet is captured that function will be called and packet will be processed by passing all parameters to the Pfilter . Example Usage .
int BP_StartPacketCapture(char * device, char * bpfilter, size_t maxPackets, pcap_handler filterCallback, void *userData){
    P_BP_PACKET_CAPTURE_CONF pcapConf = (P_BP_PACKET_CAPTURE_CONF) bpcalloc(sizeof(BP_PACKET_CAPTURE_CONF), 1);
    if(!pcapConf)
		return ERR_FAILURE;
    pcapConf->dev = bpstrndup(device, 64);
    pcapConf->descr = pcap_open_live( pcapConf->dev, BUFSIZ, 0, -1, pcapConf->errbuf);
    if(pcapConf->descr == NULL)
		return ERR_FAILURE;
	if (pcap_lookupnet(pcapConf->dev, &pcapConf->net, &pcapConf->mask, pcapConf->errbuf) == -1)
		return ERR_FAILURE;
	if (pcap_compile(pcapConf->descr, &pcapConf->fp, pcapConf->filter_expression, 0, pcapConf->net) == -1)
		return ERR_FAILURE;
	if (pcap_setfilter(pcapConf->descr, &pcapConf->fp) == -1)
		return ERR_FAILURE;
	void ** tmp = (void **) userData;
	if(userData)
		tmp[0] = pcapConf;
	pcap_loop(pcapConf->descr, maxPackets, (pcap_handler) filterCallback, (u_char *) userData);
    return ERR_SUCCESS;
}
int BP_StartPacketCapture ( char * device , char * bpfilter , size_t maxPackets , pcap_handler filterCallback , void * userData ) { P_BP_PACKET_CAPTURE_CONF pcapConf = ( P_BP_PACKET_CAPTURE_CONF ) bpcalloc ( sizeof ( BP_PACKET_CAPTURE_CONF ) , 1 ) ; if ( ! pcapConf ) return ERR_FAILURE ; pcapConf -> dev = bpstrndup ( device , 64 ) ; pcapConf -> descr = pcap_open_live ( pcapConf -> dev , BUFSIZ , 0 , -1 , pcapConf -> errbuf ) ; if ( pcapConf -> descr == NULL ) return ERR_FAILURE ; if ( pcap_lookupnet ( pcapConf -> dev , & pcapConf -> net , & pcapConf -> mask , pcapConf -> errbuf ) == -1 ) return ERR_FAILURE ; if ( pcap_compile ( pcapConf -> descr , & pcapConf -> fp , pcapConf -> filter_expression , 0 , pcapConf -> net ) == -1 ) return ERR_FAILURE ; if ( pcap_setfilter ( pcapConf -> descr , & pcapConf -> fp ) == -1 ) return ERR_FAILURE ; void * * tmp = ( void * * ) userData ; if ( userData ) tmp [ 0 ] = pcapConf ; pcap_loop ( pcapConf -> descr , maxPackets , ( pcap_handler ) filterCallback , ( u_char * ) userData ) ; return ERR_SUCCESS ; }
This function starts a packet capture given a device, a berkley packet filter
 and a filter callback function.
This function starts a packet capture given a device a berkley packet filter and a filter callback function .
// allocate space for configuration structure // device name should never be longer then 64bytes i'd imagine. // open the device // if the interface cannot be opened, report why and exit function // set the first ptrsize of userData to the pointer to our packet configuration // main pcap loop // return indicating success