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;
}ip_address;
/* 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)
}ip_header;
/* 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)
}udp_header;
/* 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);
Else
printf ("(No description available) \ n");
}
if (i==0)
{
printf ("\nno Interfaces found! Make sure WinPcap is installed.\n ");
return-1;
}
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);
return-1;
}
/* 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);
return-1;
}
/* 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);
return-1;
}
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;
Else
/* If the interface does not have an address, then we assume a mask of class C */
NETMASK=0XFFFFFF;
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);
return-1;
}
Set Filter
if (Pcap_setfilter (Adhandle, &fcode) <0)
{
fprintf (stderr, "\nerror setting the filter.\n");
/* Release Device list */
Pcap_freealldevs (Alldevs);
return-1;
}
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 (&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->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",
Ih->saddr.byte1,
Ih->saddr.byte2,
Ih->saddr.byte3,
Ih->saddr.byte4,
Sport
Ih->daddr.byte1,
Ih->daddr.byte2,
Ih->daddr.byte3,
Ih->daddr.byte4,
Dport);
}
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://s5.51cto.com/wyfs02/M01/89/E8/wKioL1gheRbCNJLaAAB5rBwpyhc666.png-wh_500x0-wm_3 -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)