]> git.sesse.net Git - vlc/blobdiff - src/network/getaddrinfo.c
Revert "src: fix crash in getaddrinfo due to bugs in the OSX runtime"
[vlc] / src / network / getaddrinfo.c
index f63b64aa0e6a44a1048a59017bbbe4b5e0d38544..16dd68e3a3e21dc7aaf2c96dba675b112a29ea19 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * getaddrinfo.c: getaddrinfo/getnameinfo replacement functions
  *****************************************************************************
- * Copyright (C) 2005 the VideoLAN team
+ * Copyright (C) 2005 VLC authors and VideoLAN
  * Copyright (C) 2002-2007 Rémi Denis-Courmont
  * $Id$
  *
  * Author: Rémi Denis-Courmont <rem # videolan.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -27,7 +27,6 @@
 #endif
 
 #include <vlc_common.h>
-#include <vlc_charset.h>
 
 #include <stddef.h> /* size_t */
 #include <string.h> /* strlen(), memcpy(), memset(), strchr() */
@@ -72,7 +71,6 @@ int vlc_getnameinfo( const struct sockaddr *sa, int salen,
 /**
  * Resolves a host name to a list of socket addresses (like getaddrinfo()).
  *
- * @param p_this a VLC object
  * @param node host name to resolve (encoded as UTF-8), or NULL
  * @param i_port port number for the socket addresses
  * @param p_hints parameters (see getaddrinfo() manual page)
@@ -81,207 +79,47 @@ int vlc_getnameinfo( const struct sockaddr *sa, int salen,
  * On failure, *res is undefined. On success, it must be freed with
  * freeaddrinfo().
  */
-int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
-                     int i_port, const struct addrinfo *p_hints,
-                     struct addrinfo **res )
+int vlc_getaddrinfo (const char *node, unsigned port,
+                     const struct addrinfo *hints, struct addrinfo **res)
 {
-    struct addrinfo hints;
-    char psz_buf[NI_MAXHOST], psz_service[6];
+    char hostbuf[NI_MAXHOST], portbuf[6], *servname;
 
     /*
      * In VLC, we always use port number as integer rather than strings
      * for historical reasons (and portability).
      */
-    if( ( i_port > 65535 ) || ( i_port < 0 ) )
+    if (port != 0)
     {
-        msg_Err( p_this, "invalid port number %d specified", i_port );
-        return EAI_SERVICE;
-    }
-
-    /* cannot overflow */
-    snprintf( psz_service, 6, "%d", i_port );
-
-    /* Check if we have to force ipv4 or ipv6 */
-    memset (&hints, 0, sizeof (hints));
-    if (p_hints != NULL)
-    {
-        const int safe_flags =
-            AI_PASSIVE |
-            AI_CANONNAME |
-            AI_NUMERICHOST |
-            AI_NUMERICSERV |
-#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;
-    }
-
-    /* We only ever use port *numbers* */
-    hints.ai_flags |= AI_NUMERICSERV;
-
-    if( hints.ai_family == AF_UNSPEC )
-    {
-#ifdef AF_INET6
-        if (var_InheritBool (p_this, "ipv6"))
-            hints.ai_family = AF_INET6;
-        else
-#endif
-        if (var_InheritBool (p_this, "ipv4"))
-            hints.ai_family = AF_INET;
+        if (port > 65535)
+            return EAI_SERVICE;
+        /* cannot overflow */
+        snprintf (portbuf, sizeof (portbuf), "%u", port);
+        servname = portbuf;
     }
+    else
+        servname = NULL;
 
     /*
      * VLC extensions :
-     * - accept "" as NULL
-     * - ignore square brackets
+     * - accept the empty string as unspecified host (i.e. NULL)
+     * - ignore square brackets (for IPv6 numerals)
      */
     if (node != NULL)
     {
         if (node[0] == '[')
         {
             size_t len = strlen (node + 1);
-            if ((len <= sizeof (psz_buf)) && (node[len] == ']'))
+            if ((len <= sizeof (hostbuf)) && (node[len] == ']'))
             {
                 assert (len > 0);
-                memcpy (psz_buf, node + 1, len - 1);
-                psz_buf[len - 1] = '\0';
-                node = psz_buf;
+                memcpy (hostbuf, node + 1, len - 1);
+                hostbuf[len - 1] = '\0';
+                node = hostbuf;
             }
         }
         if (node[0] == '\0')
             node = NULL;
     }
 
-    int ret;
-    node = ToLocale (node);
-#ifdef WIN32
-    /*
-     * Winsock tries to resolve numerical IPv4 addresses as AAAA
-     * and IPv6 addresses as A... There comes the bug-to-bug fix.
-     */
-    if ((hints.ai_flags & AI_NUMERICHOST) == 0)
-    {
-        hints.ai_flags |= AI_NUMERICHOST;
-        ret = getaddrinfo (node, psz_service, &hints, res);
-        if (ret == 0)
-            goto out;
-        hints.ai_flags &= ~AI_NUMERICHOST;
-    }
-#endif
-#ifdef AI_IDN
-    /* Run-time I18n Domain Names support */
-    hints.ai_flags |= AI_IDN;
-    ret = getaddrinfo (node, psz_service, &hints, res);
-    if (ret != EAI_BADFLAGS)
-        goto out;
-    /* IDN not available: disable and retry without it */
-    hints.ai_flags &= ~AI_IDN;
-#endif
-    ret = getaddrinfo (node, psz_service, &hints, res);
-
-#if defined(AI_IDN) || defined(WIN32)
-out:
-#endif
-    LocaleFree (node);
-    return ret;
+    return getaddrinfo (node, servname, hints, res);
 }
