Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 13 Aug 2019 10:53:52 -0600
From:      Warner Losh <imp@bsdimp.com>
To:        "Enji Cooper (yaneurabeya)" <yaneurabeya@gmail.com>
Cc:        Alan Somers <asomers@freebsd.org>, src-committers <src-committers@freebsd.org>,  svn-src-all <svn-src-all@freebsd.org>, svn-src-head <svn-src-head@freebsd.org>
Subject:   Re: svn commit: r350993 - head/sbin/ping6
Message-ID:  <CANCZdfp5BMDm68-UrteA_F4uxFE0Th7YkV8Yd%2BtE4qc=cGHRMA@mail.gmail.com>
In-Reply-To: <B49D11C2-BD72-4610-ACE1-F212C85BD568@gmail.com>
References:  <201908131622.x7DGMhUL047226@repo.freebsd.org> <B49D11C2-BD72-4610-ACE1-F212C85BD568@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Aug 13, 2019 at 10:50 AM Enji Cooper (yaneurabeya) <
yaneurabeya@gmail.com> wrote:

>
> > On Aug 13, 2019, at 09:22, Alan Somers <asomers@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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CANCZdfp5BMDm68-UrteA_F4uxFE0Th7YkV8Yd%2BtE4qc=cGHRMA>