本文整理汇总了C++中pcap_freealldevs函数的典型用法代码示例。如果您正苦于以下问题:C++ pcap_freealldevs函数的具体用法?C++ pcap_freealldevs怎么用?C++ pcap_freealldevs使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pcap_freealldevs函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: eapol_init
/*
* 初始化缓存区,生成接口句柄
* 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;
}
示例2: 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_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;
}
示例3: 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;
}
示例4: fprintf
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;
}
示例5: sprintf
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;
}
示例6: 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;
}
示例7: GetAdapterName
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;
}
示例8: list_devices
/* 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);
}
示例9: main
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;
}
示例10: pcap_ex_name
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
}
示例11: pcap_nameindex
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));
}
示例12: Call_Device
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);
}
示例13: pcap_findalldevs
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;
}
示例14: SetupProcessImage
/**
********************************************************************************
\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;
}
示例15: gen_eth_show_dev_list
/* 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);
}