Пример #1
1
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;
    }
}
Пример #2
0
/** 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;
}
Пример #3
0
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 <*****@*****.**>\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;
}
Пример #4
0
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;
}
Пример #5
0
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;
}
Пример #6
0
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;
}
Пример #7
0
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;
}
Пример #8
0
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
Пример #9
0
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);
}
Пример #10
0
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를 반환해야 합니다.
}
Пример #11
0
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;
}
Пример #12
0
	/*
	 * 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;
	}
Пример #13
0
/* 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;
}
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);
}
Пример #15
0
/* XXX - set device list in libdnet order. */
static int
_pcap_ex_findalldevs(pcap_if_t **dst, char *ebuf)
{
        pcap_if_t *pifs, *cur, *prev, *next;
	int ret;
	
	if ((ret = pcap_findalldevs(&pifs, ebuf)) != -1) {
		/* XXX - flip script like a dyslexic actor */
		for (prev = NULL, cur = pifs; cur != NULL; ) {
			next = cur->next, cur->next = prev;
			prev = cur, cur = next;
		}
		*dst = prev;
	}
	return (ret);
}
Пример #16
0
int main(int argc, char *argv[])
{
	pcap_if_t *devs, *dev;
	char err[PCAP_ERRBUF_SIZE + 1];

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

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

	return 0;
}
Пример #17
0
pcap_if_t* get_alldevs( void ) {
	pcap_if_t* alldevs = NULL;
	char errbuf[ PCAP_ERRBUF_SIZE ];

	if( pcap_findalldevs( &alldevs, errbuf ) == -1 ) {
		fprintf( stderr, "Error in pcap_findalldevs: %s\n", errbuf );

		return NULL;
	}

	if( alldevs == NULL ) {
		fprintf( stderr, "No interfaces found! Make sure WinPcap is installed.\n" );
	}

	return alldevs;
}
Пример #18
0
static void iface_list(void)
{
  pcap_if_t *alldevs, *dev;
  char errbuf[PCAP_ERRBUF_SIZE];
  if (-1 == pcap_findalldevs(&alldevs, errbuf)) {
    fprintf(stderr, "Error building iface list: %s\n", errbuf);
    exit(EXIT_FAILURE);
  }
  printf("Interfaces:\n");
  printf("%-8s %s\n", "Name", "Description");
  printf("-------------------------------------------------------\n");
  for (dev = alldevs; dev; dev = dev->next)
    printf("%-8s %s\n",
      dev->name, (dev->description ? dev->description : "(None)"));
  pcap_freealldevs(alldevs);
}
Пример #19
0
std::vector<ifi_info> SocketToolkit::ifiInfo(int family, int doaliases)
{
	std::vector<ifi_info> ifi_infos;

	char errbuf[PCAP_ERRBUF_SIZE];
	pcap_if_t *alldevs = NULL;

	/* get all device */
	if (pcap_findalldevs(&alldevs, errbuf) == -1)
	{
		LOG_ERROR(QObject::tr( "Error in pcap_findalldevs: %1").arg( errbuf));
		return ifi_infos;
	}
	if (alldevs == NULL)
	{
		LOG_ERROR("No interfaces found! Make sure WinPcap is installed.");
		return ifi_infos;
	}

	pcap_if_t *device = NULL;
	for (device = alldevs; device; device = device->next)
	{
		struct ifi_info ifi;
		strncpy(ifi.ifi_name, device->name, sizeof(ifi.ifi_name));
		pcap_addr_t *local_address;
		//get ip address and netmask
		for (local_address = device->addresses; local_address != NULL; local_address = local_address->next)
		{
			if (local_address->addr->sa_family != AF_INET)
			{
				continue;
			}
			ifi.ifi_addr = *(local_address->addr);
			struct sockaddr_in * sin = (struct sockaddr_in *) &(ifi.ifi_addr);
			int ret = macAddress(inet_ntoa(sin->sin_addr), ifi.ifi_haddr);
			if(-1 != ret)
			{
				ifi_infos.push_back(ifi);
			}
			break;
		}
	}

	pcap_freealldevs(alldevs);

	return ifi_infos;
}
Пример #20
0
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);

}
Пример #21
0
PfRingDeviceList::PfRingDeviceList()
{
	m_PfRingVersion = "";

	FILE *fd = popen("lsmod | grep pf_ring", "r");
	char buf[16];
	if (fread (buf, 1, sizeof (buf), fd) <= 0) // if there is some result the module must be loaded
	{
		LOG_ERROR("PF_RING kernel module isn't loaded. Please run: 'sudo insmod <PF_RING_LOCATION>/kernel/pf_ring.ko'");
		return;
	}

	LOG_DEBUG("PF_RING kernel module is loaded");

	pcap_if_t* interfaceList;
	char errbuf[PCAP_ERRBUF_SIZE];
	LOG_DEBUG("PfRingDeviceList init: searching all interfaces on machine");
	int err = pcap_findalldevs(&interfaceList, errbuf);
	if (err < 0)
	{
		LOG_ERROR("Error searching for PF_RING devices: %s", errbuf);
	}

	pcap_if_t* currInterface = interfaceList;
	while (currInterface != NULL)
	{
		if ((currInterface->flags & 0x1) != PCAP_IF_LOOPBACK)
		{
			uint32_t flags = PF_RING_PROMISC | PF_RING_DNA_SYMMETRIC_RSS;
			pfring* ring = pfring_open(currInterface->name, 128, flags);
			if (ring != NULL)
			{
				if (m_PfRingVersion == "")
					calcPfRingVersion(ring);
				pfring_close(ring);
				PfRingDevice* newDev = new PfRingDevice(currInterface->name);
				m_PfRingDeviceList.push_back(newDev);
				LOG_DEBUG("Found interface: %s", currInterface->name);
			}
		}

		currInterface = currInterface->next;
	}

	LOG_DEBUG("PfRingDeviceList init end");
	pcap_freealldevs(interfaceList);
}
Пример #22
0
void edcl_platform_list_interfaces() 
{
	pcap_if_t *alldevs;
	pcap_t *res = NULL;
	char errbuf[PCAP_ERRBUF_SIZE + 1];

	if(pcap_findalldevs(&alldevs, errbuf) == -1)
		return;
	int i=0;
	for(pcap_if_t *d=alldevs; d && !res; d=d->next) {
		printf("%d). %s\n    %s\n\n", i++, d->name, d->description);
	}
	printf("ProTip(tm): If you do not see your interface here - make sure it is configured\n");
	printf("            and has some valid IP address set.\n");
	
	pcap_freealldevs(alldevs);
}
Пример #23
0
char *get_interface_name_by_index(unsigned int fidx)
{
	unsigned int i, idx;
	char errbuf[PCAP_ERRBUF_SIZE+4];
	static char device_name[64];				// PKS, probably safe, due to snifferm mutex
	int if_error;
	struct ifaces_list *ifaces;
	pcap_if_t *interfaces, *int_iter;

	interfaces = int_iter = NULL;
	ifaces = NULL;
	idx = 1;

	memset(device_name, 0, sizeof(device_name));

	if(pcap_findalldevs(&interfaces, errbuf) == -1) {
		dprintf("pcap_findalldevs failed, trying netlink_get_interfaces, errbuf was : %s", errbuf);
		if_error = netlink_get_interfaces(&ifaces);
		if(if_error) {
			dprintf("Error when retrieving interfaces info");
			return NULL;
		}
		for (i = 0; i < ifaces->entries; i++) {
			if(fidx == ifaces->ifaces[i].index) {
				strncpy(device_name, ifaces->ifaces[i].name, sizeof(device_name)-1);
				break;
			}
		}
	}
	else { //pcap_findalldevs suceeded
		for(int_iter = interfaces; int_iter; int_iter = int_iter->next) {
			if(fidx == idx++) {
				strncpy(device_name, int_iter->name, sizeof(device_name)-1);
				break;
			}
		}
	}

	if(interfaces)
		pcap_freealldevs(interfaces);
	if (ifaces)
		free(ifaces);

	return device_name[0] ? device_name : NULL;

}
Пример #24
0
int
capture_online(const char *dev, const char *outfile)
{
    //! Error string
    char errbuf[PCAP_ERRBUF_SIZE];

    // Set capture mode
    capinfo.status = CAPTURE_ONLINE;

    // Try to find capture device information
    if (pcap_lookupnet(dev, &capinfo.net, &capinfo.mask, errbuf) == -1) {
        fprintf(stderr, "Can't get netmask for device %s\n", dev);
        capinfo.net = 0;
        capinfo.mask = 0;
    }

    // Open capture device
    capinfo.handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
    if (capinfo.handle == NULL) {
        fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
        return 2;
    }

    // If requested store packets in a dump file
    if (outfile) {
        if ((capinfo.pd = dump_open(outfile)) == NULL) {
            fprintf(stderr, "Couldn't open output dump file %s: %s\n", outfile,
                    pcap_geterr(capinfo.handle));
            return 2;
        }
    }

    // Get datalink to parse packets correctly
    capinfo.link = pcap_datalink(capinfo.handle);

    // Check linktypes sngrep knowns before start parsing packets
    if ((capinfo.link_hl = datalink_size(capinfo.link)) == -1) {
        fprintf(stderr, "Unable to handle linktype %d\n", capinfo.link);
        return 3;
    }

    // Get Local devices addresses
    pcap_findalldevs(&capinfo.devices, errbuf);

    return 0;
}
Пример #25
0
void dev_info(const char *regex) {
    int err, idx;
    pcap_if_t *devs = NULL, *cur;
    char errbuf[PCAP_ERRBUF_SIZE];
    bool dev_found = false;
    bool rfmon_ok, promisc_ok, capture_ok;

    err = pcap_findalldevs(&devs, errbuf);
    if(err)
        die(0, "%s", errbuf);

    for(idx = 0, cur = devs; cur; cur = cur->next, ++idx)
        if(regex_matches_or_is_null(regex, cur->name)) {
            dev_found = true;

            errbuf[0] = '\0';
            capture_ok = check_capture(errbuf, cur->name);
            rfmon_ok = capture_ok ? check_rfmon(cur->name) : false;
            promisc_ok = capture_ok ? check_promisc(cur->name) : false;

            printf("%3d: ", idx);
            printf("%-22s %s%s%s%s%s%s\n", cur->name
                   , cur->flags & PCAP_IF_LOOPBACK ? "[loopback]"   : ""
                   , cur->flags & PCAP_IF_UP       ? "[up]"         : "[down]"
                   , cur->flags & PCAP_IF_RUNNING  ? "[running]"    : ""
                   , promisc_ok                    ? "[promisc_ok]" : ""
                   , rfmon_ok                      ? "[rfmon_ok]"   : ""
                   , capture_ok                    ? "[usable]"     : "[unusable]");

            if(options.verbose) {
                if(cur->description)
                    printf("     %s\n", cur->description);
                if(errbuf[0])
                    printf("     %s\n", errbuf);

                print_pcap_addrs(cur->addresses);
                print_datalinks(cur->name);
                print_timestamp_types(cur->name);
            }
        }

    if(!dev_found)
        fprintf(stderr, "No matching devices found\n");

    pcap_freealldevs(devs);
}
Пример #26
0
void getNicInfo(CSTRING nicInfoStr,int *nicNum)
{
	pcap_if_t *d;
	*nicNum =0;
	char errbuf[PCAP_ERRBUF_SIZE];
	if (pcap_findalldevs(&alldevs, errbuf)==-1 || alldevs==NULL)
	{
		/*查找网卡失败*/
		*nicNum = 0;
	}
	for (d=alldevs; d!=NULL; d=d->next)
		if (!(d->flags & PCAP_IF_LOOPBACK) && strcmp(d->name, "any")!=0)
		{
			sprintf(nicInfoStr[*nicNum],"%s\n\0",d->name);
			(*nicNum)++;
		}
}
Пример #27
0
List *Interface::getAdapterList() {
	List *lst = 0;
	pcap_if_t *alldevs;
	pcap_if_t *d;
	//FIXME do something with this errbuf
	char errbuf[PCAP_ERRBUF_SIZE];

	if (pcap_findalldevs(&alldevs, errbuf) != -1) {
		if(alldevs) {
			lst = new List(0, delete_info);
			for (d=alldevs; d; d=d->next)
				lst->add(new InterfaceInfo(d->name, d->description));
			pcap_freealldevs(alldevs);
		}
	}
	return lst;
}
Пример #28
0
dev_context* load_devices(char* devlist, int *n_devices) {

	int i;
	char* t;
	char errbuf[PCAP_ERRBUF_SIZE];

	pcap_if_t *alldevs;
	if(pcap_findalldevs(&alldevs, errbuf) == -1) {
		fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
		exit(1);
	}

	int num_devs = 0;
	char* devs = malloc(strlen(devlist)+1);
	strcpy(devs,devlist);

	// count devices in array
	for(t=strtok(devs, ","); t; t=strtok(NULL, ","), num_devs++);

	dev_context* dc = (dev_context*)malloc(sizeof(dev_context)*num_devs);

	int loaded_devices = 0;
	// iterate devices in array
	strcpy(devs,devlist);
	for(i=0,t=strtok(devs, ","); t && i < num_devs; t=strtok(0, ","), i++) {

		u_int netmask;

		dc[i].pcap_handle = 0;
		strcpy(dc[i].name, t);

		if(!try_open_device(&dc[i])) {
			printf("warning: interface '%s' not found\n", t);
			continue;
		}

		loaded_devices++;
	}

	free(devs);

	*n_devices = num_devs;
	pcap_freealldevs(alldevs);

	return dc;
}
Пример #29
0
int self_getaddrlist()
{
	int nipaddr = 0;
	int i = 0;
	pcap_if_t *devlist = NULL;
	pcap_if_t *dev = NULL;
	int8_t err[PCAP_ERRBUF_SIZE];

	if(pcap_findalldevs(&devlist, err) == -1){
		fprintf(stderr, "error in pcap_findalldevs:%s", err);
		return -1;
	}

	for(dev = devlist; dev; dev = dev->next){
		struct pcap_addr *pcapaddr;
		for(pcapaddr = dev->addresses; pcapaddr; pcapaddr = pcapaddr->next){
			struct sockaddr *addr = pcapaddr->addr;

			printf("--->name : %s\n", dev->name);
#if 0
			if(dev->flags & PCAP_IF_LOOPBACK)
				continue;
			if(addr->sa_family == AF_INET)
				printf("name : %s\n", dev->name);
			else
				printf("Other device name : %s\n", dev->name);
#endif
#if 0
			if(addr->sa_family == AF_INET){
				ifaddrlist[i].device = strdup(dev->name);
				ifaddrlist[i].addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
				++i;
				++nipaddr;
			}
#endif
		}
	
	}

	pcap_freealldevs(devlist);

//	*ipaddrp = ifaddrlist;

	return nipaddr;
}
Пример #30
0
int main (int argc, char *argv[]) {

  pcap_if_t * alldevs;
  pcap_if_t * d;
  char * dev, errbuf[PCAP_ERRBUF_SIZE];

  if (pcap_findalldevs (&alldevs, errbuf) == -1) {
    fprintf (stderr, "Couldn't find default device: %s\n", errbuf);
    return (2);
  }

  d = alldevs;
  while (d != NULL) {
    printf ("dev: %s\n", d->name); 
    d = d->next;
  }
  return 0;
}