Add option to list network interfaces

This commit is contained in:
Joseph C. Lehner 2016-01-31 17:31:56 +02:00
parent 8ceb9370f5
commit 0bcc6da157
3 changed files with 34 additions and 2 deletions

View file

@ -153,7 +153,7 @@ struct ethsock *ethsock_create(const char *interface, uint16_t protocol)
sock->pcap = pcap_open_live(interface, BUFSIZ, 1, 1, buf);
if (!sock->pcap) {
fprintf(stderr, "%s\n", buf);
fprintf(stderr, "%s.\n", buf);
goto cleanup_malloc;
}
@ -261,3 +261,31 @@ int ethsock_set_timeout(struct ethsock *sock, unsigned msec)
sock->timeout.tv_usec = (msec % 1000) * 1000;
return 0;
}
int ethsock_list_all(void)
{
pcap_if_t *devs, *dev;
uint8_t hwaddr[8];
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs(&devs, errbuf) != 0) {
fprintf(stderr, "%s.\n", errbuf);
return -1;
}
for (dev = devs; dev; dev = dev->next) {
get_hwaddr(hwaddr, dev->name);
printf("%02x:%02x:%02x:%02x:%02x:%02x %s",
hwaddr[0], hwaddr[1], hwaddr[2],
hwaddr[3], hwaddr[4], hwaddr[5],
dev->name);
if (dev->description) {
printf(" (%s)\n", dev->description);
} else {
printf("\n");
}
}
return 0;
}

View file

@ -8,3 +8,4 @@ int ethsock_send(struct ethsock *sock, void *buf, size_t len);
ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len);
int ethsock_set_timeout(struct ethsock *sock, unsigned msec);
uint8_t *ethsock_get_hwaddr(struct ethsock *sock);
int ethsock_list_all(void);

5
main.c
View file

@ -37,6 +37,7 @@ void usage(FILE *fp)
" -T <timeout> Time to wait after successfull TFTP upload\n"
" -p <port> Port to use for TFTP upload\n"
" -V Print version and exit\n"
" -L List network interfaces\n"
" -h Show this screen\n"
"\n"
"Example:\n"
@ -69,7 +70,7 @@ int main(int argc, char **argv)
opterr = 0;
while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:hV")) != -1) {
while ((c = getopt(argc, argv, "a:f:i:m:M:p:t:T:hLV")) != -1) {
max = 0x7fffffff;
switch (c) {
case 'a':
@ -109,6 +110,8 @@ int main(int argc, char **argv)
case 'V':
printf("nmrp-flash v%s\n", NMRPD_VERSION);
return 0;
case 'L':
return ethsock_list_all();
case 'h':
usage(stdout);
return 0;