Index | Thread | Search

From:
Claudio Jeker <cjeker@diehard.n-r-g.com>
Subject:
Re: Cruft: drop htonl() etc. from libc
To:
Christian Weisgerber <naddy@mips.inka.de>
Cc:
tech@openbsd.org
Date:
Fri, 12 Apr 2024 09:13:55 +0200

Download raw body.

Thread
  • Claudio Jeker:

    Cruft: drop htonl() etc. from libc

  • On Fri, Apr 12, 2024 at 01:21:41AM +0200, Christian Weisgerber wrote:
    > Userland code compiled in a normal fashion picks up the htonl(),
    > htons(), ntohl(), ntohs() inline functions from endian.h.
    > 
    > The implementations in libc are effectively unused.  We can drop
    > the MD functions.  I chose to keep the MI functions on the off
    > chance that something looks for the symbols in libc or plays games
    > with "#undef htonl", but I changed them so they now also pick up
    > the implementation from endian.h.
    > 
    > This passed a release build on amd64.
    > 
    > No symbol changes.  aarch64 and arm have this:
    > /usr/lib/libc.so.99.0 --> obj/libc.so.99.0
    > Dynamic export changes:
    > strengthened:
    >         htonl
    >         htons
    >         ntohl
    >         ntohs
    > 
    > Comments?  OK?
    
    See below
    
    
    > blob - 0d05bac78a1c9ad61f7a7418022914c6704adfb4
    > blob + a9b2c86f5ca31081bf66ca627122a96570846b2b
    > --- lib/libc/net/ntohl.c
    > +++ lib/libc/net/ntohl.c
    > @@ -1,6 +1,5 @@
    >  /*	$OpenBSD: ntohl.c,v 1.7 2014/07/21 01:51:10 guenther Exp $ */
    >  /*
    > - * Written by J.T. Conklin <jtc@netbsd.org>.
    >   * Public domain.
    >   */
    >  
    > @@ -12,10 +11,5 @@
    >  u_int32_t
    >  ntohl(u_int32_t x)
    >  {
    > -#if BYTE_ORDER == LITTLE_ENDIAN
    > -	u_char *s = (u_char *)&x;
    > -	return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]);
    > -#else
    > -	return x;
    > -#endif
    > +	return __htobe32(x);
    >  }
    > blob - b5ea361f8304c322f46bc564e0e74a9a135d69c4
    > blob + 8a39f68a0ae5960d8ea4cbc046d157210985bad2
    > --- lib/libc/net/ntohs.c
    > +++ lib/libc/net/ntohs.c
    > @@ -1,6 +1,5 @@
    >  /*	$OpenBSD: ntohs.c,v 1.9 2014/07/21 01:51:10 guenther Exp $ */
    >  /*
    > - * Written by J.T. Conklin <jtc@netbsd.org>.
    >   * Public domain.
    >   */
    >  
    > @@ -12,10 +11,5 @@
    >  u_int16_t
    >  ntohs(u_int16_t x)
    >  {
    > -#if BYTE_ORDER == LITTLE_ENDIAN
    > -	u_char *s = (u_char *) &x;
    > -	return (u_int16_t)(s[0] << 8 | s[1]);
    > -#else
    > -	return x;
    > -#endif
    > +	return __htobe16(x);
    
    This looks the wrong way around. ntohs() is be16toh(). Same for ntohl
    above. While it does not matter for our cases I think we should use the
    right conversions here.
    
    -- 
    :wq Claudio
    
    
    
  • Claudio Jeker:

    Cruft: drop htonl() etc. from libc