<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.ethernut.de/nutwiki/index.php?action=history&amp;feed=atom&amp;title=Domain_Name_Service</id>
	<title>Domain Name Service - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.ethernut.de/nutwiki/index.php?action=history&amp;feed=atom&amp;title=Domain_Name_Service"/>
	<link rel="alternate" type="text/html" href="https://www.ethernut.de/nutwiki/index.php?title=Domain_Name_Service&amp;action=history"/>
	<updated>2026-11-22T13:26:45Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://www.ethernut.de/nutwiki/index.php?title=Domain_Name_Service&amp;diff=68&amp;oldid=prev</id>
		<title>Harald: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="https://www.ethernut.de/nutwiki/index.php?title=Domain_Name_Service&amp;diff=68&amp;oldid=prev"/>
		<updated>2016-10-27T16:02:53Z</updated>

		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 16:02, 27 October 2016&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Harald</name></author>
	</entry>
	<entry>
		<id>https://www.ethernut.de/nutwiki/index.php?title=Domain_Name_Service&amp;diff=67&amp;oldid=prev</id>
		<title>legacy&gt;Joshua: Added fix to the details section</title>
		<link rel="alternate" type="text/html" href="https://www.ethernut.de/nutwiki/index.php?title=Domain_Name_Service&amp;diff=67&amp;oldid=prev"/>
		<updated>2010-03-26T15:49:20Z</updated>

		<summary type="html">&lt;p&gt;Added fix to the details section&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Overview ==&lt;br /&gt;
This example demonstrates how to configure the DNS settings of Nut/OS and retrieve DNS and MX records for a list of servers.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Please note that the NutDnsGetMxByDomain() currently seems to be broken as it returns bogus data that is definitely not valid.&amp;#039;&amp;#039;&amp;#039; It is included for completeness of this example only. If you find NutDnsGetMxByDomain() to be working in future versions of Nut/OS, please remove this remark and update the output below.&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;dev/board.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;arpa/inet.h&amp;gt;&lt;br /&gt;
#include &amp;lt;net/route.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pro/dhcp.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;io.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;time.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define LOCAL_IP &amp;quot;192.168.192.111&amp;quot;&lt;br /&gt;
#define GATE_IP &amp;quot;192.168.192.1&amp;quot;&lt;br /&gt;
#define DNS_IP &amp;quot;192.168.192.1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
const prog_char *server[3] = { &amp;quot;egnite.de&amp;quot;, &amp;quot;google.com&amp;quot;, &amp;quot;ethernut.de&amp;quot; };&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
    unsigned long baud = 115200;&lt;br /&gt;
    uint32_t return_addr = 0;&lt;br /&gt;
    uint32_t return_mx = 0;&lt;br /&gt;
    int i = 0;&lt;br /&gt;
&lt;br /&gt;
    NutRegisterDevice(&amp;amp;DEV_DEBUG, 0, 0);&lt;br /&gt;
&lt;br /&gt;
    freopen(DEV_DEBUG_NAME, &amp;quot;w&amp;quot;, stdout);&lt;br /&gt;
    _ioctl(_fileno(stdout), UART_SETSPEED, &amp;amp;baud);&lt;br /&gt;
