* [PATCH] ip.7: INADDR_* values cannot be assigned directly to s_addr
@ 2017-12-14 21:18 Ricardo Biehl Pasquali
[not found] ` <20171214211815.GA4844-bi+AKbBUZKY6gyzm1THtWbp2dZbC/Bob@public.gmane.org>
0 siblings, 1 reply; 2+ messages in thread
From: Ricardo Biehl Pasquali @ 2017-12-14 21:18 UTC (permalink / raw)
To: Michael Kerrisk; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA
Signed-off-by: Ricardo Biehl Pasquali <pasqualirb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
According to The Open Group Base Specifications Issue 7, RATIONALE
section of <http://pubs.opengroup.org/onlinepubs/9699919799/
basedefs/netinet_in.h.html> some INADDR_* values must be converted
using htonl().
INADDR_ANY and INADDR_BROADCAST are byte-order-neutral so they do
not require htonl(), however I only comment this fact in NOTES.
On the text I recommend to use htonl(), "even if for some subset
it's not necessary".
Proof code:
#include <arpa/inet.h> /* inet_addr() htonl() */
#include <inttypes.h> /* PRIu32 */
#include <netinet/in.h> /* INADDR_* */
#include <stdio.h> /* printf() */
#define LOOPBACK_ADDRESS "127.0.0.1"
int
main(void)
{
/* uint32_t as defined in the former
* specification */
uint32_t addr;
addr = inet_addr(LOOPBACK_ADDRESS);
printf("inet_addr: %" PRIu32 "\n"
"htonl(INADDR_LOOPBACK): %" PRIu32 "\n"
"INADDR_LOOPBACK: %" PRIu32 "\n",
addr, htonl(INADDR_LOOPBACK),
INADDR_LOOPBACK);
return 0;
}
Ricardo Biehl Pasquali
man7/ip.7 | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/man7/ip.7 b/man7/ip.7
index d05d211..d7481e2 100644
--- a/man7/ip.7
+++ b/man7/ip.7
@@ -193,8 +193,11 @@ contains the host interface address in network byte order.
.I in_addr
should be assigned one of the
.BR INADDR_*
-values (e.g.,
+values
+(e.g.,
.BR INADDR_ANY )
+using
+.BR htonl (3)
or set using the
.BR inet_aton (3),
.BR inet_addr (3),
@@ -1267,6 +1270,13 @@ Using the
socket options level isn't portable; BSD-based stacks use the
.B IPPROTO_IP
level.
+.PP
+.B INADDR_ANY
+(0.0.0.0) and
+.B INADDR_BROADCAST
+(255.255.255.255) are byte-order-neutral. That means
+.BR htonl (3)
+has no effect on them.
.SS Compatibility
For compatibility with Linux 2.0, the obsolete
.BI "socket(AF_INET, SOCK_PACKET, " protocol )
--
2.9.5
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] ip.7: INADDR_* values cannot be assigned directly to s_addr
[not found] ` <20171214211815.GA4844-bi+AKbBUZKY6gyzm1THtWbp2dZbC/Bob@public.gmane.org>
@ 2017-12-18 16:42 ` Michael Kerrisk (man-pages)
0 siblings, 0 replies; 2+ messages in thread
From: Michael Kerrisk (man-pages) @ 2017-12-18 16:42 UTC (permalink / raw)
To: Ricardo Biehl Pasquali
Cc: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w,
linux-man-u79uwXL29TY76Z2rM5mHXA
Hello Ricardo,
On 12/14/2017 10:18 PM, Ricardo Biehl Pasquali wrote:
> Signed-off-by: Ricardo Biehl Pasquali <pasqualirb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Thanks. I applid you patch, but then also did
s/INADDR_ANY/INADDR_LOOPBACK/ in the first sentence that you
changed. INADDR_LOOPBACK is a better example, since it is not
byte-order neutral.
Cheers,
Michael
> ---
>
> According to The Open Group Base Specifications Issue 7, RATIONALE
> section of <http://pubs.opengroup.org/onlinepubs/9699919799/
> basedefs/netinet_in.h.html> some INADDR_* values must be converted
> using htonl().
>
> INADDR_ANY and INADDR_BROADCAST are byte-order-neutral so they do
> not require htonl(), however I only comment this fact in NOTES.
> On the text I recommend to use htonl(), "even if for some subset
> it's not necessary".
>
> Proof code:
> #include <arpa/inet.h> /* inet_addr() htonl() */
> #include <inttypes.h> /* PRIu32 */
> #include <netinet/in.h> /* INADDR_* */
> #include <stdio.h> /* printf() */
>
> #define LOOPBACK_ADDRESS "127.0.0.1"
>
> int
> main(void)
> {
> /* uint32_t as defined in the former
> * specification */
> uint32_t addr;
>
> addr = inet_addr(LOOPBACK_ADDRESS);
>
> printf("inet_addr: %" PRIu32 "\n"
> "htonl(INADDR_LOOPBACK): %" PRIu32 "\n"
> "INADDR_LOOPBACK: %" PRIu32 "\n",
> addr, htonl(INADDR_LOOPBACK),
> INADDR_LOOPBACK);
>
> return 0;
> }
>
> Ricardo Biehl Pasquali
>
> man7/ip.7 | 12 +++++++++++-
> 1 file changed, 11 insertions(+), 1 deletion(-)
>
> diff --git a/man7/ip.7 b/man7/ip.7
> index d05d211..d7481e2 100644
> --- a/man7/ip.7
> +++ b/man7/ip.7
> @@ -193,8 +193,11 @@ contains the host interface address in network byte order.
> .I in_addr
> should be assigned one of the
> .BR INADDR_*
> -values (e.g.,
> +values
> +(e.g.,
> .BR INADDR_ANY )
> +using
> +.BR htonl (3)
> or set using the
> .BR inet_aton (3),
> .BR inet_addr (3),
> @@ -1267,6 +1270,13 @@ Using the
> socket options level isn't portable; BSD-based stacks use the
> .B IPPROTO_IP
> level.
> +.PP
> +.B INADDR_ANY
> +(0.0.0.0) and
> +.B INADDR_BROADCAST
> +(255.255.255.255) are byte-order-neutral. That means
> +.BR htonl (3)
> +has no effect on them.
> .SS Compatibility
> For compatibility with Linux 2.0, the obsolete
> .BI "socket(AF_INET, SOCK_PACKET, " protocol )
>
--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2017-12-18 16:42 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-14 21:18 [PATCH] ip.7: INADDR_* values cannot be assigned directly to s_addr Ricardo Biehl Pasquali
[not found] ` <20171214211815.GA4844-bi+AKbBUZKY6gyzm1THtWbp2dZbC/Bob@public.gmane.org>
2017-12-18 16:42 ` Michael Kerrisk (man-pages)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox