Profiling packets (Microsoft Visual Studio 2010)

Source: Internet
Author: User
Tags visual studio 2010

Fenxi1.cpp: Defines the entry point of the console application.
The code is as follows:
#include "stdafx.h"
#include "Pcap.h"
#include "Bittypes.h"
#pragma comment (lib, "Ws2_32.lib")
typedef struct ip_address{
U_char byte1;
U_char Byte2;
U_char Byte3;
U_char byte4;

/* IPV4 Header */
typedef struct ip_header{
U_char VER_IHL; Version (4 bits) + header length (4 bits)
U_char tos; Types of services (type of service)
U_short Tlen; Overall length (total length)
U_short identification; Identification (identification)
U_short Flags_fo; Flag bit (flags) (3 bits) + segment offset (Fragment offset) (bits)
U_char TTL; Time to Live
U_char Proto; Protocol (PROTOCOL)
U_short CRC; Header checksum (header checksum)
IP_Address saddr; Source Address
IP_Address daddr; Destination (Destination address)
U_int Op_pad; Options and padding (option + Padding)

/* UDP Header */
typedef struct udp_header{
U_short Sport; Source Port
U_short dport; Destination Port (Destination port)
U_short Len; UDP packet Lengths (Datagram length)
U_short CRC; Checksum (Checksum)

/* Callback function prototype */
void Packet_handler (U_char *param, const struct PCAP_PKTHDR *header, const U_char *pkt_data);

int _tmain (int argc, _tchar* argv[])
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
Char Errbuf[pcap_errbuf_size];
U_int netmask;
Char packet_filter[] = "IP and UDP";
struct Bpf_program fcode;

/* Get device List */
if (PCAP_FINDALLDEVS_EX (pcap_src_if_string, NULL, &alldevs, errbuf) = =-1)
fprintf (stderr, "Error in Pcap_findalldevs:%s\n", errbuf);
Exit (1);

/* Print List */
for (D=alldevs; d; d=d->next)
printf ("%d.%s", ++i, D->name);
if (d->description)
printf ("(%s) \ n", d->description);
printf ("(No description available) \ n");

if (i==0)
printf ("\nno Interfaces found! Make sure WinPcap is installed.\n ");

printf ("Enter The interface Number (1-%d):", I);
scanf ("%d", &inum);

if (Inum < 1 | | inum > i)
printf ("\ninterface number out of range.\n");
/* Release Device list */
Pcap_freealldevs (Alldevs);

/* Jump to selected Device */
For (D=alldevs, i=0; i< inum-1;d =d->next, i++);

/* Open Adapter */
if (adhandle= pcap_open (d->name,//device name
65536,//part of the packet to capture
65535 guaranteed to capture the full contents of each packet on different data link layers
Pcap_openflag_promiscuous,//Promiscuous mode
1000,//Read time-out
NULL,//remote machine validation
ERRBUF//Error buffer pool
)) = = NULL)
fprintf (stderr, "\nunable to open the adapter.%s was not supported by winpcap\n");
/* Release Device list */
Pcap_freealldevs (Alldevs);

/* Check the Data link layer, for simplicity, we only consider Ethernet */
if (Pcap_datalink (adhandle)! = DLT_EN10MB)
fprintf (stderr, "\nthis program works only on Ethernet networks.\n");
/* Release Device list */
Pcap_freealldevs (Alldevs);

if (d->addresses! = NULL)
/* Get a mask for the first address of the interface */
netmask= (struct sockaddr_in *) (D->addresses->netmask))->sin_addr. S_un. S_ADDR;
/* If the interface does not have an address, then we assume a mask of class C */

Compiling filters
if (Pcap_compile (Adhandle, &fcode, Packet_filter, 1, netmask) <0)
fprintf (stderr, "\nunable to compile the packet filter. Check the syntax.\n ");
/* Release Device list */
Pcap_freealldevs (Alldevs);

Set Filter
if (Pcap_setfilter (Adhandle, &fcode) <0)
fprintf (stderr, "\nerror setting the filter.\n");
/* Release Device list */
Pcap_freealldevs (Alldevs);
printf ("Jiang Jiao Shou: 1306404018");
printf ("\nlistening on%s...\n", d->description);

/* Release Device list */
Pcap_freealldevs (Alldevs);

/* Start capturing */
Pcap_loop (adhandle, 0, Packet_handler, NULL);

return 0;

/* callback function, which is called by Libpcap when each packet is received */
void Packet_handler (U_char *param, const struct PCAP_PKTHDR *header, const U_char *pkt_data)
struct TM *ltime;
Char timestr[16];
Ip_header *ih;
Udp_header *uh;
U_int Ip_len;
U_short Sport,dport;
time_t local_tv_sec;

/* Convert timestamps to recognizable formats */
Local_tv_sec = header->ts.tv_sec;
Ltime=localtime (&AMP;LOCAL_TV_SEC);
Strftime (timestr, sizeof timestr, "%h:%m:%s", ltime);

/* Timestamp and length of the printed packet */
printf ("%s.%.6d len:%d", Timestr, Header->ts.tv_usec, Header->len);

/* Get the location of the IP packet header */
IH = (Ip_header *) (Pkt_data +
14); Ethernet Head Length

/* Get the UDP header position */
Ip_len = (IH-&GT;VER_IHL & 0xf) * 4;
UH = (Udp_header *) ((u_char*) IH + ip_len);

/* Converts a network byte sequence into a host byte sequence */
Sport = Ntohs (uh->sport);
Dport = Ntohs (Uh->dport);

/* Print IP address and UDP port */
printf ("%d.%d.%d.%d.%d-%d.%d.%d.%d.%d\n",

Item-->** Properties (ALT+F7)
Configuration Properties-->c/c++--> General--Additional include directory--(The file path (Include) where the head file is located (included) is added to the additional directory C:\WpdPack\Include)

Item-->** Properties (ALT+F7)
Configuring Properties------general---Add Library Directory--(Packet.lib;wpcap.lib directory (LIB) is added to the additional Library directory C:\WpdPack\Lib)

Item-->** Properties (ALT+F7)
Configuration Properties--linker--Add dependency--add "; Packet.lib;wpcap.lib "

Item-->** Properties (ALT+F7)
Configuration Properties-->c/c++--> Preprocessor---preprocessor definition--add "; Have_remote "

Add the head as follows

#include "Pcap.h"
#include "Bittypes.h"
#pragma comment (lib, "Ws2_32.lib")

Here is the result graph obtained:

650) this.width=650; "Src=" Http:// -wmp_4-s_4220245404.png "title=" Ae~_193xg9%ht$gxs~ojpid.png "alt=" Wkiol1gherbcnjlaaab5rbwpyhc666.png-wh_50 "/>

This article from the "12034896" blog, reproduced please contact the author!

Profiling packets (Microsoft Visual Studio 2010)

Related Article

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: 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.