From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=fail reason="key not found in DNS" header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202312 header.b=LQEqoWLI; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 2D8C05A0275 for ; Mon, 19 Aug 2024 03:35:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1724031318; bh=+fZo97UbsgEqGO0z5AqbgEV4w+PPFmnlA7QtrTI+cIU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=LQEqoWLIgv4NsiKx23ePFGOSLcprQPsLBVUeihD6SC4xpxiAL+OzRj4wS7J6oMycX JpfANRBjpGlKLfRQ8OtqFZKCkqeSaWFMyRq03R0Of5sABxk2/oI3b4BlteplrTfXIe /rc4kvlGSwRkZsE3w8T9oLKCNA+hbpD1QQVYw8eceV7fYc9nulem+wIjTWn4y0x4+b BAoAYN5Rd1Zdu/qGFVgaB99uKWBYeKqiKPazkGHEFx+HueQAYmdd33DODcLqp+dxVK kd21hpB8Y227M7voEUK3MwGV2zJBcKvexjlXoZ8AkLh8RfwOQHyR3KvmatIGvA2Z83 gRbtsT5YyDGFA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WnFXQ4g88z4w2F; Mon, 19 Aug 2024 11:35:18 +1000 (AEST) Date: Mon, 19 Aug 2024 11:29:46 +1000 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 02/22] util: Helper for formatting MAC addresses Message-ID: References: <20240816054004.1335006-1-david@gibson.dropbear.id.au> <20240816054004.1335006-3-david@gibson.dropbear.id.au> <20240818174455.67210442@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="FnAKHkawQw2+5V7L" Content-Disposition: inline In-Reply-To: <20240818174455.67210442@elisabeth> Message-ID-Hash: 5CHSXJFYF3LASVZGZ6NYIBF4MZGGIH7L X-Message-ID-Hash: 5CHSXJFYF3LASVZGZ6NYIBF4MZGGIH7L X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top, Paul Holzinger X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --FnAKHkawQw2+5V7L Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sun, Aug 18, 2024 at 05:44:55PM +0200, Stefano Brivio wrote: > On Fri, 16 Aug 2024 15:39:43 +1000 > David Gibson wrote: >=20 > > There are a couple of places where we somewhat messily open code format= ting > > an Ethernet like MAC address for display. Add an eth_ntop() helper for > > this. > >=20 > > Signed-off-by: David Gibson > > --- > > conf.c | 7 +++---- > > dhcp.c | 5 ++--- > > util.c | 19 +++++++++++++++++++ > > util.h | 3 +++ > > 4 files changed, 27 insertions(+), 7 deletions(-) > >=20 > > diff --git a/conf.c b/conf.c > > index ed097bdc..830f91a6 100644 > > --- a/conf.c > > +++ b/conf.c > > @@ -921,7 +921,8 @@ pasta_opts: > > */ > > static void conf_print(const struct ctx *c) > > { > > - char buf4[INET_ADDRSTRLEN], buf6[INET6_ADDRSTRLEN], ifn[IFNAMSIZ]; > > + char buf4[INET_ADDRSTRLEN], buf6[INET6_ADDRSTRLEN]; > > + char bufmac[ETH_ADDRSTRLEN], ifn[IFNAMSIZ]; > > int i; > > =20 > > info("Template interface: %s%s%s%s%s", > > @@ -955,9 +956,7 @@ static void conf_print(const struct ctx *c) > > info("Namespace interface: %s", c->pasta_ifn); > > =20 > > info("MAC:"); > > - info(" host: %02x:%02x:%02x:%02x:%02x:%02x", > > - c->mac[0], c->mac[1], c->mac[2], > > - c->mac[3], c->mac[4], c->mac[5]); > > + info(" host: %s", eth_ntop(c->mac, bufmac, sizeof(bufmac))); > > =20 > > if (c->ifi4) { > > if (!c->no_dhcp) { > > diff --git a/dhcp.c b/dhcp.c > > index aa9f59da..acc5b03e 100644 > > --- a/dhcp.c > > +++ b/dhcp.c > > @@ -276,6 +276,7 @@ static void opt_set_dns_search(const struct ctx *c,= size_t max_len) > > int dhcp(const struct ctx *c, const struct pool *p) > > { > > size_t mlen, dlen, offset =3D 0, opt_len, opt_off =3D 0; > > + char macstr[ETH_ADDRSTRLEN]; > > const struct ethhdr *eh; > > const struct iphdr *iph; > > const struct udphdr *uh; > > @@ -340,9 +341,7 @@ int dhcp(const struct ctx *c, const struct pool *p) > > return -1; > > } > > =20 > > - info(" from %02x:%02x:%02x:%02x:%02x:%02x", > > - m->chaddr[0], m->chaddr[1], m->chaddr[2], > > - m->chaddr[3], m->chaddr[4], m->chaddr[5]); > > + info(" from %s", eth_ntop(m->chaddr, macstr, sizeof(macstr))); > > =20 > > m->yiaddr =3D c->ip4.addr; > > mask.s_addr =3D htonl(0xffffffff << (32 - c->ip4.prefix_len)); > > diff --git a/util.c b/util.c > > index 0b414045..892358b1 100644 > > --- a/util.c > > +++ b/util.c > > @@ -676,6 +676,25 @@ const char *sockaddr_ntop(const void *sa, char *ds= t, socklen_t size) > > return dst; > > } > > =20 > > +/** eth_ntop() - Convert an Ethernet MAC address to text format > > + * @mac: MAC address > > + * @dst: output buffer, minimum ETH_ADDRSTRLEN bytes > > + * @size: size of buffer at @dst >=20 > Nit: s/output/Output, s/size/Size Fixed. > > + * > > + * Return: On success, a non-null pointer to @dst, NULL on failure > > + */ > > +const char *eth_ntop(const unsigned char *mac, char *dst, size_t size) > > +{ > > + int len; > > + > > + len =3D snprintf(dst, size, "%02x:%02x:%02x:%02x:%02x:%02x", > > + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); > > + if (len < 0 || (size_t)len >=3D size) > > + return NULL; > > + > > + return dst; > > +} > > + > > /** str_ee_origin() - Convert socket extended error origin to a string > > * @ee: Socket extended error structure > > * > > diff --git a/util.h b/util.h > > index cb4d181c..c1748074 100644 > > --- a/util.h > > +++ b/util.h > > @@ -215,9 +215,12 @@ static inline const char *af_name(sa_family_t af) > > =20 > > #define SOCKADDR_STRLEN MAX(SOCKADDR_INET_STRLEN, SOCKADDR_INET6_STRL= EN) > > =20 > > +#define ETH_ADDRSTRLEN (ETH_ALEN * 3) >=20 > The fact that this includes two digits plus separator for all non-last > octets of a MAC address, and two digits plus NULL terminator for the > last octet, looks a bit subtle to me. >=20 > Defining this as sizeof("00:11:22:33:44:55") wouldn't scream > "off-by-one" as much, to me. Not a strong preference. Yeah, that makes sense. Done. --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --FnAKHkawQw2+5V7L Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmbCoAkACgkQzQJF27ox 2Ge0RQ/9G+w7s/WpiusB4xJwuPmyPKXKvke0d56Z2IuVLoMMtIAxBwhJ3GR3BJ7f ieVbUpPNGforBi4XWBZ7izOTVaa+zvGrDlFEj/cs8xCZAExzKj5a/7KDSyrU8OXN CS07vk+rUkClndaTKaq9vAS1JagrN5Jyo2nRBfR3XvgtkQAQYLd7+NAKNa6Y55Yg uhlVcjVSgmaI4+86DCwwGu8BlNT2Y2GMFweVumjBPm72Wp8PFh+XMy3pkOh451KV iurDxBFguOrdgYLNABx4wdTSXR3aA0oiJTru27uYyF4THtcv9nvOODtyWPDYb1PJ ZJR+DhmSEqRhzOHTKskYdQGQdlwjRT4/n2SqYU6H+uTMFeR2xq6fYMYBOgdOZIle SbJU1BGln4rhOL3nrnHRWjUUpAXVnUCmadjoi7QHgw5nFxsP2sTUmtMlqwnuiKBs 5V4v9zG7kLSWH2r7129B3HrQGivRqDaE63KUepD0jcDudIFkR78ygfdxtZBd3VMZ RMe4Wn+/jRpI9EWRkXZfhieU+iJf1L92j+FFjYtm9K7E7v+s8k/g7n0uZEkZvAyP Rp1sjjod3abriohl06uijW5umTcHtLGfZoLdy+xYC0WwoXc5t2v3aabs5KEsrYB/ GxADYWdhmtxpx14nf6kj1EJ8iyZBRXmz1W2IV98rkXkosMDrxlo= =SwBp -----END PGP SIGNATURE----- --FnAKHkawQw2+5V7L--