I work on implementing IPv6 support for several applications, but I wondered what are these 2 fields for. There are so few questions about this here so I\'m not sure I got i
The best way to go is to use getaddrinfo().
Pseudo code:
struct addrinfo *restrict hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM };
struct addrinfo * res, r;
if (0 == getaddrinfo("foo.bar.baz", "http", &hints, &res)) {
for (r=res; r; r=r->ai_next) {
sock = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
connect(sock, r->ai_addr, r->ai_addrlen);
if error: continue
break
}
}
freeaddrinfo(res);
This will take the worry about sin6_scope_id
from you; which is normally 0
, except if you have link-local addresses like fe80::1234:56ff:fe78:9abc%eth2
. This eth2
is converted to the correct scope ID.
sin6_flowinfo
is obsolete (AFAIK) and thus set to 0 in your resulting struct addrinfo's ai_addr.