Hi all,
I am trying to perform two operations and wondering if I can use one libc function to do both. Specifically:
I was wondering if this could be done in a single operation using getaddrinfo(). I suspect so, because the 3rd argument of getaddrinfo() is a "struct addrinfo *hint", which has a member hint->ai_addr->sa_data[14]. I thought this was the non-htonl version of the IP.
However, whenever I try to use this variable my program crashes with a segmentation fault. Am I doing something wrong here? Can I do both operations using getaddrinfo() alone? Please advise.
Thanks!
I am trying to perform two operations and wondering if I can use one libc function to do both. Specifically:
- Determine if a certain string is an IPv4 or IPv6 address
- Get its numeric IP representation. (ie, ::1 is 1, 192.168.1.1 is 3232235777)
Code:
getaddrinfo("192.168.1.1", NULL, &hints, &res);
inet_pton(AF_INET, "192.168.1.1", &buf);
unsigned x = htonl(buf);
I was wondering if this could be done in a single operation using getaddrinfo(). I suspect so, because the 3rd argument of getaddrinfo() is a "struct addrinfo *hint", which has a member hint->ai_addr->sa_data[14]. I thought this was the non-htonl version of the IP.
However, whenever I try to use this variable my program crashes with a segmentation fault. Am I doing something wrong here? Can I do both operations using getaddrinfo() alone? Please advise.
Thanks!