net_sockets.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * Portions COPYRIGHT 2016 STMicroelectronics
  3. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  4. *
  5. ******************************************************************************
  6. * @file net_sockets.c
  7. * @author MCD Application Team
  8. * @version V1.1.0
  9. * @date 17-February-2017
  10. * @brief TCP/IP or UDP/IP networking functions iplementation based on LwIP API
  11. see the file "mbedTLS/library/net_socket_template.c" for the standard
  12. implmentation
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  17. *
  18. * Redistribution and use in source and binary forms, with or without modification,
  19. * are permitted provided that the following conditions are met:
  20. * 1. Redistributions of source code must retain the above copyright notice,
  21. * this list of conditions and the following disclaimer.
  22. * 2. Redistributions in binary form must reproduce the above copyright notice,
  23. * this list of conditions and the following disclaimer in the documentation
  24. * and/or other materials provided with the distribution.
  25. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  26. * may be used to endorse or promote products derived from this software
  27. * without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  30. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  32. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  35. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  37. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. ******************************************************************************
  41. */
  42. #if !defined(MBEDTLS_CONFIG_FILE)
  43. #include "mbedtls/config.h"
  44. #else
  45. #include MBEDTLS_CONFIG_FILE
  46. #endif
  47. #include <string.h>
  48. #include <stdint.h>
  49. #if defined(MBEDTLS_NET_C)
  50. #if defined(MBEDTLS_PLATFORM_C)
  51. #include "mbedtls/platform.h"
  52. #else
  53. #include <stdlib.h>
  54. #endif
  55. #include "mbedtls/net_sockets.h"
  56. #include "lwip/dhcp.h"
  57. #include "lwip/tcpip.h"
  58. #include "lwip/ip_addr.h"
  59. #include "lwip/netdb.h"
  60. #include "lwip/sockets.h"
  61. #include "netif/ethernet.h"
  62. #include "ethernetif.h"
  63. #include "stm32f4xx.h"
  64. #include "main.h"
  65. #include "FreeRTOS.h"
  66. #include "task.h"
  67. static int initialized = 0;
  68. struct sockaddr_storage client_addr;
  69. static int net_would_block( const mbedtls_net_context *ctx );
  70. /*
  71. * Initialize LwIP stack and get a dynamic IP address.
  72. */
  73. void mbedtls_net_init( mbedtls_net_context *ctx )
  74. {
  75. ctx->fd = -1;
  76. }
  77. /*
  78. * Initiate a TCP connection with host:port and the given protocol
  79. */
  80. int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto )
  81. {
  82. int ret;
  83. struct addrinfo hints;
  84. struct addrinfo *list;
  85. struct addrinfo *current;
  86. /* Do name resolution with both IPv6 and IPv4 */
  87. memset(&hints, 0, sizeof(hints));
  88. hints.ai_family = AF_UNSPEC;
  89. hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
  90. hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
  91. if(getaddrinfo(host, port, &hints, &list) != 0)
  92. return MBEDTLS_ERR_NET_UNKNOWN_HOST;
  93. /* Try the sockaddrs until a connection succeeds */
  94. ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
  95. for( current = list; current != NULL; current = current->ai_next)
  96. {
  97. ctx->fd = (int) socket(current->ai_family, current->ai_socktype, current->ai_protocol);
  98. if(ctx->fd < 0)
  99. {
  100. ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  101. continue;
  102. }
  103. if(connect(ctx->fd, current->ai_addr, (uint32_t)current->ai_addrlen) == 0)
  104. {
  105. ret = 0;
  106. break;
  107. }
  108. close( ctx->fd );
  109. ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
  110. }
  111. freeaddrinfo(list);
  112. return ret;
  113. }
  114. /*
  115. * Create a listening socket on bind_ip:port
  116. */
  117. int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
  118. {
  119. int n, ret;
  120. struct addrinfo hints, *addr_list, *cur;
  121. /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
  122. memset( &hints, 0, sizeof( hints ) );
  123. hints.ai_family = AF_UNSPEC;
  124. hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
  125. hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
  126. if( bind_ip == NULL )
  127. hints.ai_flags = AI_PASSIVE;
  128. if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
  129. return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
  130. /* Try the sockaddrs until a binding succeeds */
  131. ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
  132. for( cur = addr_list; cur != NULL; cur = cur->ai_next )
  133. {
  134. ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
  135. cur->ai_protocol );
  136. if( ctx->fd < 0 )
  137. {
  138. ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  139. continue;
  140. }
  141. n = 1;
  142. if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
  143. (const char *) &n, sizeof( n ) ) != 0 )
  144. {
  145. close( ctx->fd );
  146. ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  147. continue;
  148. }
  149. if( bind( ctx->fd, cur->ai_addr, (socklen_t)cur->ai_addrlen ) != 0 )
  150. {
  151. close( ctx->fd );
  152. ret = MBEDTLS_ERR_NET_BIND_FAILED;
  153. continue;
  154. }
  155. /* Listen only makes sense for TCP */
  156. if( proto == MBEDTLS_NET_PROTO_TCP )
  157. {
  158. if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
  159. {
  160. close( ctx->fd );
  161. ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
  162. continue;
  163. }
  164. }
  165. /* I we ever get there, it's a success */
  166. ret = 0;
  167. break;
  168. }
  169. freeaddrinfo( addr_list );
  170. return ret;
  171. }
  172. /*
  173. * Accept a connection from a remote client
  174. */
  175. int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
  176. mbedtls_net_context *client_ctx,
  177. void *client_ip, size_t buf_size, size_t *ip_len )
  178. {
  179. int ret;
  180. int type;
  181. #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
  182. defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t)
  183. #error _SOCKLEN_T
  184. socklen_t n = (socklen_t) sizeof( client_addr );
  185. socklen_t type_len = (socklen_t) sizeof( type );
  186. #else
  187. int n = (int) sizeof( client_addr );
  188. int type_len = (int) sizeof( type );
  189. #endif
  190. /* Is this a TCP or UDP socket? */
  191. if(getsockopt(bind_ctx->fd, SOL_SOCKET, SO_TYPE, (void *) &type, (u32_t *)(&type_len) ) != 0 ||
  192. (type != SOCK_STREAM && type != SOCK_DGRAM))
  193. {
  194. return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
  195. }
  196. if( type == SOCK_STREAM )
  197. {
  198. /* TCP: actual accept() */
  199. ret = client_ctx->fd = (int) accept( bind_ctx->fd, (struct sockaddr *) &client_addr, (u32_t *)(&n) );
  200. }
  201. else
  202. {
  203. /* UDP: wait for a message, but keep it in the queue */
  204. char buf[1] = { 0 };
  205. ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK, (struct sockaddr *) &client_addr, (u32_t *)(&n) );
  206. }
  207. if( ret < 0 )
  208. {
  209. if( net_would_block( bind_ctx ) != 0 )
  210. return( MBEDTLS_ERR_SSL_WANT_READ );
  211. return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
  212. }
  213. /* UDP: hijack the listening socket to communicate with the client,
  214. * then bind a new socket to accept new connections */
  215. if( type != SOCK_STREAM )
  216. {
  217. struct sockaddr_storage local_addr;
  218. int one = 1;
  219. if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 )
  220. {
  221. return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
  222. }
  223. client_ctx->fd = bind_ctx->fd;
  224. bind_ctx->fd = -1; /* In case we exit early */
  225. n = sizeof( struct sockaddr_storage );
  226. if( getsockname(client_ctx->fd, (struct sockaddr *) &local_addr, (u32_t*)(&n) ) != 0 ||
  227. (bind_ctx->fd = (int) socket( local_addr.ss_family, SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
  228. setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR, (const char *) &one, sizeof( one ) ) != 0 )
  229. {
  230. return( MBEDTLS_ERR_NET_SOCKET_FAILED );
  231. }
  232. if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
  233. {
  234. return( MBEDTLS_ERR_NET_BIND_FAILED );
  235. }
  236. }
  237. if( client_ip != NULL )
  238. {
  239. if( client_addr.ss_family == AF_INET )
  240. {
  241. #if LWIP_IPV4
  242. struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
  243. *ip_len = sizeof( addr4->sin_addr.s_addr );
  244. if( buf_size < *ip_len )
  245. {
  246. return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
  247. }
  248. memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
  249. #endif
  250. }
  251. else
  252. {
  253. #if LWIP_IPV6
  254. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
  255. *ip_len = sizeof( addr6->sin6_addr.s6_addr );
  256. if( buf_size < *ip_len )
  257. {
  258. return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
  259. }
  260. memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
  261. #endif
  262. }
  263. }
  264. return( 0 );
  265. }
  266. /*
  267. * Set the socket blocking or non-blocking
  268. */
  269. int mbedtls_net_set_block( mbedtls_net_context *ctx )
  270. {
  271. mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
  272. return 0;
  273. }
  274. int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
  275. {
  276. mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
  277. return 0;
  278. }
  279. /*
  280. * Portable usleep helper
  281. */
  282. void mbedtls_net_usleep( unsigned long usec )
  283. {
  284. mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
  285. }
  286. /*
  287. * Read at most 'len' characters
  288. */
  289. int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
  290. {
  291. int32_t ret;
  292. int32_t fd = ((mbedtls_net_context *) ctx)->fd;
  293. if( fd < 0 )
  294. {
  295. return MBEDTLS_ERR_NET_INVALID_CONTEXT;
  296. }
  297. struct timeval timeout;
  298. timeout.tv_sec = 3;
  299. timeout.tv_usec = 0;
  300. if( setsockopt( fd, SOL_SOCKET, SO_RCVTIMEO,
  301. (const char *) &timeout, sizeof( timeout ) ) != 0 )
  302. {
  303. close( fd );
  304. ret = MBEDTLS_ERR_NET_INVALID_CONTEXT;
  305. }
  306. ret = (int32_t) read( fd, buf, len );
  307. if( ret < 0 )
  308. {
  309. if(net_would_block(ctx) != 0)
  310. {
  311. return MBEDTLS_ERR_SSL_WANT_READ;
  312. }
  313. if(errno == EPIPE || errno == ECONNRESET)
  314. {
  315. return MBEDTLS_ERR_NET_CONN_RESET;
  316. }
  317. if(errno == EINTR)
  318. {
  319. return MBEDTLS_ERR_SSL_WANT_READ;
  320. }
  321. return MBEDTLS_ERR_NET_RECV_FAILED;
  322. }
  323. return ret;
  324. }
  325. /*
  326. * Read at most 'len' characters, blocking for at most 'timeout' ms
  327. */
  328. int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
  329. uint32_t timeout )
  330. {
  331. mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
  332. return mbedtls_net_recv( ctx, buf, len );
  333. }
  334. static int net_would_block( const mbedtls_net_context *ctx )
  335. {
  336. /*
  337. * Do not return 'WOULD BLOCK' on a non-blocking socket
  338. */
  339. int val = 0;
  340. UNUSED(val);
  341. if( ( fcntl( ctx->fd, F_GETFL, val) & O_NONBLOCK ) != O_NONBLOCK )
  342. return( 0 );
  343. switch( errno )
  344. {
  345. #if defined EAGAIN
  346. case EAGAIN:
  347. #endif
  348. #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
  349. case EWOULDBLOCK:
  350. #endif
  351. return( 1 );
  352. }
  353. return( 0 );
  354. }
  355. /*
  356. * Write at most 'len' characters
  357. */
  358. int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
  359. {
  360. int32_t ret;
  361. int fd = ((mbedtls_net_context *) ctx)->fd;
  362. if( fd < 0 )
  363. {
  364. return MBEDTLS_ERR_NET_INVALID_CONTEXT;
  365. }
  366. struct timeval timeout;
  367. timeout.tv_sec = 3;
  368. timeout.tv_usec = 0;
  369. if( setsockopt( fd, SOL_SOCKET, SO_SNDTIMEO,
  370. (const char *) &timeout, sizeof( timeout ) ) != 0 )
  371. {
  372. close( fd );
  373. ret = MBEDTLS_ERR_NET_INVALID_CONTEXT;
  374. }
  375. ret = (int32_t) write(fd, buf, len);
  376. if( ret < 0 )
  377. {
  378. if(net_would_block(ctx) != 0)
  379. {
  380. return MBEDTLS_ERR_SSL_WANT_WRITE;
  381. }
  382. if(errno == EPIPE || errno == ECONNRESET)
  383. {
  384. return MBEDTLS_ERR_NET_CONN_RESET;
  385. }
  386. if(errno == EINTR)
  387. {
  388. return MBEDTLS_ERR_SSL_WANT_WRITE;
  389. }
  390. return MBEDTLS_ERR_NET_SEND_FAILED;
  391. }
  392. return ret;
  393. }
  394. /*
  395. * Gracefully close the connection
  396. */
  397. void mbedtls_net_free( mbedtls_net_context *ctx )
  398. {
  399. if( ctx->fd == -1 )
  400. return;
  401. shutdown( ctx->fd, 2 );
  402. close( ctx->fd );
  403. ctx->fd = -1;
  404. initialized = 0;
  405. }
  406. #endif /* MBEDTLS_NET_C */