00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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)) && !defined(EFIX64) && \
00033 !defined(EFI32)
00034
00035 #include <winsock2.h>
00036 #include <windows.h>
00037
00038 #if defined(_WIN32_WCE)
00039 #pragma comment( lib, "ws2.lib" )
00040 #else
00041 #pragma comment( lib, "ws2_32.lib" )
00042 #endif
00043
00044 #define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
00045 #define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
00046 #define close(fd) closesocket(fd)
00047
00048 static int wsa_init_done = 0;
00049
00050 #else
00051
00052 #include <sys/types.h>
00053 #include <sys/socket.h>
00054 #include <netinet/in.h>
00055 #include <arpa/inet.h>
00056 #if defined(POLARSSL_HAVE_TIME)
00057 #include <sys/time.h>
00058 #endif
00059 #include <unistd.h>
00060 #include <signal.h>
00061 #include <fcntl.h>
00062 #include <netdb.h>
00063 #include <errno.h>
00064
00065 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
00066 defined(__DragonflyBSD__)
00067 #include <sys/endian.h>
00068 #elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
00069 defined(EFIX64) || defined(EFI32)
00070 #include <machine/endian.h>
00071 #elif defined(sun)
00072 #include <sys/isa_defs.h>
00073 #elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
00074 #include <arpa/nameser_compat.h>
00075 #else
00076 #include <endian.h>
00077 #endif
00078
00079 #endif
00080
00081 #include <stdlib.h>
00082 #include <stdio.h>
00083
00084 #if defined(POLARSSL_HAVE_TIME)
00085 #include <time.h>
00086 #endif
00087
00088 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
00089 #include <basetsd.h>
00090 typedef UINT32 uint32_t;
00091 #else
00092 #include <inttypes.h>
00093 #endif
00094
00095
00096
00097
00098
00099
00100 #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
00101 #define POLARSSL_HTONS(n) (n)
00102 #define POLARSSL_HTONL(n) (n)
00103 #else
00104 #define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
00105 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
00106 #define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
00107 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
00108 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
00109 (((unsigned long )(n) & 0xFF000000) >> 24))
00110 #endif
00111
00112 unsigned short net_htons(unsigned short n);
00113 unsigned long net_htonl(unsigned long n);
00114 #define net_htons(n) POLARSSL_HTONS(n)
00115 #define net_htonl(n) POLARSSL_HTONL(n)
00116
00117
00118
00119
00120 int net_connect( int *fd, const char *host, int port )
00121 {
00122 struct sockaddr_in server_addr;
00123 struct hostent *server_host;
00124
00125 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
00126 !defined(EFI32)
00127
00128 WSADATA wsaData;
00129
00130 if( wsa_init_done == 0 )
00131 {
00132 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
00133 return( POLARSSL_ERR_NET_SOCKET_FAILED );
00134
00135 wsa_init_done = 1;
00136 }
00137 #else
00138 #if !defined(EFIX64) && !defined(EFI32)
00139 signal( SIGPIPE, SIG_IGN );
00140 #endif
00141 #endif
00142
00143 if( ( server_host = gethostbyname( host ) ) == NULL )
00144 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
00145
00146 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
00147 return( POLARSSL_ERR_NET_SOCKET_FAILED );
00148
00149 memcpy( (void *) &server_addr.sin_addr,
00150 (void *) server_host->h_addr,
00151 server_host->h_length );
00152
00153 server_addr.sin_family = AF_INET;
00154 server_addr.sin_port = net_htons( port );
00155
00156 if( connect( *fd, (struct sockaddr *) &server_addr,
00157 sizeof( server_addr ) ) < 0 )
00158 {
00159 close( *fd );
00160 return( POLARSSL_ERR_NET_CONNECT_FAILED );
00161 }
00162
00163 return( 0 );
00164 }
00165
00166
00167
00168
00169 int net_bind( int *fd, const char *bind_ip, int port )
00170 {
00171 int n, c[4];
00172 struct sockaddr_in server_addr;
00173
00174 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
00175 !defined(EFI32)
00176 WSADATA wsaData;
00177
00178 if( wsa_init_done == 0 )
00179 {
00180 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
00181 return( POLARSSL_ERR_NET_SOCKET_FAILED );
00182
00183 wsa_init_done = 1;
00184 }
00185 #else
00186 #if !defined(EFIX64) && !defined(EFI32)
00187 signal( SIGPIPE, SIG_IGN );
00188 #endif
00189 #endif
00190
00191 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
00192 return( POLARSSL_ERR_NET_SOCKET_FAILED );
00193
00194 n = 1;
00195 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
00196 (const char *) &n, sizeof( n ) );
00197
00198 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
00199 server_addr.sin_family = AF_INET;
00200 server_addr.sin_port = net_htons( port );
00201
00202 if( bind_ip != NULL )
00203 {
00204 memset( c, 0, sizeof( c ) );
00205 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
00206
00207 for( n = 0; n < 4; n++ )
00208 if( c[n] < 0 || c[n] > 255 )
00209 break;
00210
00211 if( n == 4 )
00212 server_addr.sin_addr.s_addr = net_htonl(
00213 ( (uint32_t) c[0] << 24 ) |
00214 ( (uint32_t) c[1] << 16 ) |
00215 ( (uint32_t) c[2] << 8 ) |
00216 ( (uint32_t) c[3] ) );
00217 }
00218
00219 if( bind( *fd, (struct sockaddr *) &server_addr,
00220 sizeof( server_addr ) ) < 0 )
00221 {
00222 close( *fd );
00223 return( POLARSSL_ERR_NET_BIND_FAILED );
00224 }
00225
00226 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
00227 {
00228 close( *fd );
00229 return( POLARSSL_ERR_NET_LISTEN_FAILED );
00230 }
00231
00232 return( 0 );
00233 }
00234
00235
00236
00237
00238 static int net_is_blocking( void )
00239 {
00240 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
00241 !defined(EFI32)
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
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) || defined(_SOCKLEN_T) || \
00266 defined(_SOCKLEN_T_DECLARED)
00267 socklen_t n = (socklen_t) sizeof( client_addr );
00268 #else
00269 int n = (int) sizeof( client_addr );
00270 #endif
00271
00272 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
00273 &client_addr, &n );
00274
00275 if( *client_fd < 0 )
00276 {
00277 if( net_is_blocking() != 0 )
00278 return( POLARSSL_ERR_NET_WANT_READ );
00279
00280 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
00281 }
00282
00283 if( client_ip != NULL )
00284 memcpy( client_ip, &client_addr.sin_addr.s_addr,
00285 sizeof( client_addr.sin_addr.s_addr ) );
00286
00287 return( 0 );
00288 }
00289
00290
00291
00292
00293 int net_set_block( int fd )
00294 {
00295 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
00296 !defined(EFI32)
00297 u_long n = 0;
00298 return( ioctlsocket( fd, FIONBIO, &n ) );
00299 #else
00300 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
00301 #endif
00302 }
00303
00304 int net_set_nonblock( int fd )
00305 {
00306 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
00307 !defined(EFI32)
00308 u_long n = 1;
00309 return( ioctlsocket( fd, FIONBIO, &n ) );
00310 #else
00311 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
00312 #endif
00313 }
00314
00315 #if defined(POLARSSL_HAVE_TIME)
00316
00317
00318
00319 void net_usleep( unsigned long usec )
00320 {
00321 struct timeval tv;
00322 tv.tv_sec = 0;
00323 tv.tv_usec = usec;
00324 select( 0, NULL, NULL, NULL, &tv );
00325 }
00326 #endif
00327
00328
00329
00330
00331 int net_recv( void *ctx, unsigned char *buf, size_t len )
00332 {
00333 int ret = read( *((int *) ctx), buf, len );
00334
00335 if( ret < 0 )
00336 {
00337 if( net_is_blocking() != 0 )
00338 return( POLARSSL_ERR_NET_WANT_READ );
00339
00340 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
00341 !defined(EFI32)
00342 if( WSAGetLastError() == WSAECONNRESET )
00343 return( POLARSSL_ERR_NET_CONN_RESET );
00344 #else
00345 if( errno == EPIPE || errno == ECONNRESET )
00346 return( POLARSSL_ERR_NET_CONN_RESET );
00347
00348 if( errno == EINTR )
00349 return( POLARSSL_ERR_NET_WANT_READ );
00350 #endif
00351
00352 return( POLARSSL_ERR_NET_RECV_FAILED );
00353 }
00354
00355 return( ret );
00356 }
00357
00358
00359
00360
00361 int net_send( void *ctx, const unsigned char *buf, size_t len )
00362 {
00363 int ret = write( *((int *) ctx), buf, len );
00364
00365 if( ret < 0 )
00366 {
00367 if( net_is_blocking() != 0 )
00368 return( POLARSSL_ERR_NET_WANT_WRITE );
00369
00370 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
00371 !defined(EFI32)
00372 if( WSAGetLastError() == WSAECONNRESET )
00373 return( POLARSSL_ERR_NET_CONN_RESET );
00374 #else
00375 if( errno == EPIPE || errno == ECONNRESET )
00376 return( POLARSSL_ERR_NET_CONN_RESET );
00377
00378 if( errno == EINTR )
00379 return( POLARSSL_ERR_NET_WANT_WRITE );
00380 #endif
00381
00382 return( POLARSSL_ERR_NET_SEND_FAILED );
00383 }
00384
00385 return( ret );
00386 }
00387
00388
00389
00390
00391 void net_close( int fd )
00392 {
00393 shutdown( fd, 2 );
00394 close( fd );
00395 }
00396
00397 #endif