Thread: gethostbyaddr is slow

  1. #1
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164

    gethostbyaddr is slow

    gethostbyaddr is very slow in returning when the host can't be found. (i.e. when it returns NULL)

    Is there anyway to speed this up? Is getnameinfo any quicker in this respect?

    Thanks.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well doesn't it need to check with a DNS server (check what happens when you run ethereal).
    Since not finding something is bound to take the longest (maybe there are timeouts).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    For instance, the traceroute command resolves IP addresses if you don't specify the -d option.

    If it had to wait for gethostbyaddr to return for addresses which didn't have a hostname, the program would be very slow.


    It doesn't however and flicks through the nodes without a hostname just as fast as the ones which do.

    Would this because they set some kind of timeout facility?
    What would be the most efficient way of doing this?

    Thanks.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I think the slow response could be caused by the netbios lookup that gethostbyaddr performs. This would involve trying to contact the non-existant host and a lengthy timeout. A purely DNS lookup will only contact the DNS server. getnameinfo, unlike gethostbyaddr, will not perform a netbios lookup, so should be faster. A quick check with notepad shows that the Windows XP version of tracert does use getnameinfo. getnameinfo is only available on Windows XP+.
    Code:
    char host[NI_MAXHOST];
    char serv[NI_MAXSERV];
    getnameinfo(&addr, sizeof(addr), host, sizeof(host), serv, sizeof(serv), 0);
    As a matter of interest, let us know if it turns out to be any quicker.

  5. #5
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by eth0
    For instance, the traceroute command resolves IP addresses if you don't specify the -d option.

    If it had to wait for gethostbyaddr to return for addresses which didn't have a hostname, the program would be very slow.


    It doesn't however and flicks through the nodes without a hostname just as fast as the ones which do.

    I don't think you understand what you're saying... nor how trace route works.

    you see, the network works with ip, not hostnames!
    the idea of hostnames was created because they are easier to remember than ip addresses.

    now a trace route all it does is display the routers on the route to the target, by incrementing the TTL feild of the icmp packet (you should google it)

    what i am trying to say, trace route does not work with hostnames, but resolves the ip to hostname for YOU...
    the -d option tells the tracert program to simply do it's job without the extra "bulls.hit" , like resoloving ip to hostname, which obvisuly takes time.
    you should also lookup how dns is resolved, this will explain to you why does it take sometime so long for a dns name / ip to be resolved.

    plus, i'm agains resetting the timeout value of the dns lookup.
    because you might timeout while the answer is on it's way to you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  6. #6
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Quote Originally Posted by Devil Panther
    I don't think you understand what you're saying... nor how trace route works.
    I know 100% how traceroutes work, I'm a CCNP network engineer.

    As traceroute works with IP addresses, in order for me to display the hostname, I need to do a reverse lookup.
    My reason for asking this question was, if the IP address in question doesn't have a hostname, it takes gethostbyaddr around 10 seconds to return. This is unacceptable for my program as it brings it to a virtual standstill.


    anonytmouse, thanks for your response, the netbios lookup would make sense. I will use getnameinfo instead. I'll reply in this thread as to the results.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  7. #7
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Quote Originally Posted by eth0
    I'll reply in this thread as to the results.
    getnameinfo returns immedietley, and a packet capture shows identical behaviour in both the MS tracert and my rewrite.

    Thanks.
    Last edited by eth0; 05-16-2005 at 03:27 PM.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by eth0
    I know 100% how traceroutes work, I'm a CCNP network engineer.
    You may understand the protocol implementation (how it uses ICMP or UDP), but that doesn't tell you JACK about how it was programed.

    The point he was making, is if you tell the traceroute not to look up the domain name, it simple skips that, where as if you request the domain name to be looked up, it will request that info via DNS, and may take a while, causing a lockup or stutter effect.
    One way you might be able to deal with this, is some how break the lookup into a seperate thread, and simply use the IP untill the name is either found, or deemed unknkown, that way it doesn't interupt with the normal flow, and cause a stuttering or lockup effect (so to speak). Their are probably other ways to do it as well.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slow startup
    By Desolation in forum Tech Board
    Replies: 38
    Last Post: 12-27-2008, 07:54 AM
  2. slow typing ad highlighting
    By axon in forum Tech Board
    Replies: 10
    Last Post: 01-13-2004, 08:27 PM
  3. slow game
    By lambs4 in forum Game Programming
    Replies: 2
    Last Post: 08-21-2003, 02:08 PM
  4. Solutions for slow performance?
    By Hunter2 in forum Game Programming
    Replies: 52
    Last Post: 08-24-2002, 10:04 AM
  5. slow load of Cprog.com
    By Generator in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-13-2001, 06:31 AM