diff --git a/include/netutils/netlib.h b/include/netutils/netlib.h index 1effa7709..155c05bd0 100644 --- a/include/netutils/netlib.h +++ b/include/netutils/netlib.h @@ -104,14 +104,14 @@ extern "C" * Return: Non-zero If the IP address was parsed. */ -bool netlib_ipaddrconv(FAR const char *addrstr, uint8_t *addr); -bool netlib_hwmacconv(FAR const char *hwstr, uint8_t *hw); +bool netlib_ipv4addrconv(FAR const char *addrstr, FAR uint8_t *addr); +bool netlib_ethaddrconv(FAR const char *hwstr, FAR uint8_t *hw); #ifdef CONFIG_NET_ETHERNET /* Get and set IP/MAC addresses (Ethernet L2 only) */ -int netlib_setmacaddr(FAR const char *ifname, const uint8_t *macaddr); -int netlib_getmacaddr(FAR const char *ifname, uint8_t *macaddr); +int netlib_setmacaddr(FAR const char *ifname, FAR const uint8_t *macaddr); +int netlib_getmacaddr(FAR const char *ifname, FAR uint8_t *macaddr); #endif /* IP address support */ diff --git a/netutils/netlib/Makefile b/netutils/netlib/Makefile index a40ac39d6..ddf54a32b 100644 --- a/netutils/netlib/Makefile +++ b/netutils/netlib/Makefile @@ -40,7 +40,7 @@ include $(APPDIR)/Make.defs # UIP Library ASRCS = -CSRCS = netlib.c netlib_parsehttpurl.c +CSRCS = netlib_ipv4addrconv.c netlib_ethaddrconv.c netlib_parsehttpurl.c CSRCS += netlib_setifstatus.c netlib_getifstatus.c # IP address support diff --git a/netutils/netlib/netlib.c b/netutils/netlib/netlib_ethaddrconv.c similarity index 67% rename from netutils/netlib/netlib.c rename to netutils/netlib/netlib_ethaddrconv.c index 2d2a67838..3073c1073 100644 --- a/netutils/netlib/netlib.c +++ b/netutils/netlib/netlib_ethaddrconv.c @@ -2,7 +2,7 @@ * netutils/netlib/netlib.c * Various uIP library functions. * - * Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009, 2011, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Based on uIP which also has a BSD style license: @@ -46,63 +46,23 @@ #include #include #include -#include #include - /**************************************************************************** * Public Functions ****************************************************************************/ -bool netlib_ipaddrconv(const char *addrstr, uint8_t *ipaddr) +/**************************************************************************** + * Name: netlib_ethaddrconv + ****************************************************************************/ + +bool netlib_ethaddrconv(FAR const char *hwstr, FAR uint8_t *hw) { unsigned char tmp; - char c; - unsigned char i; - unsigned char j; - - tmp = 0; - - for (i = 0; i < 4; ++i) - { - j = 0; - do - { - c = *addrstr; - ++j; - if (j > 4) - { - return false; - } - if (c == '.' || c == 0) - { - *ipaddr = tmp; - ++ipaddr; - tmp = 0; - } - else if (c >= '0' && c <= '9') - { - tmp = (tmp * 10) + (c - '0'); - } - else - { - return false; - } - ++addrstr; - } - while (c != '.' && c != 0); - } - - return true; -} - -bool netlib_hwmacconv(const char *hwstr, uint8_t *hw) -{ - unsigned char tmp; - char c; unsigned char i; unsigned char j; + char ch; if (strlen(hwstr) != 17) { @@ -116,40 +76,35 @@ bool netlib_hwmacconv(const char *hwstr, uint8_t *hw) j = 0; do { - c = *hwstr; - ++j; - if (j > 3) + ch = *hwstr++; + if (++j > 3) { return false; } - if (c == ':' || c == 0) + if (ch == ':' || ch == 0) { - *hw = tmp; - nvdbg("HWMAC[%d]%0.2X\n",i,tmp); - ++hw; + *hw++ = tmp; tmp = 0; } - else if (c >= '0' && c <= '9') + else if (ch >= '0' && ch <= '9') { - tmp = (tmp << 4) + (c - '0'); + tmp = (tmp << 4) + (ch - '0'); } - else if (c >= 'a' && c <= 'f') + else if (ch >= 'a' && ch <= 'f') { - tmp = (tmp << 4) + (c - 'a' + 10); + tmp = (tmp << 4) + (ch - 'a' + 10); } - else if (c >= 'A' && c <= 'F') + else if (ch >= 'A' && ch <= 'F') { - tmp = (tmp << 4) + (c - 'A' + 10); + tmp = (tmp << 4) + (ch - 'A' + 10); } else { return false; } - - ++hwstr; } - while (c != ':' && c != 0); + while (ch != ':' && ch != 0); } return true; diff --git a/netutils/netlib/netlib_ipv4addrconv.c b/netutils/netlib/netlib_ipv4addrconv.c new file mode 100644 index 000000000..1195a2faa --- /dev/null +++ b/netutils/netlib/netlib_ipv4addrconv.c @@ -0,0 +1,96 @@ +/**************************************************************************** + * netutils/netlib/netlib_ipv4addrconv.c + * + * Copyright (C) 2007, 2009, 2011, 2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Based on uIP which also has a BSD style license: + * + * Author: Adam Dunkels + * Copyright (c) 2004, Adam Dunkels and the Swedish Institute of + * Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_ipv4addrconv + ****************************************************************************/ + +bool netlib_ipv4addrconv(FAR const char *addrstr, FAR uint8_t *ipaddr) +{ + unsigned char tmp; + unsigned char i; + unsigned char j; + char ch; + + tmp = 0; + + for (i = 0; i < 4; ++i) + { + j = 0; + do + { + ch = *addrstr++; + if (++j > 4) + { + return false; + } + + if (ch == '.' || ch == 0) + { + *ipaddr++ = tmp; + tmp = 0; + } + else if (ch >= '0' && ch <= '9') + { + tmp = (tmp * 10) + (ch - '0'); + } + else + { + return false; + } + } + while (ch != '.' && ch != 0); + } + + return true; +} diff --git a/nshlib/README.txt b/nshlib/README.txt index 13f4ad7f4..33b56bfa0 100644 --- a/nshlib/README.txt +++ b/nshlib/README.txt @@ -305,11 +305,11 @@ o arp [-a |-d |-s ] Will show the hardware address that the IP address is mapped to. -d - Will delete the mapping for the IP address from the ARP table + Will delete the mapping for the IP address from the ARP table. -s Will set (or replace) the mapping of the IP address to the - hardware address + hardware address . Example: diff --git a/nshlib/nsh_netcmds.c b/nshlib/nsh_netcmds.c index 0684d1b9e..85dbe2f32 100644 --- a/nshlib/nsh_netcmds.c +++ b/nshlib/nsh_netcmds.c @@ -341,7 +341,7 @@ int tftpc_parseargs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv, break; case 'h': - if (!netlib_ipaddrconv(optarg, (FAR unsigned char*)&args->ipaddr)) + if (!netlib_ipv4addrconv(optarg, (FAR unsigned char*)&args->ipaddr)) { nsh_output(vtbl, g_fmtarginvalid, argv[0]); badarg = true; @@ -876,7 +876,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { hw = argv[i+1]; i++; - badarg = !netlib_hwmacconv(hw, mac); + badarg = !netlib_ethaddrconv(hw, mac); } else { @@ -1229,7 +1229,7 @@ int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) goto errout_cmdfaild; } - nsh_output(vtbl, "HWAddr: %s\n", ether_ntoa(&mac)); + nsh_output(vtbl, "HWaddr: %s\n", ether_ntoa(&mac)); } else if (strcmp(argv[1], "-d") == 0) {