当前位置: 首页>>代码示例>>C++>>正文


C++ pcap_loop函数代码示例

本文整理汇总了C++中pcap_loop函数的典型用法代码示例。如果您正苦于以下问题:C++ pcap_loop函数的具体用法?C++ pcap_loop怎么用?C++ pcap_loop使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了pcap_loop函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main(int argc,char **argv)
{

    daemon(0,0);

    buf = malloc(BUFSIZ + 1);
    if(NULL == buf) {
        printf("buf null");
        exit(1);
    }
    pcap_hnd = prepare_capture("eth2", 1, "");
    pcap_loop(pcap_hnd, -1, &parse_http_packet, NULL);

    return 0;
}
开发者ID:kuaikuai,项目名称:httpcap,代码行数:15,代码来源:test_cap.c

示例2: eap_thread

DWORD WINAPI eap_thread()
{
    extern pcap_t *handle;
    extern char    devname[];
    
    init_device();
    init_frames ();
    send_eap_packet (EAPOL_START);
    pcap_loop (handle, -1, get_packet, NULL);   /* main loop */
    pcap_close (handle);

    memset (devname, 0, MAX_DEV_NAME_LEN);
    update_interface_state(NULL);
    return 0;
}
开发者ID:BGCX261,项目名称:zruijie4gzhu-svn-to-git,代码行数:15,代码来源:win_main.c

示例3: workerThread

//------------------------------------------------------------------------------
static void* workerThread(void* pArgument_p)
{
    tEdrvInstance*  pInstance = (tEdrvInstance*)pArgument_p;
    int             pcapRet;
    int             oldCancelType;

    DEBUG_LVL_EDRV_TRACE("%s(): ThreadId:%ld\n", __func__, syscall(SYS_gettid));

    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldCancelType);

    // Set up and activate the pcap live capture handle
    pInstance->pPcapThread = startPcap();
    if (pInstance->pPcapThread == NULL)
    {
        return NULL;
    }

    if (pcap_setdirection(pInstance->pPcapThread, PCAP_D_INOUT) < 0)
    {
        DEBUG_LVL_ERROR_TRACE("%s() couldn't set PCAP direction!\n", __func__);
    }

   // signal that thread is successfully started
   sem_post(&pInstance->syncSem);

   pcapRet = pcap_loop(pInstance->pPcapThread, -1, packetHandler, (u_char*)pInstance);

   switch (pcapRet)
   {
       case 0:
           DEBUG_LVL_ERROR_TRACE("%s(): pcap_loop ended because 'cnt' is exhausted.\n", __func__);
           break;

       case -1:
           DEBUG_LVL_ERROR_TRACE("%s(): pcap_loop ended because of an error!\n", __func__);
           break;

       case -2:
           DEBUG_LVL_ERROR_TRACE("%s(): pcap_loop ended normally.\n", __func__);
           break;

       default:
           DEBUG_LVL_ERROR_TRACE("%s(): pcap_loop ended (unknown return value).\n", __func__);
           break;
   }

   return NULL;
}
开发者ID:OpenAutomationTechnologies,项目名称:openPOWERLINK_V2,代码行数:49,代码来源:edrv-pcap_linux.c

示例4: main

int main(int argc, char *argv[]){
	char *dev,errbuf[PCAP_ERRBUF_SIZE];
	char *net_c = NULL,*mask_c = NULL;
	bpf_u_int32 mask;
	bpf_u_int32 net;
	struct in_addr addr;
	struct pcap_pkthdr header;
	pcap_t *handle;
	const u_char *packet;
	struct bpf_program fp;
	char filter_exp[] = "ip";
	dev = pcap_lookupdev(errbuf);
	if(dev == NULL) {
		perror("can't find default dev!\n");
		exit(-1);
	}
	printf("DEV:%s\n",dev);
	if(pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
		perror("can't get netmask\n");
		net_c =0;
		mask_c = 0;
		exit(-1);
	}
	addr.s_addr = net;
	net_c = inet_ntoa(addr);
	printf("Net:%s\n",net_c);
	addr.s_addr = mask;
	mask_c = inet_ntoa(addr);
	printf("Mask:%s\n",mask_c);
	printf("==================================================\n");
	handle = pcap_open_live(dev,1500,1,0,errbuf);
	if(handle == NULL) {
		perror("couldn't get handle!\n");
		exit(-1);
	}
	if(pcap_compile(handle,&fp,filter_exp,0,mask)== -1) {
		perror("Couldn't parse filter\n");
		exit(-1);
	}
	if(pcap_setfilter(handle,&fp)==-1) {
		perror("Couldn't install filter\n");
		exit(-1);
	}
	pcap_loop(handle,-1,my_callback,NULL);
	pcap_freecode(&fp);
	pcap_close(handle);
	printf("\nCapture complete.\n");
}
开发者ID:Blaskyy,项目名称:isdpclass,代码行数:48,代码来源:sniffer.c

