net_sockets_alt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2020-01-08 21:48:09
  5. * @LastEditTime : 2020-01-12 00:23:42
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #if !defined(MBEDTLS_CONFIG_FILE)
  9. #include "mbedtls/config.h"
  10. #else
  11. #include MBEDTLS_CONFIG_FILE
  12. #endif
  13. #if !defined(MBEDTLS_NET_C)
  14. #if defined(MBEDTLS_PLATFORM_C)
  15. #include "mbedtls/platform.h"
  16. #else
  17. #include <stdlib.h>
  18. #endif
  19. #include "mbedtls/net_sockets.h"
  20. #include "platform_net_socket.h"
  21. #include "platform_timer.h"
  22. /*
  23. * Initialize a context
  24. */
  25. void mbedtls_net_init(mbedtls_net_context *ctx)
  26. {
  27. if (!ctx) {
  28. return;
  29. }
  30. ctx->fd = -1;
  31. }
  32. /*
  33. * Initiate a TCP connection with host:port and the given protocol
  34. */
  35. int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto)
  36. {
  37. int net_proto;
  38. net_proto = (proto == MBEDTLS_NET_PROTO_UDP) ? PLATFORM_NET_PROTO_UDP : PLATFORM_NET_PROTO_TCP;
  39. ctx->fd = platform_net_socket_connect(host, port, net_proto);
  40. if (ctx->fd < 0) {
  41. return ctx->fd;
  42. }
  43. return 0;
  44. }
  45. /*
  46. * Set the socket blocking or non-blocking
  47. */
  48. int mbedtls_net_set_block( mbedtls_net_context *ctx )
  49. {
  50. return platform_net_socket_set_block(ctx->fd);
  51. }
  52. int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
  53. {
  54. return platform_net_socket_set_nonblock(ctx->fd);
  55. }
  56. /*
  57. * Read at most 'len' characters
  58. */
  59. int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
  60. {
  61. int ret;
  62. int fd = ((mbedtls_net_context *)ctx)->fd;
  63. if (fd < 0) {
  64. return MBEDTLS_ERR_NET_INVALID_CONTEXT;
  65. }
  66. ret = platform_net_socket_recv(fd, buf, len, 0);
  67. if (ret == 0) {
  68. return MBEDTLS_ERR_SSL_WANT_READ;
  69. } else if (ret < 0) {
  70. return MBEDTLS_ERR_NET_RECV_FAILED;
  71. }
  72. return ret;
  73. }
  74. /*
  75. * Read at most 'len' characters, blocking for at most 'timeout' ms
  76. */
  77. int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len, uint32_t timeout)
  78. {
  79. int ret;
  80. int fd = ((mbedtls_net_context *)ctx)->fd;
  81. if (fd < 0) {
  82. return MBEDTLS_ERR_NET_INVALID_CONTEXT;
  83. }
  84. ret = platform_net_socket_recv_timeout(fd, buf, len, timeout);
  85. if (ret == 0) {
  86. return MBEDTLS_ERR_SSL_TIMEOUT;
  87. } else if (ret < 0) {
  88. return MBEDTLS_ERR_NET_RECV_FAILED;
  89. }
  90. return ret;
  91. }
  92. /*
  93. * Write at most 'len' characters
  94. */
  95. int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len)
  96. {
  97. int ret;
  98. int fd = ((mbedtls_net_context *) ctx)->fd;
  99. if (fd < 0) {
  100. return MBEDTLS_ERR_NET_INVALID_CONTEXT;
  101. }
  102. ret = platform_net_socket_write(fd, (unsigned char*)buf, len);
  103. if (ret == 0) {
  104. return MBEDTLS_ERR_SSL_WANT_WRITE;
  105. } else if (ret < 0) {
  106. return MBEDTLS_ERR_NET_SEND_FAILED;
  107. }
  108. return ret;
  109. }
  110. /*
  111. * Gracefully close the connection
  112. */
  113. void mbedtls_net_free(mbedtls_net_context *ctx)
  114. {
  115. if (ctx->fd < 0) {
  116. return;
  117. }
  118. // shutdown(ctx->fd, 2);
  119. platform_net_socket_close(ctx->fd);
  120. ctx->fd = -1;
  121. }
  122. /*
  123. * Portable usleep helper
  124. */
  125. void mbedtls_net_usleep( unsigned long usec )
  126. {
  127. platform_timer_usleep(usec);
  128. }
  129. /* dtls */
  130. void mbedtls_dtls_net_init(mbedtls_net_context *ctx)
  131. {
  132. mbedtls_net_init(ctx);
  133. }
  134. int mbedtls_dtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto)
  135. {
  136. return mbedtls_net_connect(ctx, host, port, proto);
  137. }
  138. void mbedtls_dtls_net_usleep(unsigned long usec)
  139. {
  140. mbedtls_net_usleep(usec);
  141. }
  142. int mbedtls_dtls_net_recv(void *ctx, unsigned char *buf, size_t len)
  143. {
  144. return mbedtls_net_recv(ctx, buf, len);
  145. }
  146. int mbedtls_dtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len, uint32_t timeout)
  147. {
  148. return mbedtls_net_recv_timeout(ctx, buf, len, timeout);
  149. }
  150. int mbedtls_dtls_net_send(void *ctx, const unsigned char *buf, size_t len)
  151. {
  152. return mbedtls_net_send(ctx, buf, len);
  153. }
  154. void mbedtls_dtls_net_free(mbedtls_net_context *ctx)
  155. {
  156. mbedtls_net_free(ctx);
  157. }
  158. #endif /* MBEDTLS_NET_C */