Add support for interface aliases on Windows
[oweals/nmrpflash.git] / nmrp.c
diff --git a/nmrp.c b/nmrp.c
index 43519fcdc3551d72664a4f81a504183ffc0211d9..bdbb6feb83a687cfae2b7ee4108f93ec6b13d640 100644 (file)
--- a/nmrp.c
+++ b/nmrp.c
  */
 
 #define _BSD_SOURCE
-#include <netinet/if_ether.h>
-//#include <linux/if_packet.h>
-#include <arpa/inet.h>
-#include <sys/socket.h>
-#include <sys/ioctl.h>
-#include <net/if.h>
+#include <signal.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include "ethsock.h"
 #include "nmrpd.h"
 
+#ifndef NMRPFLASH_WINDOWS
+#include <arpa/inet.h>
+#else
+#include <winsock2.h>
+#endif
+
 #define NMRP_HDR_LEN 6
 #define NMRP_OPT_LEN 4
-#define NMRP_MIN_PKT_LEN (sizeof(struct ether_header) +  NMRP_HDR_LEN)
+#define NMRP_MIN_PKT_LEN (sizeof(struct eth_hdr) +  NMRP_HDR_LEN)
 
-#define MAX_OPT_SIZE 12
-#define MAX_OPT_NUM 2
+#define NMRP_MAX_OPT_SIZE 12
+#define NMRP_MAX_OPT_NUM 2
 
 #define ETH_P_NMRP 0x0912
 #define IP_LEN 4
@@ -87,8 +88,14 @@ struct nmrp_msg {
        uint32_t num_opts;
 } PACKED;
 
+struct eth_hdr {
+       uint8_t ether_dhost[6];
+       uint8_t ether_shost[6];
+       uint16_t ether_type;
+} PACKED;
+
 struct nmrp_pkt {
-       struct ether_header eh;
+       struct eth_hdr eh;
        struct nmrp_msg msg;
 } PACKED;
 
@@ -161,7 +168,7 @@ static int msg_ntoh(struct nmrp_msg *msg)
 
        // FIXME maximum of two options supported, maximum option
        // size is 12
-       if (remaining < MAX_OPT_NUM * MAX_OPT_SIZE) {
+       if (remaining < NMRP_MAX_OPT_NUM * NMRP_MAX_OPT_SIZE) {
                while (remaining > 0) {
                        if (remaining < NMRP_OPT_LEN) {
                                break;
@@ -170,7 +177,7 @@ static int msg_ntoh(struct nmrp_msg *msg)
                        opt->type = ntohs(opt->type);
                        opt->len = ntohs(opt->len);
 
-                       if (opt->len > MAX_OPT_SIZE) {
+                       if (opt->len > NMRP_MAX_OPT_SIZE) {
                                break;
                        }
 
@@ -205,7 +212,7 @@ static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
        } else if (!bytes) {
                return 2;
        } else if (bytes < NMRP_MIN_PKT_LEN) {
-               fprintf(stderr, "Short packet (%zi bytes)\n", bytes);
+               fprintf(stderr, "Short packet (%d bytes)\n", (int)bytes);
                return 1;
        }
 
@@ -213,7 +220,7 @@ static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
        len = pkt->msg.len + sizeof(pkt->eh);
 
        if (bytes != len) {
-               fprintf(stderr, "Unexpected message length (%zi bytes).\n", len);
+               fprintf(stderr, "Unexpected message length (%d bytes).\n", (int)len);
                return 1;
        }
 
@@ -244,6 +251,18 @@ static int mac_parse(const char *str, uint8_t *hwaddr)
        return 0;
 }
 
+static struct ethsock *gsock = NULL;
+
+static void sigh(int sig)
+{
+       printf("\n");
+       if (gsock) {
+               ethsock_close(gsock);
+       }
+
+       exit(1);
+}
+
 static const char *spinner = "\\|/-";
 
 int nmrp_do(struct nmrpd_args *args)
@@ -254,6 +273,7 @@ int nmrp_do(struct nmrpd_args *args)
        time_t beg;
        int i, err, ulreqs, expect;
        struct ethsock *sock;
+       void (*sigh_orig)(int);
 
        if (args->op != NMRP_UPLOAD_FW) {
                fprintf(stderr, "Operation not implemented.\n");
@@ -265,12 +285,12 @@ int nmrp_do(struct nmrpd_args *args)
                return 1;
        }
 
-       if (!inet_aton(args->ipaddr, &ipaddr)) {
-               fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
+       if ((ipaddr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
+               fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
                return 1;
        }
 
-       if (!inet_aton(args->ipmask, &ipmask)) {
+       if ((ipmask.s_addr = inet_addr(args->ipmask)) == INADDR_NONE) {
                fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
                return 1;
        }
@@ -287,13 +307,16 @@ int nmrp_do(struct nmrpd_args *args)
                return 1;
        }
 
+       gsock = sock;
+       sigh_orig = signal(SIGINT, sigh);
+
        if (ethsock_set_timeout(sock, args->rx_timeout)) {
-               return 1;
+               goto out;
        }
 
        src = ethsock_get_hwaddr(sock);
        if (!src) {
-               return 1;
+               goto out;
        }
 
        memcpy(tx.eh.ether_shost, src, 6);
@@ -318,7 +341,7 @@ int nmrp_do(struct nmrpd_args *args)
        beg = time(NULL);
 
        while (1) {
-               printf("\rAdvertising NMRP server on %s ... %c", args->intf,
+               printf("\rAdvertising NMRP server on interface ... %c",
                                spinner[i]);
                fflush(stdout);
                i = (i + 1) & 3;
@@ -470,6 +493,8 @@ int nmrp_do(struct nmrpd_args *args)
        err = 0;
 
 out:
+       signal(SIGINT, sigh_orig);
+       gsock = NULL;
        ethsock_close(sock);
        return err;
 }