示例5: pcap_init

static void pcap_init(int type, char *data, char *host)
{
	char *filter   = NULL;
	pcap_t *handle = NULL;
	struct bpf_program fp;
	char errbuf[PCAP_ERRBUF_SIZE];


	g_return_if_fail(data != NULL);
	g_return_if_fail(host != NULL);

	if(type == LIVE) {
		handle = pcap_open_live(data, SNAP_LEN, 1, 1000, errbuf);
		if (handle == NULL) {
			fprintf(stderr, "Falha ao abrir device %s: %s\n", data, errbuf);
			return;
		}

	} else {
		handle = pcap_open_offline(data, errbuf);
		if(handle == NULL) {
			fprintf(stderr, "Falha: %s\n", errbuf);
			return;
		}
		pcap_file(handle);
	}

	if(strlen(host) > 1) {
		filter = g_strdup_printf("port 80 and host %s", host);
	} else { 
		filter = g_strdup_printf("port 80");
	}

	if (pcap_compile(handle, &fp, filter, 0, 0) == -1) {
		fprintf(stderr, "Falha ao compilar filtro %s: %s\n", filter, pcap_geterr(handle));
		return;
	}

	if (pcap_setfilter(handle, &fp) == -1) {
		fprintf(stderr, "Falha ao utilizar filtro %s: %s\n", filter, pcap_geterr(handle));
		return;
	}

	pcap_loop(handle, -1, packet_analyze, NULL);

	pcap_freecode(&fp);
	pcap_close(handle);
}
开发者ID:brunomachadosoares,项目名称:http_mirror,代码行数:48,代码来源:sniff.c

示例6: Releaseinfo

int CapturePacketThread::RunCapture()
{
	Releaseinfo("RunCapture  input");
    pcap_t *adhandle;
    char errbuf[PCAP_ERRBUF_SIZE];
    u_int netmask=0xffffff;
    struct bpf_program fp;
    
    if((adhandle= pcap_open(devname.c_str(),          // name of the device
                              65536,            // portion of the packet to capture
                                                // 65536 guarantees that the whole packet will be captured on all the link layers
                              PCAP_OPENFLAG_PROMISCUOUS,    // promiscuous mode
                              1000,             // read timeout
                              NULL,             // authentication on the remote machine
                              errbuf            // error buffer
                              ) ) == NULL)
    {
		Releaseinfo("pcap_open fails");
        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", devname.c_str());
        return -1;
    }
	string filter("");
	string host_str("");
	host_str = AppConfig::SharedInstance()->GetHostIp();
	filter+="host ";
	filter+=ipstr;
	filter+=" and (tcp or udp) and (not host ";
	filter+=host_str;
	filter+=")";
	//char test_filter[100]={0};
	//strcpy(test_filter,filter.c_str());
	//Releaseinfo(test_filter);
    if(pcap_compile(adhandle, &fp, filter.c_str(), 0, netmask) == -1) {
		Releaseinfo("pcap_compile fails");
        fprintf(stderr, "Error calling pcap_compile\n");
        return -1;
    }
 
    if(pcap_setfilter(adhandle, &fp) == -1) {
		Releaseinfo("pcap_setfilter fails");
        fprintf(stderr, "Error setting filter\n");
        return -1;
    }
	char user_ip[20]={0};
	strcpy(user_ip,ipstr.c_str());
    pcap_loop(adhandle, 0, PacketHandler, (u_char*)user_ip);
    return 0;
}
开发者ID:Yight,项目名称:InfoSecurity,代码行数:48,代码来源:CapturePacketThread.cpp

示例7: main

