]> git.sesse.net Git - vlc/blobdiff - src/network/getaddrinfo.c
Removes trailing spaces. Removes tabs.
[vlc] / src / network / getaddrinfo.c
index 28de0bbdfec8b3305e6d7ae849b11d9f2b202500..d5514d2d6c4cc0e3cbc78faf87cb070a6317440d 100644 (file)
 #   define AF_UNSPEC   0
 #endif
 
-#define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
-                  NI_DGRAM)
-#define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
-
 
 #ifndef HAVE_GAI_STRERROR
 static struct
@@ -102,37 +98,9 @@ const char *vlc_gai_strerror (int errnum)
 }
 #endif
 
-#ifndef WIN32
-#if !(defined (HAVE_GETNAMEINFO) && defined (HAVE_GETADDRINFO))
-/*
- * Converts the current herrno error value into an EAI_* error code.
- * That error code is normally returned by getnameinfo() or getaddrinfo().
- */
-static int
-gai_error_from_herrno (void)
-{
-    switch (h_errno)
-    {
-        case HOST_NOT_FOUND:
-            return EAI_NONAME;
-
-        case NO_ADDRESS:
-# if (NO_ADDRESS != NO_DATA)
-        case NO_DATA:
-# endif
-            return EAI_NODATA;
-
-        case NO_RECOVERY:
-            return EAI_FAIL;
-
-        case TRY_AGAIN:
-            return EAI_AGAIN;
-    }
-    return EAI_SYSTEM;
-}
-#endif /* if !(HAVE_GETNAMEINFO && HAVE_GETADDRINFO) */
-
 #ifndef HAVE_GETNAMEINFO
+#define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
+                  NI_DGRAM)
 /*
  * getnameinfo() non-thread-safe IPv4-only implementation,
  * Address-family-independant address to hostname translation
@@ -145,9 +113,15 @@ gai_error_from_herrno (void)
  * GNU C library 2.0.x is known to lack this function, even though it defines
  * getaddrinfo().
  */
+#ifdef WIN32
+static int WSAAPI
+getnameinfo (const struct sockaddr *sa, socklen_t salen,
+             char *host, DWORD hostlen, char *serv, DWORD servlen, int flags)
+#else
 static int
 getnameinfo (const struct sockaddr *sa, socklen_t salen,
              char *host, int hostlen, char *serv, int servlen, int flags)
+#endif
 {
     if (((size_t)salen < sizeof (struct sockaddr_in))
      || (sa->sa_family != AF_INET))
@@ -174,14 +148,14 @@ getnameinfo (const struct sockaddr *sa, socklen_t salen,
 
             if (snprintf (host, hostlen, "%u.%u.%u.%u", ipv4 >> 24,
                           (ipv4 >> 16) & 0xff, (ipv4 >> 8) & 0xff,
-                          ipv4 & 0xff) >= hostlen)
+                          ipv4 & 0xff) >= (int)hostlen)
                 return EAI_OVERFLOW;
         }
 
         if (serv != NULL)
         {
             if (snprintf (serv, servlen, "%u",
-                          (unsigned int)ntohs (addr->sin_port)) >= servlen)
+                          (unsigned int)ntohs (addr->sin_port)) >= (int)servlen)
                 return EAI_OVERFLOW;
         }
     }