-
-
-/**
- * inet_pton() replacement
- */
-int vlc_inet_pton (int af, const char *src, void *dst)
-{
-#ifndef HAVE_INET_PTON
-    /* Windows Vista has inet_pton(), but not XP. */
-    /* We have a pretty good example of abstraction inversion here... */
-    struct addrinfo hints = {
-        .ai_family = af,
-        .ai_socktype = SOCK_DGRAM, /* make sure we have... */
-        .ai_protocol = IPPROTO_UDP, /* ...only one response */
-        .ai_flags = AI_NUMERICHOST,
-    }, *res;
-
-    if (getaddrinfo (src, NULL, &hints, &res))
-        return 0;
-
-    const void *data;
-    size_t len;
-
-    switch (af)
-    {
-        case AF_INET:
-            data = &((const struct sockaddr_in *)res->ai_addr)->sin_addr;
-            len = sizeof (struct in_addr);
-            break;
-#ifdef AF_INET6
-        case AF_INET6:
-            data = &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
-            len = sizeof (struct in6_addr);
-            break;
-#endif
-        default:
-            freeaddrinfo (res);
-            return -1;
-    }
-    memcpy (dst, data, len);
-    freeaddrinfo (res);
-    return 1;
-#else /* HAVE_INET_PTON */
-    return inet_pton( af, src, dst );
-#endif /* HAVE_INET_PTON */
-}
-
-/**
- * inet_ntop() replacement
- */
-const char *vlc_inet_ntop (int af, const void *src, char *dst, socklen_t cnt)
-{
-#ifndef HAVE_INET_NTOP
-    int ret = EAI_FAMILY;
-
-    switch (af)
-    {
-#ifdef AF_INET6
-        case AF_INET6:
-            {
-                struct sockaddr_in6 addr;
-                memset (&addr, 0, sizeof(addr));
-                addr.sin6_family = AF_INET6;
-                addr.sin6_addr = *(struct in6_addr *)src;
-                ret = getnameinfo ((struct sockaddr *)&addr, sizeof (addr),
-                                   dst, cnt, NULL, 0, NI_NUMERICHOST);
-            }
-
-#endif
-        case AF_INET:
-            {
-                struct sockaddr_in addr;
-                memset(&addr, 0, sizeof(addr));
-                addr.sin_family = AF_INET;
-                addr.sin_addr = *(struct in_addr *)src;
-                ret = getnameinfo ((struct sockaddr *)&addr, sizeof (addr),
-                                   dst, cnt, NULL, 0, NI_NUMERICHOST);
-            }
-    }
-    return (ret == 0) ? dst : NULL;
-#else /* HAVE_INET_NTOP */
-    return inet_ntop( af, src, dst, cnt );
-#endif /* HAVE_INET_NTOP */
-}
-