]> git.sesse.net Git - vlc/blobdiff - src/misc/net.c
implement net_ConnectUDP() (yet to be used) to create a connected UDP socket,
[vlc] / src / misc / net.c
index f2750fe8a04454f5909a1e38a9ac22007de4af8f..7106c08dad23e4ca03eeb097e1f8e7e1cb8d1cb5 100644 (file)
 #ifdef HAVE_FCNTL_H
 #   include <fcntl.h>
 #endif
-
 #ifdef HAVE_SYS_TIME_H
 #    include <sys/time.h>
 #endif
-
-#if defined( WIN32 ) || defined( UNDER_CE )
-#   if defined(UNDER_CE) && defined(sockaddr_storage)
-#       undef sockaddr_storage
-#   endif
-#   include <io.h>
-#   include <winsock2.h>
-#   include <ws2tcpip.h>
-#else
-#   include <sys/socket.h>
-#   include <netinet/in.h>
-#   ifdef HAVE_ARPA_INET_H
-#       include <arpa/inet.h>
-#   endif
-#   include <netdb.h>
-#endif
-
 #ifdef HAVE_UNISTD_H
 #   include <unistd.h>
 #endif
@@ -141,16 +123,17 @@ static int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
 }
 
 /*****************************************************************************
- * __net_OpenTCP:
+ * __net_ConnectTCP:
  *****************************************************************************
  * Open a TCP connection and return a handle
  *****************************************************************************/
