3 ms·
POSIX leaves it ambiguous whether you need to clear it or not. About sockaddr_in, POSIX-2001, 2004 edition[1] has to say: 10004 The <netinet/in.h> hea
by LukeShu 10y ago
POSIX leaves it ambiguous whether you need to clear it or not.
About sockaddr_in, POSIX-2001, 2004 edition[1] has to say:
10004 The <netinet/in.h> header shall define the sockaddr_in structure that includes at least the
10005 following members:
10006 sa_family_t sin_family AF_INET.
10007 in_port_t sin_port Port number.
10008 struct in_addr sin_addr IP address.
So implementations are free to add whatever extra members they feel like.
However, about IPv6 it says:
10016 The <netinet/in.h> header shall define the sockaddr_in6 structure that includes at least the
10017 following members:
10018 sa_family_t sin6_family AF_INET6.
10019 in_port_t sin6_port Port number.
10020 uint32_t sin6_flowinfo IPv6 traffic class and flow information.
10021 struct in6_addr sin6_addr IPv6 address.
10022 uint32_t sin6_scope_id Set of interfaces for a scope.
10023 The sin6_port and sin6_addr members shall be in network byte order.
10024 The sockaddr_in6 structure shall be set to zero by an application prior to using it, since
10025 implementations are free to have additional, implementation-defined fields in sockaddr_in6.
So raimue's point is explicitly true for sockaddr_in6; while it is
left ambiguous whether the (allowed) implementation-defined members of
sockaddr_in must be initialized.
[1]: The current UNIX standard is UNIX V7, which corresponds to SUSv4,
which corresponds to the POSIX-2008. However, macOS is certified UNIX
under the previous version, UNIX 03, which corresponds to SUSv3, which
corresponds to POSIX-2001.
- kazinator 10y agoFor IPv6 that makes a modicum of sense, because IPv6 has additional fields in the address not directly related to identifying the endpoint, and these have been historically in churn.
- kazinator 10y agoThere is no ambiguity: there is no requirement in POSIX that the additional members of struct sockaddr_in be initialized by the application. The requirements are given for sockaddr_in6 only.