4 from IPv4 to IPv6
First of all, try to use
getaddrinfo()
to get all thestruct sockaddr
info, instead of packing the structures by hand. This will keep you IP version-agnostic, and will eliminate many of the subsequent steps.Any place that you find you're hard-coding anything related to the IP version, try to wrap up in a helper function.
Change
AF_INET
toAF_INET6
.Change
PF_INET
toPF_INET6
.Change
INADDR_ANY
assignments toin6addr_any
assignments, which are slightly different:struct sockaddr_in sa; struct sockaddr_in6 sa6; sa.sin_addr.s_addr = INADDR_ANY; use my IPv4 address sa6.sin6_addr = in6addr_any; use my IPv6 address
Also, the value IN6ADDR_ANY_INIT
can be used as an initializer when the struct in6_addr
is declared, like so:
Instead of
struct sockaddr_in
usestruct sockaddr_in6
, being sure to add "6" to the fields as appropriate (see structs, above). There is no sin6zero field.Instead of
struct in_addr
usestruct in6_addr
, being sure to add "6" to the fields as appropriate (see structs, above).Instead of
inet_aton()
orinet_addr()
, useinet_pton()
.Instead of
inet_ntoa()
, useinet_ntop()
.Instead of
gethostbyname()
, use the superiorgetaddrinfo()
.Instead of
gethostbyaddr()
, use the superiorgetnameinfo()
(althoughgethostbyaddr()
can still work with IPv6).INADDR_BROADCAST
no longer works. Use IPv6 multicast instead.