svn commit: r350993 - head/sbin/ping6

Warner Losh imp at bsdimp.com
Tue Aug 13 16:54:05 UTC 2019


On Tue, Aug 13, 2019 at 10:50 AM Enji Cooper (yaneurabeya) <
yaneurabeya at gmail.com> wrote:

>
> > On Aug 13, 2019, at 09:22, Alan Somers <asomers at freebsd.org> wrote:
> >
> > Author: asomers
> > Date: Tue Aug 13 16:22:43 2019
> > New Revision: 350993
> > URL: https://svnweb.freebsd.org/changeset/base/350993
> >
> > Log:
> >  Consistently use the byteorder functions in the correct direction
> >
> >  Though ntohs and htons are functionally identical, they have different
> meanings.Using the correct one helps to document the code.
>
> This statement is only true for BE platforms. For LE platforms like
> i386/x64, ntohs and htons actually does a endianness conversion:
>
> sys/powerpc/include/endian.h:#define    __ntohs(x)
> (__bswap16((__uint16_t)(x)))
> sys/powerpc/include/endian.h:#define    __ntohs(x)      ((__uint16_t)(x))
> sys/sparc64/include/endian.h:#define    __ntohs(x)      ((__uint16_t)(x))
> sys/x86/include/endian.h:#define        __ntohs(x)      __bswap16(x)
> sys/mips/include/endian.h:#define       __ntohs(x)      ((__uint16_t)(x))
> sys/mips/include/endian.h:#define __ntohs(x)    (__bswap16((x)))
> sys/arm/include/endian.h:#define __ntohs(x)     ((__uint16_t)(x))
> sys/arm/include/endian.h:#define __ntohs(x)        (__bswap16(x))
> sys/arm64/include/endian.h:#define      __ntohs(x)        (__bswap16(x))
> sys/riscv/include/endian.h:#define      __ntohs(x)        (__bswap16(x))
> sys/sys/param.h:#define ntohs(x)        __ntohs(x)
> sys/netinet/in.h:#define        ntohs(x)        __ntohs(x)
>

The statement was that htons and ntohs are the same actual code, but
document different things. And that's correct. htons and ntohs can be used
indiscriminately and the code will still work. But using htons when putting
data into network byte order and ntohs when putting it into host order
documents the operations better. This is independent of endian: either both
do nothing, or both do bswap16.

Warner


More information about the svn-src-head mailing list