net_sockets.c 14 KB

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