Sending the query

We start main() with the following code:

/*dns_query.c*/int main(int argc, char *argv[]) {    if (argc < 3) {        printf("Usage:\n\tdns_query hostname type\n");        printf("Example:\n\tdns_query example.com aaaa\n");        exit(0);    }    if (strlen(argv[1]) > 255) {        fprintf(stderr, "Hostname too long.");        exit(1);    }

The preceding code checks that the user passed in a hostname and record type to query. If they didn't, it prints a helpful message. It also checks that the hostname isn't more than 255 characters long. Hostnames longer than that aren't allowed by the DNS standard, and checking it now ensures that we don't need to allocate too much memory.

We then try to interpret the record type requested by the user. We support the following options ...

Get Hands-On Network Programming with C now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.