*
*/
-#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,
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
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;
}
}
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;
}
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);
}
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:
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);
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;
}