Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let '-d' handle a device-name too #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions ngrep.c
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@ void usage(void) {
" -F is read the bpf filter from the specified file\n"
" -N is show sub protocol number\n"
#if defined(_WIN32)
" -d is use specified device (index) instead of the pcap default\n"
" -d is use specified device (index or name) instead of the pcap default\n"
" -L is show the winpcap device list index\n"
#else
" -d is use specified device instead of the pcap default\n"
Expand Down Expand Up @@ -1552,32 +1552,46 @@ void win32_listdevices(void) {
pcap_freealldevs(alldevs);
}

char *win32_usedevice(const char *index) {
int32_t idx = atoi(index), i = 0;
char *win32_usedevice(const char *index_or_name) {
int32_t idx, i = 0;
int is_name = 0;
int found_it = 0;
pcap_if_t *alldevs, *d;
char errbuf[PCAP_ERRBUF_SIZE];
char *dev = NULL;

if (idx <= 0) {
perror("invalid device index");
clean_exit(2);
if (!strncmp(index_or_name, "\\Device", 7))
is_name = 1;
else {
idx = atoi(index_or_name);
if (idx <= 0) {
perror("invalid device index");
clean_exit(2);
}
}

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

for (d = alldevs; d != NULL && i != idx; d = d->next)
if (++i == idx)
for (d = alldevs; d != NULL && !found_it; d = d->next, i++) {
if (is_name && !stricmp(index_or_name, d->name)) {
dev = _strdup(d->name);
found_it = 1;
}
else if (i + 1 == idx) {
dev = _strdup(d->name);
found_it = 1;
}
}

if (i <= 0) {
if (i == 0) {
perror("no known devices");
clean_exit(2);
}

if (i != idx) {
if (!found_it) {
perror("unknown device specified");
clean_exit(2);
}
Expand Down