Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of winpcap

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Get device list

1. Typically, the first thing you do when writing WinPcap-based applications is to get a list of connected network adapters. Both libpcap and WinPcap provide the pcap_findalldevs_ex () function to do this: this function returns a linked list of pcap_if structures, each of which contains the details of an adapter. It is worth noting that the data fields name and description represent an adapter name and a description that people can understand.

The following code gets the list of adapters and displays them on the screen. If the adapter is not found, an error message will be printed.

Some instructions about this code

First, pcap_findalldevs_ex (), like other libpcap functions, has an errbuf parameter. Once an error occurs, this parameter will be written to an error message of type string by libpcap.

Second, keep in mind that not all operating systems support the network program interface provided by libpcap, so if we want to write a portable application, we must consider under what circumstances description is null. In this procedure, when we encounter this situation, we will print the prompt statement "No description available".

Finally, remember that when we have finished using the device list, we will call the pcap_freealldevs () function to release the memory resources it occupies.

Let's compile and run our first sample program! In order to compile this program on Unix or Cygwin platforms, you need to simply type:

Gcc-o testprog testprog.c-lpcap

2. Specific implementation code:

/ / 1406404002.cpp: defines the entry point for the console application.

# include "stdafx.h"

# include "pcap.h"

Int _ tmain (int argc, _ TCHAR* argv [])

{

Pcap_if_t * alldevs

Pcap_if_t * d

Int iTunes 0

Char errbuf[PCAP _ ERRBUF_SIZE]

/ * get the list of local machines and equipment * /

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 0

}

/ * the device list is no longer needed, release it * /

Pcap_freealldevs (alldevs)

}

3. Specific configuration: * * = (1406404002)

Project-- > * * attribute (alt+F7)

Configure properties-- > inventory tool-- > input and output-- > embed list-- > No

Project-- > * * attribute (alt+F7)

Configuration Properties-- > Cramp Cure colors-- > General-- > additional containing directories-- > example: C:\ WpdPack_4_1_2\ WpdPack\ Include

Project-- > * * attribute (alt+F7)

Configuration properties-- > linker-- > General-- > additional library directory-- > example: C:\ WpdPack_4_1_2\ WpdPack\ Lib

Project-- > * * attribute (alt+F7)

Configure properties-- > linker-- > input-- > attach dependencies-- > supplement "; Packet.lib;wpcap.lib"

Project-- > * * attribute (alt+F7)

Configuration properties-- > Candlestick colors-- > preprocessor-- > preprocessor definition-- > supplement "; HAVE_REMOTE"

4. On the Windows platform, you need to create a project and follow the steps in programming with WinPcap. However, use WinPcap developer's pack (visit the WinPcap website, http://www.winpcap.org for details) because it provides many examples that have been configured, including all the sample code in this tutorial, and the inclusion files (include) and libraries (libraries) jiash required at compile run time assume that you have finished compiling the program, and then run it. On a WinXP computer, the result we got 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)

Running result:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report