0

I have been trying to create a DNS query in C but I am having trouble managing different data types and pointers,

I have the header build in the first position of a variable called buf. Now I want to add the lookup host on the following positions of the array. The hostname to add to the query is passed through the parameter argv[1]

I have the following:

First I trimed the hostname by the delimiter "."

unsigned char *qname;
qname = &buf[sizeof(struct DNS_QUERY_HEADER)];
char *host = argv[1]; //my hostname
strcat((char*)host,".");
unsigned char *htrim = strtok(host, ".");`

Then I go through each of those trims, I store the size of it (the number of characters inside the label) and then store that part of the hostname before the "." for each iteration

if(htrim != NULL){
    while(htrim != NULL){
        *qname++ = htons(strlen((char*)htrim));
        for(int i = 0; i< strlen((char*)htrim); i++)
        {
            *qname++ = (htrim[i]);
        }
        htrim = strtok(NULL, ".");       
    }
}

Then I added the "/0" at the end of the query *qname++='\0';

Finally the rest of the DNS message (type and class).

When I launch the program, the packet seems to be well formed. Header seems fine, Type and class ok. However, the hostname that is asking in the query is not google.com, for example, but either watson.events.data.microsoft.com or root. Therefore, I am guessing I am bulding the qname label wrong but this is the closest that I got.

Could anyone give me any hints of how should I stored the hostname in a DNS query or what I am doing wrong?

4
  • 1
    Isn't there a higher-level library that will automate this for you? Commented Oct 24, 2023 at 21:32
  • You shouldn't be doing strcat((char*)host,"."); (not sure if that's your only problem). The answers and comments at stackoverflow.com/questions/76220615/… might help Commented Oct 24, 2023 at 21:41
  • Yes, there is a library, but I have to build the packet itself as part of the requirement. Commented Oct 24, 2023 at 22:02
  • Thank very much for the input, it must not be the only problem, but it seems to build another packet with wapad.com instead of the watson query, so yes, I think the srtcat must have caused some issues. I put ir there for the root server, but it seems it is not needed. Thank you Commented Oct 24, 2023 at 22:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.