tech-net archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: struct sockaddr_storage issues



>Below is fragment of my code that initializes socket address before
>calling connect(). Various error checking is omitted for readability.
>
>
>sa_family_t             sa_family = 0;
>struct in_addr          addr_ipv4;
>struct in6_addr         addr_ipv6;
>in_port_t               port = 0;
>struct sockaddr_storage addr;
>
>/* Specific IPv4 address */
>else if ((int_val = inet_pton(AF_INET, val, &addr_ipv4)) == 1)
>[...]
>else if ((int_val = inet_pton(AF_INET6, val, &addr_ipv6)) == 1)

As an aside ... you would probably be better served by using getaddrinfo()
to generate the sockaddr structure, as it handles IPv4/IPv6 internally
and takes care of any OS-specific stuff (e.g., sin_len, since that
structure member is not required by POSIX and is difficult to use
in portable code).  If you don't wish to perform a name lookup you can
use AI_NUMERICHOST and/or AI_NUMERICSERV in the ai_flags member of
the hints structure (and if you need it, I would recommend using
getnameinfo() insteasd of inet_ntop() for similar reasons).

--Ken


Home | Main Index | Thread Index | Old Index