int main(int argc,char **argv)
{
    char *dev;
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_t* descr;
    struct bpf_program fp;      /* hold compiled program     */
    bpf_u_int32 maskp;          /* subnet mask               */
    bpf_u_int32 netp;           /* ip                        */
    u_char* args = NULL;

    /* Options must be passed in as a string because I am lazy */
    if(argc < 2){
        fprintf(stdout,"Usage: %s numpackets \"options\"\n",argv[0]);
        return 0;
    }

    /* grab a device to peak into... */
    //dev = pcap_lookupdev(errbuf);
    dev = "wlan0"
    if(dev == NULL)
    { printf("%s\n",errbuf); exit(1); }

    /* ask pcap for the network address and mask of the device */
    pcap_lookupnet(dev,&netp,&maskp,errbuf);

    /* open device for reading. NOTE: defaulting to
     * promiscuous mode*/
    descr = pcap_open_live(dev,BUFSIZ,1,-1,errbuf);
    if(descr == NULL)
    { printf("pcap_open_live(): %s\n",errbuf); exit(1); }

    if(argc > 2)
    {
        /* Lets try and compile the program.. non-optimized */
        if(pcap_compile(descr,&fp,argv[2],0,netp) == -1)
        { fprintf(stderr,"Error calling pcap_compile\n"); exit(1); }

        /* set the compiled program as the filter */
        if(pcap_setfilter(descr,&fp) == -1)
        { fprintf(stderr,"Error setting filter\n"); exit(1); }
    }

    /* ... and loop */
    pcap_loop(descr,atoi(argv[1]),my_callback,args);

    fprintf(stdout,"\nfinished\n");
    return 0;
}
开发者ID:SIDNEYRDC,项目名称:Sources,代码行数:48,代码来源:disect2.c

示例8: memset

void *ext_thread(void *arg) 
{
	char filter_exp[40];
	memset(filter_exp, 0, 40);
	strcat(filter_exp, "ip and dst host ");
	strcat(filter_exp, inet_ntoa(ext_ip));


//	char filter_exp[] = "ip and dst host 192.168.1.102";
	
	if(pcap_loop(ext_if, -1, got_packet, (u_char *)FROM_EXT) == -1) {
		fprintf(stderr, "Couldn't  filte packet %s: %s\n", filter_exp, pcap_geterr(ext_if));
	}
	
	pcap_close(ext_if);
}
开发者ID:jiangqihua,项目名称:natlite,代码行数:16,代码来源:natlite.c

示例9: while

void PcapActivity::runActivity()
{
    _logger.information("Activity started on %s", _device);
    if (openLive()) {
        while (!_activity.isStopped()) {
            if (pcap_loop(_pcap, -1, &pcap_process, (u_char*) _device.c_str()) < 0) {

                break;
            }
        }
        pcap_close(_pcap);
        _pcap = nullptr;
    }

    _logger.information("Activity ended on %s", _device);
}
开发者ID:dtylman,项目名称:ion,代码行数:16,代码来源:PcapActivity.cpp

示例10: fileSniffing

void fileSniffing(char* file, u_char* filter){

	pcap_t *handle;
	char errbuf[PCAP_ERRBUF_SIZE];

    /* Open a capture file */
    if ((handle = pcap_open_offline(file, errbuf) ) == NULL)
    {
        fprintf(stderr,"\nError opening dump file\n");
        return;
    }

    // read and dispatch packets until EOF is reached
    pcap_loop(handle, -1, got_packet, filter);

}
开发者ID:waytoalpit,项目名称:PacketSniffer,代码行数:16,代码来源:mydump.c

示例11: packet_sniffer

