Get a list of devices

Source: Internet
Author: User

In general, the first thing to write a WinPcap-based application is to get a list of connected network adapters. Both Libpcap and WinPcap provide the PCAP_FINDALLDEVS_EX () function to implement this function: This function returns a list of pcap_if structures, each of which contains the details of an adapter. It is important to note that the data field name and description represent an adapter name and a description that people can understand.

The following code can get the list of adapters and display them on the screen, and if the adapter is not found, an error message will be printed.

#include   "Pcap.h" main () {    pcap_if_t *alldevs;    pcap_if_t  *d;    int i=0;    char errbuf[PCAP_ERRBUF_SIZE];         /*  get a list of local machine devices  */    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 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;    }    /*   no longer requires a list of devices, release it  */    pcap_freealldevs (Alldevs);}

A few notes about this piece of code

First, PCAP_FINDALLDEVS_EX (), like the other Libpcap functions, has a errbuf parameter. Once an error occurs, this parameter is Libpcap written to the string type error message.

Second, remember that not all operating systems support the LIBPCAP provided by the network program interface, so if we want to write a portable application, we have to consider under what circumstances, description is null. In this program, when we encounter this situation, the prompt statement "No description available" is printed.

Finally remember that when we complete the device list of the Make

We are going to call the Pcap_freealldevs () function to release the memory resources it occupies.

The result we get is:


1. \device\npf_{4e273621-5161-46c8-895a-48d0e52a0b83} (Realtek RTL8029 (AS) Ethernet Adapter) 2. \device\npf_{5d24ae04-c486-4a96-83fb-8b5ec6c7f430} (3Com EtherLink PCI)

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/89/4A/wKioL1gPCjPSqAKVAAAgU_wFZNg033.png-wh_500x0-wm_3 -wmp_4-s_2659160903.png "style=" Float:none; "title=" 111.PNG "alt=" Wkiol1gpcjpsqakvaaagu_wfzng033.png-wh_50 "/>

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/89/4C/wKiom1gPCjOD03lbAAEzYRDQhIE635.png-wh_500x0-wm_3 -wmp_4-s_924008299.png "style=" Float:none; "title=" 222.PNG "alt=" Wkiom1gpcjod03lbaaezyrdqhie635.png-wh_50 "/>

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/89/4A/wKioL1gPCjOCLBUxAAAwK8DPuaI675.png-wh_500x0-wm_3 -wmp_4-s_1856159221.png "style=" Float:none; "title=" 333.PNG "alt=" Wkiol1gpcjoclbuxaaawk8dpuai675.png-wh_50 "/>




This article is from the "12034908" blog, please be sure to keep this source http://12044908.blog.51cto.com/12034908/1865445

Get a list of devices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.