Browse code

Use getaddrinfo(), if available, in resolve_host(). Patch by Martin Storsjö <$firstname()$firstname,st>.

Originally committed as revision 21143 to svn://svn.ffmpeg.org/ffmpeg/trunk

Ronald S. Bultje authored on 2010/01/12 02:14:16
Showing 1 changed files
... ...
@@ -63,13 +63,34 @@ int inet_aton (const char * str, struct in_addr * add)
63 63
 /* resolve host with also IP address parsing */
64 64
 int resolve_host(struct in_addr *sin_addr, const char *hostname)
65 65
 {
66
-    struct hostent *hp;
67 66
 
68 67
     if (!inet_aton(hostname, sin_addr)) {
68
+#if HAVE_GETADDRINFO
69
+        struct addrinfo *ai, *cur;
70
+        struct addrinfo hints;
71
+        memset(&hints, 0, sizeof(hints));
72
+        hints.ai_family = AF_INET;
73
+        if (getaddrinfo(hostname, NULL, &hints, &ai))
74
+            return -1;
75
+        /* getaddrinfo returns a linked list of addrinfo structs.
76
+         * Even if we set ai_family = AF_INET above, make sure
77
+         * that the returned one actually is of the correct type. */
78
+        for (cur = ai; cur; cur = cur->ai_next) {
79
+            if (cur->ai_family == AF_INET) {
80
+                *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
81
+                freeaddrinfo(ai);
82
+                return 0;
83
+            }
84
+        }
85
+        freeaddrinfo(ai);
86
+        return -1;
87
+#else
88
+        struct hostent *hp;
69 89
         hp = gethostbyname(hostname);
70 90
         if (!hp)
71 91
             return -1;
72 92
         memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
93
+#endif
73 94
     }
74 95
     return 0;
75 96
 }