PolarSSL v1.1.4
net.c
Go to the documentation of this file.
00001 /*
00002  *  TCP networking functions
00003  *
00004  *  Copyright (C) 2006-2010, Brainspark B.V.
00005  *
00006  *  This file is part of PolarSSL (http://www.polarssl.org)
00007  *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
00008  *
00009  *  All rights reserved.
00010  *
00011  *  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version.
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU General Public License for more details.
00020  *
00021  *  You should have received a copy of the GNU General Public License along
00022  *  with this program; if not, write to the Free Software Foundation, Inc.,
00023  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00024  */
00025 
00026 #include "polarssl/config.h"
00027 
00028 #if defined(POLARSSL_NET_C)
00029 
00030 #include "polarssl/net.h"
00031 
00032 #if defined(_WIN32) || defined(_WIN32_WCE)
00033 
00034 #include <winsock2.h>
00035 #include <windows.h>
00036 
00037 #if defined(_WIN32_WCE)
00038 #pragma comment( lib, "ws2.lib" )
00039 #else
00040 #pragma comment( lib, "ws2_32.lib" )
00041 #endif
00042 
00043 #define read(fd,buf,len)        recv(fd,(char*)buf,(int) len,0)
00044 #define write(fd,buf,len)       send(fd,(char*)buf,(int) len,0)
00045 #define close(fd)               closesocket(fd)
00046 
00047 static int wsa_init_done = 0;
00048 
00049 #else
00050 
00051 #include <sys/types.h>
00052 #include <sys/socket.h>
00053 #include <netinet/in.h>
00054 #include <arpa/inet.h>
00055 #include <sys/time.h>
00056 #include <unistd.h>
00057 #include <signal.h>
00058 #include <fcntl.h>
00059 #include <netdb.h>
00060 #include <errno.h>
00061 
00062 #if defined(__FreeBSD__) || defined(__OpenBSD__)
00063 #include <sys/endian.h>
00064 #elif defined(__APPLE__)
00065 #include <machine/endian.h>
00066 #else
00067 #include <endian.h>
00068 #endif
00069 
00070 #endif
00071 
00072 #include <stdlib.h>
00073 #include <stdio.h>
00074 #include <time.h>
00075 
00076 /*
00077  * htons() is not always available.
00078  * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
00079  * to help determine endianess.
00080  */
00081 #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
00082 #define POLARSSL_HTONS(n) (n)
00083 #else
00084 #define POLARSSL_HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
00085 #endif
00086 
00087 unsigned short net_htons(unsigned short n);
00088 #define net_htons(n) POLARSSL_HTONS(n)
00089 
00090 /*
00091  * Initiate a TCP connection with host:port
00092  */
00093 int net_connect( int *fd, const char *host, int port )
00094 {
00095     struct sockaddr_in server_addr;
00096     struct hostent *server_host;
00097 
00098 #if defined(_WIN32) || defined(_WIN32_WCE)
00099     WSADATA wsaData;
00100 
00101     if( wsa_init_done == 0 )
00102     {
00103         if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
00104             return( POLARSSL_ERR_NET_SOCKET_FAILED );
00105 
00106         wsa_init_done = 1;
00107     }
00108 #else
00109     signal( SIGPIPE, SIG_IGN );
00110 #endif
00111 
00112     if( ( server_host = gethostbyname( host ) ) == NULL )
00113         return( POLARSSL_ERR_NET_UNKNOWN_HOST );
00114 
00115     if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
00116         return( POLARSSL_ERR_NET_SOCKET_FAILED );
00117 
00118     memcpy( (void *) &server_addr.sin_addr,
00119             (void *) server_host->h_addr,
00120                      server_host->h_length );
00121 
00122     server_addr.sin_family = AF_INET;
00123     server_addr.sin_port   = net_htons( port );
00124 
00125     if( connect( *fd, (struct sockaddr *) &server_addr,
00126                  sizeof( server_addr ) ) < 0 )
00127     {
00128         close( *fd );
00129         return( POLARSSL_ERR_NET_CONNECT_FAILED );
00130     }
00131 
00132     return( 0 );
00133 }
00134 
00135 /*
00136  * Create a listening socket on bind_ip:port
00137  */
00138 int net_bind( int *fd, const char *bind_ip, int port )
00139 {
00140     int n, c[4];
00141     struct sockaddr_in server_addr;
00142 
00143 #if defined(_WIN32) || defined(_WIN32_WCE)
00144     WSADATA wsaData;
00145 
00146     if( wsa_init_done == 0 )
00147     {
00148         if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
00149             return( POLARSSL_ERR_NET_SOCKET_FAILED );
00150 
00151         wsa_init_done = 1;
00152     }
00153 #else
00154     signal( SIGPIPE, SIG_IGN );
00155 #endif
00156 
00157     if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
00158         return( POLARSSL_ERR_NET_SOCKET_FAILED );
00159 
00160     n = 1;
00161     setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
00162                 (const char *) &n, sizeof( n ) );
00163 
00164     server_addr.sin_addr.s_addr = INADDR_ANY;
00165     server_addr.sin_family      = AF_INET;
00166     server_addr.sin_port        = net_htons( port );
00167 
00168     if( bind_ip != NULL )
00169     {
00170         memset( c, 0, sizeof( c ) );
00171         sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
00172 
00173         for( n = 0; n < 4; n++ )
00174             if( c[n] < 0 || c[n] > 255 )
00175                 break;
00176 
00177         if( n == 4 )
00178             server_addr.sin_addr.s_addr =
00179                 ( (unsigned long) c[0] << 24 ) |
00180                 ( (unsigned long) c[1] << 16 ) |
00181                 ( (unsigned long) c[2] <<  8 ) |
00182                 ( (unsigned long) c[3]       );
00183     }
00184 
00185     if( bind( *fd, (struct sockaddr *) &server_addr,
00186               sizeof( server_addr ) ) < 0 )
00187     {
00188         close( *fd );
00189         return( POLARSSL_ERR_NET_BIND_FAILED );
00190     }
00191 
00192     if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
00193     {
00194         close( *fd );
00195         return( POLARSSL_ERR_NET_LISTEN_FAILED );
00196     }
00197 
00198     return( 0 );
00199 }
00200 
00201 /*
00202  * Check if the current operation is blocking
00203  */
00204 static int net_is_blocking( void )
00205 {
00206 #if defined(_WIN32) || defined(_WIN32_WCE)
00207     return( WSAGetLastError() == WSAEWOULDBLOCK );
00208 #else
00209     switch( errno )
00210     {
00211 #if defined EAGAIN
00212         case EAGAIN:
00213 #endif
00214 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
00215         case EWOULDBLOCK:
00216 #endif
00217             return( 1 );
00218     }
00219     return( 0 );
00220 #endif
00221 }
00222 
00223 /*
00224  * Accept a connection from a remote client
00225  */
00226 int net_accept( int bind_fd, int *client_fd, void *client_ip )
00227 {
00228     struct sockaddr_in client_addr;
00229 
00230 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) ||  \
00231     defined(_SOCKLEN_T_DECLARED)
00232     socklen_t n = (socklen_t) sizeof( client_addr );
00233 #else
00234     int n = (int) sizeof( client_addr );
00235 #endif
00236 
00237     *client_fd = accept( bind_fd, (struct sockaddr *)
00238                          &client_addr, &n );
00239 
00240     if( *client_fd < 0 )
00241     {
00242         if( net_is_blocking() != 0 )
00243             return( POLARSSL_ERR_NET_WANT_READ );
00244 
00245         return( POLARSSL_ERR_NET_ACCEPT_FAILED );
00246     }
00247 
00248     if( client_ip != NULL )
00249         memcpy( client_ip, &client_addr.sin_addr.s_addr,
00250                     sizeof( client_addr.sin_addr.s_addr ) );
00251 
00252     return( 0 );
00253 }
00254 
00255 /*
00256  * Set the socket blocking or non-blocking
00257  */
00258 int net_set_block( int fd )
00259 {
00260 #if defined(_WIN32) || defined(_WIN32_WCE)
00261     u_long n = 0;
00262     return( ioctlsocket( fd, FIONBIO, &n ) );
00263 #else
00264     return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
00265 #endif
00266 }
00267 
00268 int net_set_nonblock( int fd )
00269 {
00270 #if defined(_WIN32) || defined(_WIN32_WCE)
00271     u_long n = 1;
00272     return( ioctlsocket( fd, FIONBIO, &n ) );
00273 #else
00274     return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
00275 #endif
00276 }
00277 
00278 /*
00279  * Portable usleep helper
00280  */
00281 void net_usleep( unsigned long usec )
00282 {
00283     struct timeval tv;
00284     tv.tv_sec  = 0;
00285     tv.tv_usec = usec;
00286     select( 0, NULL, NULL, NULL, &tv );
00287 }
00288 
00289 /*
00290  * Read at most 'len' characters
00291  */
00292 int net_recv( void *ctx, unsigned char *buf, size_t len )
00293 { 
00294     int ret = read( *((int *) ctx), buf, len );
00295 
00296     if( ret < 0 )
00297     {
00298         if( net_is_blocking() != 0 )
00299             return( POLARSSL_ERR_NET_WANT_READ );
00300 
00301 #if defined(_WIN32) || defined(_WIN32_WCE)
00302         if( WSAGetLastError() == WSAECONNRESET )
00303             return( POLARSSL_ERR_NET_CONN_RESET );
00304 #else
00305         if( errno == EPIPE || errno == ECONNRESET )
00306             return( POLARSSL_ERR_NET_CONN_RESET );
00307 
00308         if( errno == EINTR )
00309             return( POLARSSL_ERR_NET_WANT_READ );
00310 #endif
00311 
00312         return( POLARSSL_ERR_NET_RECV_FAILED );
00313     }
00314 
00315     return( ret );
00316 }
00317 
00318 /*
00319  * Write at most 'len' characters
00320  */
00321 int net_send( void *ctx, const unsigned char *buf, size_t len )
00322 {
00323     int ret = write( *((int *) ctx), buf, len );
00324 
00325     if( ret < 0 )
00326     {
00327         if( net_is_blocking() != 0 )
00328             return( POLARSSL_ERR_NET_WANT_WRITE );
00329 
00330 #if defined(_WIN32) || defined(_WIN32_WCE)
00331         if( WSAGetLastError() == WSAECONNRESET )
00332             return( POLARSSL_ERR_NET_CONN_RESET );
00333 #else
00334         if( errno == EPIPE || errno == ECONNRESET )
00335             return( POLARSSL_ERR_NET_CONN_RESET );
00336 
00337         if( errno == EINTR )
00338             return( POLARSSL_ERR_NET_WANT_WRITE );
00339 #endif
00340 
00341         return( POLARSSL_ERR_NET_SEND_FAILED );
00342     }
00343 
00344     return( ret );
00345 }
00346 
00347 /*
00348  * Gracefully close the connection
00349  */
00350 void net_close( int fd )
00351 {
00352     shutdown( fd, 2 );
00353     close( fd );
00354 }
00355 
00356 #endif