The following sample provides an ifprint() function that prints the complete contents of a pcap_if structure. It is invoked by the program for every entry returned by pcap_findalldevs_ex().
#include <stdio.h>
#include "pcap.h"
#ifndef WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#else
#include <winsock.h>
#endif
char *iptos(u_long in);
char* ip6tos(struct sockaddr *sockaddr, char *address, int addrlen);
int main()
{
printf("Enter the device you want to list:\n"
"rpcap:// ==> lists interfaces in the local machine\n"
"rpcap://hostname:port ==> lists interfaces in a remote machine\n"
" (rpcapd daemon must be up and running\n"
" and it must accept 'null' authentication)\n"
"file://foldername ==> lists all pcap files in the give folder\n\n"
"Enter your choice: ");
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
exit(1);
}
for(d=alldevs;d;d=d->
next)
{
ifprint(d);
}
return 1;
}
{
char ip6str[128];
printf(
"\tAddress Family: #%d\n",a->
addr->sa_family);
switch(a->
addr->sa_family)
{
case AF_INET:
printf("\tAddress Family Name: AF_INET\n");
printf(
"\tAddress: %s\n",iptos(((
struct sockaddr_in *)a->
addr)->sin_addr.s_addr));
printf(
"\tNetmask: %s\n",iptos(((
struct sockaddr_in *)a->
netmask)->sin_addr.s_addr));
printf(
"\tBroadcast Address: %s\n",iptos(((
struct sockaddr_in *)a->
broadaddr)->sin_addr.s_addr));
printf(
"\tDestination Address: %s\n",iptos(((
struct sockaddr_in *)a->
dstaddr)->sin_addr.s_addr));
break;
case AF_INET6:
printf("\tAddress Family Name: AF_INET6\n");
printf(
"\tAddress: %s\n", ip6tos(a->
addr, ip6str,
sizeof(ip6str)));
break;
default:
printf("\tAddress Family Name: Unknown\n");
break;
}
}
printf("\n");
}
#define IPTOSBUFFERS 12
char *iptos(u_long in)
{
static char output[IPTOSBUFFERS][3*4+3+1];
static short which;
u_char *p;
p = (u_char *)∈
which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
_snprintf_s(output[which], sizeof(output[which]), sizeof(output[which]),"%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
return output[which];
}
char* ip6tos(struct sockaddr *sockaddr, char *address, int addrlen)
{
socklen_t sockaddrlen;
#ifdef WIN32
sockaddrlen = sizeof(struct sockaddr_in6);
#else
sockaddrlen = sizeof(struct sockaddr_storage);
#endif
if(getnameinfo(sockaddr,
sockaddrlen,
address,
addrlen,
NULL,
0,
NI_NUMERICHOST) != 0) address = NULL;
return address;
}
#define PCAP_IF_LOOPBACK
interface is loopback
#define PCAP_ERRBUF_SIZE
Size to use when allocating the buffer that contains the libpcap errors.
void pcap_freealldevs(pcap_if_t *alldevsp)
Free an interface list returned by pcap_findalldevs().
int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **alldevs, char *errbuf)
Create a list of network devices that can be opened with pcap_open().
Representation of an interface address, used by pcap_findalldevs().
struct sockaddr * broadaddr
if not NULL, a pointer to a struct sockaddr that contains the broadcast address corre sponding to th...
struct sockaddr * addr
a pointer to a struct sockaddr containing an address
struct sockaddr * dstaddr
if not NULL, a pointer to a struct sockaddr that contains the destination address corre sponding to ...
struct pcap_addr * next
if not NULL, a pointer to the next element in the list; NULL for the last element of the list
struct sockaddr * netmask
if not NULL, a pointer to a struct sockaddr that contains the netmask corresponding to the address po...
Item in a list of interfaces, used by pcap_findalldevs().
u_int flags
PCAP_IF_ interface flags. Currently the only possible flag is PCAP_IF_LOOPBACK, that is set if the in...
struct pcap_addr * addresses
a pointer to the first element of a list of addresses for the interface
char * name
a pointer to a string giving a name for the device to pass to pcap_open_live()
struct pcap_if * next
if not NULL, a pointer to the next element in the list; NULL for the last element of the list
char * description
if not NULL, a pointer to a string giving a human-readable description of the device