@@ -191,10 +165,42 @@ getnameinfo (const struct sockaddr *sa, socklen_t salen,
 #endif /* if !HAVE_GETNAMEINFO */
 
 #ifndef HAVE_GETADDRINFO
+#define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
+/*
+ * Converts the current herrno error value into an EAI_* error code.
+ * That error code is normally returned by getnameinfo() or getaddrinfo().
+ */
+static int
+gai_error_from_herrno (void)
+{
+    switch (h_errno)
+    {
+        case HOST_NOT_FOUND:
+            return EAI_NONAME;
+
+        case NO_ADDRESS:
+# if (NO_ADDRESS != NO_DATA)
+        case NO_DATA:
+# endif
+            return EAI_NODATA;
+
+        case NO_RECOVERY:
+            return EAI_FAIL;
+
+        case TRY_AGAIN:
+            return EAI_AGAIN;
+    }
+    return EAI_SYSTEM;
+}
+
 /*
  * This functions must be used to free the memory allocated by getaddrinfo().
  */
+#ifdef WIN32
+static void WSAAPI freeaddrinfo (struct addrinfo *res)
+#else
 static void freeaddrinfo (struct addrinfo *res)
+#endif
 {
     if (res != NULL)
     {
@@ -278,9 +284,15 @@ makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
  *
  * Only UDP and TCP over IPv4 are supported here.
  */
+#ifdef WIN32
+static int WSAAPI
+getaddrinfo (const char *node, const char *service,
+             const struct addrinfo *hints, struct addrinfo **res)
+#else
 static int
 getaddrinfo (const char *node, const char *service,
              const struct addrinfo *hints, struct addrinfo **res)
+#endif
 {
     struct addrinfo *info;
     u_long ip;
@@ -368,7 +380,7 @@ getaddrinfo (const char *node, const char *service,
             entry = gethostbyname (node);
 
         if (entry == NULL)
-            return EAI_NONAME;
+            return gai_error_from_herrno ();
 
         if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
             return EAI_FAMILY;
@@ -386,38 +398,14 @@ getaddrinfo (const char *node, const char *service,
         port = 0;
     else
     {
-        long d;
+        unsigned long d;
         char *end;
 
         d = strtoul (service, &end, 0);
-        if (end[0] /* service is not a number */
-         || (d > 65535))
-        {
-            struct servent *entry;
-            const char *protoname;
-
-            switch (protocol)
-            {
-                case IPPROTO_TCP:
-                    protoname = "tcp";
-                    break;
-
-                case IPPROTO_UDP:
-                    protoname = "udp";
-                    break;
-
-                default:
-                    protoname = NULL;
-            }
-
-            entry = getservbyname (service, protoname);
-            if (entry == NULL)
-                return EAI_SERVICE;
+        if (end[0] || (d > 65535u))
+            return EAI_SERVICE;
 
-            port = entry->s_port;
-        }
-        else
-            port = htons ((u_short)d);
+        port = htons ((u_short)d);
     }
 
     /* building results... */
@@ -450,46 +438,42 @@ getaddrinfo (const char *node, const char *service,
     return 0;
 }
 #endif /* if !HAVE_GETADDRINFO */
-#endif
 
 #if defined( WIN32 ) && !defined( UNDER_CE )
     /*
      * Here is the kind of kludge you need to keep binary compatibility among
      * varying OS versions...
      */
-typedef int (CALLBACK * GETNAMEINFO) ( const struct sockaddr*, socklen_t,
-                                           char*, DWORD, char*, DWORD, int );
-typedef int (CALLBACK * GETADDRINFO) (const char *, const char *,
-                                          const struct addrinfo *,
-                                          struct addrinfo **);
+typedef int (WSAAPI * GETNAMEINFO) ( const struct sockaddr FAR *, socklen_t,
+                                           char FAR *, DWORD, char FAR *, DWORD, int );
+typedef int (WSAAPI * GETADDRINFO) (const char FAR *, const char FAR *,
+                                          const struct addrinfo FAR *,
+                                          struct addrinfo FAR * FAR *);
 
-typedef void (CALLBACK * FREEADDRINFO) ( struct addrinfo * );
+typedef void (WSAAPI * FREEADDRINFO) ( struct addrinfo FAR * );
 
-
-static WINAPI int _ws2_getnameinfo_bind( const struct sockaddr *sa, socklen_t salen,
-               char *host, DWORD hostlen, char *serv, DWORD servlen, int flags );
-
-static WINAPI int _ws2_getaddrinfo_bind(const char *node, const char *service,
-               const struct addrinfo *hints, struct addrinfo **res);
-
-static WINAPI void _ws2_freeaddrinfo_bind( struct addrinfo *infos );
+static int WSAAPI _ws2_getnameinfo_bind ( const struct sockaddr FAR *, socklen_t,
+                                           char FAR *, DWORD, char FAR *, DWORD, int );
+static int WSAAPI _ws2_getaddrinfo_bind (const char FAR *, const char FAR *,
+                                          const struct addrinfo FAR *,
+                                          struct addrinfo FAR * FAR *);
 
 static GETNAMEINFO ws2_getnameinfo = _ws2_getnameinfo_bind;
 static GETADDRINFO ws2_getaddrinfo = _ws2_getaddrinfo_bind;
 static FREEADDRINFO ws2_freeaddrinfo;
 
-static FARPROC ws2_find_api (const char *name)
+static FARPROC ws2_find_api (LPCTSTR name)
 {
     FARPROC f = NULL;
 
-    HMODULE m = GetModuleHandle ("WS2_32");
+    HMODULE m = GetModuleHandle (TEXT("WS2_32"));
     if (m != NULL)
         f = GetProcAddress (m, name);
 
     if (f == NULL)
     {
         /* Windows 2K IPv6 preview */
-        m = LoadLibrary ("WSHIP6");
+        m = LoadLibrary (TEXT("WSHIP6"));
         if (m != NULL)
             f = GetProcAddress (m, name);
     }
@@ -497,36 +481,53 @@ static FARPROC ws2_find_api (const char *name)
     return f;
 }
 
-static WINAPI int _ws2_getnameinfo_bind( const struct sockaddr *sa, socklen_t salen,
-               char *host, DWORD hostlen, char *serv, DWORD servlen, int flags )
+static WSAAPI int _ws2_getnameinfo_bind( const struct sockaddr FAR * sa, socklen_t salen,
+               char FAR *host, DWORD hostlen, char FAR *serv, DWORD servlen, int flags )
 {
-    GETNAMEINFO entry = (GETNAMEINFO)ws2_find_api ("getnameinfo");
-    if (entry != NULL)
+    GETNAMEINFO entry = (GETNAMEINFO)ws2_find_api (TEXT("getnameinfo"));
+    int result;
+
+    if (entry == NULL)
     {
-        ws2_getnameinfo = entry;
-        return entry (sa, salen, host, hostlen, serv, servlen, flags);
+        /* not found, use replacement API instead */
+    entry = getnameinfo;
+
     }
-    /* return a possible error if API is not found */
-    WSASetLastError (WSAEAFNOSUPPORT);
-    return EAI_FAMILY;
+    /* call API before replacing function pointer to avoid crash */
+    result = entry (sa, salen, host, hostlen, serv, servlen, flags);
+    ws2_getnameinfo = entry;
+    return result;
 }
+#undef getnameinfo
+#define getnameinfo ws2_getnameinfo
 
-static WINAPI int _ws2_getaddrinfo_bind(const char *node, const char *service,
-               const struct addrinfo *hints, struct addrinfo **res)
+static WSAAPI int _ws2_getaddrinfo_bind(const char FAR *node, const char FAR *service,
+               const struct addrinfo FAR *hints, struct addrinfo FAR * FAR *res)
 {
-    GETADDRINFO entry = (GETADDRINFO)ws2_find_api ("getaddrinfo");
-    FREEADDRINFO freentry = (FREEADDRINFO)ws2_find_api ("freeaddrinfo");
+    GETADDRINFO entry;
+    FREEADDRINFO freentry;
+    int result;
+
+    entry = (GETADDRINFO)ws2_find_api (TEXT("getaddrinfo"));
+    freentry = (FREEADDRINFO)ws2_find_api (TEXT("freeaddrinfo"));
 
-    if ((entry != NULL) && (freentry != NULL))
+    if ((entry == NULL) ||  (freentry == NULL))
     {
-        ws2_freeaddrinfo = freentry;
-        ws2_getaddrinfo = entry;
-        return entry (node, service, hints, res);
+        /* not found, use replacement API instead */
+    entry = getaddrinfo;
+    freentry = freeaddrinfo;
     }
-    /* return a possible error if API is not found */
-    WSASetLastError (WSAHOST_NOT_FOUND);
-    return EAI_NONAME;
+    /* call API before replacing function pointer to avoid crash */
+    result = entry (node, service, hints, res);
+    ws2_freeaddrinfo = freentry;
+    ws2_getaddrinfo = entry;
+    return result;
 }
+#undef getaddrinfo
+#undef freeaddrinfo
+#define getaddrinfo ws2_getaddrinfo
+#define freeaddrinfo ws2_freeaddrinfo
+#define HAVE_GETADDRINFO
 #endif
 
 
@@ -549,11 +550,7 @@ int vlc_getnameinfo( const struct sockaddr *sa, int salen,
         i_servlen = 0;
     }
 
-#ifndef WIN32
     i_val = getnameinfo(sa, salen, host, hostlen, psz_serv, i_servlen, flags);
-#else
-    i_val = ws2_getnameinfo (sa, salen, host, hostlen, psz_serv, i_servlen, flags);
-#endif
 
     if( portnum != NULL )
         *portnum = atoi( psz_serv );
@@ -583,10 +580,41 @@ int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
     snprintf( psz_service, 6, "%d", i_port );
 
     /* Check if we have to force ipv4 or ipv6 */
-    if( p_hints == NULL )
-        memset( &hints, 0, sizeof( hints ) );
-    else
-        memcpy( &hints, p_hints, sizeof( hints ) );
+    memset (&hints, 0, sizeof (hints));
+    if (p_hints != NULL)
+    {
+        const int safe_flags =
+            AI_PASSIVE |
+            AI_CANONNAME |
+            AI_NUMERICHOST |
+#ifdef AI_NUMERICSERV
+            AI_NUMERICSERV |
+#endif
+#ifdef AI_ALL
+            AI_ALL |
+#endif
+#ifdef AI_ADDRCONFIG
+            AI_ADDRCONFIG |
+#endif
+#ifdef AI_V4MAPPED
+            AI_V4MAPPED |
+#endif
+            0;
+
+        hints.ai_family = p_hints->ai_family;
+        hints.ai_socktype = p_hints->ai_socktype;
+        hints.ai_protocol = p_hints->ai_protocol;
+        /* Unfortunately, some flags chang the layout of struct addrinfo, so
+         * they cannot be copied blindly from p_hints to &hints. Therefore, we
+         * only copy flags that we know for sure are "safe".
+         */
+        hints.ai_flags = p_hints->ai_flags & safe_flags;
+    }
+
+#ifdef AI_NUMERICSERV
+    /* We only ever use port *numbers* */
+    hints.ai_flags |= AI_NUMERICSERV;
+#endif
 
     if( hints.ai_family == AF_UNSPEC )
     {
@@ -642,7 +670,7 @@ int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
     {
         hints.ai_flags |= AI_NUMERICHOST;
 
-        if (ws2_getaddrinfo (psz_node, psz_service, &hints, res) == 0)
+        if (getaddrinfo (psz_node, psz_service, &hints, res) == 0)
             return 0;
 
         hints.ai_flags &= ~AI_NUMERICHOST;
@@ -668,8 +696,6 @@ int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
     }
 # endif
     return getaddrinfo (psz_node, psz_service, &hints, res);
-#elif defined (WIN32)
-    return ws2_getaddrinfo (psz_node, psz_service, &hints, res);
 #else
     int ret;
     vlc_value_t lock;
@@ -678,7 +704,7 @@ int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
     var_Get (p_this->p_libvlc, "getaddrinfo_mutex", &lock);
     vlc_mutex_lock (lock.p_address);
 
-    int ret = getaddrinfo (psz_node, psz_service, &hints, res);
+    ret = getaddrinfo (psz_node, psz_service, &hints, res);
     vlc_mutex_unlock (lock.p_address);
     return ret;
 #endif
@@ -687,9 +713,5 @@ int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
 
 void vlc_freeaddrinfo( struct addrinfo *infos )
 {
-#ifndef WIN32
     freeaddrinfo (infos);
-#else
-    ws2_freeaddrinfo (infos);
-#endif
 }