weixin_39577908
weixin_39577908
2020-12-06 00:50

ares_inet_pton() and CIDR

When building C-ares without -DHAVE_INET_PTON, the code in inet_net_pton.c accepts an address like a.c.d/8. And thus returns (as the comment for inet_net_pton_ipv4()) says:


  number of bits, either imputed classfully or specified with /CIDR,

This seems to leave the remainder of dst address to not be set. Since if I do e.g.: c:\> acountry.exe 8.8/17, without -DHAVE_INET_PTON: United States (us), number 840. sometimes other results.

c:\> acountry.exe 8.8/17, with -DHAVE_INET_PTON: Failed to lookup 8.8/17 Always correctly fails on CIDR.

inet_pton() in C-ares is contrary to Winsock which only accepts dotted-decimal notation.

So all in all, I think there is a missing memset() in inet_net_pton.c:

diff
--- a/inet_net_pton.c 2017-05-04 16:09:09
+++ b/inet_net_pton.c 2018-08-08 19:40:16
@@ -73,6 +73,8 @@
   int n, ch, tmp = 0, dirty, bits;
   const unsigned char *odst = dst;

+  memset(odst, '\0', NS_INADDRSZ);
+
   ch = *src++;
   if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
       && ISASCII(src[1])

Edit: Duh me! Writing to a const pointer. Is this better?

diff
--- a/inet_net_pton.c 2017-05-04 16:09:09
+++ b/inet_net_pton.c 2018-08-08 19:40:16
@@ -73,6 +73,8 @@
   int n, ch, tmp = 0, dirty, bits;
   const unsigned char *odst = dst;

+ for (n = 0; n < NS_INADDRSZ; n++)
+    dst[n] = 0;   

   ch = *src++;

该提问来源于开源项目:c-ares/c-ares

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_39788792 weixin_39788792 5月前

    Heh, not that I'm aware of :) Just the function imported that does dotted decimal conversion happens to "support" it ...

    点赞 评论 复制链接分享
  • weixin_39788792 weixin_39788792 5月前

    Looks like there is code around https://github.com/c-ares/c-ares/blob/master/inet_net_pton.c#L178 that is trying to handle this zero padding and just not working.

    I'm not really sure what https://github.com/c-ares/c-ares/blob/master/inet_net_pton.c#L156 is trying to do, inferring mask from the first byte of the big-endian representation of the address doesn't make sense.

    点赞 评论 复制链接分享
  • weixin_39788792 weixin_39788792 5月前

    Looks like even glibc uses the same implementation (or at least based on the same one with the same issue): https://sourceware.org/git/?p=glibc.git;a=blob;f=resolv/inet_net_pton.c;h=aab9b7b58228bb2db15cb41182666ec95122d581;hb=HEAD#l64

    I think this may be a good find as it seems to be used by everyone.

    点赞 评论 复制链接分享
  • weixin_39577908 weixin_39577908 5月前

    Okay, but where in the C-ares apps is CIDR notation useful? Nowhere AFAICS.

    点赞 评论 复制链接分享

相关推荐