WinPcap programming to get adapter information

Source: Internet
Author: User

The first thing to read about this page: http://www.winpcap.org/docs/docs_412/html/group__wpcapsamps.html

Can be combined with Chinese version to see http://www.ferrisxu.com/WinPcap/html/group__wpcapsamps.html

Create a Use Wpcap.dllThe application

To create an application that uses Wpcap.dll with Microsoft Visual C + +, you need to follow the steps:

    • In each source program that uses the library, the pcap.h header file contains (include) in.
    • If you are using a specific function in the program that is provided to the Win32 platform in WinPcap, remember to include the definition of wpcap in the preprocessing.
    • If your program uses the WinPcap remote Capture feature, add have_remoteto the preprocessing definition. do not directly add remote-ext.h directly to your source file.
    • Set up VC + + linker (Linker) to include the wpcap.lib library file. wpcap.lib can be found in the WinPcap.
    • Set up VC + + linker (Linker) to include the ws2_32.lib library file. This file is distributed in the C compiler and contains some socket functions for Windows. Some of the sample programs in this tutorial will need it.

remember the following points :

    • To add a pre-processing definition, you need to open the Project menu, select Settings, and then select the C + + tab, under the General class, you must add a definition to the text box under preprocessor definitions.
    • To add a new library to a vc++6.0 project, you must open the Project menu, select Settings, then select the Link tab, and then add the name of the new library to the Object /library in the text box under modules
    • To add a new library path to vc++6.0, you must open the Tool menu, select Options, and then select the Directories tab in the Show Directories drop-down box, select Library files, and add the new path to the directories
    • To add a new file containing the path to the vc++6.0, you must open the Tool menu, select options , and then select the Directories tab in the Show Directories drop-down box, select Include files, and add the new path to the directories

Because I use 4.1.2 version of the WinPcap, so the Chinese version of the document in the example does not apply, can only be combined with English documents to explore.

1#include <stdio.h>2#include <stdlib.h>3#include"Pcap.h"4 5 intMain ()6 {7pcap_if_t *Alldevs;8pcap_if_t *D;9     intI=0;Ten     CharErrbuf[pcap_errbuf_size]; One  A     /*get a list of devices*/ -     if(Pcap_findalldevs (&alldevs, errbuf) = =-1) -     { thefprintf (stderr,"Error in Pcap_findalldevs:%s\n", errbuf); -Exit1); -     } -  +     /*Print List*/ -      for(d=alldevs;d;d=d->next) +     { Aprintf"%d.%s", ++i, d->name); at         if(d->description) -printf"(%s) \ n", d->description); -         Elseprintf"(No description available) \ n"); -     } -  -     if(i==0) in     { -printf"\ nthe interface is not found! Make sure the WinPcap is installed. \ n"); to         return 0; +     } -  the     /*The device list is no longer needed, release it*/ * Pcap_freealldevs (Alldevs); $     return 0;Panax Notoginseng}

Output Result:

1. What is pcap_if_t? Seen from the Pcap.h

struct pcap_if pcap_if_t;

The pcap_if is described in this document:

Can be found in fact pcap_if is a list, and pcap_if has five values, where next is the link to the next structure in the list, name is the name of the adapter, description is the description of the adapter,

Addresses is the IP address information assigned to the adapter, flags is an interface flag, and currently only one flag is Pcap_if_loopback, which represents the loopback interface.

Let's look at the following important function:

Char* errbuf)

Returns a list of the interfaces available on the system.

  Pcap_findalldevs () Constructs a list of network devices that can be opened pcap_open_live (). (Note that there may be network devices that cannot open the Pcap_open_live procedure () call Pcap_findalldevs (), because, for example, this process may not have sufficient permissions to open the capture; If so, these devices will not appear in the list. ALLDEVSP is set to point to the first element of the list, and the list of each element is the pcap_if_t type.

Simply understand, pcap_open_live () is to open a physical interface real-time capture, specific can go to look at the document. This function is different from the previous version. The PCAP_FINDALLDEVS_EX () function was previously called, and the 4.1.2 version of WinPcap replaced the function.

The Pcap_freealldevs () function is a list of empty interfaces, which is not explained.

WinPcap programming to get adapter information

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.