.. _Extended5DNS:

.. raw:: html

   <script>ODSA.SETTINGS.DISP_MOD_COMP = true;ODSA.SETTINGS.MODULE_NAME = "Extended5DNS";ODSA.SETTINGS.MODULE_LONG_NAME = "Extended Example: DNS Client";ODSA.SETTINGS.MODULE_CHAPTER = "The Internet and Connectivity"; ODSA.SETTINGS.BUILD_DATE = "2021-06-14 17:15:25"; ODSA.SETTINGS.BUILD_CMAP = false;JSAV_OPTIONS['lang']='en';JSAV_EXERCISE_OPTIONS['code']='java';</script>


.. |--| unicode:: U+2013   .. en dash
.. |---| unicode:: U+2014  .. em dash, trimming surrounding whitespace
   :trim:


.. This file is part of the OpenCSF eTextbook project. It was
.. auto-generated by scripts from the OpenDSA eTextbook project.
.. See https://OpenCSF.org for more details. OpenCSF is distributed
.. under a Creative Commons Attribution-NonCommercial 4.0 International
.. License (see http://creativecommons.org/licenses/by-nc/4.0/),
.. Copyright (c) 2019-2021 by Michael S. Kirkpatrick. OpenDSA is
.. distributed under an MIT open source license, Copyright (c) 2012-2021
.. by the OpenDSA Project Contributors.

.. avmetadata::
   :author: Michael S. Kirkpatrick
   :requires:
   :satisfies: 
   :topic: 

Extended Example: DNS Client
============================

This Extended Example is a minimal client for performing a DNS query for IPv4
addresses. Given a domain name (such as ``example.com``), the client sets up
the DNS question in ``setup_dns_request()`` (lines 200 – 234). The ``packed``
attribute of the ``dns_record_a_t`` (lines 31 – 39) ensures that the compiler
does not add any unnecessary padding that would make the question improperly
structured. The ``build_domain_qname()`` function (lines 95 – 134) replaces the
dots in the domain name with an integer to denote the length of the next field.
Lines 65 – 82 perform the actual query, sending the request to OpenDNS using a
UDP socket. The client is only designed to support DNS ``A`` type records for
IPv4. I.e., this client does not support ``CNAME``, ``NS``, or ``MX`` records.
The ``print_dns_response()`` function (lines 153 – 198) will stop if any other
record types are encountered.

.. codeinclude:: Internet/ExtEx-5.c
   :linenos: true

