mirror of
https://github.com/apache/nuttx.git
synced 2026-05-19 03:03:37 +08:00
libs/libc/net: implement ether_aton/ether_aton_r
Reference here: https://www.unix.com/man-page/linux/3/ether_aton Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
@@ -70,6 +70,8 @@ int ether_ntohost(FAR char *hostname, FAR const struct ether_addr *addr);
|
||||
int ether_hostton(FAR const char *hostname, FAR struct ether_addr *addr);
|
||||
int ether_line(FAR const char *line, FAR struct ether_addr *addr,
|
||||
FAR char *hostname);
|
||||
FAR struct ether_addr *ether_aton_r(FAR const char *asc,
|
||||
FAR struct ether_addr *addr);
|
||||
|
||||
#undef EXTERN
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -35,9 +35,10 @@
|
||||
|
||||
# Add the networking C files to the build
|
||||
|
||||
CSRCS += lib_addrconfig.c lib_etherntoa.c lib_htons.c lib_htonl.c
|
||||
CSRCS += lib_addrconfig.c lib_htons.c lib_htonl.c
|
||||
CSRCS += lib_inetaddr.c lib_inetaton.c lib_inetntoa.c
|
||||
CSRCS += lib_inetntop.c lib_inetpton.c
|
||||
CSRCS += lib_etherntoa.c lib_etheraton.c
|
||||
|
||||
ifeq ($(CONFIG_NET),y)
|
||||
CSRCS += lib_recvmsg.c lib_sendmsg.c lib_shutdown.c
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
/****************************************************************************
|
||||
* libs/libc/net/lib_etheraton.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <net/ethernet.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static inline int xdigit(char c)
|
||||
{
|
||||
unsigned d;
|
||||
|
||||
d = (unsigned)(c - '0');
|
||||
if (d < 10)
|
||||
{
|
||||
return (int)d;
|
||||
}
|
||||
|
||||
d = (unsigned)(c - 'a');
|
||||
if (d < 6)
|
||||
{
|
||||
return (int)(10 + d);
|
||||
}
|
||||
|
||||
d = (unsigned)(c - 'A');
|
||||
if (d < 6)
|
||||
{
|
||||
return (int)(10 + d);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ether_aton_r
|
||||
*
|
||||
* Description:
|
||||
* Convert Ethernet address in the standard hex-digits-and-colons to binary
|
||||
* representation.
|
||||
* Re-entrant version (GNU extensions)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR struct ether_addr *ether_aton_r(FAR const char *asc,
|
||||
FAR struct ether_addr *addr)
|
||||
{
|
||||
int i, val0, val1;
|
||||
|
||||
for (i = 0; i < ETHER_ADDR_LEN; ++i)
|
||||
{
|
||||
val0 = xdigit(*asc);
|
||||
asc++;
|
||||
if (val0 < 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
val1 = xdigit(*asc);
|
||||
asc++;
|
||||
if (val1 < 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
addr->ether_addr_octet[i] = (u_int8_t)((val0 << 4) + val1);
|
||||
|
||||
if (i < ETHER_ADDR_LEN - 1)
|
||||
{
|
||||
if (*asc != ':')
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
asc++;
|
||||
}
|
||||
}
|
||||
|
||||
if (*asc != '\0')
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ether_aton
|
||||
*
|
||||
* Description:
|
||||
* Convert Ethernet address in the standard hex-digits-and-colons to binary
|
||||
* representation.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR struct ether_addr *ether_aton(FAR const char *asc)
|
||||
{
|
||||
static struct ether_addr addr;
|
||||
return ether_aton_r(asc, &addr);
|
||||
}
|
||||
Reference in New Issue
Block a user