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


C++ pcap_findalldevs函数代码示例

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


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

示例1: get_adapter_index_from_addr

/** Get the index of an adapter by its network address
 *
 * @param netaddr network address of the adapter (e.g. 192.168.1.0)
 * @return index of the adapter or negative on error
 */
static int
get_adapter_index_from_addr(struct in_addr *netaddr, char *guid, size_t guid_len)
{
   pcap_if_t *alldevs;
   pcap_if_t *d;
   char errbuf[PCAP_ERRBUF_SIZE+1];
   int index = 0;

   memset(guid, 0, guid_len);

   /* Retrieve the interfaces list */
   if (pcap_findalldevs(&alldevs, errbuf) == -1) {
      printf("Error in pcap_findalldevs: %s\n", errbuf);
      return -1;
   }
   /* Scan the list printing every entry */
   for (d = alldevs; d != NULL; d = d->next, index++) {
      pcap_addr_t *a;
      for(a = d->addresses; a != NULL; a = a->next) {
         if (a->addr->sa_family == AF_INET) {
            ULONG a_addr = ((struct sockaddr_in *)a->addr)->sin_addr.s_addr;
            ULONG a_netmask = ((struct sockaddr_in *)a->netmask)->sin_addr.s_addr;
            ULONG a_netaddr = a_addr & a_netmask;
            ULONG addr = (*netaddr).s_addr;
            if (a_netaddr == addr) {
               int ret = -1;
               char name[128];
               char *start, *end;
               size_t len = strlen(d->name);
               if(len > 127) {
                  len = 127;
               }
               memcpy(name, d->name, len);
               name[len] = 0;
               start = strstr(name, "{");
               if (start != NULL) {
                  end = strstr(start, "}");
                  if (end != NULL) {
                     size_t len = end - start + 1;
                     memcpy(guid, start, len);
                     ret = index;
                  }
               }
               pcap_freealldevs(alldevs);
               return ret;
            }
         }
      }
   }
   printf("Network address not found.\n");

   pcap_freealldevs(alldevs);
   return -1;
}
开发者ID:evelinad,项目名称:cs244-final-proj,代码行数:59,代码来源:pcapif.c

示例2: main

int main(void)
{
	pcap_if_t *iface, *devs;
	int j, i;
	char errbuf[PCAP_ERRBUF_SIZE + 1];
	FILE *fp;

	printf("Copyright (C) Ahmed Samy 2014 <[email protected]>\n\n");
	printf("\t\t\tNetwork Traffic Analyzer\n");

	if (pcap_findalldevs(&devs, errbuf) == -1 || !devs) {
		fprintf(stderr, "No network devices are currently connected\n");
		return 1;
	}

	printf("Enabled Network Devices:\n");
	for (i = 1, iface = devs; iface; iface = iface->next)
		printf("%d - %s\n", i++, iface->description);

prompt:
	printf("Device Index> ");
	scanf("%d", &j);

	/* Find the interface pointer.  */
	for (i = 1, iface = devs; iface && i != j; iface = iface->next, ++i);
	if (!iface) {
		fprintf(stderr, "Invalid device index %d, please try again.", j);
		goto prompt;
	}

	c = capture_new();
	c->capture_fn = print_data;

	if (!capture_set_iface(c, iface)) {
		fprintf(stderr, "Internal error: could not set the interface to capture!\n");
		pcap_freealldevs(devs);
		return 1;
	}
	pcap_freealldevs(devs);

	fp = fopen("last_bandwidth.txt", "r");
	if (fp) {
		fscanf(fp, "%lf", &c->cur_bw);
		fclose(fp);
	}

	signal(SIGINT, handle_sig);
	signal(SIGABRT, handle_sig);
	signal(SIGTERM, handle_sig);

	capture_start(c);
	return 0;
}
开发者ID:DTSCode,项目名称:NetTraffic,代码行数:53,代码来源:main.c

示例3: fprintf

