]> git.sesse.net Git - vlc/commitdiff
Use integer rather than strings for UDP/TCP port numbers
authorRémi Denis-Courmont <rem@videolan.org>
Sun, 26 Jun 2005 09:12:36 +0000 (09:12 +0000)
committerRémi Denis-Courmont <rem@videolan.org>
Sun, 26 Jun 2005 09:12:36 +0000 (09:12 +0000)
include/network.h
include/vlc_symbols.h
modules/access/ftp.c
src/misc/getaddrinfo.c
src/misc/net.c
src/stream_output/sap.c

index 607e0687bd2266f9771a7199a153fc81ed3104f5..11ea6cdc7e44d4e962a69d3e7014ad01499f3819 100644 (file)
@@ -388,6 +388,7 @@ VLC_EXPORT( int, __net_CheckIP, ( vlc_object_t *p_this, char *psz_ip, char **pps
 #  define NI_MAXHOST 1025
 #  define NI_MAXSERV 32
 # endif
+# define NI_MAXNUMERICHOST 48
 
 # ifndef NI_NUMERICHOST
 #  define NI_NUMERICHOST 0x01
@@ -421,8 +422,8 @@ struct addrinfo
 # endif
 
 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
-VLC_EXPORT( int, vlc_getnameinfo, ( vlc_object_t *, const struct sockaddr *, int, char *, int, char *, int, int ) );
-VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, const char *, const struct addrinfo *, struct addrinfo ** ) );
+VLC_EXPORT( int, vlc_getnameinfo, ( vlc_object_t *, const struct sockaddr *, int, char *, int, int *, int ) );
+VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );
 
 #endif
index 6656072549d10f684c50c9c3bfc984762a125a84..c09285a634911c4b3cf9a7d1f9f5d78e65f54ed9 100644 (file)
@@ -366,8 +366,8 @@ struct module_symbols_t
     void (*vout_SynchroEnd_inner) (vout_synchro_t *, int, vlc_bool_t);
     mtime_t (*vout_SynchroDate_inner) (vout_synchro_t *);
     void (*vout_SynchroNewPicture_inner) (vout_synchro_t *, int, int, mtime_t, mtime_t, int, vlc_bool_t);
-    int (*vlc_getaddrinfo_inner) (vlc_object_t *, const char *, const char *, const struct addrinfo *, struct addrinfo **);
-    int (*vlc_getnameinfo_inner) (vlc_object_t *, const struct sockaddr *, int, char *, int, char *, int, int);
+    int (*vlc_getaddrinfo_inner) (vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo **);
+    int (*vlc_getnameinfo_inner) (vlc_object_t *, const struct sockaddr *, int, char *, int, int *, int);
     void (*InitMD5_inner) (struct md5_s *);
     void (*EndMD5_inner) (struct md5_s *);
     void (*vlc_freeaddrinfo_inner) (struct addrinfo *);
