From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/2153 Path: news.gmane.org!not-for-mail From: Abdoulaye Walsimou Gaye Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH 4/4] : Add GNU extensions ether_ntoa_r() and ether_aton_r() Date: Sat, 20 Oct 2012 22:15:45 +0200 Message-ID: <1350764145-10305-5-git-send-email-awg@embtoolkit.org> References: <1350764145-10305-1-git-send-email-awg@embtoolkit.org> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1350764169 25624 80.91.229.3 (20 Oct 2012 20:16:09 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 20 Oct 2012 20:16:09 +0000 (UTC) Cc: Abdoulaye Walsimou Gaye To: musl@lists.openwall.com Original-X-From: musl-return-2154-gllmg-musl=m.gmane.org@lists.openwall.com Sat Oct 20 22:16:16 2012 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1TPfSx-0004sy-OI for gllmg-musl@plane.gmane.org; Sat, 20 Oct 2012 22:16:15 +0200 Original-Received: (qmail 32170 invoked by uid 550); 20 Oct 2012 20:16:04 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 32135 invoked from network); 20 Oct 2012 20:16:03 -0000 X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1350764145-10305-1-git-send-email-awg@embtoolkit.org> Xref: news.gmane.org gmane.linux.lib.musl.general:2153 Archived-At: Signed-off-by: Abdoulaye Walsimou Gaye --- 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