Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

Assigning hostname and IP

Hi,

I'm attempting to use the gethostbyname() function to access the structure of type hostent (he) so I can print out the Hostname (he->h_name) and assigned IP address (he->h_addr_list). However, gethostbyname() returns a NULL indicating there is nothing in the returned "he" structure. Also, when I use the gethostname() function, the expected string "Nios2" hostname is returned.

char hName;
struct hostent *he;
if (!(gethostname(hName, MAX_FILENAMELENGTH - 1)))
  {
  if ((he = gethostbyname(hName)) != NULL)
      {
      printf("\tHostname = %s.  IP Address = %d.%d.%d.%d\n\n",
    he->h_name, he->h_addr_list, he->h_addr_list,
    he->h_addr_list, he->h_addr_list);
      }
  }

Is there a setting I'm not aware of that needs to be initialized before the gethostbyname() function will return a non-NULL value? Does the hostname string (hName) need to be NULL (\0) terminated?

4 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi bjskill,

    Where is the address bound to the name? Do you have a local dns, nis, yp? Is it defined

    in /etc/hosts?

    > Is there a setting I'm not aware of that needs to be initialized before the gethostbyname() function

    > will return a non-NULL value?

    It needs to be able to find the IP address associated with the name "Nios2" as suggested above.

    > Does the hostname string (hName) need to be NULL () terminated?

    Yes.

    Regards,

    --Scott
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi smcnutt,

    I not using dns, nis, or yp since I have my Nios II eval. board directly connected to a PC over a Ethernet crossover cable and I modified /etc/rc so that the IP is set to 192.168.0.254 using the following command:

    bin/ifconfig/eth0 192.168.0.254  netmask 255.255.255.0

    I currently don't have a /etc/hosts file. If I create the /etc/hosts file and add the following command, will it bind the IP address to the already defined hostname ("Nios2") so that the gethostbyname() function will return the proper values?

    192.168.0.254    Nios2    Nios2

    I did check and the hostname ("Nios2") is NULL terminated.

    Thanks for your help.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi bjskill,

    > If I create the /etc/hosts file and add the following command, will it bind the IP address to the

    > already defined hostname ("Nios2") so that the gethostbyname() function will return the proper values?

    Yes, this should work.

    Regards,

    --Scott
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi smcnutt,

    Once I added the hosts file that was described in my last message, the gethostbyname() function returns with the proper hostname and IP address values.

    However, I was unable to create the hosts file using the command line while within the Nios II SDK shell. I tried a couple of methods that didn't seem to be supported under the minimal uClinux filesystem I was using:

    echo "192.168.0.254 Nios2" > hosts
    cat < > hosts
    192.168.0.254 Nios2
    <Ctrl-D>

    What methods are there available under the uClinux filesystem to generate a small text file from the command line? Do I need to add something to the kernel and/or filesystem to provide such support?

    Does busybox provide everything I would need? If so, what is the footprint of busybox?

    Thanks for your help.