]> git.sesse.net Git - vlc/commitdiff
inet_pton() replacement
authorRémi Denis-Courmont <rem@videolan.org>
Thu, 28 Jul 2005 15:31:10 +0000 (15:31 +0000)
committerRémi Denis-Courmont <rem@videolan.org>
Thu, 28 Jul 2005 15:31:10 +0000 (15:31 +0000)
include/network.h
src/misc/net.c
src/stream_output/sap.c

index ce65fb434e6a174f334d25ec644a8bb21125d1fa..67e4409c50f00c63709a233837aab81e4b88638e 100644 (file)
@@ -360,6 +360,12 @@ VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, c
 #define net_GetPeerAddress(a,b,c,d) __net_GetAddress(VLC_OBJECT(a),VLC_TRUE,b,c,d)
 VLC_EXPORT( int, __net_GetAddress, ( vlc_object_t *p_this, vlc_bool_t peer, int fd, char *address, int *port ) );
 
+#if !HAVE_INET_PTON
+/* only in core, so no need for C++ extern "C" */
+int inet_pton(int af, const char *src, void *dst);
+#endif
+
+
 /*****************************************************************************
  * net_StopRecv/Send
  *****************************************************************************
index 1be4ce51d15628929820efc6d056c22fb220ac51..f6a513abcf438d1a334a45c7acea542e36fe38bf 100644 (file)
@@ -1185,3 +1185,62 @@ int __net_GetAddress( vlc_object_t *p_this, vlc_bool_t peer, int fd,
     }
     return 0;
 }
+
+/*****************************************************************************
+ * inet_pton replacement for obsolete and/or crap operating systems
+ *****************************************************************************/
+#ifndef HAVE_INET_PTON
+int inet_pton(int af, const char *src, void *dst)
+{
+# ifdef WIN32
+    /* As we already know, Microsoft always go its own way, so even if they do
+     * provide IPv6, they don't provide the API. */
+    struct sockaddr_storage addr;
+    int len = sizeof( addr );
+
+    /* Damn it, they didn't even put LPCSTR for the firs parameter!!! */
+    char *workaround_for_ill_designed_api = strdup( src );
+    
+    if( !WSAStringToAddress( workaround_for_ill_designed_api, af, NULL,
+                             (LPSOCKADDR)&addr, &len ) )
+    {
+        free( workaround_for_ill_designed_api );
+        return -1;
+    }
+    free( workaround_for_ill_designed_api );
+
+    switch( af )
+    {
+        case AF_INET6:
+            memcpy( dst, &((struct sockaddr_in6 *)&addr)->sin6_addr, 16 );
+            break;
+            
+        case AF_INET:
+            memcpy( dst, &((struct sockaddr_in *)&addr)->sin_addr, 4 );
+            break;
+
+        default:
+            WSASetLastError( WSAEAFNOSUPPORT );
+            return -1;
+    }
+# else
+    /* Assume IPv6 is not supported. */
+    /* Would be safer and more simpler to use inet_aton() but it is most
+     * likely not provided either. */
+    uint32_t ipv4;
+
+    if( af != AF_INET )
+    {
+        errno = EAFNOSUPPORT;
+        return -1;
+    }
+
+    ipv4 = inet_addr( src );
+    if( ipv4 == INADDR_NONE )
+        return -1;
+
+    memcpy( dst; &ipv4, 4 );
+# endif /* WIN32 */
+    return 0;
+}
+#endif /* HAVE_INET_PTON */
index e4b12dcfcc55f27958d33af985c9a8815a359a8b..6e82a418db2aad19081b6a37bb24832a077b8eef 100644 (file)
@@ -300,7 +300,7 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
 
     switch( addr.ss_family )
     {
-#if defined (HAVE_GETADDRINFO) || defined (WIN32)
+#if defined (INET_PTON) || defined (WIN32)
         case AF_INET6:
         {
             /* See RFC3513 for list of valid IPv6 scopes */
@@ -457,29 +457,8 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
     psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */
     psz_head[3] = (i_hash & 0xFF);        /* Msg id hash 2 */
 
-#if defined(WIN32)
-    if( b_ipv6 )
-    {
-        struct sockaddr_in6 saddr;
-        int len = sizeof(saddr);
-        if( ! WSAStringToAddress(p_sap_session->p_address->psz_machine,
-                    AF_INET6, NULL, (LPSOCKADDR)&saddr, &len) )
-            return VLC_ENOMEM;
-        memcpy(psz_head+4, &saddr.sin6_addr, sizeof(struct in6_addr));
-    }
-    else
-    {
-        struct sockaddr_in saddr;
-        int len = sizeof(saddr);
-        if( ! WSAStringToAddress(p_sap_session->p_address->psz_machine,
-                    AF_INET, NULL, (LPSOCKADDR)&saddr, &len) )
-            return VLC_ENOMEM;
-        memcpy(psz_head+4, &saddr.sin_addr, sizeof(struct in_addr));
-    }
-#else
     inet_pton( b_ipv6 ? AF_INET6 : AF_INET, /* can't fail */
                p_sap_session->p_address->psz_machine, psz_head + 4 );
-#endif
 
     memcpy( psz_head + (b_ipv6 ? 20 : 8), "application/sdp", 15 );