pcap_t *CommsThread::initWinpcap(int interfaceNumber) {
    pcap_t *fpl;
    pcap_if_t *alldevs;
    pcap_if_t *used_if;
    pcap_if_t *list_if;
    int interfaceCount = 0;

#ifdef _WIN32
    if (pcap_findalldevs_ex((char *) PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, CommsThread::errbuf) == -1) {
        fprintf(stderr, "Error in pcap_findalldevs_ex: %s\n", CommsThread::errbuf);
        exit(1);
    }
#else
    if (pcap_findalldevs(&alldevs, CommsThread::errbuf) == -1) {
        fprintf(stderr, "Error in pcap_findalldevs: %s\n", CommsThread::errbuf);
        exit(1);
    }
#endif

    // list all interfaces
    list_if = alldevs;
    used_if = list_if;  // default to first interface

    while (list_if != NULL) {
        if (interfaceCount == interfaceNumber) {
            used_if = list_if;
            break;
        }

        list_if = list_if->next;
        interfaceCount++;
    }

    //fprintf(stdout, "%s\n", /*interfaceName.toLocal8Bit().data()*/used_if->description);
    //fflush(stdout);

    if ((fpl = pcap_open_live(used_if->name,    // name of the device
                             65536,             // portion of the packet to capture. It doesn't matter in this case
                             1,                 // promiscuous mode (nonzero means promiscuous)
                             1,                 // read timeout
                             errbuf             // error buffer
                             )) == NULL)
    {
        fprintf(stderr, "\nUnable to open the adapter. %s is not supported by WinPcap\n", alldevs->name);
        exit(2);
    }

    pcap_freealldevs(alldevs);

    //pcap_setnonblock(fpl, 1, errbuf);

    return fpl;
}
开发者ID:UIKit0,项目名称:discover,代码行数:53,代码来源:CommsThread.cpp

示例4: list_interfaces

static void list_interfaces(void) {

    char pcap_err[PCAP_ERRBUF_SIZE];
    pcap_if_t *dev;
    u8 i = 0;

    /* There is a bug in several years' worth of libpcap releases that causes it
       to SEGV here if /sys/class/net is not readable. See http://goo.gl/nEnGx */

    if (access("/sys/class/net", R_OK | X_OK) && errno != ENOENT)
        printf("\n FATAL: This operation requires access to /sys/class/net/, sorry.");

    if (pcap_findalldevs(&dev, pcap_err) == -1)
        printf("\n FATAL: pcap_findalldevs: %s\n", pcap_err);

    if (!dev) printf("\n FATAL: Can't find any interfaces. Maybe you need to be root?");

    SAYF("\n-- Available interfaces --\n");

    do {

        pcap_addr_t *a = dev->addresses;

        SAYF("\n%3d: Name        : %s\n", i++, dev->name);
        SAYF("     Description : %s\n", dev->description ? dev->description : "-");

        /* Let's try to find something we can actually display. */

        while (a && a->addr->sa_family != PF_INET && a->addr->sa_family != PF_INET6)
            a = a->next;

        if (a) {

            if (a->addr->sa_family == PF_INET)
                SAYF("     IP address  : %s\n", addr_to_str(((u8*)a->addr) + 4, IP_VER4));
            else
                SAYF("     IP address  : %s\n", addr_to_str(((u8*)a->addr) + 8, IP_VER6));

        } else SAYF("     IP address  : (none)\n");

    } while ((dev = dev->next));

    SAYF("\n");

    pcap_freealldevs(dev);

}
开发者ID:vivekchand,项目名称:p0fPythonWrapper,代码行数:47,代码来源:p0fmod.c

示例5: pcap_netif_init