-int __net_OpenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
+int __net_ConnectTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
 {
     struct addrinfo hints, *res, *ptr;
     const char      *psz_realhost;
     char            *psz_socks;
     int             i_realport, i_val, i_handle = -1;
+    vlc_bool_t      b_unreach = VLC_FALSE;
 
     if( i_port == 0 )
         i_port = 80; /* historical VLC thing */
@@ -169,7 +152,7 @@ int __net_OpenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
         psz_realhost = psz_socks;
         i_realport = ( psz != NULL ) ? atoi( psz ) : 1080;
 
-        msg_Dbg( p_this, "net: connecting to '%s:%d' for '%s:%d'",
+        msg_Dbg( p_this, "net: connecting to %s port %d for %s port %d",
                  psz_realhost, i_realport, psz_host, i_port );
     }
     else
@@ -177,14 +160,14 @@ int __net_OpenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
         psz_realhost = psz_host;
         i_realport = i_port;
 
-        msg_Dbg( p_this, "net: connecting to '%s:%d'", psz_realhost,
+        msg_Dbg( p_this, "net: connecting to %s port %d", psz_realhost,
                  i_realport );
     }
 
     i_val = vlc_getaddrinfo( p_this, psz_realhost, i_realport, &hints, &res );
     if( i_val )
     {
-        msg_Err( p_this, "cannot resolve '%s:%d' : %s", psz_realhost,
+        msg_Err( p_this, "cannot resolve %s port %d : %s", psz_realhost,
                  i_realport, vlc_gai_strerror( i_val ) );
         free( psz_socks );
         return -1;
@@ -205,19 +188,26 @@ int __net_OpenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
             div_t d;
             struct timeval tv;
             vlc_value_t timeout;
+
 #if defined( WIN32 ) || defined( UNDER_CE )
             if( WSAGetLastError() != WSAEWOULDBLOCK )
             {
-                msg_Warn( p_this, "connection to %s:%d failed (%d)", psz_host,
-                          i_port, WSAGetLastError( ) );
+                if( WSAGetLastError () == WSAENETUNREACH )
+                    b_unreach = VLC_TRUE;
+                else
+                    msg_Warn( p_this, "connection to %s port %d failed (%d)",
+                              psz_host, i_port, WSAGetLastError( ) );
                 net_Close( fd );
                 continue;
             }
 #else
             if( errno != EINPROGRESS )
             {
-                msg_Warn( p_this, "connection to %s:%d : %s", psz_host,
-                          i_port, strerror( errno ) );
+                if( errno == ENETUNREACH )
+                    b_unreach = VLC_TRUE;
+                else
+                    msg_Warn( p_this, "connection to %s port %d : %s", psz_host,
+                              i_port, strerror( errno ) );
                 net_Close( fd );
                 continue;
             }
@@ -289,13 +279,18 @@ int __net_OpenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
             if( getsockopt( fd, SOL_SOCKET, SO_ERROR, (void*)&i_val,
                             &i_val_size ) == -1 || i_val != 0 )
             {
+                if( i_val == ENETUNREACH )
+                    b_unreach = VLC_TRUE;
+                else
+                {
 #ifdef WIN32
-                msg_Warn( p_this, "connection to %s:%d failed (%d)", psz_host,
-                          i_port, WSAGetLastError( ) );
+                    msg_Warn( p_this, "connection to %s port %d failed (%d)",
+                              psz_host, i_port, WSAGetLastError( ) );
 #else
-                msg_Warn( p_this, "connection to %s:%d : %s", psz_host,
-                          i_port, strerror( i_val ) );
+                    msg_Warn( p_this, "connection to %s port %d : %s", psz_host,
+                              i_port, strerror( i_val ) );
 #endif
+                }
                 net_Close( fd );
                 continue;
             }
@@ -303,9 +298,17 @@ int __net_OpenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
         }
         i_handle = fd; /* success! */
     }
-    
+
     vlc_freeaddrinfo( res );
 
+    if( i_handle == -1 )
+    {
+        if( b_unreach )
+            msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
+                     i_port );
+        return -1;
+    }
+
     if( *psz_socks && *psz_socks != ':' )
     {
         char *psz_user = var_CreateGetString( p_this, "socks-user" );
@@ -343,12 +346,12 @@ int *__net_ListenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
     hints.ai_socktype = SOCK_STREAM;
     hints.ai_flags = AI_PASSIVE;
 
-    msg_Dbg( p_this, "net: listening to '%s:%d'", psz_host, i_port );
+    msg_Dbg( p_this, "net: listening to %s port %d", psz_host, i_port );
 
     i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
     if( i_val )
     {
-        msg_Err( p_this, "cannot resolve '%s:%d' : %s", psz_host, i_port,
+        msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
                  vlc_gai_strerror( i_val ) );
         return NULL;
     }
@@ -376,7 +379,7 @@ int *__net_ListenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
             net_Close( fd );
             continue;
         }
+
         /* Listen */
         if( listen( fd, 100 ) == -1 )
         {
@@ -403,7 +406,7 @@ int *__net_ListenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
             pi_handles = newpi;
         }
     }
-    
+
     vlc_freeaddrinfo( res );
 
     if( pi_handles != NULL )
@@ -498,6 +501,95 @@ int __net_Accept( vlc_object_t *p_this, int *pi_fd, mtime_t i_wait )
     return -1;
 }
 
