Adding support for IPv6 in IPv4 client/server apps - sin6_flowinfo and sin6_scope_id fields?

柔情痞子 提交于 2019-11-30 08:05:27

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!