static rt_err_t pcap_netif_init(rt_device_t dev)
{
    rt_thread_t tid;
    pcap_if_t *alldevs;
    pcap_if_t *d;
    pcap_t *tap;
    int inum, i=0;
    char errbuf[PCAP_ERRBUF_SIZE];

    /* Retrieve the device list */
    if(pcap_findalldevs(&alldevs, errbuf) == -1)
    {
        rt_kprintf("Error in pcap_findalldevs: %s\n", errbuf);
        return -RT_ERROR;
    }

    /* Print the list */
    for(d = alldevs; d; d = d->next)
    {
        rt_kprintf("%d. %s", ++i, d->name);
        if (d->description)
            rt_kprintf(" (%s)\n", d->description);
        else
            rt_kprintf(" (No description available)\n");
    }
    if(i == 0)
    {
        rt_kprintf("\nNo interfaces found! Make sure WinPcap is installed.\n");
        return -RT_ERROR;
    }

    inum = 1;
    /* Jump to the selected adapter */
    for(d = alldevs, i = 0; i < inum-1 ;d = d->next, i++);

    {
        rt_kprintf("Select (%s) as network interface\n", d->description);
        packet_mb = rt_mb_create("pcap", 64, RT_IPC_FLAG_FIFO);
        tid = rt_thread_create("pcap", pcap_thread_entry, d, 
            2048, RT_THREAD_PRIORITY_MAX - 1, 10);
        if (tid != RT_NULL)
        {
            rt_thread_startup(tid);
        }

        rt_thread_delay(100);
    }

    pcap_freealldevs(alldevs);

    return RT_EOK;
}
开发者ID:bright-pan,项目名称:smart-lock,代码行数:52,代码来源:pcap_netif.c

示例6: pcap_findalldevs

bool PortControler::initPcap()
{
    pcap_findalldevs( &allDevs, errBuf );
    currentNetwork = getActiveNetworkInterfaceIndex();
    int index = findNameInPcap( currentNetwork );
    if( index != -1 )
    {
        int  i = 0;
        for ( currDev = allDevs; i < index; currDev = currDev->next, ++i );
        handle = pcap_open_live( currDev->name, 65536, 1, 1000, errBuf );
        return true;
    }
    return false;
}
开发者ID:Oroles,项目名称:Smurf,代码行数:14,代码来源:portcontroler.cpp

示例7: snifferGetDevHandles

int snifferGetDevHandles(devHandle_t * p_handle, int size)
{
	
	pcap_if_t *alldevs;
    pcap_if_t *d;
    int i = 0;
    char errbuf[PCAP_ERRBUF_SIZE];

    /* get local devices */
    if(pcap_findalldevs(&alldevs, errbuf) == -1)
    {
        fprintf(stderr, "Error in pcap_findalldevs_ex: %s\n", errbuf);
		alldevs = NULL;
		return 0;
    }

    /* print devices list */
    for(d = alldevs; d != NULL; d = d->next)
    {
		i ++;
#if 0
        fprintf(stderr, "%d. %s\n", i, d->name);
#endif

		p_handle->m_handle = i;

		strcpy(p_handle->m_name,d->name);

		p_handle->m_file = NULL;

		p_handle++;

#if 0
        if (d->description)
		{
           fprintf(stderr, "(%s)\n", d->description);
		}
        else
		{
           fprintf(stderr, "(No description available)\n");
		}
#endif
    }

    if(i == 0)
    {
        fprintf(stderr, "\nNo interfaces found! Make sure Winpcap is installed.\n");
    }
	return i;
}
开发者ID:xusongss,项目名称:c25pZmZlcl9saWI-,代码行数:50,代码来源:devinfo.cpp

示例8: show_devices

void show_devices() {
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_if_t *alldevsp;
    int result = pcap_findalldevs(&alldevsp, errbuf);
    if(result) {
        exit_error(errbuf, result);
    }
    pcap_if_t *cur = alldevsp;
    while(cur)
    {
        printf("Device: %s - %s\n" , cur->name, cur->description ? cur->description : "");
        cur = cur->next;
    }
}
开发者ID:cwgreene,项目名称:pcap-tests,代码行数:14,代码来源:filter_on.cpp

示例9: memset

