Skip to content

Commit a6ef70a

Browse files
committed
Merge PR #25: (Win32) Let '-d' handle a device-name too
2 parents d79f93c + 55da94d commit a6ef70a

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

ngrep.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,7 @@ void usage(void) {
14611461
" -F is read the bpf filter from the specified file\n"
14621462
" -N is show sub protocol number\n"
14631463
#if defined(_WIN32)
1464-
" -d is use specified device (index) instead of the pcap default\n"
1464+
" -d is use specified device (index or name) instead of the pcap default\n"
14651465
" -L is show the winpcap device list index\n"
14661466
#else
14671467
" -d is use specified device instead of the pcap default\n"
@@ -1568,32 +1568,46 @@ void win32_listdevices(void) {
15681568
pcap_freealldevs(alldevs);
15691569
}
15701570

1571-
char *win32_usedevice(const char *index) {
1572-
int32_t idx = atoi(index), i = 0;
1571+
char *win32_usedevice(const char *index_or_name) {
1572+
int32_t idx, i = 0;
1573+
int is_name = 0;
1574+
int found_it = 0;
15731575
pcap_if_t *alldevs, *d;
15741576
char errbuf[PCAP_ERRBUF_SIZE];
15751577
char *dev = NULL;
15761578

1577-
if (idx <= 0) {
1578-
perror("invalid device index");
1579-
clean_exit(2);
1579+
if (!strncmp(index_or_name, "\\Device", 7))
1580+
is_name = 1;
1581+
else {
1582+
idx = atoi(index_or_name);
1583+
if (idx <= 0) {
1584+
perror("invalid device index");
1585+
clean_exit(2);
1586+
}
15801587
}
15811588

15821589
if (pcap_findalldevs(&alldevs, errbuf) == -1) {
15831590
perror("unable to enumerate devices");
15841591
clean_exit(2);
15851592
}
15861593

1587-
for (d = alldevs; d != NULL && i != idx; d = d->next)
1588-
if (++i == idx)
1594+
for (d = alldevs; d != NULL && !found_it; d = d->next, i++) {
1595+
if (is_name && !stricmp(index_or_name, d->name)) {
1596+
dev = _strdup(d->name);
1597+
found_it = 1;
1598+
}
1599+
else if (i + 1 == idx) {
15891600
dev = _strdup(d->name);
1601+
found_it = 1;
1602+
}
1603+
}
15901604

1591-
if (i <= 0) {
1605+
if (i == 0) {
15921606
perror("no known devices");
15931607
clean_exit(2);
15941608
}
15951609

1596-
if (i != idx) {
1610+
if (!found_it) {
15971611
perror("unknown device specified");
15981612
clean_exit(2);
15991613
}

0 commit comments

Comments
 (0)