/* * 初始化缓存区,生成接口句柄 * skfd: 被初始化的接口句柄 * @return: 0: 成功 * -1: 初始化接口句柄失败 */ static int eapol_init(pcap_t **skfd) { pcap_if_t *alldevs, *d; char errbuf[PCAP_ERRBUF_SIZE]; char ifbuff[8+IFNAMSIZ] = "rpcap://"; sendethii = (ethII_t*)sendbuff; sendeapol = (eapol_t*)((uchar*)sendethii+sizeof(ethII_t)); sendeap = (eap_t*)((uchar*)sendeapol+sizeof(eapol_t)); sendeapbody = (eapbody_t*)((uchar*)sendeap+sizeof(eap_t)); if (-1 == pcap_findalldevs(&alldevs, errbuf)) { _M("Get interface: %s\n", errbuf); return -1; } for (d = alldevs; NULL != d; d = d->next) if (0 == strcmp(ifname, d->name)) break; if (NULL == d) return -1; /* 获取mac */ LPADAPTER lpAdapter = PacketOpenAdapter(d->name); if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE)) return -1; PPACKET_OID_DATA oidData = malloc(ETH_ALEN + sizeof(PACKET_OID_DATA)); if (NULL == oidData) { PacketCloseAdapter(lpAdapter); return -1; } oidData->Oid = OID_802_3_CURRENT_ADDRESS; oidData->Length = ETH_ALEN; memset(oidData->Data, 0, ETH_ALEN); if (0 == PacketRequest(lpAdapter, FALSE, oidData)) { free(oidData); return -1; } memcpy(client_mac, oidData->Data, ETH_ALEN); PacketCloseAdapter(lpAdapter); _D("%s's MAC: %02X-%02X-%02X-%02X-%02X-%02X\n", ifname, client_mac[0],client_mac[1],client_mac[2], client_mac[3],client_mac[4],client_mac[5]); /* 获取网络接口句柄 */ strncat(ifbuff, ifname, IFNAMSIZ); if (NULL == (*skfd = pcap_open(d->name, MTU_MAX, PCAP_OPENFLAG_PROMISCUOUS, TIMEOUT*1000, NULL, errbuf))) { _M("Get interface handler:%s\n", errbuf); pcap_freealldevs(alldevs); return -1; } pcap_freealldevs(alldevs); return 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_ex(PCAP_SRC_IF_STRING, NULL, &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; }
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; }
void pcaplistener::setFilter() { int res; struct tm * ltime; char timestr[16]; struct pcap_pkthdr *header; const u_char *pkt_data; time_t local_tv_sec; if(pcap_compile(pcap, &filter, fexpr.c_str(), 1, mask) < 0) { fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n"); /* Free the device list */ pcap_freealldevs(allDevs); exit(0); } if(pcap_setfilter(pcap, &filter) < 0) { fprintf(stderr,"\nError setting the filter.\n"); /* Free the device list */ pcap_freealldevs(allDevs); exit(0); } printf("Filter is set for %s\n", fexpr.c_str()); printf("Filtering for packets...\n"); /* Retrieve the packets */ /* Using pcap_next_ex() instead of pcap_loop() */ while((res = pcap_next_ex(pcap, &header, &pkt_data)) >= 0) { if(res == 0) /* Timeout elapsed */ continue; /* convert the timestamp to readable format */ local_tv_sec = header->ts.tv_sec; ltime = localtime(&local_tv_sec); strftime( timestr, sizeof timestr, "%H:%M:%S", ltime); printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len); } if(res == -1) { printf("Error reading the packets: %s\n", pcap_geterr(pcap)); exit(0); } return; }
int net_interface_imp::set_capture_ether_type(uint16_t * ether_type, uint32_t count) { struct bpf_program fcode; char ether_type_string[512]; char ether_type_single[64]; const unsigned char * ether_packet = NULL; struct pcap_pkthdr pcap_header; ether_type_string[0] = 0; for (uint32_t index_i = 0; index_i < count; index_i++) { sprintf(ether_type_single, "ether proto 0x%04x", ether_type[index_i]); strcat(ether_type_string, ether_type_single); if ((index_i + 1) < count) { strcat(ether_type_string, " or "); } } /******************************************************* Compile a filter ************************************************/ if (pcap_compile(pcap_interface, &fcode, ether_type_string, 1, 0) < 0) { log_imp_ref->post_log_msg(LOGGING_LEVEL_ERROR, "Unable to compile the packet filter."); pcap_freealldevs(all_devs); // Free the device list return -1; } /*************************************************** Set the filter *******************************************/ if (pcap_setfilter(pcap_interface, &fcode) < 0) { log_imp_ref->post_log_msg(LOGGING_LEVEL_ERROR, "Error setting the filter."); pcap_freealldevs(all_devs); // Free the device list return -1; } /*********** Flush any packets that might be present **********/ for (uint32_t index_j = 0; index_j < 1; index_j++) { ether_packet = pcap_next(pcap_interface, &pcap_header); if (!ether_packet) { break; } } return 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; }
int GetAdapterName(int AdapterNumber, char* AdapterName, int AdapterNameSize) { int i; pcap_if_t *alldevs; pcap_if_t *device; char errbuf[PCAP_ERRBUF_SIZE + 1] = ""; if (pcap_findalldevs(&alldevs, errbuf) == -1) { printf("Error when getting the list of the installed adapters%s\n", errbuf ); return nbFAILURE; } // Get interface name i= 1; for (device=alldevs; device != NULL; device=device->next) { if (i == AdapterNumber) { strncpy(AdapterName, device->name, AdapterNameSize); AdapterName[AdapterNameSize-1]= 0; // Free the device list pcap_freealldevs(alldevs); return nbSUCCESS; } i++; } return nbFAILURE; }
/* This function blatantly ripped from http://www.winpcap.org/docs/docs31/html/group__wpcap__tut1.html */ void list_devices (void) { pcap_if_t *alldevs; pcap_if_t *d; int i=0; char errbuf[PCAP_ERRBUF_SIZE]; /* Retrieve the device list from the local machine */ if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1) { fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf); exit(1); } /* Print the list */ for(d= alldevs; d != NULL; d= d->next) { printf("%d. %s", ++i, d->name); if (d->description) printf(" (%s)\n", d->description); else printf(" (No description available)\n"); } if (i == 0) { printf("\nNo interfaces found! Make sure WinPcap is installed.\n"); return; } /* We don't need any more the device list. Free it */ pcap_freealldevs(alldevs); }
int main(int argc, char *argv[]) { struct interface_list *itfc_list; char iface[100]; pd = NULL; gui_register_exit(&exit_fun); gui_register_stop(&stop_fun); itfc_list = get_interface_list(); gui_set_interface_list(itfc_list); guipid = gui_pthread_start(&argc, &argv); cappid = pthread_self(); signal(SIGUSR1, idle_sig); while (gui_wait_capture(iface) == 0) { start_capture(iface); } printf("exit\n"); pcap_freealldevs(if_list); return 0; }
char * pcap_ex_name(char *name) { #ifdef _WIN32 /* * XXX - translate from libdnet logical interface name to * WinPcap native interface name. */ static char pcap_name[256]; pcap_if_t *pifs, *pif; char ebuf[128]; int idx, i = 0; /* XXX - according to the WinPcap FAQ, no loopback support??? */ if (strncmp(name, "eth", 3) != 0 || sscanf(name+3, "%u", &idx) != 1 || _pcap_ex_findalldevs(&pifs, ebuf) == -1) { return (name); } for (pif = pifs; pif != NULL; pif = pif->next) { if (i++ == idx) { strncpy(pcap_name, pif->name, sizeof(pcap_name)-1); pcap_name[sizeof(pcap_name)-1] = '\0'; name = pcap_name; break; } } pcap_freealldevs(pifs); return (name); #else return (name); #endif }
struct if_nameindex * pcap_nameindex (void) { #if defined (WINPCAP) || defined (LIBPCAP) char buffer [PCAP_ERRBUF_SIZE]; pcap_if_t * devices = (pcap_if_t *)(0); pcap_if_t * device; if (pcap_findalldevs (&devices, buffer) != -1) { struct if_nameindex * ifs; struct if_nameindex * ifp; unsigned count = 1; for (device = devices; device; device = device->next) { count++; } ifp = ifs = (struct if_nameindex *)(malloc (count * sizeof (struct if_nameindex))); if (ifs) for (device = devices; device; device = device->next) { ifp->if_index = device->index; ifp->if_name = strdup (device->name); ifp++; } memset (ifp, 0, sizeof (* ifp)); pcap_freealldevs (devices); return (ifs); } #endif return ((struct if_nameindex *)(0)); }
void Call_Device(char **C_dev) { pcap_if_t *alldevs; pcap_if_t *d; int i=0; char Select_device[10]; char errbuf[PCAP_ERRBUF_SIZE]; /* Retrieve the device list */ if (pcap_findalldevs(&alldevs, errbuf) == -1) fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf); /* Print the list */ for(d=alldevs;d;d=d->next) printf("%d. %s \n", ++i, d->name); if(i==0) printf("\nNo interfaces found! Make sure WinPcap is installed.\n"); printf("\nSelect Device: "); scanf("%s",&Select_device); *C_dev=Select_device; /* We don't need any more the device list. Free it */ pcap_freealldevs(alldevs); }
static char *match_dev_regex_or_die(const char *regstr) { static char dev[1024]; int matches = 0, devlen = 0; char errbuf[PCAP_ERRBUF_SIZE]; pcap_if_t *interfaces, *cur; int err; err = pcap_findalldevs(&interfaces, errbuf); if(err) die(0, "pcap_findalldevs(): %s", errbuf); for(cur = interfaces; cur; cur = cur->next) if(regex_matches_or_is_null(regstr, cur->name)) devlen = snprintf(&dev[devlen], sizeof(dev) - devlen , "%s%s" , matches++ ? ", " : "" , cur->name); if(!matches) die(0, "No device matching regex: %s", regstr); else if(matches > 1) die(0, "Ambiguous device regex: %s\nDid you mean one of these: %s", regstr, dev); pcap_freealldevs(interfaces); return dev; }
/** ******************************************************************************** \brief setup the process image SetupProcessImage() sets up the process image used by the application. \retval -1 if interface list couldn't be filled \retval 0 if interface list was successfully filled *******************************************************************************/ int InterfaceSelectDialog::fillList(void) { char sErr_Msg[PCAP_ERRBUF_SIZE]; pcap_if_t * alldevs; pcap_if_t * seldev; int numIntf = 0; /* Retrieve the device list on the local machine */ if (pcap_findalldevs(&alldevs, sErr_Msg) == -1) { return -1; } /* Add the list to the listbox */ for (seldev = alldevs; seldev != NULL; seldev = seldev->next) { numIntf ++; new QListWidgetItem(seldev->name, m_deviceListWidget); } pcap_freealldevs(alldevs); if (numIntf > 0) return 0; else return -1; }
/* Display Ethernet interfaces of the system */ int gen_eth_show_dev_list(void) { char pcap_errbuf[PCAP_ERRBUF_SIZE]; pcap_if_t *dev_list,*dev; int res; printf("Network device list:\n\n"); #ifndef CYGWIN res = pcap_findalldevs(&dev_list,pcap_errbuf); #else res = pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&dev_list,pcap_errbuf); #endif if (res < 0) { fprintf(stderr,"PCAP: unable to find device list (%s)\n",pcap_errbuf); return(-1); } for(dev=dev_list;dev;dev=dev->next) { printf(" %s : %s\n", dev->name, dev->description ? dev->description : "no info provided"); } printf("\n"); pcap_freealldevs(dev_list); return(0); }
BOOL CDeviceDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: 在此添加额外的初始化 CCUGBLinkerDlg* pMainWnd=(CCUGBLinkerDlg*)theApp.m_pMainWnd; CString curNicDes; int curNicIndex=0; pcap_if_t* alldevs=finddevs(); pcap_if_t* d=NULL; int i=0; for(d = alldevs; d != NULL; d = d->next,i++) { m_lstDev.AddString(CString(d->description)); if (CString(d->name)==m_curNIC) { curNicIndex=i; } } if (alldevs) { pcap_freealldevs(alldevs); } m_lstDev.SetCurSel(curNicIndex); OnLbnSelchangeListDevice(); return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE }
static void iface_find_or_exit(const char *wildcard) { 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("Search for interface '%s'... ", wildcard); for (dev = alldevs; dev; dev = dev->next) { if ('\0' != wildcard[0] && NULL == strstr(dev->name, wildcard) && (NULL == dev->description || NULL == strstr(dev->description, wildcard))) continue; printf("OK.\n"); strlcpy(Dev_Str[Iface_Cnt++], dev->name, sizeof Dev_Str); if (IFACE_MAX == Iface_Cnt) { printf("Interface limit reached.\n"); break; } } if (0 == Iface_Cnt) { iface_list(); fprintf(stderr, "No interfaces matched '%s', quitting.\n", wildcard); exit(EXIT_FAILURE); } pcap_freealldevs(alldevs); }
/*selectNum是从1开始*/ void selectNic(int selectNum) { pcap_if_t *d; for (d=alldevs; selectNum>1; d=d->next,selectNum--); strncpy(nic, d->name, sizeof(nic)-1); pcap_freealldevs(alldevs); }
slist * get_interface_names() { slist *sv; pcap_if_t *devs = NULL; pcap_if_t *dev; int ret; sv = sinit(); if(sv == NULL) return NULL; ret = pcap_findalldevs(&devs, NULL); if(ret) { sfree(sv); return NULL; } for(dev = devs; dev; dev = dev->next) { if(sadd(sv, dev->name) == -1) { sfree(sv); sv = NULL; break; } } pcap_freealldevs(devs); return sv; }
void DevSelectDlg::showallDev() { if(pcap_findalldevs(&alldevs, errbuf) == PCAP_ERROR) { qDebug() << "pcap_findalldevs() error : " << errbuf; QMessageBox::information(NULL, "ERROR", "pcap_findalldevs() error"); close(); } int i; QListWidgetItem *qListWidgetItem; for(devsTmp = alldevs, i = 1; NULL != devsTmp; devsTmp = devsTmp->next, i++) { if(i == 1 && devsTmp == NULL) { qDebug() << "Interface not found!"; QMessageBox::information(NULL, "ERROR", "Interface not found"); close(); } qListWidgetItem = new QListWidgetItem(ui->listWidget); qListWidgetItem->setText(devsTmp->name); } pcap_freealldevs(alldevs); }
PcapLiveDeviceList::PcapLiveDeviceList() { pcap_if_t* interfaceList; char errbuf[PCAP_ERRBUF_SIZE]; int err = pcap_findalldevs(&interfaceList, errbuf); if (err < 0) { LOG_ERROR("Error searching for devices: %s", errbuf); } pcap_if_t* currInterface = interfaceList; while (currInterface != NULL) { #ifdef WIN32 PcapLiveDevice* dev = new WinPcapLiveDevice(currInterface, true, true, true); #else //LINUX, MAC_OSX PcapLiveDevice* dev = new PcapLiveDevice(currInterface, true, true, true); #endif currInterface = currInterface->next; m_LiveDeviceList.insert(m_LiveDeviceList.end(), dev); } setDnsServers(); LOG_DEBUG("Freeing live device data"); pcap_freealldevs(interfaceList); }
void list_pcap(void) { int i=0; pcap_if_t *alldevs; pcap_if_t *d; 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; } pcap_freealldevs(alldevs); return ; }
void ointerface::pcap_getipaddr () { #if defined (WINPCAP) char buffer [PCAP_ERRBUF_SIZE]; pcap_if_t * devices = (pcap_if_t *) (0); pcap_if_t * device; if (pcap_findalldevs (& devices, buffer) == -1) { oerror::error (1, errno, "Can't enumerate interfaces"); } for (device = devices; device; device = device->next) { if (std::strcmp (this->mifname, device->name)) { continue; } std::memcpy (this->miftext, device->description, std::strlen (device->description)); if (device->addresses) { struct pcap_addr * pcap_addr = device->addresses; struct sockaddr_in * sockaddr_in = (struct sockaddr_in *) (pcap_addr->addr); struct in_addr * in_addr = (struct in_addr *) (& sockaddr_in->sin_addr); std::memcpy (this->mipaddr, & in_addr->s_addr, sizeof (this->mipaddr)); } break; } pcap_freealldevs (devices); #endif return; }
static bool TestAdapterName( const char adapter[] ) { char errbuf[PCAP_ERRBUF_SIZE]; pcap_if_t *alldevs; pcap_if_t *d; int i; bool succ = false; if( pcap_findalldevs( &alldevs, errbuf ) < 0 ) { return false; }//end if for( i = 0, d = alldevs; d != NULL; d = d->next, ++i ) { if( strcmp( d->name, adapter ) == 0 ) { succ = true; break; }//end if }//end for pcap_freealldevs(alldevs); return succ; }//end TestAdapterName
std::string NetworkDevice::getInterfaceList() { std::string ret_string("Usable interface list: \n"); pcap_if_t *alldevsp; /* list of interfaces */ pcap_if_t *alldev; char errbuf[PCAP_ERRBUF_SIZE]={0}; /* pcap error messages buffer */ if (pcap_findalldevs (&alldevsp, errbuf) < 0) { fprintf (stderr, "%s", errbuf); exit (1); } alldev=alldevsp; while (alldevsp != NULL) { ret_string+=alldevsp->name; ret_string+="\n"; alldevsp = alldevsp->next; } pcap_freealldevs(alldev); return ret_string; }
int main() { pcap_if_t *alldevs; pcap_if_t *d; char errbuf[PCAP_ERRBUF_SIZE+1]; char source[PCAP_ERRBUF_SIZE+1]; printf("Enter the device you want to list:\n" "rpcap:// ==> lists interfaces in the local machine\n" "rpcap://hostname:port ==> lists interfaces in a remote machine\n" " (rpcapd daemon must be up and running\n" " and it must accept 'null' authentication)\n" "file://foldername ==> lists all pcap files in the give folder\n\n" "Enter your choice: "); fgets(source, PCAP_ERRBUF_SIZE, stdin); source[PCAP_ERRBUF_SIZE] = '\0'; /* 获得接口列表 */ if (pcap_findalldevs_ex(source, NULL, &alldevs, errbuf) == -1) { fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf); exit(1); } /* 扫描列表并打印每一项 */ for(d=alldevs;d;d=d->next) { ifprint(d); } pcap_freealldevs(alldevs); return 1; }
int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len) { pcap_if_t *devs, *dev; struct pcap_addr *addr; struct sockaddr_in *saddr; int found = 0; char err[PCAP_ERRBUF_SIZE + 1]; if (pcap_findalldevs(&devs, err) < 0) { wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err); return -1; } for (dev = devs; dev && !found; dev = dev->next) { if (os_strcmp(dev->name, l2->ifname) != 0) continue; addr = dev->addresses; while (addr) { saddr = (struct sockaddr_in *) addr->addr; if (saddr && saddr->sin_family == AF_INET) { os_snprintf(buf, len, "%s", inet_ntoa(saddr->sin_addr)); found = 1; break; } addr = addr->next; } } pcap_freealldevs(devs); return found ? 0 : -1; }
int main() { pcap_if_t* alldev; pcap_if_t* d; int i =0; char errbuf[PCAP_ERRBUF_SIZE]; if(pcap_findalldevs(&alldev, errbuf)==-1) { fprintf(stdout, "%s\n", errbuf); exit(1); } for(d=alldev; d; d=d->next) { printf("%d : %s\n", i++, d->name); if(d->description) printf("%s\n", d->description); else printf("no descriptions\n"); ifprint(d); } if(i==0) { printf("no interface fount\n"); } pcap_freealldevs(alldev); return 0; }
void pcap_getipaddr (char const * name, void * memory) { #if defined (WINPCAP) char buffer [PCAP_ERRBUF_SIZE]; pcap_if_t * devices = (pcap_if_t *)(0); pcap_if_t * device; if (pcap_findalldevs (&devices, buffer) == -1) { error (1, errno, "Can't enumerate interfaces"); } for (device = devices; device; device = device->next) { if (strcmp (name, device->name)) { continue; } if (device->addresses) { struct pcap_addr * pcap_addr = device->addresses; struct sockaddr_in * sockaddr_in = (struct sockaddr_in *)(pcap_addr->addr->sa_data); memcpy (memory, &sockaddr_in->sin_addr.s_addr, sizeof (uint32_t)); } break; } pcap_freealldevs (devices); #endif return; }
/* * 列出所有可用网卡 */ int find_interfaces(int *n) { int ret, k; pcap_if_t *alldevsp; char errbuf[PCAP_ERRBUF_SIZE]; ret = pcap_findalldevs(&alldevsp, errbuf); if (ret == -1) { printf("find interface error\n"); return -1; } else { for (k = 0; alldevsp; alldevsp = alldevsp->next) { if (strstr(alldevsp->name, "eth") > 0 || strstr(alldevsp->name, "wlan") > 0) { //printf("interface name: %s \n", alldevsp->name); sprintf(ifdev[k], "%s", alldevsp->name); k++; } } } *n = k; pcap_freealldevs(alldevsp); return 0; }