From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/3286 Path: news.gmane.org!not-for-mail From: Strake Newsgroups: gmane.linux.lib.musl.general Subject: Re: [patch] add ether_(aton, ntoa) Date: Sun, 5 May 2013 09:02:04 -0500 Message-ID: References: <20130415014005.GR20323@brightrain.aerifal.cx> <20130420014945.GI20323@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: ger.gmane.org 1367762543 29488 80.91.229.3 (5 May 2013 14:02:23 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 5 May 2013 14:02:23 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-3290-gllmg-musl=m.gmane.org@lists.openwall.com Sun May 05 16:02:22 2013 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 1UYzW6-00006Z-2l for gllmg-musl@plane.gmane.org; Sun, 05 May 2013 16:02:18 +0200 Original-Received: (qmail 9944 invoked by uid 550); 5 May 2013 14:02:16 -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 9930 invoked from network); 5 May 2013 14:02:16 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=l7+HcT47V7XIwvBDDsBy0+HHohEiorGSimVo7Xi1sxA=; b=N5X2tH+mc2ah6FMDl+bqAhiCc78YLlP5GfMYggbFGdwC2pw46m1dUERcOOoZ6snJQL Ehti/Wm0dOVXU2ld6V/OvZBg38wLAzZbrtizpVrrqmB+WO5z3MHMGhg5RN7y9ecmbP+k meRjsTwzwUxZ4ugVjl5kzOp4oF45ppZ+mH/uqUgJ+XS7Cc8qFZfTyrHsZNeLtbml7HR6 mu0hW+Q3bC3blaYqaYWfeAI9RcpQL+VIougt6Y8yEADGi2E3cXY/BGdckDEyN0Ji+RtN OJaSucT/FPB0VlbD5m6f/pCzIuBrfl5/NJB7uF1T6hnlMquF4Y6F555Q59+op0jQNHOp QHaA== X-Received: by 10.194.61.237 with SMTP id t13mr21431759wjr.2.1367762524522; Sun, 05 May 2013 07:02:04 -0700 (PDT) In-Reply-To: <20130420014945.GI20323@brightrain.aerifal.cx> Xref: news.gmane.org gmane.linux.lib.musl.general:3286 Archived-At: On 19/04/2013, Rich Felker wrote: > On Sun, Apr 14, 2013 at 11:10:55PM -0500, Strake wrote: >> +struct ether_addr *ether_aton_r (const char *x, struct ether_addr *p_a) >> { >> + for (int ii = 0; ii < 6; ii++) { >> + unsigned long int n; >> + if (ii != 0) { >> + if (x[0] != ':') return 0; /* bad format */ >> + else x++; >> + } >> + n = strtoul (x, &x, 16); > > &x has the wrong type; strtoul requires char **, not const char **. > A separate temp var is required for the output, as in. > > char *y; > n = strtoul (x, &y, 16); > x = y; > > or similar. As far as I know the naive cast one could make to "fix" > the type mismatch is not valid; it would be an aliasing violation. What bad could happen? >> + if (n > 0xFF) return 0; /* bad byte */ >> + (*p_a).ether_addr_octet[ii] = n; > > Is there a reason you're using (*p_a). rather than p_a-> ? No sane one. > Also, I'm not sure if this is worth changing or not, but usually we > avoid writing to output pointers except on success. For standard > interfaces this is a conformance issue, but for these it probably > doesn't matter. Well, we may as well do it properly. >From 97d977299521a0ae7cc23deed16dabfd22363248 Mon Sep 17 00:00:00 2001 From: Strake Date: Sun, 14 Apr 2013 12:09:30 -0500 Subject: [PATCH] add ether_(aton, ntoa) --- include/netinet/ether.h | 14 ++++++++++++++ src/network/ether_aton.c | 26 ++++++++++++++++++++++++++ src/network/ether_ntoa.c | 17 +++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 include/netinet/ether.h create mode 100644 src/network/ether_aton.c create mode 100644 src/network/ether_ntoa.c diff --git a/include/netinet/ether.h b/include/netinet/ether.h new file mode 100644 index 0000000..c5179d5 --- /dev/null +++ b/include/netinet/ether.h @@ -0,0 +1,14 @@ +#ifndef _NETINET_ETHER_H +#define _NETINET_ETHER_H + +#include + +char *ether_ntoa (const struct ether_addr *); + +struct ether_addr *ether_aton (const char *); + +char *ether_ntoa_r (const struct ether_addr *, char *); + +struct ether_addr *ether_aton_r (const char *, struct ether_addr *); + +#endif diff --git a/src/network/ether_aton.c b/src/network/ether_aton.c new file mode 100644 index 0000000..2690cc5 --- /dev/null +++ b/src/network/ether_aton.c @@ -0,0 +1,26 @@ +#include +#include +#include + +static struct ether_addr a; + +struct ether_addr *ether_aton_r (const char *x, struct ether_addr *p_a) { + struct ether_addr a; + for (int ii = 0; ii < 6; ii++) { + unsigned long int n; + if (ii != 0) { + if (x[0] != ':') return 0; /* bad format */ + else x++; + } + n = strtoul (x, &x, 16); + if (n > 0xFF) return 0; /* bad byte */ + a.ether_addr_octet[ii] = n; + } + if (x[0] != 0) return 0; /* bad format */ + memmove (p_a, &a, sizeof (struct ether_addr)); + return p_a; +} + +struct ether_addr *ether_aton (const char *x) { + return ether_aton_r (x, &a); +} diff --git a/src/network/ether_ntoa.c b/src/network/ether_ntoa.c new file mode 100644 index 0000000..bcc773a --- /dev/null +++ b/src/network/ether_ntoa.c @@ -0,0 +1,17 @@ +#include +#include + +static char x[18]; + +char *ether_ntoa_r (const struct ether_addr *p_a, char *x) { + char *y; + y = x; + for (int ii = 0; ii < 6; ii++) { + x += sprintf (x, ii == 0 ? "%.2X" : ":%.2X", p_a -> ether_addr_octet[ii]); + } + return y; +} + +char *ether_ntoa (const struct ether_addr *p_a) { + return ether_ntoa_r (p_a, x); +} -- 1.7.11.1