From c0d749418b70fe24f7812a169ad88d713ce5ef58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Tue, 23 Jun 2015 12:30:57 +0200 Subject: [PATCH] Make 'port' a string in NET module - avoids dependency on snprintf - allows using "smtps" instead of "456" if desired --- ChangeLog | 1 + include/mbedtls/net.h | 4 ++-- library/net.c | 24 ++++-------------------- programs/pkey/dh_client.c | 4 ++-- programs/pkey/dh_server.c | 2 +- programs/ssl/dtls_client.c | 5 ++--- programs/ssl/dtls_server.c | 4 ++-- programs/ssl/ssl_client1.c | 5 ++--- programs/ssl/ssl_client2.c | 12 ++++-------- programs/ssl/ssl_fork_server.c | 2 +- programs/ssl/ssl_mail_client.c | 12 ++++-------- programs/ssl/ssl_pthread_server.c | 2 +- programs/ssl/ssl_server.c | 2 +- programs/ssl/ssl_server2.c | 14 +++++--------- programs/test/udp_proxy.c | 26 +++++++++----------------- programs/x509/cert_app.c | 14 +++++--------- 16 files changed, 46 insertions(+), 87 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0edbd1605..6ba45ea08 100644 --- a/ChangeLog +++ b/ChangeLog @@ -92,6 +92,7 @@ API Changes in config.h * net_connect() and net_bind() have a new 'proto' argument to choose between TCP and UDP, using the macros NET_PROTO_TCP or NET_PROTO_UDP. + Their 'port' argument type is changed to a string. * Some constness fixes Removals diff --git a/include/mbedtls/net.h b/include/mbedtls/net.h index 586bd34ab..ed2d5d322 100644 --- a/include/mbedtls/net.h +++ b/include/mbedtls/net.h @@ -70,7 +70,7 @@ extern "C" { * * \note Sets the socket in connected mode even with UDP. */ -int mbedtls_net_connect( int *fd, const char *host, int port, int proto ); +int mbedtls_net_connect( int *fd, const char *host, const char *port, int proto ); /** * \brief Create a receiving socket on bind_ip:port in the chosen @@ -89,7 +89,7 @@ int mbedtls_net_connect( int *fd, const char *host, int port, int proto ); * \note Regardless of the protocol, opens the sockets and binds it. * In addition, make the socket listening if protocol is TCP. */ -int mbedtls_net_bind( int *fd, const char *bind_ip, int port, int proto ); +int mbedtls_net_bind( int *fd, const char *bind_ip, const char *port, int proto ); /** * \brief Accept a connection from a remote client diff --git a/library/net.c b/library/net.c index 8dd4c0965..f284153a9 100644 --- a/library/net.c +++ b/library/net.c @@ -86,12 +86,6 @@ typedef UINT32 uint32_t; #include #endif -#if defined(MBEDTLS_PLATFORM_C) -#include "mbedtls/platform.h" -#else -#define mbedtls_snprintf snprintf -#endif - /* * Prepare for using the sockets interface */ @@ -119,26 +113,21 @@ static int net_prepare( void ) /* * Initiate a TCP connection with host:port and the given protocol */ -int mbedtls_net_connect( int *fd, const char *host, int port, int proto ) +int mbedtls_net_connect( int *fd, const char *host, const char *port, int proto ) { int ret; struct addrinfo hints, *addr_list, *cur; - char port_str[6]; if( ( ret = net_prepare() ) != 0 ) return( ret ); - /* getaddrinfo expects port as a string */ - memset( port_str, 0, sizeof( port_str ) ); - mbedtls_snprintf( port_str, sizeof( port_str ), "%d", port ); - /* Do name resolution with both IPv6 and IPv4 */ memset( &hints, 0, sizeof( hints ) ); hints.ai_family = AF_UNSPEC; hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM; hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP; - if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 ) + if( getaddrinfo( host, port, &hints, &addr_list ) != 0 ) return( MBEDTLS_ERR_NET_UNKNOWN_HOST ); /* Try the sockaddrs until a connection succeeds */ @@ -171,19 +160,14 @@ int mbedtls_net_connect( int *fd, const char *host, int port, int proto ) /* * Create a listening socket on bind_ip:port */ -int mbedtls_net_bind( int *fd, const char *bind_ip, int port, int proto ) +int mbedtls_net_bind( int *fd, const char *bind_ip, const char *port, int proto ) { int n, ret; struct addrinfo hints, *addr_list, *cur; - char port_str[6]; if( ( ret = net_prepare() ) != 0 ) return( ret ); - /* getaddrinfo expects port as a string */ - memset( port_str, 0, sizeof( port_str ) ); - mbedtls_snprintf( port_str, sizeof( port_str ), "%d", port ); - /* Bind to IPv6 and/or IPv4, but only in TCP */ memset( &hints, 0, sizeof( hints ) ); hints.ai_family = AF_UNSPEC; @@ -192,7 +176,7 @@ int mbedtls_net_bind( int *fd, const char *bind_ip, int port, int proto ) if( bind_ip == NULL ) hints.ai_flags = AI_PASSIVE; - if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 ) + if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 ) return( MBEDTLS_ERR_NET_UNKNOWN_HOST ); /* Try the sockaddrs until a binding succeeds */ diff --git a/programs/pkey/dh_client.c b/programs/pkey/dh_client.c index 0b340d516..e9523ce84 100644 --- a/programs/pkey/dh_client.c +++ b/programs/pkey/dh_client.c @@ -50,7 +50,7 @@ #endif #define SERVER_NAME "localhost" -#define SERVER_PORT 11999 +#define SERVER_PORT "11999" #if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \ !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NET_C) || \ @@ -134,7 +134,7 @@ int main( void ) /* * 3. Initiate the connection */ - mbedtls_printf( "\n . Connecting to tcp/%s/%d", SERVER_NAME, + mbedtls_printf( "\n . Connecting to tcp/%s/%s", SERVER_NAME, SERVER_PORT ); fflush( stdout ); diff --git a/programs/pkey/dh_server.c b/programs/pkey/dh_server.c index 8fda29fda..30abaa894 100644 --- a/programs/pkey/dh_server.c +++ b/programs/pkey/dh_server.c @@ -49,7 +49,7 @@ #include #endif -#define SERVER_PORT 11999 +#define SERVER_PORT "11999" #define PLAINTEXT "==Hello there!==" #if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \ diff --git a/programs/ssl/dtls_client.c b/programs/ssl/dtls_client.c index cc0d051bb..6ac83d7bd 100644 --- a/programs/ssl/dtls_client.c +++ b/programs/ssl/dtls_client.c @@ -61,7 +61,7 @@ int main( void ) #include "mbedtls/certs.h" #include "mbedtls/timing.h" -#define SERVER_PORT 4433 +#define SERVER_PORT "4433" #define SERVER_NAME "localhost" #define SERVER_ADDR "127.0.0.1" /* forces IPv4 */ #define MESSAGE "Echo this" @@ -142,8 +142,7 @@ int main( int argc, char *argv[] ) /* * 1. Start the connection */ - mbedtls_printf( " . Connecting to udp/%s/%4d...", SERVER_NAME, - SERVER_PORT ); + mbedtls_printf( " . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT ); fflush( stdout ); if( ( ret = mbedtls_net_connect( &server_fd, SERVER_ADDR, diff --git a/programs/ssl/dtls_server.c b/programs/ssl/dtls_server.c index 3ad10ceec..69da1d381 100644 --- a/programs/ssl/dtls_server.c +++ b/programs/ssl/dtls_server.c @@ -167,7 +167,7 @@ int main( void ) printf( " . Bind on udp/*/4433 ..." ); fflush( stdout ); - if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_UDP ) ) != 0 ) + if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_UDP ) ) != 0 ) { printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret ); goto exit; @@ -274,7 +274,7 @@ reset: } /* With UDP, bind_fd is hijacked by client_fd, so bind a new one */ - if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_UDP ) ) != 0 ) + if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_UDP ) ) != 0 ) { printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", -ret ); goto exit; diff --git a/programs/ssl/ssl_client1.c b/programs/ssl/ssl_client1.c index b67230514..c1da0a2ea 100644 --- a/programs/ssl/ssl_client1.c +++ b/programs/ssl/ssl_client1.c @@ -60,7 +60,7 @@ int main( void ) #include -#define SERVER_PORT 4433 +#define SERVER_PORT "4433" #define SERVER_NAME "localhost" #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n" @@ -132,8 +132,7 @@ int main( void ) /* * 1. Start the connection */ - mbedtls_printf( " . Connecting to tcp/%s/%4d...", SERVER_NAME, - SERVER_PORT ); + mbedtls_printf( " . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT ); fflush( stdout ); if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME, diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index b8a4a628d..b24d70437 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -70,7 +70,7 @@ int main( void ) #define DFL_SERVER_NAME "localhost" #define DFL_SERVER_ADDR NULL -#define DFL_SERVER_PORT 4433 +#define DFL_SERVER_PORT "4433" #define DFL_REQUEST_PAGE "/" #define DFL_REQUEST_SIZE -1 #define DFL_DEBUG_LEVEL 0 @@ -272,7 +272,7 @@ struct options { const char *server_name; /* hostname of the server (client only) */ const char *server_addr; /* address of the server (client only) */ - int server_port; /* port on which the ssl service runs */ + const char *server_port; /* port on which the ssl service runs */ int debug_level; /* level of debugging */ int nbio; /* should I/O be blocking? */ uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */ @@ -502,11 +502,7 @@ int main( int argc, char *argv[] ) else if( strcmp( p, "server_addr" ) == 0 ) opt.server_addr = q; else if( strcmp( p, "server_port" ) == 0 ) - { - opt.server_port = atoi( q ); - if( opt.server_port < 1 || opt.server_port > 65535 ) - goto usage; - } + opt.server_port = q; else if( strcmp( p, "dtls" ) == 0 ) { int t = atoi( q ); @@ -1026,7 +1022,7 @@ int main( int argc, char *argv[] ) if( opt.server_addr == NULL) opt.server_addr = opt.server_name; - mbedtls_printf( " . Connecting to %s/%s/%-4d...", + mbedtls_printf( " . Connecting to %s/%s/%s...", opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp", opt.server_addr, opt.server_port ); fflush( stdout ); diff --git a/programs/ssl/ssl_fork_server.c b/programs/ssl/ssl_fork_server.c index bafaa01f6..26855a2d3 100644 --- a/programs/ssl/ssl_fork_server.c +++ b/programs/ssl/ssl_fork_server.c @@ -202,7 +202,7 @@ int main( void ) mbedtls_printf( " . Bind on https://localhost:4433/ ..." ); fflush( stdout ); - if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_TCP ) ) != 0 ) + if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 ) { mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret ); goto exit; diff --git a/programs/ssl/ssl_mail_client.c b/programs/ssl/ssl_mail_client.c index c06d0c06d..f16eb8731 100644 --- a/programs/ssl/ssl_mail_client.c +++ b/programs/ssl/ssl_mail_client.c @@ -84,7 +84,7 @@ int main( void ) #endif #define DFL_SERVER_NAME "localhost" -#define DFL_SERVER_PORT 465 +#define DFL_SERVER_PORT "465" #define DFL_USER_NAME "user" #define DFL_USER_PWD "password" #define DFL_MAIL_FROM "" @@ -140,7 +140,7 @@ int main( void ) struct options { const char *server_name; /* hostname of the server (client only) */ - int server_port; /* port on which the ssl service runs */ + const char *server_port; /* port on which the ssl service runs */ int debug_level; /* level of debugging */ int authentication; /* if authentication is required */ int mode; /* SSL/TLS (0) or STARTTLS (1) */ @@ -416,11 +416,7 @@ int main( int argc, char *argv[] ) if( strcmp( p, "server_name" ) == 0 ) opt.server_name = q; else if( strcmp( p, "server_port" ) == 0 ) - { - opt.server_port = atoi( q ); - if( opt.server_port < 1 || opt.server_port > 65535 ) - goto usage; - } + opt.server_port = q; else if( strcmp( p, "debug_level" ) == 0 ) { opt.debug_level = atoi( q ); @@ -566,7 +562,7 @@ int main( int argc, char *argv[] ) /* * 2. Start the connection */ - mbedtls_printf( " . Connecting to tcp/%s/%-4d...", opt.server_name, + mbedtls_printf( " . Connecting to tcp/%s/%s...", opt.server_name, opt.server_port ); fflush( stdout ); diff --git a/programs/ssl/ssl_pthread_server.c b/programs/ssl/ssl_pthread_server.c index 3af66d045..3a137e315 100644 --- a/programs/ssl/ssl_pthread_server.c +++ b/programs/ssl/ssl_pthread_server.c @@ -448,7 +448,7 @@ int main( void ) mbedtls_printf( " . Bind on https://localhost:4433/ ..." ); fflush( stdout ); - if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_TCP ) ) != 0 ) + if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 ) { mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret ); goto exit; diff --git a/programs/ssl/ssl_server.c b/programs/ssl/ssl_server.c index f2f969c2d..6535ec9f4 100644 --- a/programs/ssl/ssl_server.c +++ b/programs/ssl/ssl_server.c @@ -161,7 +161,7 @@ int main( void ) mbedtls_printf( " . Bind on https://localhost:4433/ ..." ); fflush( stdout ); - if( ( ret = mbedtls_net_bind( &listen_fd, NULL, 4433, MBEDTLS_NET_PROTO_TCP ) ) != 0 ) + if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 ) { mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret ); goto exit; diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index 6ac24068d..763cccf60 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -91,7 +91,7 @@ int main( void ) #endif #define DFL_SERVER_ADDR NULL -#define DFL_SERVER_PORT 4433 +#define DFL_SERVER_PORT "4433" #define DFL_DEBUG_LEVEL 0 #define DFL_NBIO 0 #define DFL_READ_TIMEOUT 0 @@ -346,7 +346,7 @@ int main( void ) struct options { const char *server_addr; /* address on which the ssl service runs */ - int server_port; /* port on which the ssl service runs */ + const char *server_port; /* port on which the ssl service runs */ int debug_level; /* level of debugging */ int nbio; /* should I/O be blocking? */ uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */ @@ -931,11 +931,7 @@ int main( int argc, char *argv[] ) *q++ = '\0'; if( strcmp( p, "server_port" ) == 0 ) - { - opt.server_port = atoi( q ); - if( opt.server_port < 1 || opt.server_port > 65535 ) - goto usage; - } + opt.server_port = q; else if( strcmp( p, "server_addr" ) == 0 ) opt.server_addr = q; else if( strcmp( p, "dtls" ) == 0 ) @@ -1558,7 +1554,7 @@ int main( int argc, char *argv[] ) /* * 2. Setup the listening TCP socket */ - mbedtls_printf( " . Bind on %s://%s:%-4d/ ...", + mbedtls_printf( " . Bind on %s://%s:%s/ ...", opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp", opt.server_addr ? opt.server_addr : "*", opt.server_port ); @@ -1906,7 +1902,7 @@ reset: #if defined(MBEDTLS_SSL_PROTO_DTLS) if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) { - mbedtls_printf( " . Re-bind on udp://%s:%-4d/ ...", + mbedtls_printf( " . Re-bind on udp://%s:%s/ ...", opt.server_addr ? opt.server_addr : "*", opt.server_port ); fflush( stdout ); diff --git a/programs/test/udp_proxy.c b/programs/test/udp_proxy.c index 04dded3cc..f91d42f1d 100644 --- a/programs/test/udp_proxy.c +++ b/programs/test/udp_proxy.c @@ -76,9 +76,9 @@ int main( void ) #define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */ #define DFL_SERVER_ADDR "localhost" -#define DFL_SERVER_PORT 4433 +#define DFL_SERVER_PORT "4433" #define DFL_LISTEN_ADDR "localhost" -#define DFL_LISTEN_PORT 5556 +#define DFL_LISTEN_PORT "5556" #define USAGE \ "\n usage: udp_proxy param=<>...\n" \ @@ -110,9 +110,9 @@ int main( void ) static struct options { const char *server_addr; /* address to forward packets to */ - int server_port; /* port to forward packets to */ + const char *server_port; /* port to forward packets to */ const char *listen_addr; /* address for accepting client connections */ - int listen_port; /* port for accepting client connections */ + const char *listen_port; /* port for accepting client connections */ int duplicate; /* duplicate 1 in N packets (none if 0) */ int delay; /* delay 1 packet in N (none if 0) */ @@ -158,19 +158,11 @@ static void get_options( int argc, char *argv[] ) if( strcmp( p, "server_addr" ) == 0 ) opt.server_addr = q; else if( strcmp( p, "server_port" ) == 0 ) - { - opt.server_port = atoi( q ); - if( opt.server_port < 1 || opt.server_port > 65535 ) - exit_usage( p, q ); - } + opt.server_port = q; else if( strcmp( p, "listen_addr" ) == 0 ) opt.listen_addr = q; else if( strcmp( p, "listen_port" ) == 0 ) - { - opt.listen_port = atoi( q ); - if( opt.listen_port < 1 || opt.listen_port > 65535 ) - exit_usage( p, q ); - } + opt.listen_port = q; else if( strcmp( p, "duplicate" ) == 0 ) { opt.duplicate = atoi( q ); @@ -498,7 +490,7 @@ int main( int argc, char *argv[] ) /* * 0. "Connect" to the server */ - mbedtls_printf( " . Connect to server on UDP/%s/%d ...", + mbedtls_printf( " . Connect to server on UDP/%s/%s ...", opt.server_addr, opt.server_port ); fflush( stdout ); @@ -514,7 +506,7 @@ int main( int argc, char *argv[] ) /* * 1. Setup the "listening" UDP socket */ - mbedtls_printf( " . Bind on UDP/%s/%d ...", + mbedtls_printf( " . Bind on UDP/%s/%s ...", opt.listen_addr, opt.listen_port ); fflush( stdout ); @@ -544,7 +536,7 @@ accept: mbedtls_printf( " ok\n" ); fflush( stdout ); - mbedtls_printf( " . Re-bind on UDP/%s/%d ...", + mbedtls_printf( " . Re-bind on UDP/%s/%s ...", opt.listen_addr, opt.listen_port ); fflush( stdout ); diff --git a/programs/x509/cert_app.c b/programs/x509/cert_app.c index d93aeba17..bd5ccda98 100644 --- a/programs/x509/cert_app.c +++ b/programs/x509/cert_app.c @@ -70,7 +70,7 @@ int main( void ) #define DFL_CRL_FILE "" #define DFL_CA_PATH "" #define DFL_SERVER_NAME "localhost" -#define DFL_SERVER_PORT 4433 +#define DFL_SERVER_PORT "4433" #define DFL_DEBUG_LEVEL 0 #define DFL_PERMISSIVE 0 @@ -105,7 +105,7 @@ struct options const char *crl_file; /* the file with the CRL to use */ const char *ca_path; /* the path with the CA certificate(s) reside */ const char *server_name; /* hostname of the server (client only) */ - int server_port; /* port on which the ssl service runs */ + const char *server_port; /* port on which the ssl service runs */ int debug_level; /* level of debugging */ int permissive; /* permissive parsing */ } opt; @@ -226,11 +226,7 @@ int main( int argc, char *argv[] ) else if( strcmp( p, "server_name" ) == 0 ) opt.server_name = q; else if( strcmp( p, "server_port" ) == 0 ) - { - opt.server_port = atoi( q ); - if( opt.server_port < 1 || opt.server_port > 65535 ) - goto usage; - } + opt.server_port = q; else if( strcmp( p, "debug_level" ) == 0 ) { opt.debug_level = atoi( q ); @@ -383,8 +379,8 @@ int main( int argc, char *argv[] ) /* * 2. Start the connection */ - mbedtls_printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name, - opt.server_port ); + mbedtls_printf( " . SSL connection to tcp/%s/%s...", opt.server_name, + opt.server_port ); fflush( stdout ); if( ( ret = mbedtls_net_connect( &server_fd, opt.server_name,