Your third TCP/IP Library application

What would be great is if your evaluation board had a name, rather than an address, so we can simply ping the name of the board. Well, there is a way, and that is to register a name using DHCP.

Select and build the project

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

Double-click the file example_ping_by_name.c in the Source Files folder and it will open in the code editor. In main you'll find a call to ctl_net_set_host_name, before the network is brought up, to set the host name of the evaluation board:

// Set our host name.

This registers the name of the host with the DHCP server and means that you can ping the board using a friendly name, whatever the assigned IP address is.

See if it works

Power up the board and run the code. In the CrossWorks Debug Terminal you should see the same as before:

DHCP: awaiting IP address assignment
DHCP: awaiting IP address assignment
DHCP: awaiting IP address assignment
DHCP: IP address is and subnet mask is
DHCP: IP address is and subnet mask is
DHCP: IP address is and subnet mask is

Now you can ping the device by its assigned name, crossworks:

> ping crossworks

Pinging [] with 32 bytes of data:
Reply from bytes=32 time<1ms TTL=64
Reply from bytes=32 time<1ms TTL=64
Reply from bytes=32 time<1ms TTL=64
Reply from bytes=32 time<1ms TTL=64

Ping statistics for
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms


Notice that the full name of the host is This is because the LAN that the board is connected to has a domain name suffix. You might have noticed this in the output from ipconfig in the first example:

> ipconfig
Windows IP Configuration

Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::9c2d:e057:8641:2281%10
   IPv4 Address. . . . . . . . . . . :
   Subnet Mask . . . . . . . . . . . :
   Default Gateway . . . . . . . . . :

> _

The full host name is the name that we assigned to the node, crossworks, with the suffix assigned by the network,

Job done!

You now have a functioning Ethernet connection between your PC and your target board, using a dynamically-assigned IP address, and with a friendly name to contact the board.

The code
// Set up network using a DHCP-assigned IP address.

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

// Assign a fixed MAC address to the NIC.  Normally this will be blown into
// OTP or some other nonvolatile medium when the device is personalized as
// part of production.
#define FIXED_MAC_ADDRESS        "bc-28-d6-ff-ff-ff"

// Network task thread priority
#define NET_TASK_PRIORITY        200

// Network interface,
static CTL_NET_INTERFACE_t nic;

static void
  // Assign a fixed MAC address to the NIC.
  example_check_status(ctl_net_scan_mac_addr(&nic.mac.mac_addr, FIXED_MAC_ADDRESS));

  // Initialize MAC.

  // Bring up network task and use DHCP to assign an IP address.
  example_check_status(ctl_net_init(NET_TASK_PRIORITY, 0));

  // Bring up UDP and ICMP: DHCP requires UDP, and ICMP will respond to pings.

  // Start DHCP to assign us an IP address.

  char dot_ipaddr[16], dot_netmask[16];

  // Initialize platform.

  // Initialize NIC for this platform.

  // Set our host name.

  // Start network.

  // Idle away; when we're configured, dump our network.
  for (;;)
      // See if we've acquired an IP address yet...
      if (ctl_net_get_ip_address())
          // Dump message inviting a ping.
          printf("DHCP: IP address is %s and subnet mask is %s\n",
                 ctl_ip_sprint_addr(dot_ipaddr, ctl_net_get_ip_address()),
                 ctl_ip_sprint_addr(dot_netmask, ctl_net_get_subnet_mask()));
          // Can't ping me yet.
          printf("DHCP: awaiting IP address assignment\n");

      // Don't be too enthusiastic with messages.
See Also