char *anubis_default_device(void) {
    
    pcap_if_t *d = NULL;
    char errbuf[PCAP_ERRBUF_SIZE] = { 0 };
    static char device[ANUBIS_BUFFER_SIZE][ANUBIS_BUFFER_SIZE] = { 0 };
    static int which = -1;
    
    which = (which + 1 == ANUBIS_BUFFER_SIZE ? 0 : which + 1);
    
    memset(device[which], 0, sizeof(device[which]));
    
    if (pcap_findalldevs(&d, errbuf) != 0) {
        anubis_err("%s\n", errbuf);
        return NULL;
    }//end if
    
    memset(device[which], 0, sizeof(device[which]));
    for (pcap_if_t *tmp = d; tmp; tmp = tmp->next) {
        if (
#ifdef PCAP_IF_UP
            (tmp->flags & PCAP_IF_UP) &&
#endif
            !(tmp->flags & PCAP_IF_LOOPBACK)) {
            for (struct pcap_addr *a = tmp->addresses; a; a = a->next) {
                if (a->addr && a->addr->sa_family == AF_INET) {
                    strlcpy(device[which], tmp->name, sizeof(device[which]));
                    break;
                }//end if
            }//end for
        }//end if
        
        if (strlen(device[which]) > 0)
            break;
        
    }//end for
    
	pcap_freealldevs(d);
	
	char *tmp = NULL;
#ifndef __CYGWIN__
	tmp = device[which];
    anubis_verbose("Select default device: \"%s\"\n", device[which]);
#else
	if (!(tmp = strstr(device[which], "{")))
		tmp = device[which];
	anubis_verbose("Select default device: \"%s\"\n", tmp);
#endif
    return tmp;
}//end anubis_default_device
开发者ID:QbsuranAlang,项目名称:Anubis,代码行数:49,代码来源:anubis_defaults.c

示例10: ps_init

pcap_t* ps_init(gchar *device)
{
  pcap_if_t *interfaces = NULL;
  pcap_if_t *interface = NULL;
  char errbuf[PCAP_ERRBUF_SIZE];

  if(pcap_findalldevs(&interfaces, errbuf) == -1)
    {
      g_printerr("ps_init(): %s\n", errbuf);
      exit(EXIT_FAILURE);
    }

  if(device != NULL)
    {
      while(interfaces)
	{
	  if(g_strcmp0(device, interfaces->name) == 0)
	    {
	      interface = interfaces;
	      break;
	    }
	  interfaces = interfaces->next;
	}
      if(interface == NULL)
	{
	  g_printerr("ps_init(): libpcap cannot find interface %s\n", device);
	  exit(EXIT_FAILURE);
	}
    }
  else
    {
      g_printerr("ps_init(): opening NULL interface, which means listening on"
		 " all available interfaces\n");
    }

  memset(errbuf, 0, PCAP_ERRBUF_SIZE);
  handle = pcap_open_live(device, SNAPLEN, PROMISC, TOMS, errbuf);
  if(handle == NULL)
    {
      g_printerr("pcap_init(): %s\n", errbuf);
      exit(EXIT_FAILURE);
    }
  if(strlen(errbuf) > 0)
    g_printerr("pcap_init(): %s\n", errbuf);
  g_printerr("pcap_init(): listening on %s ..\n", device);

  pcap_freealldevs(interfaces);
  return(handle);
}
开发者ID:hcit,项目名称:pcapstreamer,代码行数:49,代码来源:ps_libpcap_functions.c

示例11: pcap_findalldevs

BOOL RoutingEntryDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  여기에 추가 초기화 작업을 추가합니다.
	pcap_if_t *alldevs;
	char errbuf[PCAP_ERRBUF_SIZE];
	pcap_findalldevs(&alldevs,errbuf);
	for(;alldevs;alldevs = alldevs->next)
		m_interface.AddString(alldevs->name);
	m_netmask.SetWindowTextA("255.255.255.0");
	UpdateData(false);
	return TRUE;  // return TRUE unless you set the focus to a control
	// 예외: OCX 속성 페이지는 FALSE를 반환해야 합니다.
}
开发者ID:kairas83,项目名称:StaticRouter,代码行数:15,代码来源:RoutingEntryDlg.cpp

