- Main Page
- Data Structures
- Files

# net.c

Go to the documentation of this file.

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