*/
#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
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;
// 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;
opt->type = ntohs(opt->type);
opt->len = ntohs(opt->len);
- if (opt->len > MAX_OPT_SIZE) {
+ if (opt->len > NMRP_MAX_OPT_SIZE) {
break;
}
} 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;
}
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;
}
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)
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");
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;
}
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);
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;
err = 0;
out:
+ signal(SIGINT, sigh_orig);
+ gsock = NULL;
ethsock_close(sock);
return err;
}