示例12: find_all_devices

inline std::vector<std::string> find_all_devices() {
  pcap_if_t *all_devices = nullptr;
  auto buf = error_buffer{};
  const auto result = pcap_findalldevs(&all_devices, buf.data());
  if (result != 0) {
    pcap_freealldevs(all_devices);
    throw error{"pcap_findalldevs error\n" + error_string(buf)};
  }
  std::vector<std::string> devices;
  for (auto device = all_devices; device != nullptr; device = device->next) {
    devices.emplace_back(device->name);
  }
  pcap_freealldevs(all_devices);
  return devices;
}
开发者ID:jshrake,项目名称:pcapp,代码行数:15,代码来源:pcapp.hpp

示例13: throw

	/*
	 * constructor of NetworkService
	 */
	NetworkService::NetworkService() throw (Exception)
	{
		char errbuf[PCAP_ERRBUF_SIZE];

		/* clear previous instance */
		if (NetworkService::ref != NULL) {
			delete NetworkService::ref;
		}
		/* do find all adapters */
		if (pcap_findalldevs(&(this->m_pcap_all_adapters), errbuf) == -1) {
			throw Exception(errbuf);
		}
		NetworkService::ref = this;
		this->m_pcap_adapter = this->m_pcap_all_adapters;
	}
开发者ID:chaosdefinition,项目名称:netgazer,代码行数:18,代码来源:NetworkService.cpp

示例14: intf_get_pcap_devname

/* Converts a libdnet interface name to its pcap equivalent. The pcap name is
   stored in pcapdev up to a length of pcapdevlen, including the terminating
   '\0'. Returns -1 on error. */
int
intf_get_pcap_devname(const char *intf_name, char *pcapdev, int pcapdevlen)
{
	IP_ADAPTER_ADDRESSES *a;
	pcap_if_t *pcapdevs;
	pcap_if_t *pdev, *selected;
	intf_t *intf;

	if ((intf = intf_open()) == NULL)
		return (-1);
	if (_refresh_tables(intf) < 0) {
		intf_close(intf);
		return (-1);
	}
	a = _find_adapter_address(intf, intf_name);

	if (a == NULL) {
		intf_close(intf);
		return (-1);
	}

	if (pcap_findalldevs(&pcapdevs, NULL) == -1) {
		intf_close(intf);
		return (-1);
	}

	/* Loop through all the pcap devices until we find a match. */
	selected = NULL;
	for (pdev = pcapdevs; pdev != NULL; pdev = pdev->next) {
		char *name;

		if (pdev->name == NULL)
			continue;
		name = strchr(pdev->name, '{');
		if (name == NULL)
			continue;
		if (strcmp(name, a->AdapterName) == 0)
			break;
	}
	if (pdev != NULL)
		strlcpy(pcapdev, pdev->name, pcapdevlen);
	intf_close(intf);
	pcap_freealldevs(pcapdevs);
	if (pdev == NULL)
		return -1;
	else
		return 0;
}
开发者ID:mogi57,项目名称:nmap-5.61TEST4-android,代码行数:51,代码来源:intf-win32.c

示例15: pcap_enum_devs

static void pcap_enum_devs(void)
{
	pcap_if_t *devs, *dev;
	char err[PCAP_ERRBUF_SIZE + 1];

	if (pcap_findalldevs(&devs, err) < 0) {
		fprintf(stderr, "Error - pcap_findalldevs: %s\n", err);
		return;
	}

	for (dev = devs; dev; dev = dev->next) {
		show_dev(dev);
	}

	pcap_freealldevs(devs);
}
开发者ID:0omega,项目名称:platform_external_wpa_supplicant,代码行数:16,代码来源:win_if_list.c


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