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?
strcat((char*)host,".");(not sure if that's your only problem). The answers and comments at stackoverflow.com/questions/76220615/… might help