Search code examples
ccallbackc-ares

C-Ares callback doesnt receive arg from query


I'm using the c-ares library to perform dns lookups on a set of domains stored in a file. Something like this.

char *line = NULL; 
while ((read = getline(&line, &len, fp)) != -1) {
        if (line && line[strlen(line)-1]=='\n') line[strlen(line)-1]=0;
        printf("Sending %s\n", line);
        ares_query(channel, line, ns_c_in, ns_t_mx, callback, line);
    }  

The last parameter to the ares_query function is of type void *arg which is just passed back to the callback function. Details here -

typedef void (*ares_callback)(void *arg, int status, int timeouts, unsigned char *abuf, int alen)

void ares_query(ares_channel channel, const char *name, int dnsclass, int type, ares_callback callback, void *arg)

The ares_query call above doesn't seem to pass "line" and I receive a NULL variable in the callback. However, when i call it with a literal like this, it seems to pass the arg.

ares_query(channel, line, ns_c_in, ns_t_mx, callback, "HELLO");

What is the difference between the two approaches? Why does the first method not work?


Solution

  • I think you should allocate memory to the variable passed to void pointer:

    char *line;
    line = malloc(stringLength*sizeof(char));