Fix warning when building on 32-bit OSX
[oweals/nmrpflash.git] / nmrp.c
diff --git a/nmrp.c b/nmrp.c
index bdbb6feb83a687cfae2b7ee4108f93ec6b13d640..4bca5ced37d2426a247087f1dbbc39607f423e1d 100644 (file)
--- a/nmrp.c
+++ b/nmrp.c
@@ -17,7 +17,6 @@
  *
  */
 
-#define _BSD_SOURCE
 #include <signal.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <stdio.h>
 #include <time.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 eth_hdr) +  NMRP_HDR_LEN)
 
 #define ETH_P_NMRP 0x0912
 #define IP_LEN 4
-#define PACKED __attribute__((__packed__))
 #define MAX_LOOP_RECV 1024
 
+#ifndef PACKED
+#define PACKED __attribute__((__packed__))
+#endif
+
 enum nmrp_code {
        NMRP_C_NONE = 0,
        NMRP_C_ADVERTISE = 1,
@@ -163,7 +158,6 @@ static int msg_ntoh(struct nmrp_msg *msg)
        struct nmrp_opt *opt = msg->opts;
        int remaining;
 
-       msg_hdr_ntoh(msg);
        remaining = msg->len - NMRP_HDR_LEN;
 
        // FIXME maximum of two options supported, maximum option
@@ -219,8 +213,9 @@ static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
        msg_hdr_ntoh(&pkt->msg);
        len = pkt->msg.len + sizeof(pkt->eh);
 
-       if (bytes != len) {
-               fprintf(stderr, "Unexpected message length (%d bytes).\n", (int)len);
+       if (bytes < len) {
+               fprintf(stderr, "Short packet (expected %d, got %d).\n",
+                               (int)len, (int)bytes);
                return 1;
        }
 
@@ -286,7 +281,7 @@ int nmrp_do(struct nmrpd_args *args)
        }
 
        if ((ipaddr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
-               fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
+               fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr);
                return 1;
        }
 
@@ -372,7 +367,7 @@ int nmrp_do(struct nmrpd_args *args)
 
        do {
                if (expect != NMRP_C_NONE && rx.msg.code != expect) {
-                       fprintf(stderr, "Received code 0x%02x while waiting for 0x%02x!\n", 
+                       fprintf(stderr, "Received code 0x%02x while waiting for 0x%02x!\n",
                                        rx.msg.code, expect);
                }
 
@@ -386,11 +381,8 @@ int nmrp_do(struct nmrpd_args *args)
 
                switch (rx.msg.code) {
                        case NMRP_C_ADVERTISE:
-                               printf("Received NMRP advertisement from "
-                                               "%02x:%02x:%02x:%02x:%02x:%02x.\n",
-                                               rx.eh.ether_shost[0], rx.eh.ether_shost[1],
-                                               rx.eh.ether_shost[2], rx.eh.ether_shost[3],
-                                               rx.eh.ether_shost[4], rx.eh.ether_shost[5]);
+                               printf("Received NMRP advertisement from %s.\n",
+                                               mac_to_str(rx.eh.ether_shost));
                                err = 1;
                                goto out;
                        case NMRP_C_CONF_REQ:
@@ -408,11 +400,8 @@ int nmrp_do(struct nmrpd_args *args)
 
                                expect = NMRP_C_TFTP_UL_REQ;
 
-                               printf("Received configuration request from "
-                                               "%02x:%02x:%02x:%02x:%02x:%02x.\n",
-                                               rx.eh.ether_shost[0], rx.eh.ether_shost[1],
-                                               rx.eh.ether_shost[2], rx.eh.ether_shost[3],
-                                               rx.eh.ether_shost[4], rx.eh.ether_shost[5]);
+                               printf("Received configuration request from %s.\n",
+                                               mac_to_str(rx.eh.ether_shost));
 
                                memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
 
@@ -442,8 +431,9 @@ int nmrp_do(struct nmrpd_args *args)
                                        printf("OK\nWaiting for remote to respond.\n");
                                        ethsock_set_timeout(sock, args->ul_timeout);
                                        expect = NMRP_C_CLOSE_REQ;
+                               } else if (err == -2) {
+                                       expect = NMRP_C_TFTP_UL_REQ;
                                } else {
-                                       printf("\n");
                                        goto out;
                                }