NetLib: Move functions in netlib.c to separate files; improve some naming

This commit is contained in:
Gregory Nutt 2016-02-08 16:09:24 -06:00
parent b8c8bea22c
commit 1482732f18
6 changed files with 124 additions and 73 deletions

View File

@ -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 */

View File

@ -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

View File

@ -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 <gnutt@nuttx.org>
*
* Based on uIP which also has a BSD style license:
@ -46,63 +46,23 @@
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <debug.h>
#include <apps/netutils/netlib.h>
/****************************************************************************
* 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;

View File

@ -0,0 +1,96 @@
/****************************************************************************
* netutils/netlib/netlib_ipv4addrconv.c
*
* Copyright (C) 2007, 2009, 2011, 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Based on uIP which also has a BSD style license:
*
* Author: Adam Dunkels <adam@sics.se>
* 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 <stdint.h>
#include <stdbool.h>
#include <apps/netutils/netlib.h>
/****************************************************************************
* 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;
}

View File

@ -305,11 +305,11 @@ o arp [-a <ipaddr>|-d <ipaddr>|-s <ipaddr> <hwaddr>]
Will show the hardware address that the IP address <ipaddr> is mapped to.
-d <ipaddr>
Will delete the mapping for the IP address <ipaddr> from the ARP table
Will delete the mapping for the IP address <ipaddr> from the ARP table.
-s <ipaddr> <hwaddr>
Will set (or replace) the mapping of the IP address <ipaddr> to the
hardware address <hwaddr>
hardware address <hwaddr>.
Example:

View File

@ -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)
{