tech-net archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: struct sockaddr_storage issues
On Fri, 23 Dec 2022 16:48:46 -0500
Ken Hornstein <kenh%pobox.com@localhost> wrote:
> >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
I think getaddrinfo() has a lot of baggage. For my current use case I
prefer to keep it simple and only deal with IPv4 or IPv6 addresses.
I've seen reports of buggy getaddrinfo() implementations on some
platforms, which ignore some of the flags you mentioned. May be those
bugs have been fixed by now...
Home |
Main Index |
Thread Index |
Old Index