Finding IP addresses

You've seen how to get your board registered with a name on the LAN. Now it's time to step outside and get onto the Internet. This example is how to resolve the IP address of the Rowley Associates web server,

Select and build the project

In the examples for your board, you'll find a Networking Projects solution, and within that a Resolve Domain Name project. Double-click that project to make it active and press F7 to build.

Hiding some details

Rather than repeat all the boilerplate code that brings up the network and waits for an IP address, that code is moved into example_network_support.c. This example, and all following examples, assume that example_network_support.c is included in the project.

Double-click the file example_network_support.c in the Source Files folder and it will open in the code editor. Because this example needs to resolve a domain name, it initializes the Domain Name System component of the TCP/IP Library:

// Start DNS for domain name lookup.
stat = ctl_dns_init();
if (stat < CTL_NO_ERROR)
  return stat;

Initializing the DNS part of the TCP/IP Library enables you to resolve human-readable domain names, such as into an IP address you can communicate with.

About DNS

In order to resolve a domain name to an IP address, you must have already set the domain name server (or servers) that the TCP/IP Library communicates with to resolve the domain name. If you are using DHCP to configure the TCP/IP Library, which we assume from here on, the domain name servers are automatically set as part of IP address assignment with DHCP.

If you are using a static IP address then you must configure the DNS servers the stack uses by passing in the IP addresses of the primary and (optional) secondary server when initializing the network (see ctl_net_init and CTL_IP_CONFIG_t).

Client code

Double-click the file example_resolve_domain_name.c in the Source Files folder and it will open in the code editor. The example is now much smaller:

// Resolve a domain name.

#include "libnet/ctl_net_api.h"
#include "libplatform/platform.h"
#include "libplatform/platform_network.h"
#include "example_support.h"

  CTL_STATUS_t stat;
  CTL_NET_IPv4_ADDR_t addr;
  char dot_ipaddr[16];

  // Initialize platform.

  // Start networking, wait for an IP address.

  // Dump the primary domain name server, for reference.
  printf("Using DNS server %s\n",
         ctl_ip_sprint_addr(dot_ipaddr, ctl_dns_primary_server_addr()));

  // Try to resolve  Wait a maximum of two
  // seconds for an answer.
  stat = ctl_dns_get_host_by_name("", &addr, 2000);

  // Did this resolve?
  if (stat < CTL_NO_ERROR)
      // No.
      printf("Could not resolve!\n");
      // Yes, print the resolved IP address.
      printf(" resolved to %s\n",
             ctl_ip_sprint_addr(dot_ipaddr, addr));

  // Done.
  return example_finish();

The part of interest is:

stat = ctl_dns_get_host_by_name("", &addr, 2000);

This sends a request to the DNS server to resolve the domain name and deliver the result to addr. The third parameter, 2000, indicates the maximum duration we're prepare to wait for—in this case, two seconds.

See if it works

Power up the board and run the code. In the CrossWorks Debug Terminal you will see something similar to this:

DHCP: awaiting IP address assignment
DHCP: awaiting IP address assignment
DHCP: awaiting IP address assignment
DHCP: IP address is and subnet mask is
Using DNS server resolved to
See Also