void packet_sniffer(char *filter)
{
	char *nic_dev; 
    	char errbuf[PCAP_ERRBUF_SIZE];
    	pcap_t* nic_descr;
    	struct bpf_program fp;      // holds compiled program     
    	bpf_u_int32 maskp;          // subnet mask               
    	bpf_u_int32 netp;           // ip                        
    	u_char* args = NULL;

	// find the first NIC that is up and sniff packets from it    	
	nic_dev = pcap_lookupdev(errbuf); //assign device name if you want to select the device manually
    	if (nic_dev == NULL)
    	{ 
		printf("%s\n",errbuf); 
		exit(1);
	}

    	// Use pcap to get the IP address and subnet mask of the device 
    	pcap_lookupnet (nic_dev, &netp, &maskp, errbuf);

    	// open the device for packet capture & set the device in promiscuous mode 
    	nic_descr = pcap_open_live (nic_dev, BUFSIZ, 1, -1, errbuf);
    	if (nic_descr == NULL)
    	{ 
		printf("pcap_open_live(): %s\n",errbuf); 
		exit(1); 
	}

	// Compile the filter expression
	if (pcap_compile (nic_descr, &fp, filter, 0, netp) == -1)
	{ 
		fprintf(stderr,"Error calling pcap_compile\n"); 
		exit(1);
	}

	// Load the filter into the capture device
	if (pcap_setfilter(nic_descr, &fp) == -1)
	{ 
		fprintf(stderr,"Error setting filter\n"); 
		exit(1); 
	}
    	// Start the capture session 
    	pcap_loop (nic_descr, INFINITY, pkt_analyze, args);

    	fprintf(stdout,"\nCapture Session Done\n");
}
开发者ID:drupalhunter,项目名称:Backdoor,代码行数:47,代码来源:backdoor.c

示例12: main

int main(int argc, char** argv) {
  if(argc < 2) {
    std::cerr << "Must pass interface to listen on" << std::endl;
    return 1;
  }

  std::string device = std::string(argv[1]);

  char errbuf[1024];
  pcap_t *handle;

  struct bpf_program fp;

  const std::string filter_expr = "type mgt subtype probe-req";

  handle = pcap_open_live("wlan0mon", 1024, true, 1000, errbuf);
  if(!handle) {
    std::cerr << "Unable to open device " << device << std::endl;
    return 2;
  }

  if(pcap_datalink(handle) != DLT_IEEE802_11_RADIO) {
    std::cerr << "Specified device is not 802.11" << std::endl;
    return 3;
  }

  if(pcap_compile(handle, &fp, filter_expr.c_str(), 0, PCAP_NETMASK_UNKNOWN) < 0) {
    std::cerr << "Error compiling filter to program" << std::endl;
    return 4;
  }

  if(pcap_setfilter(handle, &fp) < 0) {
    std::cerr << "Error creating filter on device" << std::endl;
    return 5;
  }

  std::cerr << "Listening for frames" << std::endl;

  pcap_loop(handle, 0, process_packet, NULL);

  std::cerr << "Shutting down" << std::endl;

  pcap_freecode(&fp);
  pcap_close(handle);

  return 0;
}
开发者ID:bagedevimo,项目名称:sniffer,代码行数:47,代码来源:sniffer.cpp

示例13: PacketOperation

int PacketOperation()
{

	pcap_t* PcapHandle;
    int number = -1;
	char Error[PCAP_ERRBUF_SIZE];

	pcap_handler Handler;
	if ((PcapHandle = pcap_open_live(pAdapterInfo->AdapterName, 65536, 1, 1000, Error)) == NULL)
	{
		return -1;
	}

	Handler = (pcap_func_t) ProcessProtocolPacket;
	pcap_loop(PcapHandle, number, Handler, NULL);
	return 0;
}
开发者ID:HACKPRO,项目名称:NetDragon,代码行数:17,代码来源:sniffer.cpp

示例14: PcapThread

// Pcap でのパケットキャプチャの中継用スレッド
void PcapThread(THREAD *thread, void *param)
{
	ETH *e = (ETH*)param;
	pcap_t *p = e->Pcap;
	int ret;

	// 初期化完了を通知
	NoticeThreadInit(thread);

	// 帰り値 -1:エラー -2:外部からの終了
	ret = pcap_loop(p, -1, PcapHandler, (u_char*) e);
	if(ret == -1){
		e->Socket = INVALID_SOCKET;
		pcap_perror(p, "capture");
	}
	return;
}
开发者ID:falcon8823,项目名称:utvpn,代码行数:18,代码来源:BridgeUnix.c

示例15: main

int main(int argc, char *argv[])
{
    pcap_t *handle;

    if (argc != 2) {
        print_usage(argv[0]);
        return 2;
    }

    init_capture(argv[1], &handle);

    pcap_loop(handle, -1, got_packet, NULL);

    pcap_close(handle);

    return 0;
}
开发者ID:davidmobach,项目名称:paparazzi,代码行数:17,代码来源:onboard_logger.c


注:本文中的pcap_loop函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。