getaddrinfo() can't resolve hostname

This is a discussion on getaddrinfo() can't resolve hostname within the C Programming forums, part of the General Programming Boards category; Hi, I have recently started network Programming for Linux (though I use windows, I believe it's still compatible via cygwin). ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    4

    getaddrinfo() can't resolve hostname

    Hi,

    I have recently started network Programming for Linux (though I use windows, I believe it's still compatible via cygwin).

    Anyways, I have a small code where I pass in the hostname and the program returns the IP addresses for this host. It works fine with common websites like google, yahoo etc.

    I can also get my own PC's IP addresses by passing in MYPC-PC as the argument. However, if I try to resolve any other computer's name that is connected to the SAME network as my PC, I get the following error:
    "Non-recoverable failure in name resolution."

    I never had this problem in C#, so I don't know how to work my way around this problem. I hope someone can help me out.

    Thanks in advance

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    How are you formatting the name?

    Do you have DNS on your router/switch?
    What are you using as your Primary DNS...

    For it to resove LAN addresses you have to be using the DNS in your router...
    I use it in my software and it's never missed resolving a lan address yet.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    4
    I name it as "Other-pc", that worked on C# so I assumed it wouldn't cause any problems in C either.

    It'd be great if you could give me an example on formatting the name

    Do you mean formatting like "other-pc.some.text.net" ?
    Last edited by chaos5687; 01-12-2011 at 12:10 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by chaos5687 View Post
    I name it as "Other-pc", that worked on C# so I assumed it wouldn't cause any problems in C either.

    It'd be great if you could give me an example on formatting the name

    Do you mean formatting like "other-pc.some.text.net" ?
    Just use the computer's name... My computers are named Volts, Amps, Watts, Joules and Ohms... So the Name parameter below would be simply "Amps" to get it's IP.

    BUT...

    You have to be using DNS from a local router or switch for this to work. Your ISP's DNS (for example) cannot look inside your network.

    Code:
    // resolve server name and port to useable LAN addr
    BOOL GetHostAddr(PTCHAR Name, WORD Port, PSOCKADDR Addr)
      { ADDRINFOT   hints = {0};    // hints for search
        PADDRINFOT  res   = NULL;   // pointer to search result
        // fill in struct
        hints.ai_flags    = AI_PASSIVE;
        hints.ai_family   = AF_INET;
        hints.ai_socktype = SOCK_DGRAM;
        hints.ai_protocol = IPPROTO_UDP;
    
        // get ip address
        if (GetAddrInfo(Name,NULL,&hints,&res))
          return 0;     \\ !0 == failure
        // transfer to local IP struct
        memcpy(Addr,res->ai_addr,sizeof(SOCKADDR));
        ((PSOCKADDR_IN) Addr)->sin_family = AF_INET;
        ((PSOCKADDR_IN) Addr)->sin_port   = htons(Port);
        // release memory 
        FreeAddrInfo(res);
        return 1; }

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    4

    Red face

    Thnx for your help so far. I have just solved the problem. All I had to do was changing hints.ai_family from AF_UNSPEC to AF_INET...

    Do you have any idea why that could be? I thought my code could work with both IPv4 and IPv6 when the ai_family is set to AF_UNSPEC...
    Last edited by chaos5687; 01-12-2011 at 03:17 PM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take a look at the docs... GetAddressInfo(), far as I know is an IPv4 function. IPV6 has it's own separate function... Plus precious few LANS are running ipv6 right now, routers and switches more than a couple of months old can't handle it...

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    As far as i know the ai_family should be either set to AF_INET or AF_INET6. Anything else would undefined. Check the spec. AF_INET would return IPv4 and AF_INET6 would return IPV6 address respectively.
    getaddrinfo(3) - Linux man page

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ssharish2005 View Post
    As far as i know the ai_family should be either set to AF_INET or AF_INET6. Anything else would undefined. Check the spec. AF_INET would return IPv4 and AF_INET6 would return IPV6 address respectively.
    getaddrinfo(3) - Linux man page

    ssharish
    In that case it would have to be called twice... once for each.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    4
    I have just checked the man pages and apparently getaddrinfo() doesn't mind hints.ai_family being set to AF_UNSPEC.In that case it will return both available IPv4 and IPv6 addresses...
    Also when I enter my own computer's name, it returns 4 IP addresses, 3 being IPv6 and 1 being IPv4...

    Well, that's weird, so I will do a bit more research on that and write here if I find anything out

    Thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I get the Hostname in Java?
    By cpjust in forum Tech Board
    Replies: 9
    Last Post: 02-07-2009, 12:13 PM
  2. getting ip by hostname
    By NiSA in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-26-2007, 11:56 PM
  3. obtain hostname or IP
    By afisher in forum Networking/Device Communication
    Replies: 3
    Last Post: 12-01-2004, 07:46 AM
  4. Resolving Hostname Using getaddrinfo(...) :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 05-03-2002, 09:35 PM
  5. Resolving Hostname :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-08-2002, 09:26 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21