HAL_UDP_rtthread.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (c) 2006-2018 RT-Thread Development Team. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * Again edit by rt-thread group
  18. * Change Logs:
  19. * Date Author Notes
  20. * 2019-07-21 MurphyZhao first edit
  21. */
  22. #include <rtthread.h>
  23. #include "infra_config.h"
  24. #if defined(HAL_UDP)
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/time.h>
  30. #include <sys/socket.h>
  31. #include <sys/select.h>
  32. #include <netdb.h>
  33. #include <unistd.h>
  34. #define DBG_TAG "ali.udp"
  35. #define DBG_LVL DBG_INFO
  36. #include <rtdbg.h>
  37. #include "infra_compat.h"
  38. intptr_t HAL_UDP_create(char *host, unsigned short port)
  39. {
  40. #define NETWORK_ADDR_LEN (16)
  41. int rc = -1;
  42. long socket_id = -1;
  43. char port_ptr[6] = {0};
  44. struct addrinfo hints;
  45. char addr[NETWORK_ADDR_LEN] = {0};
  46. struct addrinfo *res, *ainfo;
  47. struct sockaddr_in *sa = NULL;
  48. if (NULL == host) {
  49. return (-1);
  50. }
  51. sprintf(port_ptr, "%u", port);
  52. memset((char *)&hints, 0x00, sizeof(hints));
  53. hints.ai_socktype = SOCK_DGRAM;
  54. hints.ai_family = AF_INET;
  55. hints.ai_protocol = IPPROTO_UDP;
  56. rc = getaddrinfo(host, port_ptr, &hints, &res);
  57. if (0 != rc) {
  58. LOG_E("getaddrinfo error");
  59. return (-1);
  60. }
  61. for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) {
  62. if (AF_INET == ainfo->ai_family) {
  63. sa = (struct sockaddr_in *)ainfo->ai_addr;
  64. inet_ntop(AF_INET, &sa->sin_addr, addr, NETWORK_ADDR_LEN);
  65. fprintf(stderr, "The host IP %s, port is %d\r\n", addr, ntohs(sa->sin_port));
  66. socket_id = socket(ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
  67. if (socket_id < 0) {
  68. LOG_E("create socket error");
  69. continue;
  70. }
  71. if (0 == connect(socket_id, ainfo->ai_addr, ainfo->ai_addrlen)) {
  72. break;
  73. }
  74. close(socket_id);
  75. }
  76. }
  77. freeaddrinfo(res);
  78. return socket_id;
  79. #undef NETWORK_ADDR_LEN
  80. }
  81. void HAL_UDP_close(intptr_t p_socket)
  82. {
  83. long socket_id = -1;
  84. socket_id = p_socket;
  85. close(socket_id);
  86. }
  87. int HAL_UDP_write(intptr_t p_socket,
  88. const unsigned char *p_data,
  89. unsigned int datalen)
  90. {
  91. int rc = -1;
  92. long socket_id = -1;
  93. socket_id = (long)p_socket;
  94. rc = send(socket_id, (char *)p_data, (int)datalen, 0);
  95. if (-1 == rc) {
  96. return -1;
  97. }
  98. return rc;
  99. }
  100. int HAL_UDP_readTimeout(intptr_t p_socket,
  101. unsigned char *p_data,
  102. unsigned int datalen,
  103. unsigned int timeout)
  104. {
  105. int ret;
  106. struct timeval tv;
  107. fd_set read_fds;
  108. long socket_id = -1;
  109. if (0 == p_socket || NULL == p_data) {
  110. return -1;
  111. }
  112. socket_id = (long)p_socket;
  113. if (socket_id < 0) {
  114. return -1;
  115. }
  116. FD_ZERO(&read_fds);
  117. FD_SET(socket_id, &read_fds);
  118. tv.tv_sec = timeout / 1000;
  119. tv.tv_usec = (timeout % 1000) * 1000;
  120. ret = select(socket_id + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv);
  121. /* Zero fds ready means we timed out */
  122. if (ret == 0) {
  123. return -2; /* receive timeout */
  124. }
  125. if (ret < 0) {
  126. if (errno == EINTR) {
  127. return -3; /* want read */
  128. }
  129. return -4; /* receive failed */
  130. }
  131. /* This call will not block */
  132. return read(p_socket, p_data, datalen);
  133. }
  134. intptr_t HAL_UDP_create_without_connect(const char *host, unsigned short port)
  135. {
  136. struct sockaddr_in addr;
  137. long sockfd;
  138. int opt_val = 1;
  139. struct hostent *hp;
  140. struct in_addr in;
  141. uint32_t ip;
  142. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  143. if (sockfd < 0) {
  144. LOG_E("socket error");
  145. return -1;
  146. }
  147. if (0 == port) {
  148. return (intptr_t)sockfd;
  149. }
  150. memset(&addr, 0, sizeof(struct sockaddr_in));
  151. if (0 != setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST, &opt_val, sizeof(opt_val))) {
  152. LOG_E("setsockopt error");
  153. close(sockfd);
  154. return -1;
  155. }
  156. if (NULL == host) {
  157. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  158. } else {
  159. if (inet_aton(host, &in)) {
  160. ip = *(uint32_t *)&in;
  161. } else {
  162. hp = gethostbyname(host);
  163. if (!hp) {
  164. LOG_E("can't resolute the host address");
  165. close(sockfd);
  166. return -1;
  167. }
  168. ip = *(uint32_t *)(hp->h_addr);
  169. }
  170. addr.sin_addr.s_addr = ip;
  171. }
  172. addr.sin_family = AF_INET;
  173. addr.sin_port = htons(port);
  174. if (-1 == bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) {
  175. close(sockfd);
  176. return -1;
  177. }
  178. LOG_D("success to establish udp, fd=%d", (int)sockfd);
  179. return (intptr_t)sockfd;
  180. }
  181. int HAL_UDP_connect(intptr_t sockfd,
  182. const char *host,
  183. unsigned short port)
  184. {
  185. int rc = -1;
  186. char port_ptr[6] = {0};
  187. struct addrinfo hints;
  188. struct addrinfo *res, *ainfo;
  189. if (NULL == host) {
  190. return -1;
  191. }
  192. LOG_D("HAL_UDP_connect, host=%s, port=%d", host, port);
  193. sprintf(port_ptr, "%u", port);
  194. memset((char *)&hints, 0x00, sizeof(hints));
  195. hints.ai_socktype = SOCK_DGRAM;
  196. hints.ai_family = AF_INET;
  197. hints.ai_protocol = IPPROTO_UDP;
  198. rc = getaddrinfo(host, port_ptr, &hints, &res);
  199. if (0 != rc) {
  200. LOG_E("getaddrinfo error");
  201. return -1;
  202. }
  203. for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) {
  204. if (AF_INET == ainfo->ai_family) {
  205. if (0 == connect(sockfd, ainfo->ai_addr, ainfo->ai_addrlen)) {
  206. freeaddrinfo(res);
  207. return 0;
  208. }
  209. }
  210. }
  211. freeaddrinfo(res);
  212. return -1;
  213. }
  214. int HAL_UDP_close_without_connect(intptr_t sockfd)
  215. {
  216. return close((int)sockfd);
  217. }
  218. int HAL_UDP_joinmulticast(intptr_t sockfd,
  219. char *p_group)
  220. {
  221. int err = -1;
  222. int socket_id = -1;
  223. int loop = 0;
  224. struct ip_mreq mreq;
  225. if (NULL == p_group) {
  226. return -1;
  227. }
  228. /*set loopback*/
  229. socket_id = (int)sockfd;
  230. err = setsockopt(socket_id, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
  231. if (err < 0) {
  232. LOG_E("setsockopt error");
  233. return err;
  234. }
  235. mreq.imr_multiaddr.s_addr = inet_addr(p_group);
  236. mreq.imr_interface.s_addr = htonl(INADDR_ANY); /*default networt interface*/
  237. /*join to the multicast group*/
  238. err = setsockopt(socket_id, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
  239. if (err < 0) {
  240. LOG_E("setsockopt error");
  241. return err;
  242. }
  243. return 0;
  244. }
  245. int HAL_UDP_recvfrom(intptr_t sockfd,
  246. NetworkAddr *p_remote,
  247. unsigned char *p_data,
  248. unsigned int datalen,
  249. unsigned int timeout_ms)
  250. {
  251. int ret;
  252. struct sockaddr_in addr;
  253. socklen_t addr_len = sizeof(addr);
  254. fd_set read_fds;
  255. struct timeval timeout = {timeout_ms / 1000, (timeout_ms % 1000) * 1000};
  256. FD_ZERO(&read_fds);
  257. FD_SET(sockfd, &read_fds);
  258. ret = select(sockfd + 1, &read_fds, NULL, NULL, &timeout);
  259. if (ret == 0) {
  260. return 0; /* receive timeout */
  261. }
  262. if (ret < 0) {
  263. if (errno == EINTR) {
  264. return -3; /* want read */
  265. }
  266. return -4; /* receive failed */
  267. }
  268. ret = recvfrom(sockfd, p_data, datalen, 0, (struct sockaddr *)&addr, &addr_len);
  269. if (ret > 0) {
  270. if (NULL != p_remote) {
  271. p_remote->port = ntohs(addr.sin_port);
  272. strcpy((char *)p_remote->addr, inet_ntoa(addr.sin_addr));
  273. }
  274. return ret;
  275. }
  276. return -1;
  277. }
  278. int HAL_UDP_sendto(intptr_t sockfd,
  279. const NetworkAddr *p_remote,
  280. const unsigned char *p_data,
  281. unsigned int datalen,
  282. unsigned int timeout_ms)
  283. {
  284. int ret;
  285. uint32_t ip;
  286. struct in_addr in;
  287. struct hostent *hp;
  288. struct sockaddr_in addr;
  289. fd_set write_fds;
  290. struct timeval timeout = {timeout_ms / 1000, (timeout_ms % 1000) * 1000};
  291. if (inet_aton((char *)p_remote->addr, &in)) {
  292. ip = *(uint32_t *)&in;
  293. } else {
  294. hp = gethostbyname((char *)p_remote->addr);
  295. if (!hp) {
  296. LOG_E("can't resolute the host address");
  297. return -1;
  298. }
  299. ip = *(uint32_t *)(hp->h_addr);
  300. }
  301. FD_ZERO(&write_fds);
  302. FD_SET(sockfd, &write_fds);
  303. ret = select(sockfd + 1, NULL, &write_fds, NULL, &timeout);
  304. if (ret == 0) {
  305. return 0; /* write timeout */
  306. }
  307. if (ret < 0) {
  308. if (errno == EINTR) {
  309. return -3; /* want write */
  310. }
  311. return -4; /* write failed */
  312. }
  313. addr.sin_addr.s_addr = ip;
  314. addr.sin_family = AF_INET;
  315. addr.sin_port = htons(p_remote->port);
  316. ret = sendto(sockfd, p_data, datalen, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
  317. if (ret < 0) {
  318. LOG_E("sendto error");
  319. }
  320. return (ret) > 0 ? ret : -1;
  321. }
  322. #endif /* #if defined(HAL_UDP) */