&lt;br /&gt;
    NutRegisterDevice(&amp;amp;DEV_ETHER, 0, 0);&lt;br /&gt;
    if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) {&lt;br /&gt;
        puts(&amp;quot;Error: Cannot configure network.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    puts(&amp;quot;DNS example\n&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    NutDnsConfig2(0, 0, inet_addr(DNS_IP), inet_addr(DNS_IP));&lt;br /&gt;
    NutIpRouteAdd(inet_addr(LOCAL_IP), 24, inet_addr(GATE_IP), &amp;amp;DEV_ETHER);&lt;br /&gt;
&lt;br /&gt;
    for (i = 0; i &amp;lt; 3; i++) {&lt;br /&gt;
        return_addr = NutDnsGetHostByName(server[i]);&lt;br /&gt;
        return_mx = NutDnsGetMxByDomain(server[i]);&lt;br /&gt;
        printf(&amp;quot;Host was: %s\n\tIP: %s\n\tMX: %s\n&amp;quot;, server[i]);&lt;br /&gt;
        printf(&amp;quot;\tIP: %s\n&amp;quot;, inet_ntoa(return_addr)); &lt;br /&gt;
        printf(&amp;quot;\tMX: %s\n&amp;quot;, inet_ntoa(return_mx));&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    for (;;);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Details ==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define LOCAL_IP &amp;quot;192.168.192.111&amp;quot;&lt;br /&gt;
#define GATE_IP &amp;quot;192.168.192.1&amp;quot;&lt;br /&gt;
#define DNS_IP &amp;quot;192.168.192.1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
const prog_char *server[3] = { &amp;quot;egnite.de&amp;quot;, &amp;quot;google.com&amp;quot;, &amp;quot;ethernut.de&amp;quot; };&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First thing we do is define the LOCAL_IP (our board&amp;#039;s IP), the GATE_IP (gateway IP, usually your router or the computer that provides the internet connection for your board) and DNS_IP (usually the router, too, but you may wish to use a different DNS server so we define it independently).&lt;br /&gt;
&lt;br /&gt;
Next we declare a list of domain names we want to look up. The prog_char* data type is technically the same as a normal char* data type, except that it is stored in program space and thus doesn&amp;#039;t waste RAM but only inflates the binary image by a few bytes. Unlike defines we can even declare arrays this way which will make our task way easier later on.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
    unsigned long baud = 115200;&lt;br /&gt;
    uint32_t return_addr = 0;&lt;br /&gt;
    uint32_t return_mx = 0;&lt;br /&gt;
    int i = 0;&lt;br /&gt;
&lt;br /&gt;
    NutRegisterDevice(&amp;amp;DEV_DEBUG, 0, 0);&lt;br /&gt;
&lt;br /&gt;
    freopen(DEV_DEBUG_NAME, &amp;quot;w&amp;quot;, stdout);&lt;br /&gt;
    _ioctl(_fileno(stdout), UART_SETSPEED, &amp;amp;baud);&lt;br /&gt;
&lt;br /&gt;
    NutRegisterDevice(&amp;amp;DEV_ETHER, 0, 0);&lt;br /&gt;
    if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) {&lt;br /&gt;
        puts(&amp;quot;Error: Cannot configure network.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    puts(&amp;quot;DNS example\n&amp;quot;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We set up the debug device, declare a variable to hold one returned IP and one returned MX record and output a banner. For more info on network configuration see the corresponding examples.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
NutDnsConfig2(0, 0, inet_addr(DNS_IP), inet_addr(DNS_IP));&lt;br /&gt;
NutIpRouteAdd(inet_addr(LOCAL_IP), 24, inet_addr(GATE_IP), &amp;amp;DEV_ETHER);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we configure the DNS settings of the board. 0 means &amp;quot;default hostname&amp;quot; and &amp;quot;default domain&amp;quot; respectively, the next two parameters are 2 DNS servers. The inet_addr call makes a numeric representation of our IP. It was stored as string, remember? Strings don&amp;#039;t make for good number crunching and IPs are numbers anyway. It is common practice to pass 2 DNS servers, one for normal use and one as fallback, if the first doesn&amp;#039;t work or is unable to process certain requests. We pass the same server twice to keep it simple.&lt;br /&gt;
&lt;br /&gt;
The next line adds an IP route, telling Nut/OS which requests to handle via which Gateway. Here we assign our gateway to make sure we&amp;#039;re able to contact &amp;quot;the web&amp;quot;. The second parameter may look a little confusing if you&amp;#039;re not used to netmasks. We want the board to work in the subnet &amp;quot;255.255.255.0&amp;quot;. A short way of writing netmasks is simply saying &amp;quot;the first x bit are 1, the rest is 0. This is exactly what we do here. The first 24 bit are 1, the last 8 are 0, resulting in &amp;quot;255.255.255.0&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
for (i = 0; i &amp;lt; 3; i++) {&lt;br /&gt;
    return_addr = NutDnsGetHostByName(server[i]);&lt;br /&gt;
    return_mx = NutDnsGetMxByDomain(server[i]);&lt;br /&gt;
    printf(&amp;quot;Host was: %s\n\tIP: %s\n\tMX: %s\n&amp;quot;, server[i]);&lt;br /&gt;
    printf(&amp;quot;\tIP: %s\n&amp;quot;, inet_ntoa(return_addr)); &lt;br /&gt;
    printf(&amp;quot;\tMX: %s\n&amp;quot;, inet_ntoa(return_mx));&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After all this setting up the actual DNS request is quite simple. A call to NutDnsGetHostByName - and NutDnsGetMxByDomain respectively - returns a numeric IP representation which we store in our variables. This call may block for a moment so don&amp;#039;t worry if your program needs a moment to output something.&lt;br /&gt;
&lt;br /&gt;
Now we output the hostname used and then our 2 retrieved records. Did you notice the strange strdup? Great. inet_ntoa is a function that turns our numeric IP into a string which looks like an IP that we&amp;#039;re used to. Here&amp;#039;s the catch: inet_ntoa is not threadsafe, meaning that if another thread was to call inet_ntoa, it would replace the result of the last call. This would make us output a totally wrong address so we duplicate the returned string to another RAM area first. In this example this is not needed since it is not multithreaded anyway, it&amp;#039;s just included to make sure you are aware of this when starting your own application.&lt;br /&gt;
&lt;br /&gt;
== Output ==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
DNS example&lt;br /&gt;
&lt;br /&gt;
Host was: egnite.de&lt;br /&gt;
        IP: 88.198.132.201&lt;br /&gt;
        MX: 0.10.192.12&lt;br /&gt;
Host was: google.com&lt;br /&gt;
        IP: 74.125.127.100&lt;br /&gt;
        MX: 0.0.0.0&lt;br /&gt;
Host was: ethernut.de&lt;br /&gt;
        IP: 88.198.132.203&lt;br /&gt;
        MX: 0.10.192.12&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* More [[Nut/OS Examples]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
{{Languages}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Code Snippets]]&lt;/div&gt;</summary>
		<author><name>legacy&gt;Joshua</name></author>
	</entry>
</feed>