Libpcap packet capture source code

Source: Internet
Author: User

* Program function: Only output the captured data packets to the Source Target host whose network access layer header is Ethernet protocol Internet layer header is ip4 transport layer header is tcp, mac address, IP address, port Number */

 
* Program function: Only output the captured data packets to the Source Target host whose network access layer header is Ethernet protocol Internet layer header is ip4 transport layer header is tcp, mac address, IP address, port Number */
 
 
# Include <stdio. h>
 
# Include <stdlib. h>
 
# Include <unistd. h>
 
# Include <pcap. h>
 

 
# Include <netinet/if_ether.h>
 
 
# Define IP 2048
# Define TCP 6
 
Typedef unsigned char UINT1;
 
Typedef unsigned short UINT2;
 
Typedef unsigned int UINT4;
 

 
Typedef struct ether
{
 
UINT1 dest [6];
 
UINT1 src [6];
 
UINT2 proto;
 
UINT1 data [0];
 
} TEther;
 

 
Typedef struct ip
{
 
UINT1 hlen;/* Header Length + version */
 
UINT1 tos;/* Service */
 
UINT2 len;/* total length */
 
UINT2 ipid;/* ID */
 
UINT2 flagoff;/* indicates an offset */
 
UINT1 ttl;/* survival time */
 
UINT1 proto;/* Protocol */
 
UINT2 cksum;/* header check */
 
UINT4 src;/* Source Address */
 
UINT4 dest;/* Destination Address */
 
UINT1 data [0];
 

 
} TIp;
 
 
Typedef struct tcp
{
 
UINT2 sport;
 
UINT2 dport;
 
UINT4 seq;
 
UINT4 ack;
 
UINT1 hlen;
 
UINT1 code;
 
UINT2 window;
 
UINT2 chsum;
 
UINT2 urg;
 
Char data [0];
 

 
} TTcp;
 

 

 
Void proc_pkt (u_char * user, const struct pcap_pkthdr * hp,
Const u_char * packet );
 
Void net_host (UINT4 ipaddr );
 
Int
Main ()
{
 
Char * dev = NULL;
 
Pcap_t * descr;
 
Struct pcap_pkthdr hdr;
 
U_char * packet;
 
Char errbuf [PCAP_ERRBUF_SIZE];
 
Int promisc = 0, cnt = 5;
 
Int pcap_time_out = 100;
 
Struct tEther * pEpkt;
 
UINT4 net, mask;
 
Dev = pcap_lookupdev (errbuf );
 
Pcap_lookupnet (dev, & net, & mask, errbuf );
 
Descr = pcap_open_live (dev, BUFSIZ, promisc, pcap_time_out, errbuf );
Printf ("network number :");
Net_host (net );
Printf ("network mask :");
Net_host (mask );
Printf ("\ n ");
Pcap_loop (descr,-1, proc_pkt, NULL );
 

 

 
Printf ("% s \ n", dev );
 
 
Return 0;
}
 

 
Void
Proc_pkt (u_char * user, const struct pcap_pkthdr * hp, const u_char * packet)
{
TEther * pEther;
TIp * pIp;
Int I;
PEther = (tEther *) packet;
 
 
If (ntohs (pEther-> proto) = IP)/* the network layer header is IP */
{
 
 
PIp = (tIp *) pEther-> data;
 
If (pIp-> proto = TCP)/* the transport layer header is tcp */
{
 
 
TTcp * pTcp;
 
PTcp = (tTcp *) pIp-> data;
 
 
Printf ("target MAC address :");
For (I = 0; I <6; I ++)
{
If (pEther-> dest [I]) <16)
Printf ("0 ");
Printf ("% x", pEther-> dest [I]);
}
Printf ("\ n source MAC address :");
For (I = 0; I <6; I ++)
{
If (pEther-> src [I]) <16 ))
Printf ("0 ");
Printf ("% x", pEther-> src [I]);
 
}
Printf ("\ n ");
Printf ("Source IP Address :");
 
Net_host (pIp-> src );
 
Printf ("Destination IP Address :");
 
Net_host (pIp-> dest );
 
 
Printf ("Source Port :");
 
Printf ("% hu \ n", ntohs (pTcp-> sport ));
 
Printf ("Destination Port :");
 
Printf ("% hu \ n", ntohs (pTcp-> dport ));
 
}
 
}
 
Return;
}
 
Void
Net_host (UINT4 ip_mask)/* converts IP addresses and network masks in the byte network into common formats */
{
UINT4 one, two, three, four;
 
One = ip_mask;
 
 
One = one> 24;
 
Two = ip_mask;
Two = two> 16;
 
Two = two & 0xff;
Three = ip_mask;
 
Three = three> 8;
 
Three = three & 0xff;
 
Four = ip_mask;
 
 
Four = four & 0xff;
 
Printf ("% u. % u \ n", four, three, two, one );
}
 

Author "programmer"

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