IPv6 parsing in C

前端 未结 7 1119
猫巷女王i
猫巷女王i 2020-12-15 08:35

I wanted to know how I can parse an IPv6 address in C and convert it to a 128 bit value?

So a hex address like 1:22:333:aaaa:b:c:d:e needs to be convert

7条回答
  •  温柔的废话
    2020-12-15 09:19

    You can use POSIX inet_pton to convert a string to a struct in6_addr.

    #include 
    
      ...
    
    const char *ip6str = "::2";
    struct in6_addr result;
    
    if (inet_pton(AF_INET6, ip6str, &result) == 1) // success!
    {
        //successfully parsed string into "result"
    }
    else
    {
        //failed, perhaps not a valid representation of IPv6?
    }
    

提交回复
热议问题