From: Joseph C. Lehner Date: Sun, 31 Jan 2016 15:31:56 +0000 (+0200) Subject: Add option to list network interfaces X-Git-Tag: v0.9~76 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=0bcc6da15750fc110f39142736fd10d320783e4d;p=oweals%2Fnmrpflash.git Add option to list network interfaces --- diff --git a/ethsock.c b/ethsock.c index f618456..56f5549 100644 --- a/ethsock.c +++ b/ethsock.c @@ -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; +} diff --git a/ethsock.h b/ethsock.h index 6e2c50f..43942e5 100644 --- a/ethsock.h +++ b/ethsock.h @@ -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); diff --git a/main.c b/main.c index f352ef7..51ca00c 100644 --- a/main.c +++ b/main.c @@ -37,6 +37,7 @@ void usage(FILE *fp) " -T Time to wait after successfull TFTP upload\n" " -p 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;