net_sockets.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. static int initialized = 0;
  66. struct sockaddr_storage client_addr;
  67. static int net_would_block( const mbedtls_net_context *ctx );
  68. /*
  69. * Initialize LwIP stack and get a dynamic IP address.
  70. */
  71. void mbedtls_net_init( mbedtls_net_context *ctx )
  72. {
  73. ctx->fd = -1;
  74. }
  75. /*
  76. * Initiate a TCP connection with host:port and the given protocol
  77. */
  78. int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto )
  79. {
  80. int ret;
  81. struct addrinfo hints;
  82. struct addrinfo *list;
  83. struct addrinfo *current;
  84. /* Do name resolution with both IPv6 and IPv4 */
  85. memset(&hints, 0, sizeof(hints));
  86. hints.ai_family = AF_UNSPEC;
  87. hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
  88. hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
  89. if(getaddrinfo(host, port, &hints, &list) != 0)
  90. return MBEDTLS_ERR_NET_UNKNOWN_HOST;
  91. /* Try the sockaddrs until a connection succeeds */
  92. ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
  93. for( current = list; current != NULL; current = current->ai_next)
  94. {
  95. ctx->fd = (int) socket(current->ai_family, current->ai_socktype, current->ai_protocol);
  96. if(ctx->fd < 0)
  97. {
  98. ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  99. continue;
  100. }
  101. if(connect(ctx->fd, current->ai_addr, (uint32_t)current->ai_addrlen) == 0)
  102. {
  103. ret = 0;
  104. break;
  105. }
  106. close( ctx->fd );
  107. ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
  108. }
  109. freeaddrinfo(list);
  110. return ret;
  111. }
  112. /*
  113. * Create a listening socket on bind_ip:port
  114. */
  115. int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
  116. {
  117. int n, ret;
  118. struct addrinfo hints, *addr_list, *cur;
  119. /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
  120. memset( &hints, 0, sizeof( hints ) );
  121. hints.ai_family = AF_UNSPEC;
  122. hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
  123. hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
  124. if( bind_ip == NULL )
  125. hints.ai_flags = AI_PASSIVE;
  126. if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
  127. return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
  128. /* Try the sockaddrs until a binding succeeds */
  129. ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
  130. for( cur = addr_list; cur != NULL; cur = cur->ai_next )
  131. {
  132. ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
  133. cur->ai_protocol );
  134. if( ctx->fd < 0 )
  135. {
  136. ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  137. continue;
  138. }
  139. n = 1;
  140. if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
  141. (const char *) &n, sizeof( n ) ) != 0 )
  142. {
  143. close( ctx->fd );
  144. ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
  145. continue;
  146. }
  147. if( bind( ctx->fd, cur->ai_addr, (socklen_t)cur->ai_addrlen ) != 0 )
  148. {
  149. close( ctx->fd );
  150. ret = MBEDTLS_ERR_NET_BIND_FAILED;
  151. continue;
  152. }
  153. /* Listen only makes sense for TCP */
  154. if( proto == MBEDTLS_NET_PROTO_TCP )
  155. {
  156. if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
  157. {
  158. close( ctx->fd );
  159. ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
  160. continue;
  161. }
  162. }
  163. /* I we ever get there, it's a success */
  164. ret = 0;
  165. break;
  166. }
  167. freeaddrinfo( addr_list );
  168. return ret;
  169. }
  170. /*
  171. * Accept a connection from a remote client
  172. */
  173. int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
  174. mbedtls_net_context *client_ctx,
  175. void *client_ip, size_t buf_size, size_t *ip_len )
  176. {
  177. int ret;
  178. int type;
  179. #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
  180. defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t)
  181. #error _SOCKLEN_T
  182. socklen_t n = (socklen_t) sizeof( client_addr );
  183. socklen_t type_len = (socklen_t) sizeof( type );
  184. #else
  185. int n = (int) sizeof( client_addr );
  186. int type_len = (int) sizeof( type );
  187. #endif
  188. /* Is this a TCP or UDP socket? */
  189. if(getsockopt(bind_ctx->fd, SOL_SOCKET, SO_TYPE, (void *) &type, (u32_t *)(&type_len) ) != 0 ||
  190. (type != SOCK_STREAM && type != SOCK_DGRAM))
  191. {
  192. return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
  193. }
  194. if( type == SOCK_STREAM )
  195. {
  196. /* TCP: actual accept() */
  197. ret = client_ctx->fd = (int) accept( bind_ctx->fd, (struct sockaddr *) &client_addr, (u32_t *)(&n) );
  198. }
  199. else
  200. {
  201. /* UDP: wait for a message, but keep it in the queue */
  202. char buf[1] = { 0 };
  203. ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK, (struct sockaddr *) &client_addr, (u32_t *)(&n) );
  204. }
  205. if( ret < 0 )
  206. {
  207. if( net_would_block( bind_ctx ) != 0 )
  208. return( MBEDTLS_ERR_SSL_WANT_READ );
  209. return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
  210. }
  211. /* UDP: hijack the listening socket to communicate with the client,
  212. * then bind a new socket to accept new connections */
  213. if( type != SOCK_STREAM )
  214. {
  215. struct sockaddr_storage local_addr;
  216. int one = 1;
  217. if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 )
  218. {
  219. return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
  220. }
  221. client_ctx->fd = bind_ctx->fd;
  222. bind_ctx->fd = -1; /* In case we exit early */
  223. n = sizeof( struct sockaddr_storage );
  224. if( getsockname(client_ctx->fd, (struct sockaddr *) &local_addr, (u32_t*)(&n) ) != 0 ||
  225. (bind_ctx->fd = (int) socket( local_addr.ss_family, SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
  226. setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR, (const char *) &one, sizeof( one ) ) != 0 )
  227. {
  228. return( MBEDTLS_ERR_NET_SOCKET_FAILED );
  229. }
  230. if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
  231. {
  232. return( MBEDTLS_ERR_NET_BIND_FAILED );
  233. }
  234. }
  235. if( client_ip != NULL )
  236. {
  237. if( client_addr.ss_family == AF_INET )
  238. {
  239. #if LWIP_IPV4
  240. struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
  241. *ip_len = sizeof( addr4->sin_addr.s_addr );
  242. if( buf_size < *ip_len )
  243. {
  244. return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
  245. }
  246. memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
  247. #endif
  248. }
  249. else
  250. {
  251. #if LWIP_IPV6
  252. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
  253. *ip_len = sizeof( addr6->sin6_addr.s6_addr );
  254. if( buf_size < *ip_len )
  255. {
  256. return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
  257. }
  258. memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
  259. #endif
  260. }
  261. }
  262. return( 0 );
  263. }
  264. /*
  265. * Set the socket blocking or non-blocking
  266. */
  267. int mbedtls_net_set_block( mbedtls_net_context *ctx )
  268. {
  269. mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
  270. return 0;
  271. }
  272. int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
  273. {
  274. mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
  275. return 0;
  276. }
  277. /*
  278. * Portable usleep helper
  279. */
  280. void mbedtls_net_usleep( unsigned long usec )
  281. {
  282. mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
  283. }
  284. /*
  285. * Read at most 'len' characters
  286. */
  287. int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
  288. {
  289. int32_t ret;
  290. int32_t fd = ((mbedtls_net_context *) ctx)->fd;
  291. if( fd < 0 )
  292. {
  293. return MBEDTLS_ERR_NET_INVALID_CONTEXT;
  294. }
  295. ret = (int32_t) read( fd, buf, len );
  296. if( ret < 0 )
  297. {
  298. if(net_would_block(ctx) != 0)
  299. {
  300. return MBEDTLS_ERR_SSL_WANT_READ;
  301. }
  302. if(errno == EPIPE || errno == ECONNRESET)
  303. {
  304. return MBEDTLS_ERR_NET_CONN_RESET;
  305. }
  306. if(errno == EINTR)
  307. {
  308. return MBEDTLS_ERR_SSL_WANT_READ;
  309. }
  310. return MBEDTLS_ERR_NET_RECV_FAILED;
  311. }
  312. return ret;
  313. }
  314. /*
  315. * Read at most 'len' characters, blocking for at most 'timeout' ms
  316. */
  317. int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
  318. uint32_t timeout )
  319. {
  320. mbedtls_printf ("%s() NOT IMPLEMENTED!!\n", __FUNCTION__);
  321. return mbedtls_net_recv( ctx, buf, len );
  322. }
  323. static int net_would_block( const mbedtls_net_context *ctx )
  324. {
  325. /*
  326. * Do not return 'WOULD BLOCK' on a non-blocking socket
  327. */
  328. int val = 0;
  329. UNUSED(val);
  330. if( ( fcntl( ctx->fd, F_GETFL, val) & O_NONBLOCK ) != O_NONBLOCK )
  331. return( 0 );
  332. switch( errno )
  333. {
  334. #if defined EAGAIN
  335. case EAGAIN:
  336. #endif
  337. #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
  338. case EWOULDBLOCK:
  339. #endif
  340. return( 1 );
  341. }
  342. return( 0 );
  343. }
  344. /*
  345. * Write at most 'len' characters
  346. */
  347. int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
  348. {
  349. int32_t ret;
  350. int fd = ((mbedtls_net_context *) ctx)->fd;
  351. if( fd < 0 )
  352. {
  353. return MBEDTLS_ERR_NET_INVALID_CONTEXT;
  354. }
  355. ret = (int32_t) write(fd, buf, len);
  356. if( ret < 0 )
  357. {
  358. if(net_would_block(ctx) != 0)
  359. {
  360. return MBEDTLS_ERR_SSL_WANT_WRITE;
  361. }
  362. if(errno == EPIPE || errno == ECONNRESET)
  363. {
  364. return MBEDTLS_ERR_NET_CONN_RESET;
  365. }
  366. if(errno == EINTR)
  367. {
  368. return MBEDTLS_ERR_SSL_WANT_WRITE;
  369. }
  370. return MBEDTLS_ERR_NET_SEND_FAILED;
  371. }
  372. return ret;
  373. }
  374. /*
  375. * Gracefully close the connection
  376. */
  377. void mbedtls_net_free( mbedtls_net_context *ctx )
  378. {
  379. if( ctx->fd == -1 )
  380. return;
  381. shutdown( ctx->fd, 2 );
  382. close( ctx->fd );
  383. ctx->fd = -1;
  384. initialized = 0;
  385. }
  386. #endif /* MBEDTLS_NET_C */