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

前端 未结 1 946
野趣味
野趣味 2020-12-29 03:56

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

相关标签:
1条回答
  • 2020-12-29 04:05

    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.

    0 讨论(0)
提交回复
热议问题