3

I am trying to perform a simple DNS lookup in C. Here is my code (which I mostly coppied from an example):

#include <winsock2.h>
#include <windns.h>
#include <stdio.h>

char* DNS_Lookup(char* host) {
    DNS_STATUS status;               //Return value of  DnsQuery_A() function.
    PDNS_RECORD pDnsRecord;          //Pointer to DNS_RECORD structure.
    PIP4_ARRAY pSrvList = NULL;      //Pointer to IP4_ARRAY structure.
    WORD wType;                      //Type of the record to be queried.
    char* pOwnerName = host;         //Owner name to be queried.
    char pReversedIP[255];           //Reversed IP address.
    char DnsServIp[255];             //DNS server ip address.
    DNS_FREE_TYPE freetype;
    freetype = DnsFreeRecordList;// DnsFreeRecordListDeep;
    IN_ADDR ipaddr;

    status = DnsQuery(pOwnerName,                 //Pointer to OwnerName. 
                        wType,                      //Type of the record to be queried.
                        DNS_QUERY_BYPASS_CACHE,     // Bypasses the resolver cache on the lookup. 
                        pSrvList,                   //Contains DNS server IP address.
                        &pDnsRecord,                //Resource record that contains the response.
                        NULL);                     //Reserved for future use.
    if(status) {
        printf("Failed to query the host record for %s and the error is %d \n", pOwnerName, status);
    } else {
        ipaddr.S_un.S_addr = (pDnsRecord->Data.A.IpAddress);
        printf("The IP address of the host %s is %s \n", pOwnerName,inet_ntoa(ipaddr));

        // Free memory allocated for DNS records. 
        DnsRecordListFree(pDnsRecord, freetype);
    }
    LocalFree(pSrvList);
    return inet_ntoa(ipaddr);
}

When I try to compile, I get linker errors (which I've cleaned up a little bit for legibility):

gcc -g main.c dns.c -o build.exe -D WIN -lws2_32 -w
c:/<..>/mingw32/bin/ld.exe: <..>/ccRVIgMi.o: in function `Z10DNS_LookupPc':
../dns.c:17: undefined reference to `DnsQuery_A@24'
c:/<..>/mingw32/bin/ld.exe: <..>/dns.c:30: undefined reference to `DnsRecordListFree@8'
collect2.exe: error: ld returned 1 exit status

I know in order to compile code using winsock2.h I had to add -lws2_32 to my gcc command, and I am guessing I have to do something similar to compile code using windns.h, but I have been unable to find any help online.

FelisPhasma
  • 332
  • 5
  • 13

1 Answers1

2

The correct answer should be

 gcc -g main.c dns.c -o build.exe -D WIN -lws2_32 -ldnsapi -w

but as you've discovered it doesn't work. Thankfully gcc is smart enough to privde a way to recover:

 gcc -g main.c dns.c -o build.exe -D WIN -lws2_32 -ldnsapi.dll -w

If for some reason that doesn't work and you are compiling on Windows and not cross-compiling, this would work:

 gcc -g main.c dns.c -o build.exe -D WIN -lws2_32 -lc:\windows\system32\dnsapi.dll -w
Joshua
  • 40,822
  • 8
  • 72
  • 132
  • Thanks! I am actually able to compile using just `-ldnsapi` without the `.dll` at the end. – FelisPhasma Mar 09 '19 at 21:21
  • 1
    @FelisPhasma: Typo'd something in your (deleted) self-answer? Anyway, it doesn't work for me so I spelled out the long way. – Joshua Mar 09 '19 at 21:22