From patchwork Thu May 17 17:40:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Huth X-Patchwork-Id: 915622 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40mzBC49sGz9s3c for ; Fri, 18 May 2018 03:41:23 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 40mzBC2gQFzF27m for ; Fri, 18 May 2018 03:41:23 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com X-Original-To: slof@lists.ozlabs.org Delivered-To: slof@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=redhat.com (client-ip=66.187.233.73; helo=mx1.redhat.com; envelope-from=thuth@redhat.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com Received: from mx1.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 40mz9q4r8lzF24j for ; Fri, 18 May 2018 03:41:03 +1000 (AEST) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 80B644201B08 for ; Thu, 17 May 2018 17:41:01 +0000 (UTC) Received: from thh440s.redhat.com (ovpn-116-19.ams2.redhat.com [10.36.116.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id D9B8AD74CB for ; Thu, 17 May 2018 17:41:00 +0000 (UTC) From: Thomas Huth To: slof@lists.ozlabs.org Date: Thu, 17 May 2018 19:40:51 +0200 Message-Id: <1526578856-30967-5-git-send-email-thuth@redhat.com> In-Reply-To: <1526578856-30967-1-git-send-email-thuth@redhat.com> References: <1526578856-30967-1-git-send-email-thuth@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Thu, 17 May 2018 17:41:01 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Thu, 17 May 2018 17:41:01 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'thuth@redhat.com' RCPT:'' Subject: [SLOF] [PATCH 4/9] libnet: Put code for determing TFTP error strings into a separate function X-BeenThere: slof@lists.ozlabs.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "Patches for https://github.com/aik/SLOF" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: slof-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "SLOF" This way we can easily re-use the rc --> string translation in later patches. Signed-off-by: Thomas Huth Reviewed-by: Greg Kurz --- lib/libnet/netload.c | 79 +++------------------------------------ lib/libnet/tftp.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/libnet/tftp.h | 2 + 3 files changed, 111 insertions(+), 73 deletions(-) diff --git a/lib/libnet/netload.c b/lib/libnet/netload.c index add447c..9208e10 100644 --- a/lib/libnet/netload.c +++ b/lib/libnet/netload.c @@ -414,79 +414,12 @@ static int tftp_load(filename_ip_t *fnip, void *buffer, int len) if (rc > 0) { printf(" TFTP: Received %s (%d KBytes)\n", fnip->filename, rc / 1024); - } else if (rc == -1) { - netload_error(0x3003, "unknown TFTP error"); - return -103; - } else if (rc == -2) { - netload_error(0x3004, "TFTP buffer of %d bytes " - "is too small for %s", - len, fnip->filename); - return -104; - } else if (rc == -3) { - netload_error(0x3009, "file not found: %s", - fnip->filename); - return -108; - } else if (rc == -4) { - netload_error(0x3010, "TFTP access violation"); - return -109; - } else if (rc == -5) { - netload_error(0x3011, "illegal TFTP operation"); - return -110; - } else if (rc == -6) { - netload_error(0x3012, "unknown TFTP transfer ID"); - return -111; - } else if (rc == -7) { - netload_error(0x3013, "no such TFTP user"); - return -112; - } else if (rc == -8) { - netload_error(0x3017, "TFTP blocksize negotiation failed"); - return -116; - } else if (rc == -9) { - netload_error(0x3018, "file exceeds maximum TFTP transfer size"); - return -117; - } else if (rc <= -10 && rc >= -15) { - const char *icmp_err_str; - switch (rc) { - case -ICMP_NET_UNREACHABLE - 10: - icmp_err_str = "net unreachable"; - break; - case -ICMP_HOST_UNREACHABLE - 10: - icmp_err_str = "host unreachable"; - break; - case -ICMP_PROTOCOL_UNREACHABLE - 10: - icmp_err_str = "protocol unreachable"; - break; - case -ICMP_PORT_UNREACHABLE - 10: - icmp_err_str = "port unreachable"; - break; - case -ICMP_FRAGMENTATION_NEEDED - 10: - icmp_err_str = "fragmentation needed and DF set"; - break; - case -ICMP_SOURCE_ROUTE_FAILED - 10: - icmp_err_str = "source route failed"; - break; - default: - icmp_err_str = " UNKNOWN"; - break; - } - netload_error(0x3005, "ICMP ERROR \"%s\"", icmp_err_str); - return -105; - } else if (rc == -40) { - netload_error(0x3014, "TFTP error occurred after " - "%d bad packets received", - tftp_err.bad_tftp_packets); - return -113; - } else if (rc == -41) { - netload_error(0x3015, "TFTP error occurred after " - "missing %d responses", - tftp_err.no_packets); - return -114; - } else if (rc == -42) { - netload_error(0x3016, "TFTP error missing block %d, " - "expected block was %d", - tftp_err.blocks_missed, - tftp_err.blocks_received); - return -115; + } else { + int ecode; + char *errstr = NULL; + rc = tftp_get_error_info(fnip, &tftp_err, rc, &errstr, &ecode); + if (errstr) + netload_error(ecode, errstr); } return rc; diff --git a/lib/libnet/tftp.c b/lib/libnet/tftp.c index d760f03..6d4d209 100644 --- a/lib/libnet/tftp.c +++ b/lib/libnet/tftp.c @@ -696,3 +696,106 @@ int parse_tftp_args(char buffer[], char *server_ip, char filename[], int fd, return 0; } } + +int tftp_get_error_info(filename_ip_t *fnip, tftp_err_t *tftperr, int rc, + char **errstr, int *ecode) +{ + static char estrbuf[80]; + int dummy; + + if (!ecode) + ecode = &dummy; + + if (rc == -1) { + *ecode = 0x3003; + *errstr = "unknown TFTP error"; + return -103; + } else if (rc == -2) { + *ecode = 0x3004; + sprintf(estrbuf, "TFTP buffer of %d bytes is too small for %s", + len, fnip->filename); + *errstr = estrbuf; + return -104; + } else if (rc == -3) { + *ecode = 0x3009; + sprintf(estrbuf, "file not found: %s", fnip->filename); + *errstr = estrbuf; + return -108; + } else if (rc == -4) { + *ecode = 0x3010; + *errstr = "TFTP access violation"; + return -109; + } else if (rc == -5) { + *ecode = 0x3011; + *errstr = "illegal TFTP operation"; + return -110; + } else if (rc == -6) { + *ecode = 0x3012; + *errstr = "unknown TFTP transfer ID"; + return -111; + } else if (rc == -7) { + *ecode = 0x3013; + *errstr = "no such TFTP user"; + return -112; + } else if (rc == -8) { + *ecode = 0x3017; + *errstr = "TFTP blocksize negotiation failed"; + return -116; + } else if (rc == -9) { + *ecode = 0x3018; + *errstr = "file exceeds maximum TFTP transfer size"; + return -117; + } else if (rc <= -10 && rc >= -15) { + const char *icmp_err_str; + switch (rc) { + case -ICMP_NET_UNREACHABLE - 10: + icmp_err_str = "net unreachable"; + break; + case -ICMP_HOST_UNREACHABLE - 10: + icmp_err_str = "host unreachable"; + break; + case -ICMP_PROTOCOL_UNREACHABLE - 10: + icmp_err_str = "protocol unreachable"; + break; + case -ICMP_PORT_UNREACHABLE - 10: + icmp_err_str = "port unreachable"; + break; + case -ICMP_FRAGMENTATION_NEEDED - 10: + icmp_err_str = "fragmentation needed and DF set"; + break; + case -ICMP_SOURCE_ROUTE_FAILED - 10: + icmp_err_str = "source route failed"; + break; + default: + icmp_err_str = "UNKNOWN"; + break; + } + *ecode = 0x3005; + sprintf(estrbuf, "ICMP ERROR \"%s\"", icmp_err_str); + *errstr = estrbuf; + return -105; + } else if (rc == -40) { + *ecode = 0x3014; + sprintf(estrbuf, + "TFTP error occurred after %d bad packets received", + tftp_err->bad_tftp_packets); + *errstr = estrbuf; + return -113; + } else if (rc == -41) { + *ecode = 0x3015; + sprintf(estrbuf, + "TFTP error occurred after missing %d responses", + tftp_err->no_packets); + *errstr = estrbuf; + return -114; + } else if (rc == -42) { + *ecode = 0x3016; + sprintf(estrbuf, + "TFTP error missing block %d, expected block was %d", + tftp_err->blocks_missed, tftp_err->blocks_received); + *errstr = estrbuf; + return -115; + } + + return rc; +} diff --git a/lib/libnet/tftp.h b/lib/libnet/tftp.h index 6f68d15..3483091 100644 --- a/lib/libnet/tftp.h +++ b/lib/libnet/tftp.h @@ -48,5 +48,7 @@ int tftp(filename_ip_t *fnip, unsigned char *buf, int len, tftp_err_t *err, int32_t handle_tftp(int fd, uint8_t *, int32_t); void handle_tftp_dun(uint8_t err_code); int parse_tftp_args(char buffer[], char *server_ip, char filename[], int fd, int len); +int tftp_get_error_info(filename_ip_t *fnip, tftp_err_t *tftperr, int rc, + char **errstr, int *ecode); #endif