static int ifprint(pcap_if_t *d);
static char *iptos(bpf_u_int32 in);
#ifdef _WIN32
#include "portability.h"
/*
* Generate a string for a Win32-specific error (i.e. an error generated when
* calling a Win32 API).
* For errors occurred during standard C calls, we still use pcap_strerror()
*/
#define ERRBUF_SIZE 1024
static const char *
win32_strerror(DWORD error)
{
static char errbuf[ERRBUF_SIZE+1];
size_t errlen;
/*
* "FormatMessage()" "helpfully" sticks CR/LF at the end of the
* message. Get rid of it.
*/
errlen = strlen(errbuf);
if (errlen >= 2) {
errbuf[errlen - 1] = '\0';
errbuf[errlen - 2] = '\0';
errlen -= 2;
}
return errbuf;
}
#ifdef ENABLE_REMOTE
if (argc >= 2)
{
if (pcap_findalldevs_ex(argv[1], NULL, &alldevs, errbuf) == -1)
{
/*
* OK, try it with a user name and password.
*/
fprintf(stderr, "User name: ");
if (fgets(username, sizeof username, stdin) == NULL)
exit(1);
p = strchr(username, '\n');
if (p != NULL)
*p = '\0';
password = getpass("Password: ");
auth.type = RPCAP_RMTAUTH_PWD;
auth.username = username;
auth.password = password;
if (pcap_findalldevs_ex(argv[1], &auth, &alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
exit(1);
}
}
}
else
#endif
{
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
exit(1);
}
}
for(d=alldevs;d;d=d->next)
{
if (!ifprint(d))
exit_status = 2;
}
if (alldevs != NULL)
{
if (pcap_lookupnet(alldevs->name, &net, &mask, errbuf) < 0)
{
/*
* XXX - this doesn't distinguish between "a real error
* occurred" and "this interface doesn't *have* an IPv4
* address". The latter shouldn't be treated as an error.
*
* We look for the interface name, followed by a colon and
* a space, and, if we find it,w e see if what follows it
* is "no IPv4 address assigned".
*/
size_t devnamelen = strlen(alldevs->name);
if (strncmp(errbuf, alldevs->name, devnamelen) == 0 &&
strncmp(errbuf + devnamelen, ": ", 2) == 0 &&
strcmp(errbuf + devnamelen + 2, "no IPv4 address assigned") == 0)
printf("Preferred device is not on an IPv4 network\n");
else {
fprintf(stderr,"Error in pcap_lookupnet: %s\n",errbuf);
exit_status = 2;
}
}
else
{
printf("Preferred device is on network: %s/%s\n",iptos(net), iptos(mask));
}
}
pcap_freealldevs(alldevs);
exit(exit_status);
}
static int ifprint(pcap_if_t *d)
{
pcap_addr_t *a;
char ipv4_buf[INET_ADDRSTRLEN];
#ifdef INET6
char ipv6_buf[INET6_ADDRSTRLEN];
#endif
const char *sep;
int status = 1; /* success */