Find the answer to your Linux question:
Results 1 to 5 of 5
So, I've got a custom ppc-8560 running linux 2.6.14, I boot from uBoot, and pass the static IP address from uBoot to the kernel at bootup, along with gateway IP, ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Location
    US
    Posts
    3

    gethostbyname returns NULL - help

    So, I've got a custom ppc-8560 running linux 2.6.14, I boot from uBoot, and pass the static IP address from uBoot to the kernel at bootup, along with gateway IP, machine name. IP works, I can ping the box, ssh in, etc. When I do ifconfig eth0, it also correctly reports IP address. However, this doesn't work:

    gethostname(hostname, 256);
    struct hostent *self = gethostbyname(hostname);

    self is NULL? So if I add a line in the /etc/hosts file I can get it to work, but I don't want to have to do that. At least I think I don't. Eventually, I want these machines to be an assigned address from a DHCP server somewhere else, or allow the user to set the IP & name locally on-screen.

    So how come ifconfig can get the IP address, but nothing else can? I also tried getaddrinfo with all sorts of hints, flags, etc, nothing reported anything useful, just 127.0.0.1.

    This is not your typical PC mind you, no keyboard, console is a serial connection, no boot drive, its uBoot in flash. I am considering figuring out how to override the IP address and eth0 config uBoot sends the kernel, just not sure how yet, probably the /etc/hosts file again.

    Any thoughts? Thanks!

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Maybe I don't understand your question.

    You're trying to use gethostbyname() to get the IP address of your /dev/eth0? I've never used it for that. The normal way is to use ioctl(socket_fd,SIOCGIFCONF,&whatever). That, and SIOCGIFFLAGS.

    google those guys. If you still have difficulty, come back.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Feb 2008
    Location
    US
    Posts
    3
    Thanks. The ioctl stuff is actually what ifconfig itself uses. I actually have always used the gethostbyname and inspected the self->h_addr_list[0] field. I guess creating a temp socket in order get its IP address makes sense.

    I'll give it a try soon, and post back results. Thanks again.

  4. #4
    Just Joined!
    Join Date
    Feb 2008
    Location
    US
    Posts
    3

    Solved!

    the ioctl works great! A 'man netdevice' was helpful with the actual structs involved! Thanks.

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    If you ever get seriously involved with network programming issues on a fulltime basis, you should run (not walk) to your nearest bookseller and get the Addison Wesley book UNIX Network Programming, Volume One, by W. Richard Stevens of happy memory. Curl up on a rainy weekend and drink deep from that book.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...