Add option to list network interfaces
authorJoseph C. Lehner <joseph.c.lehner@gmail.com>
Sun, 31 Jan 2016 15:31:56 +0000 (17:31 +0200)
committerJoseph C. Lehner <joseph.c.lehner@gmail.com>
Sun, 31 Jan 2016 15:32:06 +0000 (17:32 +0200)
ethsock.c
ethsock.h
main.c

index f61845653f46e5c06c9baf8b966ce765835975b2..56f554904cd726f8acff10f1740d68c70d7b2b34 100644 (file)
--- 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;
+}
index 6e2c50f35983659679cb8ba54a6012e91ad78429..43942e5928bed898dea20608d77c7742935f4fe5 100644 (file)
--- 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 f352ef789723331a13c6c4092eba72227eb5d7f2..51ca00cc087f7728f52c3e557e7c87fd6b9ecec3 100644 (file)
--- a/main.c
+++ b/main.c
@@ -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;