getaddrinfo()

This is a discussion on getaddrinfo() within the Windows Programming forums, part of the Platform Specific Boards category; I'm looking for a way to translate hostnames to IPs (or sockaddr_in structs), while being threadsafe. gethostbyname() doesn't meet this, ...

  1. #1
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902

    getaddrinfo()

    I'm looking for a way to translate hostnames to IPs (or sockaddr_in structs), while being threadsafe. gethostbyname() doesn't meet this, as it returns pointers to static data. So, while looking for an alternative, I found getaddrinfo(). My original code compiled fine on both Windows and Linux, suceeded in Linux, but fails in Windows with:
    "The <executable name> file is linked to missing export ws2_32.dll:freeaddrinfo."
    ...and my program never executes, period, AFAIK. The following example also fails with the same error.

    MSDN seems to claim this function is compatible back to Win95, and should be in ws2_32.dll. Looking at ws2_32.dll, there does not appear to be a freeaddrinfo or getaddrinfo.

    What am I doing wrong, or is there some other (better?) way of connecting to say "google.com"?

    Using MinGW v3.4.2 on Win98.

    Example found on web, exhibits above error:
    Code:
    #include <stdio.h>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    
    int main()
    {
    	char *hostname = "localhost";
    	struct addrinfo hints, *res;
    	struct in_addr addr;
    	int err;
    
    	WSADATA data;
    	WSAStartup(MAKEWORD(2,0), &data);
    
    	memset(&hints, 0, sizeof(hints));
    	hints.ai_socktype = SOCK_STREAM;
    	hints.ai_family = AF_INET;
    
    	if((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0)
    	{
    		printf("error %d\n", err);
    		return 1;
    	}
    
    	addr.S_un = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.S_un;
    
    	printf("ip address : %s\n", inet_ntoa(addr));
    
    	freeaddrinfo(res);
    	WSACleanup();
    
    	return 0;
    }
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    As outlined on MSDN, getaddrinfo is only present in ws2_32.dll on Windows XP and above. For earlier platforms, Microsoft provides an implementation of this function in the header file WSPiApi.h. This header file is included with the platform SDK. Because it contains Microsoft copyrighted code, MinGW can not copy it directly and may not provide their own implementation.

    The good news is that on Windows, the static structure returned by gethostbyname is allocated on a per-thread basis:
    Quote Originally Posted by MSDN gethostbyname
    The application must never attempt to modify this structure or to free any of its components. Furthermore, only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other Windows Sockets function calls.
    The implementation of getaddrinfo provided in WSPiApi.h ultimately calls gethostbyname.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is getaddrinfo really necessary?
    By sunjayc99 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-28-2008, 03:14 PM
  2. Why create a wrapper fo getaddrinfo()
    By Overworked_PhD in forum Linux Programming
    Replies: 2
    Last Post: 11-10-2007, 12:37 AM
  3. using gethostbyname , getaddrinfo & WSAAsyncGetHostByName
    By hanhao in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-04-2004, 12:07 AM
  4. Resolving Hostname Using getaddrinfo(...) :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 05-03-2002, 08:35 PM

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