adapt_ql_port.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <sys/time.h>
  8. #include "ql_api_datacall.h"
  9. #include "ql_api_osi.h"
  10. #include "ql_api_osi.h"
  11. #include "ql_api_nw.h"
  12. #include "ql_log.h"
  13. #include "sockets.h"
  14. #include "lwip/netdb.h"
  15. #include "lwip/netif.h"
  16. #include "lwip/inet.h"
  17. #include "lwip/tcp.h"
  18. #include "lwip/ip_addr.h"
  19. #include "lwip/ip6_addr.h"
  20. #include "platform.h"
  21. #include "aiot_state_api.h"
  22. #include "aiot_sysdep_api.h"
  23. #define QL_ADAPT_LOG_LEVEL QL_LOG_LEVEL_INFO
  24. #define QL_ADAPT_LOG(msg, ...) QL_LOG(QL_ADAPT_LOG_LEVEL, "ql_Adapt", msg, ##__VA_ARGS__)
  25. /* socket建联时间默认最大值 */
  26. #define CORE_SYSDEP_DEFAULT_CONNECT_TIMEOUT_MS (10 * 1000)
  27. /*
  28. * CORE_SYSDEP_MBEDTLS_ENABLED 不是一个用户需要关心的编译开关
  29. *
  30. * 大多数情况下, 就保持它如下的设置即可
  31. * 只有少数时候, SDK的用户关心对接层代码的ROM尺寸, 并且也没有选择用TLS连接服务器
  32. * 那时才会出现, 将 CORE_SYSDEP_MBEDTLS_ENABLED 宏定义关闭的改动, 以减小对接尺寸
  33. *
  34. * 我们不建议去掉 #define CORE_SYSDEP_MBEDTLS_ENABLED 这行代码
  35. * 虽然物联网平台接收TCP方式的连接, 但我们不推荐这样做, TLS是更安全的通信方式
  36. *
  37. */
  38. #define CORE_SYSDEP_MBEDTLS_ENABLED
  39. #define DEFAULT_SELECT_TIME_OUT 5000
  40. #define DEFAULT_PDP_INDEX 1
  41. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  42. #include "mbedtls/net_sockets.h"
  43. #include "mbedtls/ssl.h"
  44. #include "mbedtls/ctr_drbg.h"
  45. #include "mbedtls/debug.h"
  46. #include "mbedtls/platform.h"
  47. #include "mbedtls/timing.h"
  48. #endif
  49. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  50. typedef struct {
  51. mbedtls_net_context net_ctx;
  52. mbedtls_ssl_context ssl_ctx;
  53. mbedtls_ssl_config ssl_config;
  54. mbedtls_timing_delay_context timer_delay_ctx;
  55. mbedtls_x509_crt x509_server_cert;
  56. mbedtls_x509_crt x509_client_cert;
  57. mbedtls_pk_context x509_client_pk;
  58. } core_sysdep_mbedtls_t;
  59. #endif
  60. typedef struct {
  61. int fd;
  62. uint16_t profile_idx;
  63. core_sysdep_socket_type_t socket_type;
  64. aiot_sysdep_network_cred_t *cred;
  65. char *host;
  66. char backup_ip[16];
  67. uint16_t port;
  68. uint32_t connect_timeout_ms;
  69. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  70. core_sysdep_psk_t psk;
  71. core_sysdep_mbedtls_t mbedtls;
  72. #endif
  73. } core_network_handle_t;
  74. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  75. #define MBEDTLS_MEM_INFO_MAGIC (0x12345678)
  76. static unsigned int g_mbedtls_total_mem_used = 0;
  77. static unsigned int g_mbedtls_max_mem_used = 0;
  78. typedef struct {
  79. int magic;
  80. int size;
  81. } mbedtls_mem_info_t;
  82. /*
  83. static void *_core_mbedtls_calloc(size_t n, size_t size)
  84. {
  85. unsigned char *buf = NULL;
  86. mbedtls_mem_info_t *mem_info = NULL;
  87. if (n == 0 || size == 0) {
  88. return NULL;
  89. }
  90. buf = (unsigned char *)malloc(n * size + sizeof(mbedtls_mem_info_t));
  91. if (NULL == buf) {
  92. return NULL;
  93. } else {
  94. memset(buf, 0, n * size + sizeof(mbedtls_mem_info_t));
  95. }
  96. mem_info = (mbedtls_mem_info_t *)buf;
  97. mem_info->magic = MBEDTLS_MEM_INFO_MAGIC;
  98. mem_info->size = n * size;
  99. buf += sizeof(mbedtls_mem_info_t);
  100. g_mbedtls_total_mem_used += mem_info->size;
  101. if (g_mbedtls_total_mem_used > g_mbedtls_max_mem_used) {
  102. g_mbedtls_max_mem_used = g_mbedtls_total_mem_used;
  103. }
  104. return buf;
  105. }
  106. static void _core_mbedtls_free(void *ptr)
  107. {
  108. mbedtls_mem_info_t *mem_info = NULL;
  109. if (NULL == ptr) {
  110. return;
  111. }
  112. mem_info = (mbedtls_mem_info_t *)((unsigned char *)ptr - sizeof(mbedtls_mem_info_t));
  113. if (mem_info->magic != MBEDTLS_MEM_INFO_MAGIC) {
  114. return;
  115. }
  116. g_mbedtls_total_mem_used -= mem_info->size;
  117. free(mem_info);
  118. }
  119. */
  120. #endif
  121. void *core_sysdep_malloc(uint32_t size, char *name)
  122. {
  123. return malloc(size);
  124. }
  125. void core_sysdep_free(void *ptr)
  126. {
  127. if(ptr)
  128. free(ptr);
  129. return;
  130. }
  131. uint64_t core_sysdep_time(void)
  132. {
  133. ql_timeval_t time;
  134. memset(&time, 0, sizeof(ql_timeval_t));
  135. ql_gettimeofday(&time);
  136. return ((uint64_t)time.sec * 1000 + time.usec / 1000);
  137. }
  138. void core_sysdep_sleep(uint64_t time_ms)
  139. {
  140. ql_rtos_task_sleep_ms(time_ms);
  141. }
  142. void *core_sysdep_network_init(void)
  143. {
  144. core_network_handle_t *handle = NULL;
  145. handle = malloc(sizeof(core_network_handle_t));
  146. if (handle == NULL) {
  147. return NULL;
  148. }
  149. memset(handle, 0, sizeof(core_network_handle_t));
  150. handle->connect_timeout_ms = CORE_SYSDEP_DEFAULT_CONNECT_TIMEOUT_MS;
  151. return handle;
  152. }
  153. int32_t core_sysdep_network_setopt(void *handle, core_sysdep_network_option_t option, void *data)
  154. {
  155. core_network_handle_t *network_handle = (core_network_handle_t *)handle;
  156. if (handle == NULL || data == NULL) {
  157. return STATE_PORT_INPUT_NULL_POINTER;
  158. }
  159. if (option >= CORE_SYSDEP_NETWORK_MAX) {
  160. return STATE_PORT_INPUT_OUT_RANGE;
  161. }
  162. switch (option) {
  163. case CORE_SYSDEP_NETWORK_PROFILE_IDX:
  164. network_handle->profile_idx = *(uint32_t *)data;
  165. break;
  166. case CORE_SYSDEP_NETWORK_SOCKET_TYPE: {
  167. network_handle->socket_type = *(core_sysdep_socket_type_t *)data;
  168. }
  169. break;
  170. case CORE_SYSDEP_NETWORK_HOST: {
  171. network_handle->host = malloc(strlen(data) + 1);
  172. if (network_handle->host == NULL) {
  173. return STATE_PORT_MALLOC_FAILED;
  174. }
  175. memset(network_handle->host, 0, strlen(data) + 1);
  176. memcpy(network_handle->host, data, strlen(data));
  177. }
  178. break;
  179. case CORE_SYSDEP_NETWORK_BACKUP_IP: {
  180. memcpy(network_handle->backup_ip, data, strlen(data));
  181. }
  182. break;
  183. case CORE_SYSDEP_NETWORK_PORT: {
  184. network_handle->port = *(uint16_t *)data;
  185. }
  186. break;
  187. case CORE_SYSDEP_NETWORK_CONNECT_TIMEOUT_MS: {
  188. network_handle->connect_timeout_ms = *(uint32_t *)data;
  189. }
  190. break;
  191. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  192. case CORE_SYSDEP_NETWORK_CRED: {
  193. network_handle->cred = malloc(sizeof(aiot_sysdep_network_cred_t));
  194. if (network_handle->cred == NULL) {
  195. return STATE_PORT_MALLOC_FAILED;
  196. }
  197. memset(network_handle->cred, 0, sizeof(aiot_sysdep_network_cred_t));
  198. memcpy(network_handle->cred, data, sizeof(aiot_sysdep_network_cred_t));
  199. }
  200. break;
  201. case CORE_SYSDEP_NETWORK_PSK: {
  202. core_sysdep_psk_t *psk = (core_sysdep_psk_t *)data;
  203. network_handle->psk.psk_id = malloc(strlen(psk->psk_id) + 1);
  204. if (network_handle->psk.psk_id == NULL) {
  205. return STATE_PORT_MALLOC_FAILED;
  206. }
  207. memset(network_handle->psk.psk_id, 0, strlen(psk->psk_id) + 1);
  208. memcpy(network_handle->psk.psk_id, psk->psk_id, strlen(psk->psk_id));
  209. network_handle->psk.psk = malloc(strlen(psk->psk) + 1);
  210. if (network_handle->psk.psk == NULL) {
  211. free(network_handle->psk.psk_id);
  212. return STATE_PORT_MALLOC_FAILED;
  213. }
  214. memset(network_handle->psk.psk, 0, strlen(psk->psk) + 1);
  215. memcpy(network_handle->psk.psk, psk->psk, strlen(psk->psk));
  216. }
  217. break;
  218. #endif
  219. default: {
  220. QL_ADAPT_LOG("unknown option\n");
  221. }
  222. }
  223. return STATE_SUCCESS;
  224. }
  225. static void _port_uint2str(uint16_t input, char *output)
  226. {
  227. uint8_t i = 0, j = 0;
  228. char temp[6] = {0};
  229. do {
  230. temp[i++] = input % 10 + '0';
  231. } while ((input /= 10) > 0);
  232. do {
  233. output[--i] = temp[j++];
  234. } while (i > 0);
  235. }
  236. static int32_t _core_sysdep_network_connect(int profile_index, char *host, uint16_t port, int family, int socktype, int protocol, uint32_t timeout_ms, int *fd_out)
  237. {
  238. int32_t res = STATE_SUCCESS;
  239. int fd = 0, sock_option = 0;
  240. char service[6] = {0};
  241. struct addrinfo hints;
  242. struct addrinfo *addrInfoList = NULL, *pos = NULL;
  243. ql_data_call_info_s info ={0};
  244. memset(&hints, 0, sizeof(struct addrinfo));
  245. hints.ai_family = family;
  246. hints.ai_socktype = socktype;
  247. hints.ai_protocol = protocol;
  248. hints.ai_flags = 0;
  249. _port_uint2str(port, service);
  250. ql_get_data_call_info(0, profile_index, &info);
  251. res = getaddrinfo_with_pcid(host, service, &hints, &addrInfoList, profile_index);
  252. if(res != 0)
  253. {
  254. QL_ADAPT_LOG("*** getaddrinfo_with_pcid fail ret =%d ***\r\n",res);
  255. return res;
  256. }
  257. QL_ADAPT_LOG("getaddrinfo_with_pcid success profile_index=%d",profile_index);
  258. if (res == 0)
  259. {
  260. for (pos = addrInfoList; pos != NULL; pos = pos->ai_next) {
  261. fd = socket(pos->ai_family, pos->ai_socktype, pos->ai_protocol);
  262. if (fd < 0) {
  263. res = STATE_PORT_NETWORK_SOCKET_CREATE_FAILED;
  264. continue;
  265. }
  266. struct sockaddr_in ip4_local_addr = {0};
  267. ip4_local_addr.sin_family = AF_INET;
  268. ip4_local_addr.sin_port = 0;
  269. //ip4_local_addr.sin_addr = info.v4.addr.ip;
  270. inet_aton(ip4addr_ntoa(&info.v4.addr.ip), &ip4_local_addr.sin_addr);
  271. res = bind(fd, (struct sockaddr *)&ip4_local_addr, sizeof(ip4_local_addr));
  272. if(res < 0)
  273. {
  274. return res;
  275. }
  276. res = fcntl(fd, F_GETFL, 0);
  277. QL_ADAPT_LOG("fcntl res =%d",res);
  278. if (res != -1) {
  279. res = fcntl(fd, F_SETFL, sock_option | O_NONBLOCK);
  280. QL_ADAPT_LOG("fcntl res =%d",res);
  281. }
  282. if (res == -1) {
  283. if (connect(fd, pos->ai_addr, pos->ai_addrlen) == 0) {
  284. *fd_out = fd;
  285. res = STATE_SUCCESS;
  286. break;
  287. } else {
  288. res = STATE_PORT_NETWORK_CONNECT_FAILED;
  289. }
  290. } else
  291. {
  292. fd_set read_sets;
  293. fd_set write_sets;
  294. struct timeval timeselect;
  295. socklen_t optlen = 0;
  296. int sock_error = 0;
  297. FD_ZERO(&write_sets);
  298. FD_ZERO(&read_sets);
  299. FD_SET(fd, &read_sets);
  300. FD_SET(fd, &write_sets);
  301. if(timeout_ms == 0 || timeout_ms < 2000)
  302. timeout_ms = DEFAULT_SELECT_TIME_OUT;
  303. timeselect.tv_sec = timeout_ms / 1000;
  304. timeselect.tv_usec = timeout_ms % 1000 * 1000;
  305. res =connect(fd, pos->ai_addr, pos->ai_addrlen);
  306. if (res == 0) {
  307. *fd_out = fd;
  308. res = STATE_SUCCESS;
  309. QL_ADAPT_LOG("connect success");
  310. break;
  311. } else if (lwip_get_error(fd) != EINPROGRESS) {
  312. QL_ADAPT_LOG("connect EINPROGRESS errno =%d",lwip_get_error(fd));
  313. res = STATE_PORT_NETWORK_CONNECT_FAILED;
  314. } else
  315. {
  316. res = select(fd + 1, &read_sets, &write_sets, NULL, &timeselect);
  317. if (res == 0 ) {
  318. QL_ADAPT_LOG("connect STATE_MQTT_LOG_CONNECT_TIMEOUT");
  319. res = STATE_MQTT_LOG_CONNECT_TIMEOUT;
  320. } else if (res < 0) {
  321. QL_ADAPT_LOG("connect STATE_PORT_NETWORK_CONNECT_FAILED");
  322. res = STATE_PORT_NETWORK_CONNECT_FAILED;
  323. } else {
  324. if(!FD_ISSET(fd, &read_sets) && !FD_ISSET(fd, &write_sets))
  325. {
  326. QL_ADAPT_LOG("*** connect fail ***\r\n");
  327. res = STATE_PORT_NETWORK_CONNECT_FAILED;
  328. }
  329. else if(FD_ISSET(fd, &read_sets) && FD_ISSET(fd, &write_sets))
  330. {
  331. optlen = sizeof(sock_error);
  332. if(getsockopt(fd, SOL_SOCKET, SO_ERROR, &sock_error, &optlen) == 0 && sock_error == 0)
  333. {
  334. QL_ADAPT_LOG("connect success\r\n");
  335. *fd_out = fd;
  336. res = STATE_SUCCESS;
  337. break;
  338. }
  339. else
  340. {
  341. QL_ADAPT_LOG("*** connect fail, sock_err = %d, errno = %u ***\r\n", sock_error, lwip_get_error(fd));
  342. res = STATE_PORT_NETWORK_CONNECT_FAILED;
  343. }
  344. }
  345. else if(!FD_ISSET(fd, &read_sets) && FD_ISSET(fd, &write_sets))
  346. {
  347. QL_ADAPT_LOG("connect success\r\n");
  348. *fd_out = fd;
  349. res = STATE_SUCCESS;
  350. break;
  351. }
  352. else if(FD_ISSET(fd, &read_sets) && !FD_ISSET(fd, &write_sets))
  353. {
  354. QL_ADAPT_LOG("*** connect fail ***\r\n");
  355. res = STATE_PORT_NETWORK_CONNECT_FAILED;
  356. }
  357. else
  358. {
  359. QL_ADAPT_LOG("*** connect fail ***\r\n");
  360. res = STATE_PORT_NETWORK_CONNECT_FAILED;
  361. }
  362. }
  363. }
  364. }
  365. close(fd);
  366. QL_ADAPT_LOG("connect error, errno: %d\n", lwip_get_error(fd));
  367. }
  368. }
  369. if (res < 0) {
  370. QL_ADAPT_LOG("fail to establish tcp , res: %d\r\n\r\n",res);
  371. } else {
  372. QL_ADAPT_LOG("success to establish tcp, fd=%d\n", *fd_out);
  373. res = STATE_SUCCESS;
  374. }
  375. freeaddrinfo(addrInfoList);
  376. return res;
  377. }
  378. static int32_t _core_sysdep_network_tcp_establish(core_network_handle_t *network_handle)
  379. {
  380. int32_t res = STATE_SUCCESS;
  381. QL_ADAPT_LOG("establish tcp connection with server(host='%s', port=[%u])\n", network_handle->host, network_handle->port);
  382. res = _core_sysdep_network_connect(network_handle->profile_idx, network_handle->host, network_handle->port,
  383. AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, network_handle->connect_timeout_ms, &network_handle->fd);
  384. if (res == STATE_PORT_NETWORK_DNS_FAILED && strlen(network_handle->backup_ip) > 0) {
  385. QL_ADAPT_LOG("using backup ip: %s\n", network_handle->backup_ip);
  386. res = _core_sysdep_network_connect(network_handle->profile_idx, network_handle->backup_ip, network_handle->port,
  387. AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, network_handle->connect_timeout_ms, &network_handle->fd);
  388. }
  389. return res;
  390. }
  391. static int32_t _core_sysdep_network_udp_server_establish(core_network_handle_t *network_handle)
  392. {
  393. int32_t sockfd;
  394. struct sockaddr_in servaddr;
  395. int opt_val = 1;
  396. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  397. if (sockfd < 0) {
  398. return STATE_PORT_NETWORK_SOCKET_CREATE_FAILED;
  399. }
  400. if (0 != setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof(opt_val))) {
  401. QL_ADAPT_LOG("setsockopt(SO_REUSEADDR) falied, errno: %d\n", lwip_get_error(sockfd));
  402. //perror("setsockopt(SO_REUSEADDR) error");
  403. close(sockfd);
  404. return STATE_PORT_NETWORK_SOCKET_CONFIG_FAILED;
  405. }
  406. memset(&servaddr, 0, sizeof(struct sockaddr_in));
  407. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  408. servaddr.sin_family = AF_INET;
  409. servaddr.sin_port = htons(network_handle->port);
  410. if (-1 == bind(sockfd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr_in))) {
  411. QL_ADAPT_LOG("bind(%d) falied, errno: %d\n", (int)sockfd, lwip_get_error(sockfd));
  412. //perror("bind(%d) error");
  413. close(sockfd);
  414. return STATE_PORT_NETWORK_SOCKET_BIND_FAILED;
  415. }
  416. network_handle->fd = sockfd;
  417. QL_ADAPT_LOG("success to establish udp, fd=%d\n", (int)sockfd);
  418. return 0;
  419. }
  420. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  421. void core_sysdep_rand(uint8_t *output, uint32_t output_len);
  422. static int _mbedtls_random(void *handle, unsigned char *output, size_t output_len)
  423. {
  424. core_sysdep_rand(output, output_len);
  425. return 0;
  426. }
  427. static uint8_t _host_is_ip(char *host)
  428. {
  429. uint32_t idx = 0;
  430. if (strlen(host) >= 16) {
  431. return 0;
  432. }
  433. for (idx = 0;idx < strlen(host);idx++) {
  434. if ((host[idx] != '.') && (host[idx] < '0' || host[idx] > '9')) {
  435. return 0;
  436. }
  437. }
  438. return 1;
  439. }
  440. static void _mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str)
  441. {
  442. ((void) level);
  443. if (NULL != ctx) {
  444. QL_ADAPT_LOG("%s\n", str);
  445. }
  446. }
  447. //设置自定义证书校验,经过实际测试aliyun证书需要忽略Common Name (CN) 选项才可以校验通过
  448. static int my_verify(void *data, mbedtls_x509_crt *crt,
  449. int depth, uint32_t *flags)
  450. {
  451. char buf[1024];
  452. ((void)data);
  453. QL_ADAPT_LOG("\nVerify requested for (Depth %d):\n", depth);
  454. mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
  455. QL_ADAPT_LOG("%s", buf);
  456. QL_ADAPT_LOG("user ignore flags: 0x%08X, peer cert verify flags on curt way: 0x%08X", MBEDTLS_X509_BADCERT_NOT_TRUSTED, *flags);
  457. if (*flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
  458. {
  459. *flags &= ~(MBEDTLS_X509_BADCERT_NOT_TRUSTED);
  460. mbedtls_x509_crt_verify_info(buf, sizeof(buf), " ! ", *flags);
  461. QL_ADAPT_LOG("%s\n", buf);
  462. return (0);
  463. }
  464. return (0);
  465. }
  466. static int32_t _core_sysdep_network_mbedtls_establish(core_network_handle_t *network_handle)
  467. {
  468. int32_t res = 0;
  469. char port_str[6] = {0};
  470. #if defined(MBEDTLS_DEBUG_C)
  471. mbedtls_debug_set_threshold(4);
  472. #endif /* #if defined(MBEDTLS_DEBUG_C) */
  473. mbedtls_net_init(&network_handle->mbedtls.net_ctx);
  474. mbedtls_ssl_init(&network_handle->mbedtls.ssl_ctx);
  475. mbedtls_ssl_config_init(&network_handle->mbedtls.ssl_config);
  476. // mbedtls_platform_set_calloc_free(_core_mbedtls_calloc, _core_mbedtls_free);
  477. g_mbedtls_total_mem_used = g_mbedtls_max_mem_used = 0;
  478. if (network_handle->cred->max_tls_fragment == 0) {
  479. QL_ADAPT_LOG("invalid max_tls_fragment parameter\n");
  480. return STATE_PORT_TLS_INVALID_MAX_FRAGMENT;
  481. }
  482. QL_ADAPT_LOG("establish mbedtls connection with server(host='%s', port=[%u], profile_idx=[%d])\n", network_handle->host, network_handle->port,network_handle->profile_idx);
  483. _port_uint2str(network_handle->port, port_str);
  484. if (network_handle->cred->max_tls_fragment <= 512) {
  485. res = mbedtls_ssl_conf_max_frag_len(&network_handle->mbedtls.ssl_config, MBEDTLS_SSL_MAX_FRAG_LEN_512);
  486. } else if (network_handle->cred->max_tls_fragment <= 1024) {
  487. res = mbedtls_ssl_conf_max_frag_len(&network_handle->mbedtls.ssl_config, MBEDTLS_SSL_MAX_FRAG_LEN_1024);
  488. } else if (network_handle->cred->max_tls_fragment <= 2048) {
  489. res = mbedtls_ssl_conf_max_frag_len(&network_handle->mbedtls.ssl_config, MBEDTLS_SSL_MAX_FRAG_LEN_2048);
  490. } else if (network_handle->cred->max_tls_fragment <= 4096) {
  491. res = mbedtls_ssl_conf_max_frag_len(&network_handle->mbedtls.ssl_config, MBEDTLS_SSL_MAX_FRAG_LEN_4096);
  492. } else {
  493. res = mbedtls_ssl_conf_max_frag_len(&network_handle->mbedtls.ssl_config, MBEDTLS_SSL_MAX_FRAG_LEN_NONE);
  494. }
  495. if (res < 0) {
  496. QL_ADAPT_LOG("mbedtls_ssl_conf_max_frag_len error, res: -0x%04X\n", -res);
  497. return res;
  498. }
  499. if (network_handle->socket_type == CORE_SYSDEP_SOCKET_TCP_CLIENT) {
  500. res = _core_sysdep_network_connect(network_handle->profile_idx, network_handle->host, network_handle->port,
  501. AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, network_handle->connect_timeout_ms, &network_handle->mbedtls.net_ctx.fd);
  502. } else if (network_handle->socket_type == CORE_SYSDEP_SOCKET_UDP_CLIENT) {
  503. res = _core_sysdep_network_connect(network_handle->profile_idx, network_handle->host, network_handle->port,
  504. AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, network_handle->connect_timeout_ms, &network_handle->mbedtls.net_ctx.fd);
  505. }
  506. if (res == STATE_PORT_NETWORK_DNS_FAILED && (strlen(network_handle->backup_ip) > 0)) {
  507. QL_ADAPT_LOG("using backup ip: %s\n", network_handle->backup_ip);
  508. if (network_handle->socket_type == CORE_SYSDEP_SOCKET_TCP_CLIENT) {
  509. res = _core_sysdep_network_connect(network_handle->profile_idx, network_handle->host, network_handle->port,
  510. AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, network_handle->connect_timeout_ms, &network_handle->mbedtls.net_ctx.fd);
  511. } else if (network_handle->socket_type == CORE_SYSDEP_SOCKET_UDP_CLIENT) {
  512. res = _core_sysdep_network_connect(network_handle->profile_idx, network_handle->host, network_handle->port,
  513. AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, network_handle->connect_timeout_ms, &network_handle->mbedtls.net_ctx.fd);
  514. }
  515. }
  516. if (res < STATE_SUCCESS) {
  517. return res;
  518. }
  519. if (network_handle->socket_type == CORE_SYSDEP_SOCKET_TCP_CLIENT) {
  520. res = mbedtls_ssl_config_defaults(&network_handle->mbedtls.ssl_config, MBEDTLS_SSL_IS_CLIENT,
  521. MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
  522. } else if (network_handle->socket_type == CORE_SYSDEP_SOCKET_UDP_CLIENT) {
  523. res = mbedtls_ssl_config_defaults(&network_handle->mbedtls.ssl_config, MBEDTLS_SSL_IS_CLIENT,
  524. MBEDTLS_SSL_TRANSPORT_DATAGRAM, MBEDTLS_SSL_PRESET_DEFAULT);
  525. }
  526. if (res < 0) {
  527. QL_ADAPT_LOG("mbedtls_ssl_config_defaults error, res: -0x%04X\n", -res);
  528. return res;
  529. }
  530. mbedtls_ssl_conf_max_version(&network_handle->mbedtls.ssl_config, MBEDTLS_SSL_MAJOR_VERSION_3,
  531. MBEDTLS_SSL_MINOR_VERSION_3);
  532. mbedtls_ssl_conf_min_version(&network_handle->mbedtls.ssl_config, MBEDTLS_SSL_MAJOR_VERSION_3,
  533. MBEDTLS_SSL_MINOR_VERSION_3);
  534. mbedtls_ssl_conf_handshake_timeout(&network_handle->mbedtls.ssl_config,(MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN * 2),
  535. (MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN * 2 * 4));
  536. mbedtls_ssl_conf_rng(&network_handle->mbedtls.ssl_config, _mbedtls_random, NULL);
  537. mbedtls_ssl_conf_dbg(&network_handle->mbedtls.ssl_config, _mbedtls_debug, "[MBEDTLS]");
  538. mbedtls_ssl_conf_verify(&network_handle->mbedtls.ssl_config, my_verify, NULL);
  539. if (network_handle->cred->option == AIOT_SYSDEP_NETWORK_CRED_SVRCERT_CA) {
  540. if (network_handle->cred->x509_server_cert == NULL && network_handle->cred->x509_server_cert_len == 0) {
  541. QL_ADAPT_LOG("invalid x509 server cert\n");
  542. return STATE_PORT_TLS_INVALID_SERVER_CERT;
  543. }
  544. mbedtls_x509_crt_init(&network_handle->mbedtls.x509_server_cert);
  545. res = mbedtls_x509_crt_parse(&network_handle->mbedtls.x509_server_cert,
  546. (const unsigned char *)network_handle->cred->x509_server_cert, (size_t)network_handle->cred->x509_server_cert_len + 1);
  547. if (res < 0) {
  548. QL_ADAPT_LOG("mbedtls_x509_crt_parse server cert error, res: -0x%04X\n", -res);
  549. return STATE_PORT_TLS_INVALID_SERVER_CERT;
  550. }
  551. if (network_handle->cred->x509_client_cert != NULL && network_handle->cred->x509_client_cert_len > 0 &&
  552. network_handle->cred->x509_client_privkey != NULL && network_handle->cred->x509_client_privkey_len > 0) {
  553. mbedtls_x509_crt_init(&network_handle->mbedtls.x509_client_cert);
  554. mbedtls_pk_init(&network_handle->mbedtls.x509_client_pk);
  555. res = mbedtls_x509_crt_parse(&network_handle->mbedtls.x509_client_cert,
  556. (const unsigned char *)network_handle->cred->x509_client_cert, (size_t)network_handle->cred->x509_client_cert_len + 1);
  557. if (res < 0) {
  558. QL_ADAPT_LOG("mbedtls_x509_crt_parse client cert error, res: -0x%04X\n", -res);
  559. return STATE_PORT_TLS_INVALID_CLIENT_CERT;
  560. }
  561. res = mbedtls_pk_parse_key(&network_handle->mbedtls.x509_client_pk,
  562. (const unsigned char *)network_handle->cred->x509_client_privkey,
  563. (size_t)network_handle->cred->x509_client_privkey_len + 1, NULL, 0);
  564. if (res < 0) {
  565. QL_ADAPT_LOG("mbedtls_pk_parse_key client pk error, res: -0x%04X\n", -res);
  566. return STATE_PORT_TLS_INVALID_CLIENT_KEY;
  567. }
  568. res = mbedtls_ssl_conf_own_cert(&network_handle->mbedtls.ssl_config, &network_handle->mbedtls.x509_client_cert,
  569. &network_handle->mbedtls.x509_client_pk);
  570. if (res < 0) {
  571. QL_ADAPT_LOG("mbedtls_ssl_conf_own_cert error, res: -0x%04X\n", -res);
  572. return STATE_PORT_TLS_INVALID_CLIENT_CERT;
  573. }
  574. }
  575. mbedtls_ssl_conf_ca_chain(&network_handle->mbedtls.ssl_config, &network_handle->mbedtls.x509_server_cert, NULL);
  576. } else if (network_handle->cred->option == AIOT_SYSDEP_NETWORK_CRED_SVRCERT_PSK) {
  577. static const int ciphersuites[1] = {MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA};
  578. res = mbedtls_ssl_conf_psk(&network_handle->mbedtls.ssl_config,
  579. (const unsigned char *)network_handle->psk.psk, (size_t)strlen(network_handle->psk.psk),
  580. (const unsigned char *)network_handle->psk.psk_id, (size_t)strlen(network_handle->psk.psk_id));
  581. if (res < 0) {
  582. QL_ADAPT_LOG("mbedtls_ssl_conf_psk error, res = -0x%04X\n", -res);
  583. return STATE_PORT_TLS_CONFIG_PSK_FAILED;
  584. }
  585. mbedtls_ssl_conf_ciphersuites(&network_handle->mbedtls.ssl_config, ciphersuites);
  586. } else {
  587. QL_ADAPT_LOG("unsupported security option\n");
  588. return STATE_PORT_TLS_INVALID_CRED_OPTION;
  589. }
  590. res = mbedtls_ssl_setup(&network_handle->mbedtls.ssl_ctx, &network_handle->mbedtls.ssl_config);
  591. if (res < 0) {
  592. QL_ADAPT_LOG("mbedtls_ssl_setup error, res: -0x%04X\n", -res);
  593. return res;
  594. }
  595. if (_host_is_ip(network_handle->host) == 0) {
  596. res = mbedtls_ssl_set_hostname(&network_handle->mbedtls.ssl_ctx, network_handle->host);
  597. if (res < 0) {
  598. QL_ADAPT_LOG("mbedtls_ssl_set_hostname error, res: -0x%04X\n", -res);
  599. return res;
  600. }
  601. }
  602. if (network_handle->socket_type == CORE_SYSDEP_SOCKET_UDP_CLIENT) {
  603. mbedtls_ssl_set_timer_cb(&network_handle->mbedtls.ssl_ctx, (void *)&network_handle->mbedtls.timer_delay_ctx,
  604. mbedtls_timing_set_delay, mbedtls_timing_get_delay);
  605. }
  606. mbedtls_ssl_set_bio(&network_handle->mbedtls.ssl_ctx, &network_handle->mbedtls.net_ctx, mbedtls_net_send,
  607. mbedtls_net_recv, mbedtls_net_recv_timeout);
  608. mbedtls_ssl_conf_read_timeout(&network_handle->mbedtls.ssl_config, network_handle->connect_timeout_ms);
  609. mbedtls_ssl_conf_authmode(&network_handle->mbedtls.ssl_config, MBEDTLS_SSL_VERIFY_OPTIONAL);
  610. while ((res = mbedtls_ssl_handshake(&network_handle->mbedtls.ssl_ctx)) != 0) {
  611. if ((res != MBEDTLS_ERR_SSL_WANT_READ) && (res != MBEDTLS_ERR_SSL_WANT_WRITE)) {
  612. QL_ADAPT_LOG("mbedtls_ssl_handshake error, res: -0x%04X\n", -res);
  613. if (res == MBEDTLS_ERR_SSL_INVALID_RECORD) {
  614. res = STATE_PORT_TLS_INVALID_RECORD;
  615. } else {
  616. res = STATE_PORT_TLS_INVALID_HANDSHAKE;
  617. }
  618. return res;
  619. }
  620. }
  621. res = mbedtls_ssl_get_verify_result(&network_handle->mbedtls.ssl_ctx);
  622. if (res < 0) {
  623. QL_ADAPT_LOG("mbedtls_ssl_get_verify_result error, res: -0x%04X\n", -res);
  624. return res;
  625. }
  626. QL_ADAPT_LOG("success to establish mbedtls connection, fd = %d(cost %d bytes in total, max used %d bytes)\n",
  627. (int)network_handle->mbedtls.net_ctx.fd,
  628. g_mbedtls_total_mem_used, g_mbedtls_max_mem_used);
  629. return 0;
  630. }
  631. #endif
  632. int32_t core_sysdep_network_establish(void *handle)
  633. {
  634. core_network_handle_t *network_handle = (core_network_handle_t *)handle;
  635. if (handle == NULL) {
  636. return STATE_PORT_INPUT_NULL_POINTER;
  637. }
  638. if (network_handle->socket_type == CORE_SYSDEP_SOCKET_TCP_CLIENT) {
  639. if (network_handle->host == NULL) {
  640. return STATE_PORT_MISSING_HOST;
  641. }
  642. if (network_handle->cred == NULL) {
  643. return _core_sysdep_network_tcp_establish(network_handle);
  644. } else {
  645. if (network_handle->cred->option == AIOT_SYSDEP_NETWORK_CRED_NONE) {
  646. return _core_sysdep_network_tcp_establish(network_handle);
  647. }
  648. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  649. else {
  650. return _core_sysdep_network_mbedtls_establish(network_handle);
  651. }
  652. #endif
  653. }
  654. } else if (network_handle->socket_type == CORE_SYSDEP_SOCKET_TCP_SERVER) {
  655. return STATE_PORT_TCP_SERVER_NOT_IMPLEMENT;
  656. } else if (network_handle->socket_type == CORE_SYSDEP_SOCKET_UDP_CLIENT) {
  657. if (network_handle->host == NULL) {
  658. return STATE_PORT_MISSING_HOST;
  659. }
  660. if (network_handle->cred == NULL) {
  661. return STATE_PORT_UDP_CLIENT_NOT_IMPLEMENT;
  662. } else {
  663. if (network_handle->cred->option == AIOT_SYSDEP_NETWORK_CRED_NONE) {
  664. return STATE_PORT_UDP_CLIENT_NOT_IMPLEMENT;
  665. }
  666. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  667. else {
  668. return _core_sysdep_network_mbedtls_establish(network_handle);
  669. }
  670. #endif
  671. }
  672. } else if (network_handle->socket_type == CORE_SYSDEP_SOCKET_UDP_SERVER) {
  673. return _core_sysdep_network_udp_server_establish(network_handle);
  674. }
  675. QL_ADAPT_LOG("unknown nwk type or tcp host absent\n");
  676. return STATE_PORT_NETWORK_UNKNOWN_SOCKET_TYPE;
  677. }
  678. static int32_t _core_sysdep_network_tcp_recv(core_network_handle_t *network_handle, uint8_t *buffer, uint32_t len,
  679. uint32_t timeout_ms)
  680. {
  681. int res = 0;
  682. int32_t recv_bytes = 0;
  683. ssize_t recv_res = 0;
  684. uint64_t timestart_ms = 0, timenow_ms = 0, timeselect_ms = 0;
  685. fd_set recv_sets;
  686. ql_timeval_t timestart, timenow;
  687. struct timeval timeselect = {0};
  688. FD_ZERO(&recv_sets);
  689. FD_SET(network_handle->fd, &recv_sets);
  690. /* Start Time */
  691. ql_gettimeofday(&timestart);
  692. timestart_ms = (uint64_t)timestart.sec * 1000 + (uint64_t)timestart.usec / 1000;
  693. timenow_ms = timestart_ms;
  694. do {
  695. ql_gettimeofday(&timenow);
  696. timenow_ms = (uint64_t)timenow.sec * 1000 + (uint64_t)timenow.usec / 1000;
  697. if (timenow_ms - timestart_ms >= timenow_ms ||
  698. timeout_ms - (timenow_ms - timestart_ms) > timeout_ms) {
  699. break;
  700. }
  701. timeselect_ms = timeout_ms - (timenow_ms - timestart_ms);
  702. timeselect.tv_sec = timeselect_ms / 1000;
  703. timeselect.tv_usec = timeselect_ms % 1000 * 1000;
  704. res = select(network_handle->fd + 1, &recv_sets, NULL, NULL, &timeselect);
  705. if (res == 0) {
  706. /* QL_ADAPT_LOG("_core_sysdep_network_tcp_recv, nwk select timeout\n"); */
  707. continue;
  708. } else if (res < 0) {
  709. QL_ADAPT_LOG("_core_sysdep_network_tcp_recv, errno: %d\n", lwip_get_error(network_handle->fd));
  710. // perror("_core_sysdep_network_tcp_recv, nwk select failed: ");
  711. return STATE_PORT_NETWORK_SELECT_FAILED;
  712. } else {
  713. if (FD_ISSET(network_handle->fd, &recv_sets)) {
  714. recv_res = recv(network_handle->fd, buffer + recv_bytes, len - recv_bytes, 0);
  715. if (recv_res == 0) {
  716. QL_ADAPT_LOG("_core_sysdep_network_tcp_recv, nwk connection closed\n");
  717. return STATE_PORT_NETWORK_RECV_CONNECTION_CLOSED;
  718. } else if (recv_res < 0) {
  719. QL_ADAPT_LOG("_core_sysdep_network_tcp_recv, errno: %d\n", lwip_get_error(network_handle->fd));
  720. // perror("_core_sysdep_network_tcp_recv, nwk recv error: ");
  721. if (lwip_get_error(network_handle->fd) == EINTR) {
  722. continue;
  723. }
  724. return STATE_PORT_NETWORK_RECV_FAILED;
  725. } else {
  726. recv_bytes += recv_res;
  727. /* QL_ADAPT_LOG("recv_bytes: %d, len: %d\n",recv_bytes,len); */
  728. if (recv_bytes == len) {
  729. break;
  730. }
  731. }
  732. }
  733. }
  734. } while (((timenow_ms - timestart_ms) < timeout_ms) && (recv_bytes < len));
  735. /* QL_ADAPT_LOG("%s: recv over\n",__FUNCTION__); */
  736. return recv_bytes;
  737. }
  738. static int32_t _core_sysdep_network_udp_recv(core_network_handle_t *network_handle, uint8_t *buffer, uint32_t len, uint32_t timeout_ms, core_sysdep_addr_t *addr)
  739. {
  740. int res;
  741. struct sockaddr_in cliaddr;
  742. socklen_t addr_len = sizeof(cliaddr);
  743. fd_set read_fds;
  744. struct timeval timeout = {timeout_ms / 1000, (timeout_ms % 1000) * 1000};
  745. FD_ZERO(&read_fds);
  746. FD_SET(network_handle->fd, &read_fds);
  747. res = select(network_handle->fd + 1, &read_fds, NULL, NULL, &timeout);
  748. if (res == 0) {
  749. QL_ADAPT_LOG("select timeout\n");
  750. return 0;
  751. }
  752. else if (res < 0) {
  753. QL_ADAPT_LOG("_linux_nwk_udp_read select errno: %d\n", lwip_get_error(network_handle->fd));
  754. // perror("_linux_nwk_udp_read select error: ");
  755. return STATE_PORT_NETWORK_SELECT_FAILED;
  756. }
  757. res = recvfrom(network_handle->fd, buffer, len, 0, (struct sockaddr *)&cliaddr, &addr_len);
  758. if (res >= 0) {
  759. if (NULL != addr) {
  760. addr->port = ntohs(cliaddr.sin_port);
  761. strcpy((char *)addr->addr, inet_ntoa(cliaddr.sin_addr));
  762. }
  763. return res;
  764. } else {
  765. QL_ADAPT_LOG("_linux_nwk_udp_read errno: %d\n", lwip_get_error(network_handle->fd));
  766. // perror("_linux_nwk_udp_read error: ");
  767. return STATE_PORT_NETWORK_RECV_FAILED;
  768. }
  769. }
  770. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  771. static int32_t _core_sysdep_network_mbedtls_recv(core_network_handle_t *network_handle, uint8_t *buffer, uint32_t len,
  772. uint32_t timeout_ms)
  773. {
  774. int res = 0;
  775. int32_t recv_bytes = 0;
  776. mbedtls_ssl_conf_read_timeout(&network_handle->mbedtls.ssl_config, timeout_ms);
  777. do {
  778. res = mbedtls_ssl_read(&network_handle->mbedtls.ssl_ctx, buffer + recv_bytes, len - recv_bytes);
  779. if (res < 0) {
  780. if (res == MBEDTLS_ERR_SSL_TIMEOUT) {
  781. break;
  782. } else if (res != MBEDTLS_ERR_SSL_WANT_READ &&
  783. res != MBEDTLS_ERR_SSL_WANT_WRITE &&
  784. res != MBEDTLS_ERR_SSL_CLIENT_RECONNECT) {
  785. if (recv_bytes == 0) {
  786. QL_ADAPT_LOG("mbedtls_ssl_recv error, res: -0x%04X\n", -res);
  787. if (res == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  788. return STATE_PORT_TLS_RECV_CONNECTION_CLOSED;
  789. } else if (res == MBEDTLS_ERR_SSL_INVALID_RECORD) {
  790. return STATE_PORT_TLS_INVALID_RECORD;
  791. } else {
  792. return STATE_PORT_TLS_RECV_FAILED;
  793. }
  794. }
  795. break;
  796. }
  797. } else if (res == 0) {
  798. break;
  799. } else {
  800. recv_bytes += res;
  801. }
  802. } while (recv_bytes < len);
  803. return recv_bytes;
  804. }
  805. #endif
  806. int32_t core_sysdep_network_recv(void *handle, uint8_t *buffer, uint32_t len, uint32_t timeout_ms,
  807. core_sysdep_addr_t *addr)
  808. {
  809. core_network_handle_t *network_handle = (core_network_handle_t *)handle;
  810. if (handle == NULL || buffer == NULL) {
  811. return STATE_PORT_INPUT_NULL_POINTER;
  812. }
  813. if (len == 0 || timeout_ms == 0) {
  814. return STATE_PORT_INPUT_OUT_RANGE;
  815. }
  816. if (network_handle->socket_type == CORE_SYSDEP_SOCKET_TCP_CLIENT) {
  817. if (network_handle->cred == NULL) {
  818. return _core_sysdep_network_tcp_recv(network_handle, buffer, len, timeout_ms);
  819. } else {
  820. if (network_handle->cred->option == AIOT_SYSDEP_NETWORK_CRED_NONE) {
  821. return _core_sysdep_network_tcp_recv(network_handle, buffer, len, timeout_ms);
  822. }
  823. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  824. else {
  825. return _core_sysdep_network_mbedtls_recv(network_handle, buffer, len, timeout_ms);
  826. }
  827. #endif
  828. }
  829. } else if (network_handle->socket_type == CORE_SYSDEP_SOCKET_TCP_SERVER) {
  830. return STATE_PORT_TCP_SERVER_NOT_IMPLEMENT;
  831. } else if (network_handle->socket_type == CORE_SYSDEP_SOCKET_UDP_CLIENT) {
  832. if (network_handle->cred == NULL) {
  833. return STATE_PORT_UDP_CLIENT_NOT_IMPLEMENT;
  834. } else {
  835. if (network_handle->cred->option == AIOT_SYSDEP_NETWORK_CRED_NONE) {
  836. return STATE_PORT_UDP_CLIENT_NOT_IMPLEMENT;
  837. }
  838. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  839. else {
  840. return _core_sysdep_network_mbedtls_recv(network_handle, buffer, len, timeout_ms);
  841. }
  842. #endif
  843. }
  844. } else if (network_handle->socket_type == CORE_SYSDEP_SOCKET_UDP_SERVER) {
  845. return _core_sysdep_network_udp_recv(network_handle, buffer, len, timeout_ms, addr);
  846. }
  847. QL_ADAPT_LOG("unknown nwk type\n");
  848. return STATE_PORT_NETWORK_UNKNOWN_SOCKET_TYPE;
  849. }
  850. int32_t _core_sysdep_network_tcp_send(core_network_handle_t *network_handle, uint8_t *buffer, uint32_t len,
  851. uint32_t timeout_ms)
  852. {
  853. int res = 0;
  854. int32_t send_bytes = 0;
  855. ssize_t send_res = 0;
  856. uint64_t timestart_ms = 0, timenow_ms = 0, timeselect_ms = 0;
  857. fd_set send_sets;
  858. ql_timeval_t timestart, timenow;
  859. struct timeval timeselect = {0};
  860. FD_ZERO(&send_sets);
  861. FD_SET(network_handle->fd, &send_sets);
  862. /* Start Time */
  863. ql_gettimeofday(&timestart);
  864. timestart_ms = (uint64_t)timestart.sec * 1000 + timestart.usec / 1000;
  865. timenow_ms = timestart_ms;
  866. do {
  867. ql_gettimeofday(&timenow);
  868. timenow_ms = (uint64_t)timenow.sec * 1000 + timenow.usec / 1000;
  869. if (timenow_ms - timestart_ms >= timenow_ms ||
  870. timeout_ms - (timenow_ms - timestart_ms) > timeout_ms) {
  871. break;
  872. }
  873. timeselect_ms = timeout_ms - (timenow_ms - timestart_ms);
  874. timeselect.tv_sec = timeselect_ms / 1000;
  875. timeselect.tv_usec = timeselect_ms % 1000 * 1000;
  876. res = select(network_handle->fd + 1, NULL, &send_sets, NULL, &timeselect);
  877. if (res == 0) {
  878. QL_ADAPT_LOG("_core_sysdep_network_tcp_send, nwk select timeout\n");
  879. continue;
  880. } else if (res < 0) {
  881. QL_ADAPT_LOG("_core_sysdep_network_tcp_send, errno: %d\n", lwip_get_error(network_handle->fd));
  882. // perror("_core_sysdep_network_tcp_send, nwk select failed: ");
  883. return STATE_PORT_NETWORK_SELECT_FAILED;
  884. } else {
  885. if (FD_ISSET(network_handle->fd, &send_sets)) {
  886. send_res = send(network_handle->fd, buffer + send_bytes, len - send_bytes, 0);
  887. if (send_res == 0) {
  888. QL_ADAPT_LOG("_core_sysdep_network_tcp_send, nwk connection closed\n");
  889. return STATE_PORT_NETWORK_SEND_CONNECTION_CLOSED;
  890. } else if (send_res < 0) {
  891. QL_ADAPT_LOG("_core_sysdep_network_tcp_send, errno: %d\n", lwip_get_error(network_handle->fd));
  892. // perror("_core_sysdep_network_tcp_send, nwk recv error: ");
  893. if (lwip_get_error(network_handle->fd) == EINTR) {
  894. continue;
  895. }
  896. return STATE_PORT_NETWORK_SEND_FAILED;
  897. } else {
  898. send_bytes += send_res;
  899. if (send_bytes == len) {
  900. break;
  901. }
  902. }
  903. }
  904. }
  905. } while (((timenow_ms - timestart_ms) < timeout_ms) && (send_bytes < len));
  906. return send_bytes;
  907. }
  908. int32_t _core_sysdep_network_udp_send(core_network_handle_t *network_handle, uint8_t *buffer, uint32_t len, uint32_t timeout_ms, core_sysdep_addr_t *addr)
  909. {
  910. struct sockaddr_in cliaddr;
  911. fd_set write_fds;
  912. struct timeval timeout = {timeout_ms / 1000, (timeout_ms % 1000) * 1000};
  913. int res;
  914. if (addr == NULL) {
  915. QL_ADAPT_LOG("invalid parameter addr\n");
  916. return STATE_PORT_NETWORK_SEND_FAILED;
  917. }
  918. FD_ZERO(&write_fds);
  919. FD_SET(network_handle->fd, &write_fds);
  920. res = select(network_handle->fd + 1, NULL, &write_fds, NULL, &timeout);
  921. if (res == 0) {
  922. QL_ADAPT_LOG("select timeout\n");
  923. return 0;
  924. } else if (res < 0) {
  925. QL_ADAPT_LOG("_linux_nwk_udp_write select errno: %d\n", lwip_get_error(network_handle->fd));
  926. // perror("_linux_nwk_udp_write select error");
  927. return STATE_PORT_NETWORK_SELECT_FAILED;
  928. }
  929. res = inet_aton((char *)addr->addr, &cliaddr.sin_addr);
  930. if (res < 0) {
  931. QL_ADAPT_LOG("sys_nwk_write, addr error\r\n");
  932. return STATE_PORT_NETWORK_SEND_FAILED;
  933. }
  934. cliaddr.sin_family = AF_INET;
  935. cliaddr.sin_port = htons(addr->port);
  936. res = sendto(network_handle->fd, buffer, len, 0, (struct sockaddr *)&cliaddr, sizeof(struct sockaddr_in));
  937. if (res < 0) {
  938. QL_ADAPT_LOG("_linux_nwk_udp_write errno: %d\n", lwip_get_error(network_handle->fd));
  939. // perror("_linux_nwk_udp_write error");
  940. return STATE_PORT_NETWORK_SEND_FAILED;
  941. }
  942. return res;
  943. }
  944. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  945. int32_t _core_sysdep_network_mbedtls_send(core_network_handle_t *network_handle, uint8_t *buffer, uint32_t len,
  946. uint32_t timeout_ms)
  947. {
  948. int32_t res = 0;
  949. int32_t send_bytes = 0;
  950. uint64_t timestart_ms = 0, timenow_ms = 0;
  951. ql_timeval_t timestart, timenow;// timeout;
  952. /* timeout */
  953. // timeout.sec = timeout_ms / 1000;
  954. // timeout.usec = (timeout_ms % 1000) * 1000;
  955. /* Start Time */
  956. ql_gettimeofday(&timestart);
  957. timestart_ms = (uint64_t)timestart.sec * 1000 + (uint64_t)timestart.usec / 1000;
  958. timenow_ms = timestart_ms;
  959. /*
  960. res = setsockopt(network_handle->mbedtls.net_ctx.fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
  961. if (res < 0) {
  962. QL_ADAPT_LOG("setsockopt error, errno: %d\r\n", lwip_get_error(network_handle->mbedtls.net_ctx.fd));
  963. return STATE_PORT_TLS_SEND_FAILED;
  964. }
  965. */
  966. do {
  967. ql_gettimeofday(&timenow);
  968. timenow_ms = (uint64_t)timenow.sec * 1000 + timenow.usec / 1000;
  969. if (timenow_ms - timestart_ms >= timenow_ms ||
  970. timeout_ms - (timenow_ms - timestart_ms) > timeout_ms) {
  971. break;
  972. }
  973. res = mbedtls_ssl_write(&network_handle->mbedtls.ssl_ctx, buffer + send_bytes, len - send_bytes);
  974. if (res < 0) {
  975. if (res != MBEDTLS_ERR_SSL_WANT_READ &&
  976. res != MBEDTLS_ERR_SSL_WANT_WRITE) {
  977. if (send_bytes == 0) {
  978. QL_ADAPT_LOG("mbedtls_ssl_send error, res: -0x%04X\n", -res);
  979. if (res == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  980. return STATE_PORT_TLS_SEND_CONNECTION_CLOSED;
  981. } else if (res == MBEDTLS_ERR_SSL_INVALID_RECORD) {
  982. return STATE_PORT_TLS_INVALID_RECORD;
  983. } else {
  984. return STATE_PORT_TLS_SEND_FAILED;
  985. }
  986. }
  987. break;
  988. }
  989. } else if (res == 0) {
  990. break;
  991. } else {
  992. send_bytes += res;
  993. }
  994. ql_rtos_task_sleep_ms(100);
  995. } while (((timenow_ms - timestart_ms) < timeout_ms) && (send_bytes < len));
  996. return send_bytes;
  997. }
  998. #endif
  999. int32_t core_sysdep_network_send(void *handle, uint8_t *buffer, uint32_t len, uint32_t timeout_ms,
  1000. core_sysdep_addr_t *addr)
  1001. {
  1002. core_network_handle_t *network_handle = (core_network_handle_t *)handle;
  1003. if (handle == NULL || buffer == NULL) {
  1004. QL_ADAPT_LOG("invalid parameter\n");
  1005. return STATE_PORT_INPUT_NULL_POINTER;
  1006. }
  1007. if (len == 0 || timeout_ms == 0) {
  1008. return STATE_PORT_INPUT_OUT_RANGE;
  1009. }
  1010. if (network_handle->socket_type == CORE_SYSDEP_SOCKET_TCP_CLIENT) {
  1011. if (network_handle->cred == NULL) {
  1012. return _core_sysdep_network_tcp_send(network_handle, buffer, len, timeout_ms);
  1013. } else {
  1014. if (network_handle->cred->option == AIOT_SYSDEP_NETWORK_CRED_NONE) {
  1015. return _core_sysdep_network_tcp_send(network_handle, buffer, len, timeout_ms);
  1016. }
  1017. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  1018. else {
  1019. return _core_sysdep_network_mbedtls_send(network_handle, buffer, len, timeout_ms);
  1020. }
  1021. #endif
  1022. }
  1023. } else if (network_handle->socket_type == CORE_SYSDEP_SOCKET_TCP_SERVER) {
  1024. return STATE_PORT_TCP_SERVER_NOT_IMPLEMENT;
  1025. } else if (network_handle->socket_type == CORE_SYSDEP_SOCKET_UDP_CLIENT) {
  1026. if (network_handle->cred == NULL) {
  1027. return STATE_PORT_UDP_CLIENT_NOT_IMPLEMENT;
  1028. } else {
  1029. if (network_handle->cred->option == AIOT_SYSDEP_NETWORK_CRED_NONE) {
  1030. return STATE_PORT_UDP_CLIENT_NOT_IMPLEMENT;
  1031. }
  1032. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  1033. else {
  1034. return _core_sysdep_network_mbedtls_send(network_handle, buffer, len, timeout_ms);
  1035. }
  1036. #endif
  1037. }
  1038. } else if (network_handle->socket_type == CORE_SYSDEP_SOCKET_UDP_SERVER) {
  1039. return _core_sysdep_network_udp_send(network_handle, buffer, len, timeout_ms, addr);
  1040. }
  1041. QL_ADAPT_LOG("unknown nwk type\n");
  1042. return STATE_PORT_NETWORK_UNKNOWN_SOCKET_TYPE;
  1043. }
  1044. static void _core_sysdep_network_tcp_disconnect(core_network_handle_t *network_handle)
  1045. {
  1046. shutdown(network_handle->fd, 2);
  1047. close(network_handle->fd);
  1048. }
  1049. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  1050. static void _core_sysdep_network_mbedtls_disconnect(core_network_handle_t *network_handle)
  1051. {
  1052. mbedtls_ssl_close_notify(&network_handle->mbedtls.ssl_ctx);
  1053. mbedtls_net_free(&network_handle->mbedtls.net_ctx);
  1054. if (network_handle->cred->option == AIOT_SYSDEP_NETWORK_CRED_SVRCERT_CA) {
  1055. mbedtls_x509_crt_free(&network_handle->mbedtls.x509_server_cert);
  1056. mbedtls_x509_crt_free(&network_handle->mbedtls.x509_client_cert);
  1057. mbedtls_pk_free(&network_handle->mbedtls.x509_client_pk);
  1058. }
  1059. mbedtls_ssl_free(&network_handle->mbedtls.ssl_ctx);
  1060. mbedtls_ssl_config_free(&network_handle->mbedtls.ssl_config);
  1061. g_mbedtls_total_mem_used = g_mbedtls_max_mem_used = 0;
  1062. }
  1063. #endif
  1064. int32_t core_sysdep_network_deinit(void **handle)
  1065. {
  1066. core_network_handle_t *network_handle = NULL;
  1067. if (handle == NULL || *handle == NULL) {
  1068. return STATE_PORT_INPUT_NULL_POINTER;
  1069. }
  1070. network_handle = *(core_network_handle_t **)handle;
  1071. if ((network_handle->socket_type == CORE_SYSDEP_SOCKET_TCP_CLIENT ||
  1072. network_handle->socket_type == CORE_SYSDEP_SOCKET_UDP_CLIENT) && network_handle->host != NULL) {
  1073. if (network_handle->cred == NULL) {
  1074. _core_sysdep_network_tcp_disconnect(network_handle);
  1075. } else {
  1076. if (network_handle->cred->option == AIOT_SYSDEP_NETWORK_CRED_NONE) {
  1077. _core_sysdep_network_tcp_disconnect(network_handle);
  1078. }
  1079. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  1080. else {
  1081. _core_sysdep_network_mbedtls_disconnect(network_handle);
  1082. }
  1083. #endif
  1084. }
  1085. }
  1086. if (network_handle->host != NULL) {
  1087. free(network_handle->host);
  1088. network_handle->host = NULL;
  1089. }
  1090. if (network_handle->cred != NULL) {
  1091. free(network_handle->cred);
  1092. network_handle->cred = NULL;
  1093. }
  1094. #ifdef CORE_SYSDEP_MBEDTLS_ENABLED
  1095. if (network_handle->psk.psk_id != NULL) {
  1096. free(network_handle->psk.psk_id);
  1097. network_handle->psk.psk_id = NULL;
  1098. }
  1099. if (network_handle->psk.psk != NULL) {
  1100. free(network_handle->psk.psk);
  1101. network_handle->psk.psk = NULL;
  1102. }
  1103. #endif
  1104. free(network_handle);
  1105. *handle = NULL;
  1106. return 0;
  1107. }
  1108. void core_sysdep_rand(uint8_t *output, uint32_t output_len)
  1109. {
  1110. uint32_t idx = 0, bytes = 0, rand_num = 0;
  1111. ql_timeval_t time;
  1112. memset(&time, 0, sizeof(ql_timeval_t));
  1113. ql_gettimeofday(&time);
  1114. srand((unsigned int)(time.sec * 1000 + time.usec / 1000) + rand());
  1115. for (idx = 0; idx < output_len;) {
  1116. if (output_len - idx < 4) {
  1117. bytes = output_len - idx;
  1118. } else {
  1119. bytes = 4;
  1120. }
  1121. rand_num = rand();
  1122. while (bytes-- > 0) {
  1123. output[idx++] = (uint8_t)(rand_num >> bytes * 8);
  1124. }
  1125. }
  1126. }
  1127. void *core_sysdep_mutex_init(void)
  1128. {
  1129. ql_mutex_t mutex = NULL;
  1130. ql_rtos_mutex_create(&mutex);
  1131. return (void *)mutex;
  1132. }
  1133. void core_sysdep_mutex_lock(void *mutex)
  1134. {
  1135. if(mutex)
  1136. ql_rtos_mutex_lock((ql_mutex_t)mutex, QL_WAIT_FOREVER);
  1137. }
  1138. void core_sysdep_mutex_unlock(void *mutex)
  1139. {
  1140. if(mutex)
  1141. ql_rtos_mutex_unlock((ql_mutex_t)mutex);
  1142. }
  1143. void core_sysdep_mutex_deinit(void **mutex)
  1144. {
  1145. if(mutex && *mutex)
  1146. {
  1147. ql_rtos_mutex_delete((ql_mutex_t)*mutex);
  1148. *mutex = NULL;
  1149. }
  1150. }
  1151. aiot_sysdep_portfile_t g_aiot_sysdep_portfile = {
  1152. core_sysdep_malloc,
  1153. core_sysdep_free,
  1154. core_sysdep_time,
  1155. core_sysdep_sleep,
  1156. core_sysdep_network_init,
  1157. core_sysdep_network_setopt,
  1158. core_sysdep_network_establish,
  1159. core_sysdep_network_recv,
  1160. core_sysdep_network_send,
  1161. core_sysdep_network_deinit,
  1162. core_sysdep_rand,
  1163. core_sysdep_mutex_init,
  1164. core_sysdep_mutex_lock,
  1165. core_sysdep_mutex_unlock,
  1166. core_sysdep_mutex_deinit,
  1167. };