📝 Recursive vs iterative DNS resolution (15 MCQs)
Two Ways DNS Queries Obtain an Answer
DNS resolution can use recursive or iterative query behavior, describing how much work the queried server must perform. With recursion, a server is asked to obtain a final answer or report failure on the client's behalf. With iteration, a server returns the best information it has, often a referral to another name server. Its distributed structure avoids reliance on one central database for every Internet name worldwide.
Resolver Responsibilities and Referral Messages
A stub resolver commonly sends a recursive query to its configured recursive resolver. That resolver may perform iterative queries to root, TLD and authoritative servers. An authoritative server generally responds with data for its zone or a referral, rather than recursively searching the rest of the DNS hierarchy for the client.
Recursive Requests and Iterative Referrals
In recursive resolution, the queried server accepts responsibility for completing the lookup. In iterative resolution, the requester follows referrals by sending another query to a server named in the previous response. The DNS message's RD bit requests recursion, while the RA bit indicates whether the responding server supports recursion. The lookup process combines local caching with referrals across delegated administrative zones.
A Lookup Through Root, TLD and Authoritative Servers
A laptop asks its recursive resolver for www.example.com and sets recursion desired. If its cache is empty, the resolver queries a root server, receives a com referral, queries a com server, receives an example.com referral and finally asks an authoritative server for the A record. It returns the answer to the laptop.
Combining Recursive Clients with Iterative Server Queries
The client's interaction with a recursive resolver can be recursive even though the resolver's upstream work is iterative. This division reduces work on end devices and lets a resolver cache results. Authoritative servers focus on their own zones, while recursive resolvers provide broader lookup service for clients. This division supports scalable administration while keeping naming separate from packet forwarding.
Misunderstandings About Recursive DNS
'Recursive means a server asks itself repeatedly' does not describe the protocol; it means the server completes resolution for the requester. 'Every query from a client is iterative' is false because clients commonly request recursion. 'An authoritative server must contact the root for every answer' ignores its own zone data and caching or referral context.
Tracing Query Flags and Resolution Direction
In message examples, recognize RD as recursion desired and RA as recursion available. Draw the client-to-resolver recursive request separately from the resolver's iterative root-to-TLD-to-authority queries. Explain which device follows referrals and where the final answer is returned, and avoid assuming that all DNS participants perform the same role. Practice each lookup stage and match returned data to the requested record type.
DNS Resolution Mode Terms
- Recursive query - Requests a complete answer from a server.
- Iterative query - Allows referrals to guide further queries.
- RD bit - Requests recursive service.
- RA bit - Indicates recursion availability.
- Referral - Points toward another name server.
- Stub resolver - Sends queries to a DNS resolver.
📝 All Recursive vs iterative DNS resolution MCQs
Q1. Which function creates a UDP socket?
📖 Explanation: The UDP socket is created by calling socket with the domain PF_INET (or AF_INET), the type SOCK_DGRAM, and protocol 0. This combination selects the datagram service of IPv4. The other options specify either stream sockets or the wrong domain, which would not produce a UDP socket.
Q2. Why is a null character appended to the echoed message before printing?
📖 Explanation: Appending '\0' creates a C‑style string terminator, allowing functions like printf or puts to know where the text ends. Without it, the printing routine would continue reading beyond the intended message, potentially outputting garbage data. This step is purely for safe, readable debugging output.
Q3. Which statement best compares TCP and UDP regarding reliability?
📖 Explanation: TCP is connection‑oriented and provides sequencing, acknowledgments, and retransmission, ensuring that data arrives in order. UDP is connectionless and sends datagrams without such guarantees; packets may arrive out of order, be duplicated, or be lost. Therefore, only TCP offers reliable, ordered delivery.
Q4. What is the purpose of the htons function in the code snippet servAddr.sin_port = htons(servPort);?
📖 Explanation: Network protocols use big‑endian (network) byte order. htons (“host to network short”) transforms a 16‑bit port number from the host’s native ordering to network ordering, ensuring that the receiving side interprets the port correctly. Without this conversion, mismatched byte orders could cause connection failures.
Q5. If sendto returns -1, what can be logically inferred?
📖 Explanation: A return value of -1 from sendto indicates that the system call failed, typically setting errno to describe the problem (e.g., EACCES, ENOTCONN). It means the data was not transmitted, and the program should handle the error before proceeding.
Q6. How does the error‑handling block if ((s = socket(PF_INET, SOCK_DGRAM, 0) < 0)) affect program robustness?
📖 Explanation: The conditional checks whether the socket call returned a negative descriptor, which signals failure. By invoking perror and exit(1), the program reports the issue and terminates, preventing subsequent undefined behavior. This defensive pattern improves robustness by ensuring resources are valid before use.
Q7. Why is it important to close the socket with close(s) before exiting the program?
📖 Explanation: Closing the socket returns the file descriptor to the operating system, allowing other processes to reuse it and preventing resource leaks. For UDP, it also discards any pending datagrams. Failing to close may leave descriptors open, consuming kernel resources and potentially causing exhaustion in long‑running applications.
Q8. What would be the effect of omitting the null character when printing the echoed message?
📖 Explanation: Without a terminating '\0', printing functions treat the buffer as a C‑string and continue reading memory until a zero byte is encountered. This can result in extra, unintended characters being displayed, possibly exposing sensitive data or causing confusing output during debugging.
Q9. Compare send and sendto when used with UDP sockets. Which statement is accurate?
📖 Explanation: When a UDP socket is unconnected, sendto allows specifying the target address with each call, while send can be used after a prior connect to fix the peer. Both functions place datagrams on the network; neither provides reliability or retransmission, which are intrinsic to TCP only.
Q10. How does the connection‑oriented nature of TCP influence the sequence of socket calls compared to UDP?
📖 Explanation: TCP establishes a reliable session via connect, which negotiates sequence numbers and flow control. After this handshake, send and recv operate on the established connection. UDP is connectionless; it can send datagrams directly with sendto without a prior connect, though an optional connect can set a default peer.
Q11. If the programmer forgets to apply htons to servPort, what is the most likely outcome?
📖 Explanation: Omitting htons leaves the port in host byte order. On little‑endian machines this swaps the high and low bytes, causing the socket to bind to a different numeric port than intended. The server will then listen on the wrong port, and clients will fail to connect.
Q12. Why is printing the echoed message considered a useful debugging technique in this code?
📖 Explanation: Displaying the received buffer lets the developer verify that the round‑trip transmission succeeded and that the content matches the original string. Any discrepancy, such as truncation or corruption, becomes apparent instantly, allowing quicker identification of logic or network issues during development.
Q13. When using sizeof(servAddr) versus sizeof(struct sockaddr_in) in the sendto call, what conceptual difference matters?
📖 Explanation: In this context servAddr is declared as struct sockaddr_in, so sizeof(servAddr) and sizeof(struct sockaddr_in) are identical. However, expressing the size with the explicit type makes the code clearer to readers and avoids accidental mismatches if the variable’s type changes in the future.
Q14. What logical consequence follows if the client specifies an incorrect servPort value?
📖 Explanation: Ports identify specific services on a host. Supplying an incorrect port means the datagrams are sent to a destination where no listening socket exists, leading to silent discarding by the network stack or reception by an unintended service, effectively causing communication failure.
Q15. Evaluate how the program would need to change to support multiple concurrent clients without altering the core logic. Which modification is essential?
📖 Explanation: To handle many clients simultaneously while keeping the existing UDP‑based logic, the program should spawn a new thread or process for each incoming request, allowing independent handling of messages. UDP itself already supports multiple clients, but concurrency requires parallel execution to avoid blocking on a single client.