7 years, 3 months ago.

How to set the TCP/IP stack 's hostname property?

Hi,

When the ethernetinterface is brought up with DHCP the router shows

Quote:

ip=192.168.1.10 mac=00-02-F7-F0-55-3E name=lwiplpc

I would like to change the nameto a more friendly name, I 've searched the forum but haven't found any usefull information, I'm using the new offcial libraries.

Can anybody shed some light?

KK

I may not be able to answer your question, but hope you still are able to see this message and help me. Your MAC and Name suggest to me that you have an IP camera made by TUNGSON CCTV SYSTEM . I am in trouble with mine and have lost the PORT # that was changed from the default and I can no longer view the UI or video stream. Would you know how to reset the camera to its original default configuration and post the results? Do you have a link to the instruction manual that came with the camera? Thank you. BTW, I can change the name of any device in my ROUTER Client List that shows the client's intranet IP and MAC addresses just by clicking on the Client Name.

posted by Wesley Boyer 05 Feb 2018

1 Answer

5 years, 10 months ago.

This is an old question, but I didn't spot any answer, and with quite a few appliances on the network, here's what I've done, based on EthernetInterface rev 45, which I've derived into my own version. Because this change needs to access the statically defined netif structure, it is not possible to set the node name without modifying the library file.

EthernetInterface.h

  /** setName 
  *
  * Set the network name for this device. Apply this before
  * calling 'connect'.
  *
  * \param myname is the name to assign for this node. 
  *        Only the first 32 characters will be used if the 
  *        name is longer.
  *        Only '0'-'9', 'A'-'Z', 'a'-'z' are accepted,
  *        any others are converted to '-'.
  * \return 0 on success, a negative number on failure.
  */
  static int setName(const char * myname);

EthernetInterface.cpp

static char myName[33];  // holds the name, when setName() is called.
...
int EthernetInterface::setName(const char * myname) {
    int i;
    
    strncpy(myName, myname, 32);
    myName[32] = '\0';  // be sure it is NULL terminated.
    // make the name 'safe'
    for (i=0; i<32 && myName[i]; i++) {
        if (!inRange(myName[i], '0', '9')
        &&  !inRange(myName[i], 'A', 'Z')
        &&  !inRange(myName[i], 'a', 'z'))
            myName[i] = '-';
    }
    netif_set_hostname(&netif, myName);
    return 0;
}

and finally in my startup code:

set the name

    pc.printf("Initializing network interface...\r\n");
    if (0 == eth.init()) {  //Use DHCP
        eth.setName("mydevice");
        do {
            pc.printf("Connecting to network...\r\n");
            if (0 == eth.connect()) {

Now try it:

ping

c:\Tools>ping -n 1 -a 192.168.1.119

Pinging mydevice.mydomain.net [192.168.1.119] with 32 bytes of data:
Reply from 192.168.1.119: bytes=32 time<1ms TTL=255

Per internet standards, I think the hostname can be a lot longer than 32 chars, and probably supports more than just the base alphanumeric character set, but I [arbitrarily] set these limits as more than meeting my needs, and not wasting precious RAM in an embedded system.

Hi David, Again thanks for this nice patch. Could you send a pull request to mbed-official so they can integrate this patch and your other useful patches like get eth status etc into official release? That will really benefit the community.

posted by Zhiyong Li 18 Oct 2015