Factor our some code in net.c

This commit is contained in:
Manuel Pégourié-Gonnard 2013-12-13 11:55:32 +01:00
parent 452f6ba1a6
commit 2e5c3163db

View File

@ -115,16 +115,12 @@ unsigned long net_htonl(unsigned long n);
#define net_htonl(n) POLARSSL_HTONL(n) #define net_htonl(n) POLARSSL_HTONL(n)
/* /*
* Initiate a TCP connection with host:port * Prepare for using the sockets interface
*/ */
int net_connect( int *fd, const char *host, int port ) static void net_prepare( void )
{ {
struct sockaddr_in server_addr;
struct hostent *server_host;
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
!defined(EFI32) !defined(EFI32)
WSADATA wsaData; WSADATA wsaData;
if( wsa_init_done == 0 ) if( wsa_init_done == 0 )
@ -139,6 +135,17 @@ int net_connect( int *fd, const char *host, int port )
signal( SIGPIPE, SIG_IGN ); signal( SIGPIPE, SIG_IGN );
#endif #endif
#endif #endif
}
/*
* Initiate a TCP connection with host:port
*/
int net_connect( int *fd, const char *host, int port )
{
struct sockaddr_in server_addr;
struct hostent *server_host;
net_prepare();
if( ( server_host = gethostbyname( host ) ) == NULL ) if( ( server_host = gethostbyname( host ) ) == NULL )
return( POLARSSL_ERR_NET_UNKNOWN_HOST ); return( POLARSSL_ERR_NET_UNKNOWN_HOST );
@ -171,22 +178,7 @@ int net_bind( int *fd, const char *bind_ip, int port )
int n, c[4]; int n, c[4];
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ net_prepare();
!defined(EFI32)
WSADATA wsaData;
if( wsa_init_done == 0 )
{
if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
return( POLARSSL_ERR_NET_SOCKET_FAILED );
wsa_init_done = 1;
}
#else
#if !defined(EFIX64) && !defined(EFI32)
signal( SIGPIPE, SIG_IGN );
#endif
#endif
if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 ) if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
return( POLARSSL_ERR_NET_SOCKET_FAILED ); return( POLARSSL_ERR_NET_SOCKET_FAILED );