Skip to content

Commit c9f7277

Browse files
author
risso
committed
pcap_lookupdev() converts the adapter list to unicode for backward compatibility.
1 parent 9cc72b7 commit c9f7277

File tree

1 file changed

+62
-34
lines changed

1 file changed

+62
-34
lines changed

inet.c

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
#ifndef lint
3636
static const char rcsid[] =
37-
"@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.56 2003-07-23 05:29:20 guy Exp $ (LBL)";
37+
"@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.57 2003-09-22 11:51:37 risso Exp $ (LBL)";
3838
#endif
3939

4040
#ifdef HAVE_CONFIG_H
@@ -136,20 +136,6 @@ add_or_find_if(pcap_if_t **curdev_ret, pcap_if_t **alldevs, const char *name,
136136
pcap_if_t *curdev, *prevdev, *nextdev;
137137
int this_instance;
138138

139-
/*
140-
* Can we open this interface for live capture?
141-
*/
142-
p = pcap_open_live(name, 68, 0, 0, errbuf);
143-
if (p == NULL) {
144-
/*
145-
* No. Don't bother including it.
146-
* Don't treat this as an error, though.
147-
*/
148-
*curdev_ret = NULL;
149-
return (0);
150-
}
151-
pcap_close(p);
152-
153139
/*
154140
* Is there already an entry in the list for this interface?
155141
*/
@@ -617,13 +603,50 @@ pcap_lookupdev(errbuf)
617603
return (AdaptersName);
618604
} else {
619605
/*
620-
* Windows NT (NT 4.0, W2K, WXP).
606+
* Windows NT (NT 4.0, W2K, WXP). Convert the names to UNICODE for backward compatibility
621607
*/
622608
ULONG NameLength = 8192;
623609
static WCHAR AdaptersName[8192];
624-
625-
PacketGetAdapterNames((PTSTR)AdaptersName,&NameLength);
626-
610+
char *tAstr;
611+
WCHAR *tUstr;
612+
WCHAR *TAdaptersName = (WCHAR*)malloc(8192 * sizeof(WCHAR));
613+
int NAdapts = 0;
614+
615+
if(TAdaptersName == NULL)
616+
{
617+
(void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
618+
return NULL;
619+
}
620+
621+
PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength);
622+
623+
tAstr = (char*)TAdaptersName;
624+
tUstr = (WCHAR*)AdaptersName;
625+
626+
/*
627+
* Convert and copy the device names
628+
*/
629+
while(sscanf(tAstr, "%S", tUstr) > 0)
630+
{
631+
tAstr += strlen(tAstr) + 1;
632+
tUstr += wcslen(tUstr) + 1;
633+
NAdapts ++;
634+
}
635+
636+
tAstr++;
637+
*tUstr = 0;
638+
tUstr++;
639+
640+
/*
641+
* Copy the descriptions
642+
*/
643+
while(NAdapts--)
644+
{
645+
strcpy((char*)tUstr, tAstr);
646+
(char*)tUstr += strlen(tAstr) + 1;;
647+
tAstr += strlen(tAstr) + 1;
648+
}
649+
627650
return (char *)(AdaptersName);
628651
}
629652
}
@@ -636,30 +659,35 @@ pcap_lookupnet(device, netp, maskp, errbuf)
636659
register char *errbuf;
637660
{
638661
/*
639-
* We need only the first address, so we allocate a single
640-
* npf_if_addr structure and we set if_addr_size to 1.
662+
* We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
663+
* in order to skip non IPv4 (i.e. IPv6 addresses)
641664
*/
642-
npf_if_addr if_addrs;
665+
npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
643666
LONG if_addr_size = 1;
644667
struct sockaddr_in *t_addr;
668+
unsigned int i;
645669

646-
if (!PacketGetNetInfoEx((void *)device, &if_addrs, &if_addr_size)) {
670+
if (!PacketGetNetInfoEx((void *)device, if_addrs, &if_addr_size)) {
647671
*netp = *maskp = 0;
648672
return (0);
649673
}
650674

651-
t_addr = (struct sockaddr_in *) &(if_addrs.IPAddress);
652-
*netp = t_addr->sin_addr.S_un.S_addr;
653-
t_addr = (struct sockaddr_in *) &(if_addrs.SubnetMask);
654-
*maskp = t_addr->sin_addr.S_un.S_addr;
655-
656-
/*
657-
* XXX - will we ever get back a 0 netmask?
658-
* If so, we should presumably make the "if (*maskp == 0)" code
659-
* above common, rather than non-Win32-specific.
660-
*/
675+
for(i=0; i<MAX_NETWORK_ADDRESSES; i++)
676+
{
677+
if(if_addrs[i].IPAddress.ss_family == AF_INET)
678+
{
679+
t_addr = (struct sockaddr_in *) &(if_addrs[i].IPAddress);
680+
*netp = t_addr->sin_addr.S_un.S_addr;
681+
t_addr = (struct sockaddr_in *) &(if_addrs[i].SubnetMask);
682+
*maskp = t_addr->sin_addr.S_un.S_addr;
683+
684+
*netp &= *maskp;
685+
return (0);
686+
}
687+
688+
}
661689

662-
*netp &= *maskp;
690+
*netp = *maskp = 0;
663691
return (0);
664692
}
665693

0 commit comments

Comments
 (0)