Book Image

Hands-On Network Programming with C

By : Lewis Van Winkle
Book Image

Hands-On Network Programming with C

By: Lewis Van Winkle

Overview of this book

Network programming enables processes to communicate with each other over a computer network, but it is a complex task that requires programming with multiple libraries and protocols. With its support for third-party libraries and structured documentation, C is an ideal language to write network programs. Complete with step-by-step explanations of essential concepts and practical examples, this C network programming book begins with the fundamentals of Internet Protocol, TCP, and UDP. You’ll explore client-server and peer-to-peer models for information sharing and connectivity with remote computers. The book will also cover HTTP and HTTPS for communicating between your browser and website, and delve into hostname resolution with DNS, which is crucial to the functioning of the modern web. As you advance, you’ll gain insights into asynchronous socket programming and streams, and explore debugging and error handling. Finally, you’ll study network monitoring and implement security best practices. By the end of this book, you’ll have experience of working with client-server applications and be able to implement new network programs in C. The code in this book is compatible with the older C99 version as well as the latest C18 and C++17 standards. You’ll work with robust, reliable, and secure code that is portable across operating systems, including Winsock sockets for Windows and POSIX sockets for Linux and macOS.
Table of Contents (26 chapters)
Title Page
About Packt

Chapter 5, Hostname Resolution and DNS

  1. Which function fills in an address needed for socket programming in a portable and protocol-independent way?

getaddrinfo()is the function to use for this.

  1. Which socket programming function can be used to convert an IP address back into a name?

getnameinfo()can be used to convert addresses back to names.

  1. A DNS query converts a name to an address, and a reverse DNS query converts an address back into a name. If you run a DNS query on a name, and then a reverse DNS query on the resulting address, do you always get back the name you started with?

Sometimes, you will get the same name back but not always. This is because the forward and reverse lookups use independent records. It's also possible to have many names point to one address, but that one address can only have one record that points back to a single name.

  1. What are the DNS record types used to return IPv4 and IPv6 addresses for a name?

TheArecord type returns an IPv4 address, and theAAAArecord type returns an IPv6 address.

  1. Which DNS record type stores special information about email servers?

TheMXrecord type is used to return email server information.

  1. Does getaddrinfo() always return immediately? Or can it block?

Ifgetaddrinfo()is doing name lookups, it will often block. In the worst-case scenario, many UDP messages would need to be sent to various DNS servers, so this can be a noticeable delay. This is one reason why DNS caching is important.If you are simply usinggetaddrinfo()to convert from a text IP address, then it shouldn't block.

  1. What happens when a DNS response is too large to fit into a single UDP packet?

The DNS response will have theTCbit set in its header. This indicates that the message was truncated. The query should be resent using TCP.