index f4184667fb572510ba52eea98f1f529938fb40da..c1aa968fce0907365c4459640782ec1823d9088e 100644 (file)
@@ -240,7 +240,7 @@ static int Open( vlc_object_t *p_this )
 
     if( ftp_ReadCommand( p_access, &i_answer, NULL ) == 2 )
     {
-        char hostaddr[NI_MAXHOST];
+        char hostaddr[NI_MAXNUMERICHOST];
         struct sockaddr_storage addr;
         socklen_t len = sizeof (addr);
 
@@ -251,7 +251,7 @@ static int Open( vlc_object_t *p_this )
         }
 
         i_answer = vlc_getnameinfo( p_this, (struct sockaddr *)&addr, len,
-                                    hostaddr, sizeof( hostaddr ), NULL, 0,
+                                    hostaddr, sizeof( hostaddr ), NULL,
                                     NI_NUMERICHOST );
         if( i_answer )
         {
index 5cf640ee80fde6caaa67dd9c55c4a5a40c1be9b0..c7379c3786f891690ec6d9565fb4aeb022d6991b 100644 (file)
@@ -494,8 +494,22 @@ __getaddrinfo (const char *node, const char *service,
 
 
 int vlc_getnameinfo( vlc_object_t *p_this, const struct sockaddr *sa, int salen,
-                     char *host, int hostlen, char *serv, int servlen, int flags )
+                     char *host, int hostlen, int *portnum, int flags )
 {
+    char psz_servbuf[6], *psz_serv;
+    int i_servlen, i_val;
+
+    flags |= NI_NUMERICSERV;
+    if( portnum != NULL )
+    {
+        psz_serv = psz_servbuf;
+        i_servlen = sizeof( psz_servbuf );
+    }
+    else
+    {
+        psz_serv = NULL;
+        i_servlen = 0;
+    }
 #ifdef WIN32
     /*
      * Here is the kind of kludge you need to keep binary compatibility among
@@ -514,11 +528,12 @@ int vlc_getnameinfo( vlc_object_t *p_this, const struct sockaddr *sa, int salen,
 
         if( ws2_getnameinfo != NULL )
         {
-            int i_val;
-
-            i_val = ws2_getnameinfo( sa, salen, host, hostlen, serv, servlen,
-                                     flags );
+            i_val = ws2_getnameinfo( sa, salen, host, hostlen, psz_serv,
+                                     i_servlen, flags );
             FreeLibrary( wship6_module );
+
+            if( portnum != NULL )
+                *portnum = atoi( psz_serv );
             return i_val;
         }
             
@@ -526,33 +541,51 @@ int vlc_getnameinfo( vlc_object_t *p_this, const struct sockaddr *sa, int salen,
     }
 #endif
 #if HAVE_GETNAMEINFO
-    return getnameinfo( sa, salen, host, hostlen, serv, servlen, flags );
+    i_val = getnameinfo( sa, salen, host, hostlen, psz_serv, i_servlen,
+                         flags );
 #else
-{
-    vlc_value_t lock;
-    int i_val;
+    {
+        vlc_value_t lock;
+    
+        /* my getnameinfo implementation is not thread-safe as it uses
+        * gethostbyaddr and the likes */
+        var_Create( p_this->p_libvlc, "getnameinfo_mutex", VLC_VAR_MUTEX );
+        var_Get( p_this->p_libvlc, "getnameinfo_mutex", &lock );
+        vlc_mutex_lock( lock.p_address );
+    
+        i_val = __getnameinfo( sa, salen, host, hostlen, psz_serv, i_servlen,
+                               flags );
+        vlc_mutex_unlock( lock.p_address );
+    }
+#endif
 
-    /* my getnameinfo implementation is not thread-safe as it uses
-     * gethostbyaddr and the likes */
-    var_Create( p_this->p_libvlc, "getnameinfo_mutex", VLC_VAR_MUTEX );
-    var_Get( p_this->p_libvlc, "getnameinfo_mutex", &lock );
-    vlc_mutex_lock( lock.p_address );
+    if( portnum != NULL )
+        *portnum = atoi( psz_serv );
 
-    i_val = __getnameinfo( sa, salen, host, hostlen, serv, servlen, flags );
-    vlc_mutex_unlock( lock.p_address );
     return i_val;
 }
-#endif
-}
 
 
 /* TODO: support for setting sin6_scope_id */
 int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
