net.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #ifdef PRINTF_STDLIB
  67. #include <stdio.h>
  68. #endif
  69. #ifdef PRINTF_CUSTOM
  70. #include "tinystdio.h"
  71. #endif
  72. #include <time.h>
  73. /*
  74. * htons() is not always available.
  75. * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
  76. * to help determine endianess.
  77. */
  78. #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
  79. #define POLARSSL_HTONS(n) (n)
  80. #else
  81. #define POLARSSL_HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
  82. #endif
  83. unsigned short net_htons(unsigned short n);
  84. #define net_htons(n) POLARSSL_HTONS(n)
  85. /*
  86. * Initiate a TCP connection with host:port
  87. */
  88. int net_connect( int *fd, const char *host, int port )
  89. {
  90. struct sockaddr_in server_addr;
  91. #if LWIP_DNS == 1
  92. struct hostent *server_host;
  93. #endif
  94. #if defined(_WIN32) || defined(_WIN32_WCE)
  95. WSADATA wsaData;
  96. if( wsa_init_done == 0 )
  97. {
  98. if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
  99. return( POLARSSL_ERR_NET_SOCKET_FAILED );
  100. wsa_init_done = 1;
  101. }
  102. #else
  103. #if (!defined(__ICCARM__)) && (!defined(__CC_ARM)) && (!defined(__GNUC__)) && (!defined(__TASKING__))
  104. signal( SIGPIPE, SIG_IGN );
  105. #endif
  106. #endif
  107. #if LWIP_DNS == 1
  108. if( ( server_host = gethostbyname( host ) ) == NULL ) {
  109. printf( "SSL: ! gethostbyname ERROR :" );
  110. printf(host);
  111. printf( "\n" );
  112. return( POLARSSL_ERR_NET_UNKNOWN_HOST );
  113. }
  114. printf ( "SSL: gethostbyname OK\n" );
  115. memcpy( (void *) &server_addr.sin_addr,
  116. (void *) server_host->h_addr,
  117. server_host->h_length );
  118. #else
  119. printf( "SSL: no DNS mode\n" );
  120. memset(&server_addr, 0, sizeof(server_addr));
  121. server_addr.sin_len = sizeof(server_addr);
  122. server_addr.sin_addr.s_addr = inet_addr(host);
  123. #endif
  124. if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
  125. return( POLARSSL_ERR_NET_SOCKET_FAILED );
  126. server_addr.sin_family = AF_INET;
  127. server_addr.sin_port = net_htons( port );
  128. if( connect( *fd, (struct sockaddr *) &server_addr,
  129. sizeof( server_addr ) ) < 0 )
  130. {
  131. close( *fd );
  132. return( POLARSSL_ERR_NET_CONNECT_FAILED );
  133. }
  134. return( 0 );
  135. }
  136. /*
  137. * Create a listening socket on bind_ip:port
  138. */
  139. int net_bind( int *fd, const char *bind_ip, int port )
  140. {
  141. int n, c[4];
  142. struct sockaddr_in server_addr;
  143. #if defined(_WIN32) || defined(_WIN32_WCE)
  144. WSADATA wsaData;
  145. if( wsa_init_done == 0 )
  146. {
  147. if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
  148. return( POLARSSL_ERR_NET_SOCKET_FAILED );
  149. wsa_init_done = 1;
  150. }
  151. #else
  152. #if (!defined(__ICCARM__)) && (!defined(__CC_ARM)) && (!defined(__GNUC__)) && (!defined(__TASKING__))
  153. signal( SIGPIPE, SIG_IGN );
  154. #endif
  155. #endif
  156. if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
  157. return( POLARSSL_ERR_NET_SOCKET_FAILED );
  158. n = 1;
  159. setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
  160. (const char *) &n, sizeof( n ) );
  161. server_addr.sin_addr.s_addr = INADDR_ANY;
  162. server_addr.sin_family = AF_INET;
  163. server_addr.sin_port = net_htons( port );
  164. if( bind_ip != NULL )
  165. {
  166. memset( c, 0, sizeof( c ) );
  167. sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
  168. for( n = 0; n < 4; n++ )
  169. if( c[n] < 0 || c[n] > 255 )
  170. break;
  171. if( n == 4 )
  172. server_addr.sin_addr.s_addr =
  173. ( (unsigned long) c[0] << 24 ) |
  174. ( (unsigned long) c[1] << 16 ) |
  175. ( (unsigned long) c[2] << 8 ) |
  176. ( (unsigned long) c[3] );
  177. }
  178. if( bind( *fd, (struct sockaddr *) &server_addr,
  179. sizeof( server_addr ) ) < 0 )
  180. {
  181. close( *fd );
  182. return( POLARSSL_ERR_NET_BIND_FAILED );
  183. }
  184. if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
  185. {
  186. close( *fd );
  187. return( POLARSSL_ERR_NET_LISTEN_FAILED );
  188. }
  189. return( 0 );
  190. }
  191. /*
  192. * Check if the current operation is blocking
  193. */
  194. static int net_is_blocking( void )
  195. {
  196. #if defined(_WIN32) || defined(_WIN32_WCE)
  197. return( WSAGetLastError() == WSAEWOULDBLOCK );
  198. #else
  199. switch( errno )
  200. {
  201. #if defined EAGAIN
  202. case EAGAIN:
  203. #endif
  204. #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
  205. case EWOULDBLOCK:
  206. #endif
  207. return( 1 );
  208. }
  209. return( 0 );
  210. #endif
  211. }
  212. /*
  213. * Accept a connection from a remote client
  214. */
  215. int net_accept( int bind_fd, int *client_fd, void *client_ip )
  216. {
  217. struct sockaddr_in client_addr;
  218. #if defined(__socklen_t_defined) || defined(_SOCKLEN_T)
  219. socklen_t n = (socklen_t) sizeof( client_addr );
  220. #else
  221. int n = (int) sizeof( client_addr );
  222. #endif
  223. *client_fd = accept( bind_fd, (struct sockaddr *)
  224. &client_addr, &n );
  225. if( *client_fd < 0 )
  226. {
  227. if( net_is_blocking() != 0 )
  228. return( POLARSSL_ERR_NET_WANT_READ );
  229. return( POLARSSL_ERR_NET_ACCEPT_FAILED );
  230. }
  231. if( client_ip != NULL )
  232. memcpy( client_ip, &client_addr.sin_addr.s_addr,
  233. sizeof( client_addr.sin_addr.s_addr ) );
  234. return( 0 );
  235. }
  236. /*
  237. * Set the socket blocking or non-blocking
  238. */
  239. int net_set_block( int fd )
  240. {
  241. #if defined(WIN32) || defined(_WIN32_WCE) || defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__) || defined(__TASKING__)
  242. unsigned long n = 0;
  243. return( ioctlsocket( fd, FIONBIO, &n ) );
  244. #else
  245. return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
  246. #endif
  247. }
  248. int net_set_nonblock( int fd )
  249. {
  250. #if defined(WIN32) || defined(_WIN32_WCE) || defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__) || defined(__TASKING__)
  251. unsigned long n = 1;
  252. return( ioctlsocket( fd, FIONBIO, &n ) );
  253. #else
  254. return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
  255. #endif
  256. }
  257. /*
  258. * Portable usleep helper
  259. */
  260. void net_usleep( unsigned long usec )
  261. {
  262. struct timeval tv;
  263. tv.tv_sec = 0;
  264. tv.tv_usec = usec;
  265. select( 0, NULL, NULL, NULL, &tv );
  266. }
  267. /*
  268. * Read at most 'len' characters
  269. */
  270. int net_recv( void *ctx, unsigned char *buf, size_t len )
  271. {
  272. int ret = read( *((int *) ctx), buf, len );
  273. if( ret < 0 )
  274. {
  275. if( net_is_blocking() != 0 )
  276. return( POLARSSL_ERR_NET_WANT_READ );
  277. #if defined(_WIN32) || defined(_WIN32_WCE)
  278. if( WSAGetLastError() == WSAECONNRESET )
  279. return( POLARSSL_ERR_NET_CONN_RESET );
  280. #else
  281. if( errno == EPIPE || errno == ECONNRESET )
  282. return( POLARSSL_ERR_NET_CONN_RESET );
  283. if( errno == EINTR )
  284. return( POLARSSL_ERR_NET_WANT_READ );
  285. #endif
  286. return( POLARSSL_ERR_NET_RECV_FAILED );
  287. }
  288. return( ret );
  289. }
  290. /*
  291. * Write at most 'len' characters
  292. */
  293. int net_send( void *ctx, const unsigned char *buf, size_t len )
  294. {
  295. int ret = write( *((int *) ctx), buf, len );
  296. if( ret < 0 )
  297. {
  298. if( net_is_blocking() != 0 )
  299. return( POLARSSL_ERR_NET_WANT_WRITE );
  300. #if defined(_WIN32) || defined(_WIN32_WCE)
  301. if( WSAGetLastError() == WSAECONNRESET )
  302. return( POLARSSL_ERR_NET_CONN_RESET );
  303. #else
  304. if( errno == EPIPE || errno == ECONNRESET )
  305. return( POLARSSL_ERR_NET_CONN_RESET );
  306. if( errno == EINTR )
  307. return( POLARSSL_ERR_NET_WANT_WRITE );
  308. #endif
  309. return( POLARSSL_ERR_NET_SEND_FAILED );
  310. }
  311. return( ret );
  312. }
  313. /*
  314. * Gracefully close the connection
  315. */
  316. void net_close( int fd )
  317. {
  318. shutdown( fd, 2 );
  319. close( fd );
  320. }
  321. #endif