Thread: getaddrinfo : who is right?

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    53

    getaddrinfo : who is right?

    My man says that both
    The gethostbyname*() and gethostbyaddr*() functions are obsolete.
    So i decided to change them to getaddrinfo. So i tried to run


    Code:
    l_error=getaddrinfo(host, NULL, NULL, ServerSocketAddresses);
    where host="173.194.35.145" and
    static struct addrinfo **ServerSocketAddresses=NULL;
    Program crushes:
    Breakpoint 1, set_server_info (host=0x7fffffffe38e "173.194.35.145", port=80, addr_len=0x7fffffffdf1c) at socket_ip_client.c:38
    38 l_error=getaddrinfo(host, NULL, NULL, ServerSocketAddresses);
    (gdb) p host
    $1 = 0x7fffffffe38e "173.194.35.145"
    (gdb) n

    Program received signal SIGSEGV, Segmentation fault.
    0x00007ffff7ae9d1a in getaddrinfo () from /lib/x86_64-linux-gnu/libc.so.6
    From documentation it seems that every parameter under rule and memory for ServerSocketAddresses supposed to be allocated inside call (that's why we have freeaddrinfo for later...)
    Am i doing smth wrong or glibc does so?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    getaddrinfo() will point a struct addrinfo* to the head of a linked list of results. As such, it needs a pointer to that pointer so it can modify it (in exactly the same way that you must pass an int* to a function that is meant to modify the int).

    What you're doing is passing a null pointer instead of the address of a struct addrinfo*. What you want is something like:
    Code:
    struct addrinfo *res;
    getaddrinfo(host, NULL, NULL, &res);
    Now res points to the head of the list (or is NULL if the lookup was unsuccessful).

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    53
    yep, thanks, found it too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ip address using getaddrinfo()
    By ueg1990 in forum C Programming
    Replies: 4
    Last Post: 07-07-2012, 06:54 PM
  2. is getaddrinfo really necessary?
    By sunjayc99 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-28-2008, 03:14 PM
  3. Why create a wrapper fo getaddrinfo()
    By Overworked_PhD in forum Linux Programming
    Replies: 2
    Last Post: 11-10-2007, 01:37 AM
  4. getaddrinfo()
    By Cactus_Hugger in forum Windows Programming
    Replies: 1
    Last Post: 07-24-2006, 02:41 AM
  5. using gethostbyname , getaddrinfo & WSAAsyncGetHostByName
    By hanhao in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-04-2004, 01:07 AM