all: test.c gcc -g -Wall -o test test.c -lpcap clean: rm -rf *.o test
#include <pcap.h> #include <stdio.h> int main() { char errBuf[PCAP_ERRBUF_SIZE], * device; device = pcap_lookupdev(errBuf); if(device) { printf("success: device: %s\n", device); } else { printf("error: %s\n", errBuf); } return 0; }
struct pcap_pkthdr { struct timeval ts; /* time stamp */ bpf_u_int32 caplen; /* length of portion present */ bpf_u_int32 len; /* length this packet (off wire) */ };
这个函数和pcap_loop()非常类似,只是在超过to_ms毫秒后就会返回(to_ms是pcap_open_live()的第4个参数)
例子:
test2:
#include <pcap.h> #include <time.h> #include <stdlib.h> #include <stdio.h> int main() { char errBuf[PCAP_ERRBUF_SIZE], * devStr; /* get a device */ devStr = pcap_lookupdev(errBuf); if(devStr) { printf("success: device: %s\n", devStr); } else { printf("error: %s\n", errBuf); exit(1); } /* open a device, wait until a packet arrives */ pcap_t * device = pcap_open_live(devStr, 65535, 1, 0, errBuf); if(!device) { printf("error: pcap_open_live(): %s\n", errBuf); exit(1); } /* wait a packet to arrive */ struct pcap_pkthdr packet; const u_char * pktStr = pcap_next(device, &packet); if(!pktStr) { printf("did not capture a packet!\n"); exit(1); } printf("Packet length: %d\n", packet.len); printf("Number of bytes: %d\n", packet.caplen); printf("Recieved time: %s\n", ctime((const time_t *)&packet.ts.tv_sec)); pcap_close(device); return 0; }
#include <pcap.h> #include <time.h> #include <stdlib.h> #include <stdio.h> void getPacket(u_char * arg, const struct pcap_pkthdr * pkthdr, const u_char * packet) { int * id = (int *)arg; printf("id: %d\n", ++(*id)); printf("Packet length: %d\n", pkthdr->len); printf("Number of bytes: %d\n", pkthdr->caplen); printf("Recieved time: %s", ctime((const time_t *)&pkthdr->ts.tv_sec)); int i; for(i=0; i<pkthdr->len; ++i) { printf(" %02x", packet[i]); if( (i + 1) % 16 == 0 ) { printf("\n"); } } printf("\n\n"); } int main() { char errBuf[PCAP_ERRBUF_SIZE], * devStr; /* get a device */ devStr = pcap_lookupdev(errBuf); if(devStr) { printf("success: device: %s\n", devStr); } else { printf("error: %s\n", errBuf); exit(1); } /* open a device, wait until a packet arrives */ pcap_t * device = pcap_open_live(devStr, 65535, 1, 0, errBuf); if(!device) { printf("error: pcap_open_live(): %s\n", errBuf); exit(1); } /* wait loop forever */ int id = 0; pcap_loop(device, -1, getPacket, (u_char*)&id); pcap_close(device); return 0; }
#include <pcap.h> #include <time.h> #include <stdlib.h> #include <stdio.h> void getPacket(u_char * arg, const struct pcap_pkthdr * pkthdr, const u_char * packet) { int * id = (int *)arg; printf("id: %d\n", ++(*id)); printf("Packet length: %d\n", pkthdr->len); printf("Number of bytes: %d\n", pkthdr->caplen); printf("Recieved time: %s", ctime((const time_t *)&pkthdr->ts.tv_sec)); int i; for(i=0; i<pkthdr->len; ++i) { printf(" %02x", packet[i]); if( (i + 1) % 16 == 0 ) { printf("\n"); } } printf("\n\n"); } int main() { char errBuf[PCAP_ERRBUF_SIZE], * devStr; /* get a device */ devStr = pcap_lookupdev(errBuf); if(devStr) { printf("success: device: %s\n", devStr); } else { printf("error: %s\n", errBuf); exit(1); } /* open a device, wait until a packet arrives */ pcap_t * device = pcap_open_live(devStr, 65535, 1, 0, errBuf); if(!device) { printf("error: pcap_open_live(): %s\n", errBuf); exit(1); } /* construct a filter */ struct bpf_program filter; pcap_compile(device, &filter, "dst port 80", 1, 0); pcap_setfilter(device, &filter); /* wait loop forever */ int id = 0; pcap_loop(device, -1, getPacket, (u_char*)&id); pcap_close(device); return 0; }
Makefile:
all: tcp_client.c tcp_server.c gcc -g -Wall -o tcp_client tcp_client.c gcc -g -Wall -o tcp_server tcp_server.c clean: rm -rf *.o tcp_client tcp_server
tcp_server:
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #define PORT 9832 #define SERVER_IP "192.168.56.101" int main() { /* create a socket */ int server_sockfd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(SERVER_IP); server_addr.sin_port = htons(PORT); /* bind with the local file */ bind(server_sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); /* listen */ listen(server_sockfd, 5); char ch; int client_sockfd; struct sockaddr_in client_addr; socklen_t len = sizeof(client_addr); while(1) { printf("server waiting:\n"); /* accept a connection */ client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_addr, &len); /* exchange data */ read(client_sockfd, &ch, 1); printf("get char from client: %c\n", ch); ++ch; write(client_sockfd, &ch, 1); /* close the socket */ close(client_sockfd); } return 0; }
tcp_client:
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #define PORT 9832 #define SERVER_IP "192.168.56.101" int main() { /* create a socket */ int sockfd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in address; address.sin_family = AF_INET; address.sin_addr.s_addr = inet_addr(SERVER_IP); address.sin_port = htons(PORT); /* connect to the server */ int result = connect(sockfd, (struct sockaddr *)&address, sizeof(address)); if(result == -1) { perror("connect failed: "); exit(1); } /* exchange data */ char ch = 'A'; write(sockfd, &ch, 1); read(sockfd, &ch, 1); printf("get char from server: %c\n", ch); /* close the socket */ close(sockfd); return 0; }
全部的包如下:
hutao@hutao-VirtualBox:~/test3$ sudo ./test success: device: eth0 id: 1 Packet length: 60 Number of bytes: 60 Recieved time: Sat Apr 28 19:57:50 2012 ff ff ff ff ff ff 0a 00 27 00 00 00 08 06 00 01 08 00 06 04 00 01 0a 00 27 00 00 00 c0 a8 38 01 00 00 00 00 00 00 c0 a8 38 65 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 id: 2 Packet length: 42 Number of bytes: 42 Recieved time: Sat Apr 28 19:57:50 2012 0a 00 27 00 00 00 08 00 27 9c ff b1 08 06 00 01 08 00 06 04 00 02 08 00 27 9c ff b1 c0 a8 38 65 0a 00 27 00 00 00 c0 a8 38 01 id: 3 Packet length: 74 Number of bytes: 74 Recieved time: Sat Apr 28 19:57:50 2012 08 00 27 9c ff b1 0a 00 27 00 00 00 08 00 45 00 00 3c d4 af 40 00 40 06 74 55 c0 a8 38 01 c0 a8 38 65 8e 20 26 68 79 e1 63 8c 00 00 00 00 a0 02 39 08 d4 13 00 00 02 04 05 b4 04 02 08 0a 00 14 b7 23 00 00 00 00 01 03 03 06 id: 4 Packet length: 74 Number of bytes: 74 Recieved time: Sat Apr 28 19:57:50 2012 0a 00 27 00 00 00 08 00 27 9c ff b1 08 00 45 00 00 3c 00 00 40 00 40 06 49 05 c0 a8 38 65 c0 a8 38 01 26 68 8e 20 b6 c4 e6 e5 79 e1 63 8d a0 12 38 90 f1 e5 00 00 02 04 05 b4 04 02 08 0a 00 57 a1 2c 00 14 b7 23 01 03 03 05 id: 5 Packet length: 66 Number of bytes: 66 Recieved time: Sat Apr 28 19:57:50 2012 08 00 27 9c ff b1 0a 00 27 00 00 00 08 00 45 00 00 34 d4 b0 40 00 40 06 74 5c c0 a8 38 01 c0 a8 38 65 8e 20 26 68 79 e1 63 8d b6 c4 e6 e6 80 10 00 e5 fb c1 00 00 01 01 08 0a 00 14 b7 24 00 57 a1 2c id: 6 Packet length: 67 Number of bytes: 67 Recieved time: Sat Apr 28 19:57:50 2012 08 00 27 9c ff b1 0a 00 27 00 00 00 08 00 45 00 00 35 d4 b1 40 00 40 06 74 5a c0 a8 38 01 c0 a8 38 65 8e 20 26 68 79 e1 63 8d b6 c4 e6 e6 80 18 00 e5 ba b7 00 00 01 01 08 0a 00 14 b7 25 00 57 a1 2c 41 id: 7 Packet length: 66 Number of bytes: 66 Recieved time: Sat Apr 28 19:57:50 2012 0a 00 27 00 00 00 08 00 27 9c ff b1 08 00 45 00 00 34 47 cb 40 00 40 06 01 42 c0 a8 38 65 c0 a8 38 01 26 68 8e 20 b6 c4 e6 e6 79 e1 63 8e 80 10 01 c5 f1 dd 00 00 01 01 08 0a 00 57 a1 2e 00 14 b7 25 id: 8 Packet length: 67 Number of bytes: 67 Recieved time: Sat Apr 28 19:57:50 2012 0a 00 27 00 00 00 08 00 27 9c ff b1 08 00 45 00 00 35 47 cc 40 00 40 06 01 40 c0 a8 38 65 c0 a8 38 01 26 68 8e 20 b6 c4 e6 e6 79 e1 63 8e 80 18 01 c5 f1 de 00 00 01 01 08 0a 00 57 a1 2e 00 14 b7 25 42 id: 9 Packet length: 66 Number of bytes: 66 Recieved time: Sat Apr 28 19:57:50 2012 0a 00 27 00 00 00 08 00 27 9c ff b1 08 00 45 00 00 34 47 cd 40 00 40 06 01 40 c0 a8 38 65 c0 a8 38 01 26 68 8e 20 b6 c4 e6 e7 79 e1 63 8e 80 11 01 c5 f1 dd 00 00 01 01 08 0a 00 57 a1 2e 00 14 b7 25 id: 10 Packet length: 66 Number of bytes: 66 Recieved time: Sat Apr 28 19:57:50 2012 08 00 27 9c ff b1 0a 00 27 00 00 00 08 00 45 00 00 34 d4 b2 40 00 40 06 74 5a c0 a8 38 01 c0 a8 38 65 8e 20 26 68 79 e1 63 8e b6 c4 e6 e7 80 10 00 e5 fb bc 00 00 01 01 08 0a 00 14 b7 25 00 57 a1 2e id: 11 Packet length: 66 Number of bytes: 66 Recieved time: Sat Apr 28 19:57:50 2012 08 00 27 9c ff b1 0a 00 27 00 00 00 08 00 45 00 00 34 d4 b3 40 00 40 06 74 59 c0 a8 38 01 c0 a8 38 65 8e 20 26 68 79 e1 63 8e b6 c4 e6 e7 80 11 00 e5 fb bb 00 00 01 01 08 0a 00 14 b7 25 00 57 a1 2e id: 12 Packet length: 66 Number of bytes: 66 Recieved time: Sat Apr 28 19:57:50 2012 0a 00 27 00 00 00 08 00 27 9c ff b1 08 00 45 00 00 34 47 ce 40 00 40 06 01 3f c0 a8 38 65 c0 a8 38 01 26 68 8e 20 b6 c4 e6 e8 79 e1 63 8f 80 10 01 c5 f1 dd 00 00 01 01 08 0a 00 57 a1 2e 00 14 b7 25 id: 13 Packet length: 66 Number of bytes: 66 Recieved time: Sat Apr 28 19:57:50 2012 08 00 27 9c ff b1 0a 00 27 00 00 00 08 00 45 00 00 34 d4 b4 40 00 40 06 74 58 c0 a8 38 01 c0 a8 38 65 8e 20 26 68 79 e1 63 8f b6 c4 e6 e8 80 10 00 e5 fb b9 00 00 01 01 08 0a 00 14 b7 26 00 57 a1 2e
下面的这个程序可以获取eth0的ip和子网掩码等信息:
test5:
#include <stdio.h> #include <stdlib.h> #include <pcap.h> #include <errno.h> #include <netinet/in.h> #include <arpa/inet.h> int main() { /* ask pcap to find a valid device for use to sniff on */ char * dev; /* name of the device */ char errbuf[PCAP_ERRBUF_SIZE]; dev = pcap_lookupdev(errbuf); /* error checking */ if(!dev) { printf("pcap_lookupdev() error: %s\n", errbuf); exit(1); } /* print out device name */ printf("dev name: %s\n", dev); /* ask pcap for the network address and mask of the device */ bpf_u_int32 netp; /* ip */ bpf_u_int32 maskp; /* subnet mask */ int ret; /* return code */ ret = pcap_lookupnet(dev, &netp, &maskp, errbuf); if(ret == -1) { printf("pcap_lookupnet() error: %s\n", errbuf); exit(1); } /* get the network address in a human readable form */ char * net; /* dot notation of the network address */ char * mask; /* dot notation of the network mask */ struct in_addr addr; addr.s_addr = netp; net = inet_ntoa(addr); if(!net) { perror("inet_ntoa() ip error: "); exit(1); } printf("ip: %s\n", net); /* do the same as above for the device's mask */ addr.s_addr = maskp; mask = inet_ntoa(addr); if(!mask) { perror("inet_ntoa() sub mask error: "); exit(1); } printf("sub mask: %s\n", mask); return 0; }
netp:传出参数,指定网络接口的ip地址
maskp:传出参数,指定网络接口的子网掩码
更多参考可以man pcap
最后为了方便大家,本文的所有代码和上述的pdf文档都一并上传上来了:
http://download.csdn.net/detail/htttw/4264686
完成!