From: Joseph C. Lehner Date: Fri, 5 Feb 2016 12:31:57 +0000 (+0200) Subject: Better error messages X-Git-Tag: v0.9~45 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=ad7f69f0c5b18d2336b5243e6c231b1afb9873a5;p=oweals%2Fnmrpflash.git Better error messages --- diff --git a/tftp.c b/tftp.c index a87f426..a9aac18 100644 --- a/tftp.c +++ b/tftp.c @@ -17,13 +17,13 @@ * */ -#define _BSD_SOURCE #include #include #include #include #include #include +#include #include "nmrpd.h" #define TFTP_PKT_SIZE 516 @@ -130,12 +130,18 @@ static ssize_t tftp_recvfrom(int sock, char *pkt, struct sockaddr_in *src) if (opcode == ERR) { fprintf(stderr, "Error (%d): %.511s\n", pkt_num(pkt + 2), pkt + 4); return -1; - } else if (!opcode || opcode > ERR) { - /* The EX2700 I've tested this on sends a raw TFTP packet with no - * opcode, and an error message starting at offset 0. + } else if (isprint(pkt[0])) { + /* In case of a firmware checksum error, the EX2700 I've tested this + * on sends a raw UDP packet containing just an error message starting + * at offset 0. The limit of 32 chars is arbitrary. */ fprintf(stderr, "Error: %.32s\n", pkt); return -3; + } else { + fprintf(stderr, "Received invalid packet: "); + pkt_print(pkt, stderr); + fprintf(stderr, ".\n"); + return -2; } return len; @@ -162,8 +168,9 @@ static ssize_t tftp_sendto(int sock, char *pkt, size_t len, len = 4 + strlen(pkt + 4); break; default: - fprintf(stderr, "Error: Invalid packet "); + fprintf(stderr, "Attempted to send invalid packet "); pkt_print(pkt, stderr); + fprintf(stderr, "; this is a bug!\n"); return -1; }