net.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * TCP networking functions
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /* Note: This file has been modified by ST's MCD Application Team */
  26. #include "config.h"
  27. #if defined(POLARSSL_NET_C)
  28. #include "polarssl/net.h"
  29. #if defined(_WIN32) || defined(_WIN32_WCE)
  30. #include <winsock2.h>
  31. #include <windows.h>
  32. #if defined(_WIN32_WCE)
  33. #pragma comment( lib, "ws2.lib" )
  34. #else
  35. #pragma comment( lib, "ws2_32.lib" )
  36. #endif
  37. #define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
  38. #define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
  39. #define close(fd) closesocket(fd)
  40. static int wsa_init_done = 0;
  41. #else
  42. #if (!defined(__ICCARM__)) && (!defined(__CC_ARM)) && (!defined(__GNUC__)) && (!defined(__TASKING__))
  43. #include <sys/types.h>
  44. #endif
  45. #include "lwip/sockets.h"
  46. #include "lwip/inet.h"
  47. #if LWIP_DNS == 1
  48. #include "lwip/netdb.h"
  49. #endif
  50. //#include <sys/time.h>
  51. //#include <unistd.h>
  52. #include <signal.h>
  53. #if (!defined(__ICCARM__)) && (!defined(__CC_ARM)) && (!defined(__GNUC__)) && (!defined(__TASKING__))
  54. #include <fcntl.h>
  55. #endif
  56. #include <errno.h>
  57. #if defined(__FreeBSD__) || defined(__OpenBSD__)
  58. #include <sys/endian.h>
  59. #elif defined(__APPLE__)
  60. #include <machine/endian.h>
  61. #else
  62. //#include <endian.h>
  63. #endif
  64. #endif
  65. #include <stdlib.h>
  66. #include <stdio.h>
  67. #include <time.h>
  68. /*
  69. * htons() is not always available.
  70. * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
  71. * to help determine endianess.
  72. */
  73. #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
  74. #define POLARSSL_HTONS(n) (n)
  75. #else
  76. #define POLARSSL_HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
  77. #endif
  78. unsigned short net_htons(unsigned short n);
  79. #define net_htons(n) POLARSSL_HTONS(n)
  80. /*
  81. * Initiate a TCP connection with host:port
  82. */
  83. int net_connect( int *fd, const char *host, int port )
  84. {
  85. struct sockaddr_in server_addr;
  86. #if LWIP_DNS == 1
  87. struct hostent *server_host;
  88. #endif
  89. #if defined(_WIN32) || defined(_WIN32_WCE)
  90. WSADATA wsaData;
  91. if( wsa_init_done == 0 )
  92. {
  93. if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
  94. return( POLARSSL_ERR_NET_SOCKET_FAILED );
  95. wsa_init_done = 1;
  96. }
  97. #else
  98. #if (!defined(__ICCARM__)) && (!defined(__CC_ARM)) && (!defined(__GNUC__)) && (!defined(__TASKING__))
  99. signal( SIGPIPE, SIG_IGN );
  100. #endif
  101. #endif
  102. #if LWIP_DNS == 1
  103. if( ( server_host = gethostbyname( host ) ) == NULL ) {
  104. printf( "SSL: ! gethostbyname ERROR :" );
  105. printf(host);
  106. printf( "\n" );
  107. return( POLARSSL_ERR_NET_UNKNOWN_HOST );
  108. }
  109. printf ( "SSL: gethostbyname OK\n" );
  110. memcpy( (void *) &server_addr.sin_addr,
  111. (void *) server_host->h_addr,
  112. server_host->h_length );
  113. #else
  114. printf( "SSL: no DNS mode\n" );
  115. memset(&server_addr, 0, sizeof(server_addr));
  116. server_addr.sin_len = sizeof(server_addr);
  117. server_addr.sin_addr.s_addr = inet_addr(host);
  118. #endif
  119. if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
  120. return( POLARSSL_ERR_NET_SOCKET_FAILED );
  121. server_addr.sin_family = AF_INET;
  122. server_addr.sin_port = net_htons( port );
  123. if( connect( *fd, (struct sockaddr *) &server_addr,
  124. sizeof( server_addr ) ) < 0 )
  125. {
  126. close( *fd );
  127. return( POLARSSL_ERR_NET_CONNECT_FAILED );
  128. }
  129. return( 0 );
  130. }
  131. /*
  132. * Create a listening socket on bind_ip:port
  133. */
  134. int net_bind( int *fd, const char *bind_ip, int port )
  135. {
  136. int n, c[4];
  137. struct sockaddr_in server_addr;
  138. #if defined(_WIN32) || defined(_WIN32_WCE)
  139. WSADATA wsaData;
  140. if( wsa_init_done == 0 )
  141. {
  142. if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
  143. return( POLARSSL_ERR_NET_SOCKET_FAILED );
  144. wsa_init_done = 1;
  145. }
  146. #else
  147. #if (!defined(__ICCARM__)) && (!defined(__CC_ARM)) && (!defined(__GNUC__)) && (!defined(__TASKING__))
  148. signal( SIGPIPE, SIG_IGN );
  149. #endif
  150. #endif
  151. if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
  152. return( POLARSSL_ERR_NET_SOCKET_FAILED );
  153. n = 1;
  154. setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
  155. (const char *) &n, sizeof( n ) );
  156. server_addr.sin_addr.s_addr = INADDR_ANY;
  157. server_addr.sin_family = AF_INET;
  158. server_addr.sin_port = net_htons( port );
  159. if( bind_ip != NULL )
  160. {
  161. memset( c, 0, sizeof( c ) );
  162. sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
  163. for( n = 0; n < 4; n++ )
  164. if( c[n] < 0 || c[n] > 255 )
  165. break;
  166. if( n == 4 )
  167. server_addr.sin_addr.s_addr =
  168. ( (unsigned long) c[0] << 24 ) |
  169. ( (unsigned long) c[1] << 16 ) |
  170. ( (unsigned long) c[2] << 8 ) |
  171. ( (unsigned long) c[3] );
  172. }
  173. if( bind( *fd, (struct sockaddr *) &server_addr,
  174. sizeof( server_addr ) ) < 0 )
  175. {
  176. close( *fd );
  177. return( POLARSSL_ERR_NET_BIND_FAILED );
  178. }
  179. if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
  180. {
  181. close( *fd );
  182. return( POLARSSL_ERR_NET_LISTEN_FAILED );
  183. }
  184. return( 0 );
  185. }
  186. /*
  187. * Check if the current operation is blocking
  188. */
  189. static int net_is_blocking( void )
  190. {
  191. #if defined(_WIN32) || defined(_WIN32_WCE)
  192. return( WSAGetLastError() == WSAEWOULDBLOCK );
  193. #else
  194. switch( errno )
  195. {
  196. #if defined EAGAIN
  197. case EAGAIN:
  198. #endif
  199. #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
  200. case EWOULDBLOCK:
  201. #endif
  202. return( 1 );
  203. }
  204. return( 0 );
  205. #endif
  206. }
  207. /*
  208. * Accept a connection from a remote client
  209. */
  210. int net_accept( int bind_fd, int *client_fd, void *client_ip )
  211. {
  212. struct sockaddr_in client_addr;
  213. #if defined(__socklen_t_defined) || defined(_SOCKLEN_T)
  214. socklen_t n = (socklen_t) sizeof( client_addr );
  215. #else
  216. int n = (int) sizeof( client_addr );
  217. #endif
  218. *client_fd = accept( bind_fd, (struct sockaddr *)
  219. &client_addr, &n );
  220. if( *client_fd < 0 )
  221. {
  222. if( net_is_blocking() != 0 )
  223. return( POLARSSL_ERR_NET_WANT_READ );
  224. return( POLARSSL_ERR_NET_ACCEPT_FAILED );
  225. }
  226. if( client_ip != NULL )
  227. memcpy( client_ip, &client_addr.sin_addr.s_addr,
  228. sizeof( client_addr.sin_addr.s_addr ) );
  229. return( 0 );
  230. }
  231. /*
  232. * Set the socket blocking or non-blocking
  233. */
  234. int net_set_block( int fd )
  235. {
  236. #if defined(WIN32) || defined(_WIN32_WCE) || defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__) || defined(__TASKING__)
  237. unsigned long n = 0;
  238. return( ioctlsocket( fd, FIONBIO, &n ) );
  239. #else
  240. return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
  241. #endif
  242. }
  243. int net_set_nonblock( int fd )
  244. {
  245. #if defined(WIN32) || defined(_WIN32_WCE) || defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__) || defined(__TASKING__)
  246. unsigned long n = 1;
  247. return( ioctlsocket( fd, FIONBIO, &n ) );
  248. #else
  249. return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
  250. #endif
  251. }
  252. /*
  253. * Portable usleep helper
  254. */
  255. void net_usleep( unsigned long usec )
  256. {
  257. struct timeval tv;
  258. tv.tv_sec = 0;
  259. tv.tv_usec = usec;
  260. select( 0, NULL, NULL, NULL, &tv );
  261. }
  262. /*
  263. * Read at most 'len' characters
  264. */
  265. int net_recv( void *ctx, unsigned char *buf, size_t len )
  266. {
  267. int ret = read( *((int *) ctx), buf, len );
  268. if( ret < 0 )
  269. {
  270. if( net_is_blocking() != 0 )
  271. return( POLARSSL_ERR_NET_WANT_READ );
  272. #if defined(_WIN32) || defined(_WIN32_WCE)
  273. if( WSAGetLastError() == WSAECONNRESET )
  274. return( POLARSSL_ERR_NET_CONN_RESET );
  275. #else
  276. if( errno == EPIPE || errno == ECONNRESET )
  277. return( POLARSSL_ERR_NET_CONN_RESET );
  278. if( errno == EINTR )
  279. return( POLARSSL_ERR_NET_WANT_READ );
  280. #endif
  281. return( POLARSSL_ERR_NET_RECV_FAILED );
  282. }
  283. return( ret );
  284. }
  285. /*
  286. * Write at most 'len' characters
  287. */
  288. int net_send( void *ctx, const unsigned char *buf, size_t len )
  289. {
  290. int ret = write( *((int *) ctx), buf, len );
  291. if( ret < 0 )
  292. {
  293. if( net_is_blocking() != 0 )
  294. return( POLARSSL_ERR_NET_WANT_WRITE );
  295. #if defined(_WIN32) || defined(_WIN32_WCE)
  296. if( WSAGetLastError() == WSAECONNRESET )
  297. return( POLARSSL_ERR_NET_CONN_RESET );
  298. #else
  299. if( errno == EPIPE || errno == ECONNRESET )
  300. return( POLARSSL_ERR_NET_CONN_RESET );
  301. if( errno == EINTR )
  302. return( POLARSSL_ERR_NET_WANT_WRITE );
  303. #endif
  304. return( POLARSSL_ERR_NET_SEND_FAILED );
  305. }
  306. return( ret );
  307. }
  308. /*
  309. * Gracefully close the connection
  310. */
  311. void net_close( int fd )
  312. {
  313. shutdown( fd, 2 );
  314. close( fd );
  315. }
  316. #endif