+
+/*****************************************************************************
+ * __net_ConnectUDP:
+ *****************************************************************************
+ * Open a UDP socket to send data to a defined destination, with an optional
+ * hop limit.
+ *****************************************************************************/
+int __net_ConnectUDP( vlc_object_t *p_this, const char *psz_host, int i_port,
+                      int hlim )
+{
+    struct addrinfo hints, *res, *ptr;
+    int             i_val, i_handle = -1;
+    vlc_bool_t      b_unreach = VLC_FALSE;
+
+    if( i_port == 0 )
+        i_port = 1234; /* historical VLC thing */
+
+    memset( &hints, 0, sizeof( hints ) );
+    hints.ai_socktype = SOCK_DGRAM;
+
+    msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
+
+    i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
+    if( i_val )
+    {
+        msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
+                 vlc_gai_strerror( i_val ) );
+        return -1;
+    }
+
+    for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
+    {
+        int fd;
+
+        fd = net_Socket( p_this, ptr->ai_family, ptr->ai_socktype,
+                         ptr->ai_protocol );
+        if( fd == -1 )
+            continue;
+#if !defined( SYS_BEOS )
+        else
+        {
+            int i_val;
+
+            /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
+            * packet loss caused by scheduling problems */
+            i_val = 0x80000;
+            setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *)&i_val,
+                        sizeof( i_val ) );
+            i_val = 0x80000;
+            setsockopt( i_handle, SOL_SOCKET, SO_SNDBUF, (void *)&i_val,
+                        sizeof( i_val ) );
+        }
+#endif
+
+        if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
+        {
+            /* success */
+            i_handle = fd;
+            break;
+        }
+
+#if defined( WIN32 ) || defined( UNDER_CE )
+        if( WSAGetLastError () == WSAENETUNREACH )
+#else
+        if( errno == ENETUNREACH )
+#endif
+            b_unreach = VLC_TRUE;
+        else
+        {
+            msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
+                      strerror( errno ) );
+            net_Close( fd );
+            continue;
+        }
+    }
+
+    vlc_freeaddrinfo( res );
+
+    if( i_handle == -1 )
+    {
+        if( b_unreach )
+            msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
+                     i_port );
+        return -1;
+    }
+
+    return i_handle;
+}
+
 /*****************************************************************************
  * __net_OpenUDP:
  *****************************************************************************
@@ -511,6 +603,13 @@ int __net_OpenUDP( vlc_object_t *p_this, const char *psz_bind, int i_bind,
     network_socket_t sock;
     module_t         *p_network = NULL;
 
+    if( ( psz_server != NULL ) && ( psz_server[0] == '\0' ) )
+        msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
+                  "is obsolete - use net_ConnectUDP instead" );
+    if( i_server != 0 )
+        msg_Warn( p_this, "calling net_OpenUDP with an explicit destination "
+                  "port is obsolete - use __net_ConnectUDP instead" );
+
     if( psz_server == NULL ) psz_server = "";
     if( psz_bind == NULL ) psz_bind = "";
 
@@ -691,7 +790,7 @@ int __net_Read( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
             else if( WSAGetLastError() == WSAEINTR ) continue;
             else msg_Err( p_this, "recv failed (%i)", WSAGetLastError() );
 #else
-            /* EAGAIN only happens with p_vs (SSL) and it's not an error */
+            /* EAGAIN only happens with p_vs (TLS) and it's not an error */
             if( errno != EAGAIN )
                 msg_Err( p_this, "recv failed (%s)", strerror(errno) );
 #endif
@@ -1038,7 +1137,7 @@ static int SocksNegociate( vlc_object_t *p_obj,
         buffer[2] = 0x00;               /* - No auth required */
         i_len = 3;
     }
-    
+
     if( net_Write( p_obj, fd, NULL, buffer, i_len ) != i_len )
         return VLC_EGENERIC;
     if( net_Read( p_obj, fd, NULL, buffer, 2, VLC_TRUE ) != 2 )
@@ -1207,8 +1306,15 @@ int inet_pton(int af, const char *src, void *dst)
     int len = sizeof( addr );
 
     /* Damn it, they didn't even put LPCSTR for the firs parameter!!! */
+#ifdef UNICODE
+    wchar_t *workaround_for_ill_designed_api =
+        malloc( MAX_PATH * sizeof(wchar_t) );
+    mbstowcs( workaround_for_ill_designed_api, src, MAX_PATH );
+    workaround_for_ill_designed_api[MAX_PATH-1] = 0;
+#else
     char *workaround_for_ill_designed_api = strdup( src );
-    
+#endif
+
     if( !WSAStringToAddress( workaround_for_ill_designed_api, af, NULL,
                              (LPSOCKADDR)&addr, &len ) )
     {
@@ -1222,7 +1328,7 @@ int inet_pton(int af, const char *src, void *dst)
         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;