From patchwork Fri Jul 7 07:13:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Huth X-Patchwork-Id: 785384 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3x3m6L6xR0z9s4s for ; Fri, 7 Jul 2017 17:13:14 +1000 (AEST) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3x3m6L68CBzDr5q for ; Fri, 7 Jul 2017 17:13:14 +1000 (AEST) X-Original-To: slof@lists.ozlabs.org Delivered-To: slof@lists.ozlabs.org Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3x3m6F594PzDqj8 for ; Fri, 7 Jul 2017 17:13:09 +1000 (AEST) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 676B114AAA; Fri, 7 Jul 2017 07:13:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 676B114AAA Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=thuth@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 676B114AAA Received: from thh440s.str.redhat.com (dhcp-200-180.str.redhat.com [10.33.200.180]) by smtp.corp.redhat.com (Postfix) with ESMTP id C514A6F950; Fri, 7 Jul 2017 07:13:05 +0000 (UTC) From: Thomas Huth To: slof@lists.ozlabs.org Date: Fri, 7 Jul 2017 09:13:05 +0200 Message-Id: <1499411585-31024-1-git-send-email-thuth@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Fri, 07 Jul 2017 07:13:06 +0000 (UTC) Subject: [SLOF] [PATCH] libnet: Move parse_tftp_args to tftp.c X-BeenThere: slof@lists.ozlabs.org X-Mailman-Version: 2.1.23 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" To be able to re-use the libnet code in other projects (where the rather Open Firmware specific netload.c can not be used), the function parse_tftp_args() must be moved to another file. The function is related to TFTP, and its prototype is already declared in tftp.h, so the code should reside in tftp.c. Signed-off-by: Thomas Huth --- lib/libnet/netload.c | 106 --------------------------------------------------- lib/libnet/tftp.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 106 deletions(-) diff --git a/lib/libnet/netload.c b/lib/libnet/netload.c index c549e69..cecb2a0 100644 --- a/lib/libnet/netload.c +++ b/lib/libnet/netload.c @@ -17,7 +17,6 @@ #include #include #include -#include #include #include #include @@ -735,108 +734,3 @@ int netload(char *buffer, int len, char *ret_buffer, int huge_load, return rc; } - -/** - * Parses a tftp arguments, extracts all - * parameters and fills server ip according to this - * - * Parameters: - * @param buffer string with arguments, - * @param server_ip server ip as result - * @param filename default filename - * @param fd Socket descriptor - * @param len len of the buffer, - * @return 0 on SUCCESS and -1 on failure - */ -int parse_tftp_args(char buffer[], char *server_ip, char filename[], int fd, - int len) -{ - char *raw; - char *tmp, *tmp1; - int i, j = 0; - char domainname[256]; - uint8_t server_ip6[16]; - - raw = malloc(len); - if (raw == NULL) { - printf("\n unable to allocate memory, parsing failed\n"); - return -1; - } - strncpy(raw,(const char *)buffer,len); - /*tftp url contains tftp://[fd00:4f53:4444:90:214:5eff:fed9:b200]/testfile*/ - if(strncmp(raw,"tftp://",7)){ - printf("\n tftp missing in %s\n",raw); - free(raw); - return -1; - } - tmp = strchr(raw,'['); - if(tmp != NULL && *tmp == '[') { - /*check for valid ipv6 address*/ - tmp1 = strchr(tmp,']'); - if (tmp1 == NULL) { - printf("\n missing ] in %s\n",raw); - free(raw); - return -1; - } - i = tmp1 - tmp; - /*look for file name*/ - tmp1 = strchr(tmp,'/'); - if (tmp1 == NULL) { - printf("\n missing filename in %s\n",raw); - free(raw); - return -1; - } - tmp[i] = '\0'; - /*check for 16 byte ipv6 address */ - if (!str_to_ipv6((tmp+1), (uint8_t *)(server_ip))) { - printf("\n wrong format IPV6 address in %s\n",raw); - free(raw); - return -1;; - } - else { - /*found filename */ - strcpy(filename,(tmp1+1)); - free(raw); - return 0; - } - } - else { - /*here tftp://hostname/testfile from option request of dhcp*/ - /*look for dns server name */ - tmp1 = strchr(raw,'.'); - if(tmp1 == NULL) { - printf("\n missing . seperator in %s\n",raw); - free(raw); - return -1; - } - /*look for domain name beyond dns server name - * so ignore the current . and look for one more - */ - tmp = strchr((tmp1+1),'.'); - if(tmp == NULL) { - printf("\n missing domain in %s\n",raw); - free(raw); - return -1; - } - tmp1 = strchr(tmp1,'/'); - if (tmp1 == NULL) { - printf("\n missing filename in %s\n",raw); - free(raw); - return -1; - } - j = tmp1 - (raw + 7); - tmp = raw + 7; - tmp[j] = '\0'; - strcpy(domainname, tmp); - if (dns_get_ip(fd, domainname, server_ip6, 6) == 0) { - printf("\n DNS failed for IPV6\n"); - return -1; - } - ipv6_to_str(server_ip6, server_ip); - - strcpy(filename,(tmp1+1)); - free(raw); - return 0; - } - -} diff --git a/lib/libnet/tftp.c b/lib/libnet/tftp.c index 108092b..cda8bf3 100644 --- a/lib/libnet/tftp.c +++ b/lib/libnet/tftp.c @@ -21,6 +21,7 @@ #include #include #include +#include //#define __DEBUG__ @@ -593,3 +594,106 @@ int tftp(filename_ip_t * _fn_ip, unsigned char *_buffer, int _len, return received_len; } + +/** + * Parses a tftp arguments, extracts all + * parameters and fills server ip according to this + * + * Parameters: + * @param buffer string with arguments, + * @param server_ip server ip as result + * @param filename default filename + * @param fd Socket descriptor + * @param len len of the buffer, + * @return 0 on SUCCESS and -1 on failure + */ +int parse_tftp_args(char buffer[], char *server_ip, char filename[], int fd, + int len) +{ + char *raw; + char *tmp, *tmp1; + int i, j = 0; + char domainname[256]; + uint8_t server_ip6[16]; + + raw = malloc(len); + if (raw == NULL) { + printf("\n unable to allocate memory, parsing failed\n"); + return -1; + } + strncpy(raw, (const char *)buffer, len); + /* tftp url contains tftp://[fd00:4f53:4444:90:214:5eff:fed9:b200]/testfile */ + if (strncmp(raw, "tftp://", 7)){ + printf("\n tftp missing in %s\n", raw); + free(raw); + return -1; + } + tmp = strchr(raw, '['); + if (tmp != NULL && *tmp == '[') { + /* check for valid ipv6 address */ + tmp1 = strchr(tmp, ']'); + if (tmp1 == NULL) { + printf("\n missing ] in %s\n", raw); + free(raw); + return -1; + } + i = tmp1 - tmp; + /* look for file name */ + tmp1 = strchr(tmp, '/'); + if (tmp1 == NULL) { + printf("\n missing filename in %s\n", raw); + free(raw); + return -1; + } + tmp[i] = '\0'; + /* check for 16 byte ipv6 address */ + if (!str_to_ipv6(tmp + 1, (uint8_t *)server_ip)) { + printf("\n wrong format IPV6 address in %s\n", raw); + free(raw); + return -1;; + } + else { + /* found filename */ + strcpy(filename, tmp1 + 1); + free(raw); + return 0; + } + } + else { + /* here tftp://hostname/testfile from option request of dhcp */ + /* look for dns server name */ + tmp1 = strchr(raw, '.'); + if (tmp1 == NULL) { + printf("\n missing . seperator in %s\n", raw); + free(raw); + return -1; + } + /* look for domain name beyond dns server name + * so ignore the current . and look for one more */ + tmp = strchr(tmp1 + 1, '.'); + if (tmp == NULL) { + printf("\n missing domain in %s\n", raw); + free(raw); + return -1; + } + tmp1 = strchr(tmp1, '/'); + if (tmp1 == NULL) { + printf("\n missing filename in %s\n", raw); + free(raw); + return -1; + } + j = tmp1 - (raw + 7); + tmp = raw + 7; + tmp[j] = '\0'; + strcpy(domainname, tmp); + if (dns_get_ip(fd, domainname, server_ip6, 6) == 0) { + printf("\n DNS failed for IPV6\n"); + return -1; + } + ipv6_to_str(server_ip6, server_ip); + + strcpy(filename, tmp1 + 1); + free(raw); + return 0; + } +}