cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Compiler error with c-ares and Visual studio 2012

From: Kim Vandry <vandry_at_TZoNE.ORG>
Date: Sun, 10 Nov 2013 12:08:57 -0500

On 2013-11-10 08:40 , Andreas Rieke wrote:
> when compiling libcurl 7.33.0 with c-ares 1.10.0 under Windows Server
> 2012 with Visual Studio 2012, I get a compilation error in file
> lib/asyn-ares.c and function CURLcode Curl_set_dns_local_ip4(struct
> SessionHandle *data, const char *local_ip4) in line 648: The compiler
> simply does not understand uint32_t.
>
> Instead of
>> uint32_t a4;
> I put
>> # ifdef WIN32
>> u_long a4;
>> # else
>> uint32_t a4;
>> # endif
> in the beginning of the function - works well,

Hello Andres,

I guess I wrote that code so I'll respond :-)

Thanks for identifying this issue.

I would prefer not to use "u_long" for this variable because that type,
which I guess is the same as "unsigned long", is not guaranteed by C to
be any particular width: it could be more than 32 bits. But in this case
we have an IPv4 address, which is known to need exactly 32 bits, no
more, no less.

I am surprised that uint32_t would not be usable here because it's used
apparently successfully by c-ares itself (it's in the declaration of
"ares_set_local_ip4").

I surveyed some other locations in the Curl source code, and it appears
that including <stdint.h> and using uint32_t is not common. Instead, the
convention throughout seems to be to use "struct in_addr" to hold IPv4
addresses. So let's do that.

-kv

---
  lib/asyn-ares.c |    6 +++---
  1 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/lib/asyn-ares.c b/lib/asyn-ares.c
index 0ef36cb..94ee767 100644
--- a/lib/asyn-ares.c
+++ b/lib/asyn-ares.c
@@ -645,10 +645,10 @@ CURLcode Curl_set_dns_local_ip4(struct 
SessionHandle *data,
                                  const char *local_ip4)
  {
  #if (ARES_VERSION >= 0x010704)
-  uint32_t a4;
+  struct in_addr a4;
    if((!local_ip4) || (local_ip4[0] == 0)) {
-    a4 = 0; /* disabled: do not bind to a specific address */
+    a4.s_addr = 0; /* disabled: do not bind to a specific address */
    }
    else {
      if(Curl_inet_pton(AF_INET, local_ip4, &a4) != 1) {
@@ -656,7 +656,7 @@ CURLcode Curl_set_dns_local_ip4(struct SessionHandle 
*data,
      }
    }
-  ares_set_local_ip4((ares_channel)data->state.resolver, ntohl(a4));
+  ares_set_local_ip4((ares_channel)data->state.resolver, ntohl(a4.s_addr));
    return CURLE_OK;
  #else /* c-ares version too old! */
-- 
1.7.3.2
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
Received on 2013-11-10