आप getaddrinfo()
POSIX फ़ंक्शन का उपयोग कर सकते हैं। यह inet_pton()
से अधिक लचीला है, उदाहरण के लिए यह स्वचालित रूप से आईपीवी 4 और आईपीवी 6 पता प्रारूपों का पता लगाता है, यह होस्टनामों (DNS समाधान का उपयोग करके) और पोर्ट/सेवा नामों का उपयोग कर सकता है (/etc/services
का उपयोग कर)।
#include <sys/types.h>
#include <netdb.h>
#include <netdb.h>
....
const char *ip6str = "::2";
struct sockaddr_storage result;
socklen_t result_len;
struct addrinfo *res = NULL;
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_DEFAULT | AI_NUMERICHOST | AI_NUMERICSERV;
rc = getaddrinfo(ip6str, NULL, &hints, &res);
if (rc != 0)
{
fprintf(stderr, "Failure to parse host '%s': %s (%d)", ip6str, gai_strerror(rc), rc);
return -1;
}
if (res == NULL)
{
// Failure to resolve 'ip6str'
fprintf(stderr, "No host found for '%s'", ip6str);
return -1;
}
// We use the first returned entry
result_len = res->ai_addrlen;
memcpy(&result, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
IPv6 पता struct sockaddr_storage result
चर में संग्रहित है।
if (result.ss_family == AF_INET6) // Ensure that we deal with IPv6
{
struct sockaddr_in6 * sa6 = (struct sockaddr_in6 *) &result;
struct in6_addr * in6 = &sa6->sin6_addr;
in6->s6_addr[0]; // This is a first byte of the IPv6
in6->s6_addr[15]; // This is a last byte of the IPv6
}
स्रोत
2017-07-11 11:39:15
ऐसा लगता है कि आपके पास 144-बिट आईपी पता है। – Thanatos
ओह ... इसे इंगित करने के लिए धन्यवाद .. असल में मेरा मतलब था 1: 22: 333: aaaa: b: c: d: e –