WinPcap中文API http://www.ferrisxu.com/WinPcap/html/index.html
1: #include"pcap.h"
2: #include<WinSock.h>
3: #define IPTOSBUFFERS 12
4: char *iptos(u_long in){
5: static char output[IPTOSBUFFERS][3*4+3+1];
6: static short which;
7: u_char *p=(u_char*)∈
8: which=(which+1==IPTOSBUFFERS?0:which+1);
9: sprintf(output[which],"%d. %d. %d. %d",p[0],p[1],p[2],p[3]);
10: return output[which];
11: }
12: char* ip6tos(struct sockaddr *sockaddr,char*address,int addrlen){
13: socklen_t sockaddrlen=sizeof(sockaddr_in6);
14: if(getnameinfo(sockaddr,sockaddrlen,address,addrlen,NULL,0,NI_NUMERICHOST)!=0)
15: address=NULL;
16: return address;
17: }
18: void ifprint(pcap_if_t *d){
19:
20: char ip6str[128];
21:
22: //device name
23: printf("Device Name: %s\n",d->name);
24: printf("Description: %s\n",d->description);
25:
26: //loopback address
27: if(d->flags&&PCAP_IF_LOOPBACK)
28: printf("loopback: yes\n");
29: else
30: printf("loopback: yes\n");
31:
32: for(pcap_addr_t *a=d->addresses;a;a=a->next){
33: printf("Address Family: %d\n",a->addr->sa_family);
34: switch(a->addr->sa_family){
35: case AF_INET:
36: printf("Address Family: AF_INTET\n");
37: if(a->addr)
38: printf("Address: %s\n",iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr));
39: if(a->netmask)
40: printf("Netmask: %s\n",iptos((((struct sockaddr_in *)a->netmask)->sin_addr.s_addr)));
41: if(a->broadaddr)
42: printf("Broadcast Address: %s\n",iptos(((struct sockaddr_in *)a->broadaddr)->sin_addr.s_addr));
43: if(a->dstaddr)
44: printf("Destination Address: %s\n",iptos(((struct sockaddr_in *)a->dstaddr)->sin_addr.s_addr));
45: break;
46: case AF_INET6:
47: printf("Address Family Name: AF_INET6\n");
48: if(a->addr)
49: printf("Address: %s\n",ip6tos(a->addr,ip6str,sizeof(ip6str)));
50: break;
51: default:
52: printf("Adress Family Name: Unknown\n");
53: break;
54: }
55: }
56: }
57:
58:
59: int main(){
60: pcap_if_t *all_devs,*d;
61:
62: char errBuf[PCAP_ERRBUF_SIZE+1],source[PCAP_ERRBUF_SIZE+1];
63: printf("Enter the device you want to list:\n");
64: printf("rpcap://==>list interface int the machine\n");
65: printf("rpcap://hostname:port==>list interface in a remote machine\n");
66: printf("file ://foldname==>list all pcap files in the give folder\n");
67: printf("Enter your choice: ");
68: fgets(source ,PCAP_ERRBUF_SIZE,stdin);
69: source[PCAP_ERRBUF_SIZE]='\0';
70:
71:
72: //get local device list\n
73: if(pcap_findalldevs_ex(source,NULL,&all_devs,errBuf)==-1){
74: fprintf(stderr,"ERROR:%s\n",errBuf);
75: exit(1);
76: }
77:
78: //print list
79: for(pcap_if_t *d=all_devs;d;d=d->next){
80: ifprint(d);
81: }
82:
83: //fianlly free any resource
84: pcap_freealldevs(all_devs);
85: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
试验结果: