Commit 7f432665 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Use integer rather than strings for UDP/TCP port numbers

parent 1a57857c
...@@ -388,6 +388,7 @@ VLC_EXPORT( int, __net_CheckIP, ( vlc_object_t *p_this, char *psz_ip, char **pps ...@@ -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_MAXHOST 1025
# define NI_MAXSERV 32 # define NI_MAXSERV 32
# endif # endif
# define NI_MAXNUMERICHOST 48
# ifndef NI_NUMERICHOST # ifndef NI_NUMERICHOST
# define NI_NUMERICHOST 0x01 # define NI_NUMERICHOST 0x01
...@@ -421,8 +422,8 @@ struct addrinfo ...@@ -421,8 +422,8 @@ struct addrinfo
# endif # endif
VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) ); 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_getnameinfo, ( vlc_object_t *, const struct sockaddr *, int, char *, int, int *, int ) );
VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, const char *, const struct addrinfo *, struct addrinfo ** ) ); VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) ); VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );
#endif #endif
...@@ -366,8 +366,8 @@ struct module_symbols_t ...@@ -366,8 +366,8 @@ struct module_symbols_t
void (*vout_SynchroEnd_inner) (vout_synchro_t *, int, vlc_bool_t); void (*vout_SynchroEnd_inner) (vout_synchro_t *, int, vlc_bool_t);
mtime_t (*vout_SynchroDate_inner) (vout_synchro_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); 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_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, char *, int, int); int (*vlc_getnameinfo_inner) (vlc_object_t *, const struct sockaddr *, int, char *, int, int *, int);
void (*InitMD5_inner) (struct md5_s *); void (*InitMD5_inner) (struct md5_s *);
void (*EndMD5_inner) (struct md5_s *); void (*EndMD5_inner) (struct md5_s *);
void (*vlc_freeaddrinfo_inner) (struct addrinfo *); void (*vlc_freeaddrinfo_inner) (struct addrinfo *);
......
...@@ -240,7 +240,7 @@ static int Open( vlc_object_t *p_this ) ...@@ -240,7 +240,7 @@ static int Open( vlc_object_t *p_this )
if( ftp_ReadCommand( p_access, &i_answer, NULL ) == 2 ) if( ftp_ReadCommand( p_access, &i_answer, NULL ) == 2 )
{ {
char hostaddr[NI_MAXHOST]; char hostaddr[NI_MAXNUMERICHOST];
struct sockaddr_storage addr; struct sockaddr_storage addr;
socklen_t len = sizeof (addr); socklen_t len = sizeof (addr);
...@@ -251,7 +251,7 @@ static int Open( vlc_object_t *p_this ) ...@@ -251,7 +251,7 @@ static int Open( vlc_object_t *p_this )
} }
i_answer = vlc_getnameinfo( p_this, (struct sockaddr *)&addr, len, i_answer = vlc_getnameinfo( p_this, (struct sockaddr *)&addr, len,
hostaddr, sizeof( hostaddr ), NULL, 0, hostaddr, sizeof( hostaddr ), NULL,
NI_NUMERICHOST ); NI_NUMERICHOST );
if( i_answer ) if( i_answer )
{ {
......
...@@ -494,8 +494,22 @@ __getaddrinfo (const char *node, const char *service, ...@@ -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, 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 #ifdef WIN32
/* /*
* Here is the kind of kludge you need to keep binary compatibility among * 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, ...@@ -514,11 +528,12 @@ int vlc_getnameinfo( vlc_object_t *p_this, const struct sockaddr *sa, int salen,
if( ws2_getnameinfo != NULL ) if( ws2_getnameinfo != NULL )
{ {
int i_val; i_val = ws2_getnameinfo( sa, salen, host, hostlen, psz_serv,
i_servlen, flags );
i_val = ws2_getnameinfo( sa, salen, host, hostlen, serv, servlen,
flags );
FreeLibrary( wship6_module ); FreeLibrary( wship6_module );
if( portnum != NULL )
*portnum = atoi( psz_serv );
return i_val; return i_val;
} }
...@@ -526,11 +541,11 @@ int vlc_getnameinfo( vlc_object_t *p_this, const struct sockaddr *sa, int salen, ...@@ -526,11 +541,11 @@ int vlc_getnameinfo( vlc_object_t *p_this, const struct sockaddr *sa, int salen,
} }
#endif #endif
#if HAVE_GETNAMEINFO #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 #else
{ {
vlc_value_t lock; vlc_value_t lock;
int i_val;
/* my getnameinfo implementation is not thread-safe as it uses /* my getnameinfo implementation is not thread-safe as it uses
* gethostbyaddr and the likes */ * gethostbyaddr and the likes */
...@@ -538,21 +553,39 @@ int vlc_getnameinfo( vlc_object_t *p_this, const struct sockaddr *sa, int salen, ...@@ -538,21 +553,39 @@ int vlc_getnameinfo( vlc_object_t *p_this, const struct sockaddr *sa, int salen,
var_Get( p_this->p_libvlc, "getnameinfo_mutex", &lock ); var_Get( p_this->p_libvlc, "getnameinfo_mutex", &lock );
vlc_mutex_lock( lock.p_address ); vlc_mutex_lock( lock.p_address );
i_val = __getnameinfo( sa, salen, host, hostlen, serv, servlen, flags ); i_val = __getnameinfo( sa, salen, host, hostlen, psz_serv, i_servlen,
flags );
vlc_mutex_unlock( lock.p_address ); vlc_mutex_unlock( lock.p_address );
return i_val; }
}
#endif #endif
if( portnum != NULL )
*portnum = atoi( psz_serv );
return i_val;
} }
/* TODO: support for setting sin6_scope_id */ /* TODO: support for setting sin6_scope_id */
int vlc_getaddrinfo( vlc_object_t *p_this, const char *node, 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 **res )
{ {
struct addrinfo hints; 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 */ /* Check if we have to force ipv4 or ipv6 */
if( p_hints == NULL ) if( p_hints == NULL )
...@@ -585,8 +618,6 @@ int vlc_getaddrinfo( vlc_object_t *p_this, const char *node, ...@@ -585,8 +618,6 @@ int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
if( ( node == NULL ) || (node[0] == '\0' ) ) if( ( node == NULL ) || (node[0] == '\0' ) )
{ {
psz_node = NULL; psz_node = NULL;
if( service == NULL )
service = "";
} }
else else
{ {
...@@ -608,10 +639,6 @@ int vlc_getaddrinfo( vlc_object_t *p_this, const char *node, ...@@ -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 #ifdef WIN32
{ {
typedef int (CALLBACK * GETADDRINFO) ( const char *, const char *, typedef int (CALLBACK * GETADDRINFO) ( const char *, const char *,
...@@ -640,7 +667,7 @@ int vlc_getaddrinfo( vlc_object_t *p_this, const char *node, ...@@ -640,7 +667,7 @@ int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
} }
#endif #endif
#if HAVE_GETADDRINFO #if HAVE_GETADDRINFO
return getaddrinfo( psz_node, service, &hints, res ); return getaddrinfo( psz_node, psz_service, &hints, res );
#else #else
{ {
int i_ret; int i_ret;
......
...@@ -85,11 +85,9 @@ int __net_OpenTCP( vlc_object_t *p_this, const char *psz_host, int i_port ) ...@@ -85,11 +85,9 @@ int __net_OpenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
{ {
struct addrinfo hints, *res, *ptr; struct addrinfo hints, *res, *ptr;
const char *psz_realhost; const char *psz_realhost;
char *psz_realport, *psz_socks; char *psz_socks;
int i_val, i_handle = -1; 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 ) if( i_port == 0 )
i_port = 80; /* historical VLC thing */ 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 ) ...@@ -105,33 +103,25 @@ int __net_OpenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
*psz++ = '\0'; *psz++ = '\0';
psz_realhost = psz_socks; 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'", msg_Dbg( p_this, "net: connecting to '%s:%d' for '%s:%d'",
psz_realhost, psz_realport, psz_host, i_port ); psz_realhost, i_realport, psz_host, i_port );
} }
else else
{ {
psz_realhost = psz_host; psz_realhost = psz_host;
psz_realport = malloc( 6 ); i_realport = i_port;
if( psz_realport == NULL )
{
free( psz_socks );
return -1;
}
sprintf( psz_realport, "%d", i_port ); msg_Dbg( p_this, "net: connecting to '%s:%d'", psz_realhost,
msg_Dbg( p_this, "net: connecting to '%s:%s'", psz_realhost, i_realport );
psz_realport );
} }
i_val = vlc_getaddrinfo( p_this, psz_realhost, psz_realport, &hints, i_val = vlc_getaddrinfo( p_this, psz_realhost, i_realport, &hints, &res );
&res );
free( psz_realport );
if( i_val ) if( i_val )
{ {
msg_Err( p_this, "cannot resolve '%s' : %s", psz_realhost, msg_Err( p_this, "cannot resolve '%s:%d' : %s", psz_realhost,
vlc_gai_strerror( i_val ) ); i_realport, vlc_gai_strerror( i_val ) );
free( psz_socks ); free( psz_socks );
return -1; return -1;
} }
...@@ -322,29 +312,17 @@ int *__net_ListenTCP( vlc_object_t *p_this, const char *psz_host, int i_port ) ...@@ -322,29 +312,17 @@ int *__net_ListenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
{ {
struct addrinfo hints, *res, *ptr; struct addrinfo hints, *res, *ptr;
int i_val, *pi_handles, i_size; 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 ) ); memset( &hints, 0, sizeof( hints ) );
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
psz_port = malloc( 6 ); msg_Dbg( p_this, "net: listening to '%s:%d'", psz_host, i_port );
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 );
i_val = vlc_getaddrinfo( p_this, psz_host, psz_port, &hints, &res ); i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
free( psz_port );
if( i_val ) 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 ) ); vlc_gai_strerror( i_val ) );
return NULL; return NULL;
} }
...@@ -1110,7 +1088,7 @@ static int SocksHandshakeTCP( vlc_object_t *p_obj, ...@@ -1110,7 +1088,7 @@ static int SocksHandshakeTCP( vlc_object_t *p_obj,
/* v4 only support ipv4 */ /* v4 only support ipv4 */
hints.ai_family = PF_INET; 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; return VLC_EGENERIC;
buffer[0] = i_socks_version; buffer[0] = i_socks_version;
......
...@@ -295,12 +295,12 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap, ...@@ -295,12 +295,12 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_NUMERICHOST; 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 ); &hints, &res );
if( i == 0 ) if( i == 0 )
i = vlc_getnameinfo( (vlc_object_t *)p_sap, res->ai_addr, i = vlc_getnameinfo( (vlc_object_t *)p_sap, res->ai_addr,
res->ai_addrlen, psz_buf, sizeof( psz_buf ), res->ai_addrlen, psz_buf, sizeof( psz_buf ),
NULL, 0, NI_NUMERICHOST ); NULL, NI_NUMERICHOST );
if( i ) if( i )
{ {
msg_Err( p_sap, "Invalid URI for SAP announce : %s : %s", 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 ) ...@@ -584,7 +584,7 @@ static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session )
{ {
int64_t i_sdp_id = mdate(); int64_t i_sdp_id = mdate();
int i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff); 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; char ipv;
psz_group = convert_to_utf8( p_sap, p_session->psz_group ); psz_group = convert_to_utf8( p_sap, p_session->psz_group );
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment