You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2020/12/11 02:37:39 UTC

[incubator-nuttx] branch master updated: libs/libc/net: implement ether_aton/ether_aton_r

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 866bb35  libs/libc/net: implement ether_aton/ether_aton_r
866bb35 is described below

commit 866bb35d2ddf371526a4ad7669aa848b3e8bda33
Author: chao.an <an...@xiaomi.com>
AuthorDate: Wed Dec 9 10:57:56 2020 +0800

    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 <an...@xiaomi.com>
---
 include/netinet/ether.h       |   2 +
 libs/libc/net/Make.defs       |   3 +-
 libs/libc/net/lib_etheraton.c | 130 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 134 insertions(+), 1 deletion(-)

diff --git a/include/netinet/ether.h b/include/netinet/ether.h
index 63416df..95fb0b7 100644
--- a/include/netinet/ether.h
+++ b/include/netinet/ether.h
@@ -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
diff --git a/libs/libc/net/Make.defs b/libs/libc/net/Make.defs
index d4c8d20..cc96901 100644
--- a/libs/libc/net/Make.defs
+++ b/libs/libc/net/Make.defs
@@ -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
diff --git a/libs/libc/net/lib_etheraton.c b/libs/libc/net/lib_etheraton.c
new file mode 100644
index 0000000..faf93dc
--- /dev/null
+++ b/libs/libc/net/lib_etheraton.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);
+}