-                     const char *service, const struct addrinfo *p_hints,
+                     int i_port, const struct addrinfo *p_hints,
                      struct addrinfo **res )
 {
     struct addrinfo hints;
-    char psz_buf[NI_MAXHOST], *psz_node;
+    char psz_buf[NI_MAXHOST], *psz_node, psz_service[6];
+
+    /*
+     * In VLC, we always use port number as integer rather than strings
+     * for historical reasons (and portability).
+     */
+    if( ( i_port > 65535 ) || ( i_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 */
     if( p_hints == NULL )
@@ -585,8 +618,6 @@ int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
     if( ( node == NULL ) || (node[0] == '\0' ) )
     {
         psz_node = NULL;
-        if( service == NULL )
-            service = "";
     }
     else
     {
@@ -608,10 +639,6 @@ int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
         }
     }
 
-    if( ( service != NULL ) && ( *service == '\0' ) )
-        /* We could put NULL, but you can't have both node and service NULL */
-        service = "0";
-
 #ifdef WIN32
     {
         typedef int (CALLBACK * GETADDRINFO) ( const char *, const char *,
@@ -640,7 +667,7 @@ int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
     }
 #endif
 #if HAVE_GETADDRINFO
-    return getaddrinfo( psz_node, service, &hints, res );
+    return getaddrinfo( psz_node, psz_service, &hints, res );
 #else
 {
     int i_ret;
index fcfb78434f459085d8aa456e30ac9f2889c9e7f3..d8ce7758ab65ccc40ce9707fee90a39d29cb102b 100644 (file)
@@ -85,11 +85,9 @@ int __net_OpenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
 {
     struct addrinfo hints, *res, *ptr;
     const char      *psz_realhost;
-    char            *psz_realport, *psz_socks;
-    int             i_val, i_handle = -1;
+    char            *psz_socks;
+    int             i_realport, i_val, i_handle = -1;
 
-    if( ( i_port < 0 ) || ( i_port > 65535 ) )
-        return -1; /* I don't expect the next TCP version shortly */
     if( i_port == 0 )
         i_port = 80; /* historical VLC thing */
 
@@ -105,33 +103,25 @@ int __net_OpenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
             *psz++ = '\0';
 
         psz_realhost = psz_socks;
-        psz_realport = strdup( ( psz != NULL ) ? psz : "1080" );
+        i_realport = ( psz != NULL ) ? atoi( psz ) : 1080;
 
-        msg_Dbg( p_this, "net: connecting to '%s:%s' for '%s:%d'",
-                 psz_realhost, psz_realport, psz_host, i_port );
+        msg_Dbg( p_this, "net: connecting to '%s:%d' for '%s:%d'",
+                 psz_realhost, i_realport, psz_host, i_port );
     }
     else
     {
         psz_realhost = psz_host;
-        psz_realport = malloc( 6 );
-        if( psz_realport == NULL )
-        {
-            free( psz_socks );
-            return -1;
-        }
+        i_realport = i_port;
 
-        sprintf( psz_realport, "%d", i_port );
-        msg_Dbg( p_this, "net: connecting to '%s:%s'", psz_realhost,
-                 psz_realport );
+        msg_Dbg( p_this, "net: connecting to '%s:%d'", psz_realhost,
+                 i_realport );
     }
 
-    i_val = vlc_getaddrinfo( p_this, psz_realhost, psz_realport, &hints,
-                             &res );
-    free( psz_realport );
+    i_val = vlc_getaddrinfo( p_this, psz_realhost, i_realport, &hints, &res );
     if( i_val )
     {
-        msg_Err( p_this, "cannot resolve '%s' : %s", psz_realhost,
-                 vlc_gai_strerror( i_val ) );
+        msg_Err( p_this, "cannot resolve '%s:%d' : %s", psz_realhost,
+                 i_realport, vlc_gai_strerror( i_val ) );
         free( psz_socks );
         return -1;
     }
@@ -322,29 +312,17 @@ int *__net_ListenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
 {
     struct addrinfo hints, *res, *ptr;
     int             i_val, *pi_handles, i_size;
-    char            *psz_port;
-
-    if( ( i_port < 0 ) || ( i_port > 65535 ) )
-        return NULL; /* I don't expect the next TCP version shortly */
-    if( i_port == 0 )
-        i_port = 80; /* historical VLC thing */
 
     memset( &hints, 0, sizeof( hints ) );
     hints.ai_socktype = SOCK_STREAM;
     hints.ai_flags = AI_PASSIVE;
 
-    psz_port = malloc( 6 );
-    if( psz_port == NULL )
-        return NULL;
-
-    sprintf( psz_port, "%d", i_port );
-    msg_Dbg( p_this, "net: listening to '%s:%s'", psz_host, psz_port );
+    msg_Dbg( p_this, "net: listening to '%s:%d'", psz_host, i_port );
 
-    i_val = vlc_getaddrinfo( p_this, psz_host, psz_port, &hints, &res );
-    free( psz_port );
+    i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
     if( i_val )
     {
-        msg_Err( p_this, "cannot resolve '%s' : %s", psz_host,
+        msg_Err( p_this, "cannot resolve '%s:%d' : %s", psz_host, i_port,
                  vlc_gai_strerror( i_val ) );
         return NULL;
     }
@@ -1110,7 +1088,7 @@ static int SocksHandshakeTCP( vlc_object_t *p_obj,
 
         /* v4 only support ipv4 */
         hints.ai_family = PF_INET;
-        if( vlc_getaddrinfo( p_obj, psz_host, NULL, &hints, &p_res ) )
+        if( vlc_getaddrinfo( p_obj, psz_host, 0, &hints, &p_res ) )
             return VLC_EGENERIC;
 
         buffer[0] = i_socks_version;
index 7904ea6edbad2f6a5ccd1eddf5ad9060d938dbf0..7996cef6d1362778c7471dcddffa9d6821ecbbc2 100644 (file)
@@ -295,12 +295,12 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
         hints.ai_socktype = SOCK_DGRAM;
         hints.ai_flags = AI_NUMERICHOST;
 
-        i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, NULL,
+        i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, 0,
                              &hints, &res );
         if( i == 0 )
             i = vlc_getnameinfo( (vlc_object_t *)p_sap, res->ai_addr,
                                  res->ai_addrlen, psz_buf, sizeof( psz_buf ),
-                                 NULL, 0, NI_NUMERICHOST );
+                                 NULL, NI_NUMERICHOST );
         if( i )
         {
             msg_Err( p_sap, "Invalid URI for SAP announce : %s : %s",
@@ -584,7 +584,7 @@ static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session )
 {
     int64_t i_sdp_id = mdate();
     int     i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
-    char *psz_group, *psz_name, psz_uribuf[48], *psz_uri;
+    char *psz_group, *psz_name, psz_uribuf[NI_MAXNUMERICHOST], *psz_uri;
     char ipv;
 
     psz_group = convert_to_utf8( p_sap, p_session->psz_group );