mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Abdoulaye Walsimou Gaye <awg@embtoolkit.org>
To: musl@lists.openwall.com
Cc: Abdoulaye Walsimou Gaye <awg@embtoolkit.org>
Subject: [PATCH 4/4] <netinet/ether.h>: Add GNU extensions ether_ntoa_r() and ether_aton_r()
Date: Sat, 20 Oct 2012 22:15:45 +0200	[thread overview]
Message-ID: <1350764145-10305-5-git-send-email-awg@embtoolkit.org> (raw)
In-Reply-To: <1350764145-10305-1-git-send-email-awg@embtoolkit.org>

Signed-off-by: Abdoulaye Walsimou Gaye <awg@embtoolkit.org>
---
 include/netinet/ether.h |    2 ++
 src/network/ethers.c    |   59 +++++++++++++++++++++++++++++++++++------------
 2 files changed, 46 insertions(+), 15 deletions(-)

diff --git a/include/netinet/ether.h b/include/netinet/ether.h
index 44c614e..55be0d8 100644
--- a/include/netinet/ether.h
+++ b/include/netinet/ether.h
@@ -5,7 +5,9 @@
 
 __BEGIN_DECLS
 char	*ether_ntoa(const struct ether_addr *);
+char	*ether_ntoa_r(const struct ether_addr *, char *);
 struct 	ether_addr *ether_aton(const char *);
+struct	ether_addr *ether_aton_r(const char *, struct ether_addr *);
 int	ether_ntohost(char *, const struct ether_addr *);
 int	ether_hostton(const char *, struct ether_addr *);
 int	ether_line(const char *, struct ether_addr *, char *);
diff --git a/src/network/ethers.c b/src/network/ethers.c
index 8014581..930712e 100644
--- a/src/network/ethers.c
+++ b/src/network/ethers.c
@@ -36,13 +36,25 @@ char *ether_ntoa(const struct ether_addr *e)
 {
 	static char a[18];
 
+	return ether_ntoa_r(e, a);
+}
+
+/*
+ * ether_ntoa_r():
+ * Thread safe version of ether_ntoa() (does not make use of static buffer).
+ */
+char *ether_ntoa_r(const struct ether_addr *e, char *a)
+{
+	int sz;
 	assert(e != NULL);
+	assert(a != NULL);
+
+	sz = sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x",
+			e->ether_addr_octet[0], e->ether_addr_octet[1],
+			e->ether_addr_octet[2], e->ether_addr_octet[3],
+			e->ether_addr_octet[4], e->ether_addr_octet[5]);
 
-	(void) snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x",
-	    e->ether_addr_octet[0], e->ether_addr_octet[1],
-	    e->ether_addr_octet[2], e->ether_addr_octet[3],
-	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
-	return a;
+	return sz < 17 ? NULL : a;
 }
 
 /*
@@ -54,20 +66,37 @@ char *ether_ntoa(const struct ether_addr *e)
 struct ether_addr *ether_aton(const char *s)
 {
 	static struct ether_addr n;
-	unsigned int i[6];
 
 	assert(s != NULL);
 
-	if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
-	    &i[2], &i[3], &i[4], &i[5]) == 6) {
-		n.ether_addr_octet[0] = (unsigned char)i[0];
-		n.ether_addr_octet[1] = (unsigned char)i[1];
-		n.ether_addr_octet[2] = (unsigned char)i[2];
-		n.ether_addr_octet[3] = (unsigned char)i[3];
-		n.ether_addr_octet[4] = (unsigned char)i[4];
-		n.ether_addr_octet[5] = (unsigned char)i[5];
-		return &n;
+	return ether_aton_r(s, &n);
+}
+
+/*
+ * ether_aton_r():
+ * Thread safe version of ether_aton(), (does not make use of static structure).
+ */
+struct ether_addr *ether_aton_r(const char *s, struct ether_addr *n)
+{
+	unsigned int i[6];
+	int sz;
+
+	assert(s != NULL);
+	assert(n != NULL);
+
+	sz = sscanf(s, " %x:%x:%x:%x:%x:%x ",
+			&i[0], &i[1], &i[2], &i[3], &i[4], &i[5]);
+
+	if (sz == 6) {
+		n->ether_addr_octet[0] = (unsigned char)i[0];
+		n->ether_addr_octet[1] = (unsigned char)i[1];
+		n->ether_addr_octet[2] = (unsigned char)i[2];
+		n->ether_addr_octet[3] = (unsigned char)i[3];
+		n->ether_addr_octet[4] = (unsigned char)i[4];
+		n->ether_addr_octet[5] = (unsigned char)i[5];
+		return n;
 	}
+
 	return NULL;
 }
 
-- 
1.7.9.5



  parent reply	other threads:[~2012-10-20 20:15 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-20 20:15 [PATCH 0/4] Import <netinet/ether.h> features from NetBSD Abdoulaye Walsimou Gaye
2012-10-20 20:15 ` [PATCH 1/4] Build system: give ability to install lib/crt*.o files separately Abdoulaye Walsimou Gaye
2012-10-20 23:49   ` Rich Felker
2012-10-20 20:15 ` [PATCH 2/4] Add basic sys/cdefs.h found on most unix Abdoulaye Walsimou Gaye
2012-10-20 23:18   ` Isaac Dunham
2012-10-20 23:38     ` Abdoulaye Walsimou GAYE
2012-10-20 23:38       ` Rich Felker
2012-10-21  0:13         ` Abdoulaye Walsimou GAYE
2012-10-21  0:11           ` Rich Felker
2012-10-21  0:38             ` Abdoulaye Walsimou GAYE
2012-10-21  0:39               ` John Spencer
2012-10-21  1:21                 ` John Spencer
2012-10-20 23:50       ` Isaac Dunham
2012-10-20 23:44         ` Rich Felker
2012-10-20 20:15 ` [PATCH 3/4] Import BSD functions defined in <netinet/ether.h> from NetBSD Abdoulaye Walsimou Gaye
2012-10-20 23:37   ` idunham
2012-10-21  0:40     ` Abdoulaye Walsimou GAYE
2012-10-21  0:42   ` John Spencer
2012-10-21  0:41     ` Rich Felker
2012-10-21  0:52     ` Abdoulaye Walsimou GAYE
2012-10-21  0:48       ` Rich Felker
2012-10-21  0:57       ` John Spencer
2012-10-21  0:53         ` Rich Felker
2012-10-20 20:15 ` Abdoulaye Walsimou Gaye [this message]
2012-10-20 23:53   ` [PATCH 4/4] <netinet/ether.h>: Add GNU extensions ether_ntoa_r() and ether_aton_r() Rich Felker
2012-10-21  0:43     ` Abdoulaye Walsimou GAYE
2012-10-21  0:46       ` John Spencer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1350764145-10305-5-git-send-email-awg@embtoolkit.org \
    --to=awg@embtoolkit.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).