utils_net.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. //based on Alibaba c-sdk
  16. /*
  17. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  18. */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "uiot_defs.h"
  22. #include "utils_net.h"
  23. #include "uiot_import.h"
  24. #ifdef PKG_USING_UCLOUD_TLS
  25. static int read_ssl(utils_network_pt pNetwork, unsigned char *buffer, size_t len, uint32_t timeout_ms)
  26. {
  27. if (NULL == pNetwork) {
  28. LOG_ERROR("network is null");
  29. return FAILURE_RET;
  30. }
  31. return HAL_TLS_Read((uintptr_t)pNetwork->handle, buffer, len, timeout_ms);
  32. }
  33. static int write_ssl(utils_network_pt pNetwork, unsigned char *buffer, size_t len, uint32_t timeout_ms)
  34. {
  35. if (NULL == pNetwork) {
  36. LOG_ERROR("network is null");
  37. return FAILURE_RET;
  38. }
  39. return HAL_TLS_Write((uintptr_t)pNetwork->handle, buffer, len, timeout_ms);
  40. }
  41. static int disconnect_ssl(utils_network_pt pNetwork)
  42. {
  43. if (NULL == pNetwork) {
  44. LOG_ERROR("network is null");
  45. return FAILURE_RET;
  46. }
  47. HAL_TLS_Disconnect((uintptr_t)pNetwork->handle);
  48. pNetwork->handle = 0;
  49. return SUCCESS_RET;
  50. }
  51. static int connect_ssl(utils_network_pt pNetwork)
  52. {
  53. if (NULL == pNetwork) {
  54. LOG_ERROR("network is null");
  55. return FAILURE_RET;
  56. }
  57. if (0 != (pNetwork->handle = (intptr_t)HAL_TLS_Connect(
  58. pNetwork->pHostAddress,
  59. pNetwork->port,
  60. pNetwork->authmode,
  61. pNetwork->ca_crt,
  62. pNetwork->ca_crt_len))) {
  63. return SUCCESS_RET;
  64. }
  65. else {
  66. return FAILURE_RET;
  67. }
  68. }
  69. #else
  70. /*** TCP connection ***/
  71. static int read_tcp(utils_network_pt pNetwork, unsigned char *buffer, size_t len, uint32_t timeout_ms)
  72. {
  73. if (NULL == pNetwork) {
  74. LOG_ERROR("network is null");
  75. return FAILURE_RET;
  76. }
  77. return HAL_TCP_Read((uintptr_t)pNetwork->handle, buffer, len, timeout_ms);
  78. }
  79. static int write_tcp(utils_network_pt pNetwork, unsigned char *buffer, size_t len, uint32_t timeout_ms)
  80. {
  81. if (NULL == pNetwork) {
  82. LOG_ERROR("network is null");
  83. return FAILURE_RET;
  84. }
  85. return HAL_TCP_Write((uintptr_t)pNetwork->handle, buffer, len, timeout_ms);
  86. }
  87. static int disconnect_tcp(utils_network_pt pNetwork)
  88. {
  89. if (NULL == pNetwork) {
  90. LOG_ERROR("network is null");
  91. return FAILURE_RET;
  92. }
  93. HAL_TCP_Disconnect(pNetwork->handle);
  94. pNetwork->handle = (uintptr_t)(-1);
  95. return SUCCESS_RET;
  96. }
  97. static int connect_tcp(utils_network_pt pNetwork)
  98. {
  99. if (NULL == pNetwork) {
  100. LOG_ERROR("network is null");
  101. return FAILURE_RET;
  102. }
  103. pNetwork->handle = HAL_TCP_Connect(pNetwork->pHostAddress, pNetwork->port);
  104. if (pNetwork->handle == (uintptr_t)(-1)) {
  105. return FAILURE_RET;
  106. }
  107. return SUCCESS_RET;
  108. }
  109. #endif /* #ifdef PKG_USING_UCLOUD_TLS */
  110. /****** network interface ******/
  111. int utils_net_read(utils_network_pt pNetwork, unsigned char *buffer, size_t len, uint32_t timeout_ms)
  112. {
  113. int ret = 0;
  114. #ifdef PKG_USING_UCLOUD_TLS
  115. ret = read_ssl(pNetwork, buffer, len, timeout_ms);
  116. #else
  117. ret = read_tcp(pNetwork, buffer, len, timeout_ms);
  118. #endif
  119. return ret;
  120. }
  121. int utils_net_write(utils_network_pt pNetwork,unsigned char *buffer, size_t len, uint32_t timeout_ms)
  122. {
  123. int ret = 0;
  124. #ifdef PKG_USING_UCLOUD_TLS
  125. ret = write_ssl(pNetwork, buffer, len, timeout_ms);
  126. #else
  127. ret = write_tcp(pNetwork, buffer, len, timeout_ms);
  128. #endif
  129. return ret;
  130. }
  131. int utils_net_disconnect(utils_network_pt pNetwork)
  132. {
  133. int ret = 0;
  134. #ifdef PKG_USING_UCLOUD_TLS
  135. ret = disconnect_ssl(pNetwork);
  136. #else
  137. ret = disconnect_tcp(pNetwork);
  138. #endif
  139. return ret;
  140. }
  141. int utils_net_connect(utils_network_pt pNetwork)
  142. {
  143. int ret = 0;
  144. #ifdef PKG_USING_UCLOUD_TLS
  145. ret = connect_ssl(pNetwork);
  146. #else
  147. ret = connect_tcp(pNetwork);
  148. #endif
  149. return ret;
  150. }
  151. int utils_net_init(utils_network_pt pNetwork, const char *host, uint16_t port, uint16_t authmode, const char *ca_crt)
  152. {
  153. if (!pNetwork || !host) {
  154. LOG_ERROR("parameter error! pNetwork=%p, host = %p", pNetwork, host);
  155. return FAILURE_RET;
  156. }
  157. pNetwork->pHostAddress = host;
  158. pNetwork->port = port;
  159. pNetwork->authmode = authmode;
  160. pNetwork->ca_crt = ca_crt;
  161. if (NULL == ca_crt) {
  162. pNetwork->ca_crt_len = 0;
  163. } else {
  164. pNetwork->ca_crt_len = strlen(ca_crt);
  165. }
  166. pNetwork->handle = 0;
  167. pNetwork->read = utils_net_read;
  168. pNetwork->write = utils_net_write;
  169. pNetwork->disconnect = utils_net_disconnect;
  170. pNetwork->connect = utils_net_connect;
  171. return SUCCESS_RET;
  172. }