standards/188316: Visibility of ntohl etc. and POSIX 2008

Christian Neukirchen chneukirchen at
Sun Apr 6 14:40:00 UTC 2014

>Number:         188316
>Category:       standards
>Synopsis:       Visibility of ntohl etc. and POSIX 2008
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-standards
>State:          open
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Apr 06 14:40:00 UTC 2014
>Originator:     Christian Neukirchen
>Release:        10.0-RELEASE
FreeBSD 10.0-RELEASE FreeBSD 10.0-RELEASE #0 r260789: Thu Jan 16 22:34:59 UTC 2014     root at  amd64

According to POSIX 2008, the functions ntohl etc. are defined in <arpa/inet.h> (, which works because they are defined unconditionally there.

However, just including <netinet/in.h> should also define them (

> The htonl(), htons(), ntohl(), and ntohs() functions shall be available
> as described in <arpa/inet.h>. Inclusion of the <netinet/in.h> header may
> also make visible all symbols from <arpa/inet.h>.

But ntohl etc are inside a "#if !defined(_KERNEL) && __BSD_VISIBLE" in <netinet/in.h>, violating the "shall be" requirement above.
echo '#include <arpa/inet.h>\nint main(){htonl(1);}' | cc -D_XOPEN_SOURCE=700 -x c -
(all good)

echo '#include <netinet/in.h>\nint main(){htonl(1);}' | cc -D_XOPEN_SOURCE=700 -x c -                                                                               
<stdin>:2:12: warning: implicit declaration of function 'htonl' is invalid in C99 [-Wimplicit-function-declaration]
int main(){htonl(1);}
1 warning generated.

Guard the definition with __POSIX_VISIBLE >= 200112.


More information about the freebsd-standards mailing list