Subject: DNS lookup always raises "Misformatted domain name" on Windows 10

DNS lookup always raises "Misformatted domain name" on Windows 10

From: silvioprog <silvioprog_at_gmail.com>
Date: Mon, 7 Nov 2016 17:03:42 -0300

Hello list, this is my first post here. :-)

So, I've tried to use c-ares on Windows 10 in a simple DNS lookup, however,
I can't understand why it does not work. Consider the following code (found
by googling, unfortunately I can't find any c-ares official example or any
"get started" :-/ ):

#include <ares.h>
#include <stdio.h>

static void state_cb(void *data, int s, int read, int write) {
    printf("Change state fd %d read:%d write:%d\n", s, read, write);
}

static void callback(void *arg, int status, int timeouts, struct hostent
*host) {
    if (!host || status != ARES_SUCCESS) {
        printf("Failed to lookup %s\n", ares_strerror(status));
        return;
    }
    printf("Found address name %s\n", host->h_name);
    char ip[INET6_ADDRSTRLEN];
    int i = 0;
    for (i = 0; host->h_addr_list[i]; ++i) {
        ares_inet_ntop(host->h_addrtype, host->h_addr_list[i], ip,
sizeof(ip));
        printf("%s\n", ip);
    }
}

static void wait_ares(ares_channel channel) {
    for (;;) {
        struct timeval *tvp, tv;
        fd_set read_fds, write_fds;
        int nfds;
        FD_ZERO(&read_fds);
        FD_ZERO(&write_fds);
        nfds = ares_fds(channel, &read_fds, &write_fds);
        if (nfds == 0) {
            break;
        }
        tvp = ares_timeout(channel, NULL, &tv);
        select(nfds, &read_fds, &write_fds, NULL, tvp);
        ares_process(channel, &read_fds, &write_fds);
    }
}

int main() {
    ares_channel channel = NULL;
    int status;
    struct ares_options options = {0};
    int optmask = 0;how to fix this problem?

    status = ares_library_init(ARES_LIB_INIT_ALL);
    if (status != ARES_SUCCESS) {
        printf("ares_library_init: %s\n", ares_strerror(status));
        return 1;
    }

    //options.sock_state_cb_data = state_cb;
    options.sock_state_cb = (ares_sock_state_cb) state_cb;

    optmask |= ARES_OPT_SOCK_STATE_CB;

    status = ares_init_options(&channel, &options, optmask);

    if (status != ARES_SUCCESS) {
        printf("ares_init_options: %s\n", ares_strerror(status));
        return 1;
    }

    ares_gethostbyname(channel, "www.google.com.br", AF_INET, callback,
NULL);
    //ares_gethostbyname(channel, "google.com", AF_INET6, callback, NULL);
    wait_ares(channel);
    ares_destroy(channel);
    ares_library_cleanup();
    return 0;
}

This same example works fine on Ubuntu, but on Windows it always returns
"ares_init_options: Misformatted domain name". :-(

So, what I'm doing wrong?

My environment:

Project compiled for win 32 bits
Windows 10
MinGW w64
c-ares-1.12.0 (tried c-ares-1.10.0 too, same problem)
Dlls in the app folder: libcares-2.dll, libgcc_s_dw2-1.dll
and libwinpthread-1.dll

-- 
Silvio Cl├ęcio
Received on 2016-11-07