Skip to content

Commit 7bcefa5

Browse files
committed
On IPv6 socket use with IPv4.
1 parent 2874ea3 commit 7bcefa5

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

network.tex

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,35 +1018,52 @@
10181018
are consulted according to the specification in the \texttt{/etc/nsswitch.conf}
10191019
file.
10201020
\item The structure \texttt{addrinfo} is defined in \texttt{netdb.h}.
1021-
\item \texttt{getaddrinfo} is able to convert both string containing hostname
1022-
and IP address. Similarly for ports.
1021+
\item \texttt{getaddrinfo} is able to convert a string containing either a
1022+
hostname or an IP address. Similarly for ports. So, whether you use a hostname
1023+
or an IP address as an command line argument, you can use the same code to
1024+
process that.
10231025
\item The \texttt{getaddrinfo} function returns the results in the \emph{res}
1024-
parameter. The result is a list of structures \texttt{sockaddr}, that correspond
1025-
to given inputs.
1026-
\item There is a difference whether the address structures are to be used for
1027-
server or client; e.g. for server it is sufficient to specify \emph{nodename} as
1028-
\texttt{NULL} (wildcard socket). Similarly for the member \texttt{ai\_flags} of
1029-
the \texttt{addrinfo} structure used for the \emph{hint} parameter -- for server
1030-
it is usually set to \texttt{AI\_PASSIVE}. (Otherwise it would contain loopback
1031-
address if \texttt{NULL} address is specified.)
1026+
parameter. The result is a list of \texttt{sockaddr} structures corresponding to
1027+
given input.
1028+
\item There is a difference whether the address structures are to be used for a
1029+
server or client; e.g. for a server it is sufficient to specify \emph{nodename}
1030+
as \texttt{NULL} (meaning any address). Similarly for the member
1031+
\texttt{ai\_flags} of the \texttt{addrinfo} structure used for the \emph{hint}
1032+
parameter -- for a server, use \texttt{AI\_PASSIVE} to indicate the returned
1033+
address structures will be used in \funnm{bind}.
10321034
\item After the results are no longer needed, it is prudent to call
10331035
\texttt{freeaddrinfo}, that will free the memory allocated by
10341036
\texttt{getaddrinfo}.
10351037
\item \label{GETADDRINFO} example: \example{resolving/getaddrinfo.c}
10361038
\item In the past the functions \funnm{gethostbyname} and
10371039
\funnm{gethostbyaddr} were used. These only work with IPv4 addresses and are
10381040
therefore considered legacy and not recommended to use. The functions
1039-
\texttt{getipnodebyname} and \texttt{getipnodebyaddr} can be used instead
1040-
(\texttt{getipnode*} is found in many systems, nevertheless they were removed
1041-
from GNU \texttt{libc}, so cannot be relied on universally). Moreover they are
1042-
not part of UNIX standards.
1043-
\texttt{get\-addr\-info} and \texttt{getnameinfo} are part of the POSIX.1-2001
1044-
standard.
1041+
\funnm{getipnodebyname} and \funnm{get\-ip\-no\-de\-by\-addr} can be used
1042+
instead (\funnm{getipnode*} are found in many systems, nevertheless they were
1043+
removed from GNU \texttt{libc}, so cannot be relied on universally). What is
1044+
more, they are not part of UNIX standards. \funnm{get\-addr\-info} and
1045+
\funnm{getnameinfo} are part of the POSIX.1-2001 standard.
10451046
\item It is not strictly necessary to convert old IPv4 specific code to the
1046-
generic APIs. However when writing new code, always use them even when you
1047-
think the program will only use IPv4.
1048-
\end{itemize}
1047+
generic APIs. However when writing new code, always use them even when you think
1048+
the program will only use IPv4.
1049+
\item IPv6 sockets may be used for both IPv4 and IPv6 communications. That
1050+
means if you try to bind addresses returned by \funnm{getaddrinfo}, \funnm{bind}
1051+
should return \texttt{EADDRINUSE} after its first call if you use both IPv4 and
1052+
IPv6 addresses. Binding on an IPv6 address should be sufficient to accept
1053+
connections over IPv4 as well. Details are in RFC~3493. If you want to
1054+
restrict a socket to IPv6 only, use the following code on a socket from the
1055+
\texttt{AF\_INET6} family:
1056+
1057+
\begin{verbatim}
1058+
int on = 1;
10491059
1060+
if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&on,
1061+
sizeof(on)) == -1) {
1062+
/* deal with the error here */
1063+
}
1064+
\end{verbatim}
1065+
1066+
\end{itemize}
10501067

10511068
%%%%%%
10521069

0 commit comments

Comments
 (0)