platform_net_socket.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2020-01-10 23:45:59
  5. * @LastEditTime: 2020-06-18 20:13:02
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #include "platform_net_socket.h"
  9. int platform_net_socket_connect(const char *host, const char *port, int proto)
  10. {
  11. int fd, ret = MQTT_SOCKET_UNKNOWN_HOST_ERROR;
  12. #ifdef MQTT_NETSOCKET_USING_AT
  13. fd = tos_sal_module_connect(host, port, TOS_SAL_PROTO_TCP);
  14. if (fd < 0) {
  15. return MQTT_CONNECT_FAILED_ERROR;
  16. }
  17. ret = fd;
  18. #else
  19. struct addrinfo hints, *addr_list, *cur;
  20. /* Do name resolution with both IPv6 and IPv4 */
  21. memset(&hints, 0, sizeof(hints));
  22. hints.ai_family = AF_UNSPEC;
  23. hints.ai_socktype = (proto == PLATFORM_NET_PROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
  24. hints.ai_protocol = (proto == PLATFORM_NET_PROTO_UDP) ? IPPROTO_UDP : IPPROTO_TCP;
  25. if (getaddrinfo(host, port, &hints, &addr_list) != 0) {
  26. return ret;
  27. }
  28. for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
  29. fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
  30. if (fd < 0) {
  31. ret = MQTT_SOCKET_FAILED_ERROR;
  32. continue;
  33. }
  34. if (connect(fd, cur->ai_addr, cur->ai_addrlen) == 0) {
  35. ret = fd;
  36. break;
  37. }
  38. platform_net_socket_close(fd);
  39. ret = MQTT_CONNECT_FAILED_ERROR;
  40. }
  41. freeaddrinfo(addr_list);
  42. #endif
  43. return ret;
  44. }
  45. int platform_net_socket_recv(int fd, void *buf, size_t len, int flags)
  46. {
  47. #ifdef MQTT_NETSOCKET_USING_AT
  48. return tos_sal_module_recv(fd, buf, len);
  49. #else
  50. return recv(fd, buf, len, flags);
  51. #endif
  52. }
  53. int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int timeout)
  54. {
  55. #ifdef MQTT_NETSOCKET_USING_AT
  56. return tos_sal_module_recv_timeout(fd, buf, len, timeout);
  57. #else
  58. int nread;
  59. int nleft = len;
  60. unsigned char *ptr;
  61. ptr = buf;
  62. struct timeval tv = {
  63. timeout / 1000,
  64. (timeout % 1000) * 1000
  65. };
  66. if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec <= 0)) {
  67. tv.tv_sec = 0;
  68. tv.tv_usec = 100;
  69. }
  70. platform_net_socket_setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
  71. while (nleft > 0) {
  72. nread = platform_net_socket_recv(fd, ptr, nleft, 0);
  73. if (nread < 0) {
  74. return -1;
  75. } else if (nread == 0) {
  76. break;
  77. }
  78. nleft -= nread;
  79. ptr += nread;
  80. }
  81. return len - nleft;
  82. #endif
  83. }
  84. int platform_net_socket_write(int fd, void *buf, size_t len)
  85. {
  86. #ifdef MQTT_NETSOCKET_USING_AT
  87. return tos_sal_module_send(fd, buf, len);
  88. #else
  89. return write(fd, buf, len);
  90. #endif
  91. }
  92. int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout)
  93. {
  94. #ifdef MQTT_NETSOCKET_USING_AT
  95. return tos_sal_module_send(fd, buf, len);
  96. #else
  97. struct timeval tv = {
  98. timeout / 1000,
  99. (timeout % 1000) * 1000
  100. };
  101. if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec <= 0)) {
  102. tv.tv_sec = 0;
  103. tv.tv_usec = 100;
  104. }
  105. platform_net_socket_setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));
  106. return write(fd, buf, len);
  107. #endif
  108. }
  109. int platform_net_socket_close(int fd)
  110. {
  111. #ifdef MQTT_NETSOCKET_USING_AT
  112. return tos_sal_module_close(fd);
  113. #else
  114. return close(fd);
  115. #endif
  116. }
  117. #ifndef MQTT_NETSOCKET_USING_AT
  118. int platform_net_socket_set_block(int fd)
  119. {
  120. return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, F_GETFL) & ~O_NONBLOCK);
  121. }
  122. int platform_net_socket_set_nonblock(int fd)
  123. {
  124. return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, F_GETFL) | O_NONBLOCK);
  125. }
  126. int platform_net_socket_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
  127. {
  128. return setsockopt(fd, level, optname, optval, optlen);
  129. }
  130. #endif