| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- /*
- * Copyright (c) 2006-2018 RT-Thread Development Team. All rights reserved.
- * License-Identifier: Apache-2.0
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may
- * not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * Again edit by rt-thread group
- * Change Logs:
- * Date Author Notes
- * 2019-07-21 MurphyZhao first edit
- */
- #include <rtthread.h>
- #include "infra_config.h"
- #if defined(HAL_UDP)
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/time.h>
- #include <sys/socket.h>
- #include <sys/select.h>
- #include <netdb.h>
- #include <unistd.h>
- #define DBG_TAG "ali.udp"
- #define DBG_LVL DBG_INFO
- #include <rtdbg.h>
- #include "infra_compat.h"
- intptr_t HAL_UDP_create(char *host, unsigned short port)
- {
- #define NETWORK_ADDR_LEN (16)
- int rc = -1;
- long socket_id = -1;
- char port_ptr[6] = {0};
- struct addrinfo hints;
- char addr[NETWORK_ADDR_LEN] = {0};
- struct addrinfo *res, *ainfo;
- struct sockaddr_in *sa = NULL;
- if (NULL == host) {
- return (-1);
- }
- sprintf(port_ptr, "%u", port);
- memset((char *)&hints, 0x00, sizeof(hints));
- hints.ai_socktype = SOCK_DGRAM;
- hints.ai_family = AF_INET;
- hints.ai_protocol = IPPROTO_UDP;
- rc = getaddrinfo(host, port_ptr, &hints, &res);
- if (0 != rc) {
- LOG_E("getaddrinfo error");
- return (-1);
- }
- for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) {
- if (AF_INET == ainfo->ai_family) {
- sa = (struct sockaddr_in *)ainfo->ai_addr;
- inet_ntop(AF_INET, &sa->sin_addr, addr, NETWORK_ADDR_LEN);
- fprintf(stderr, "The host IP %s, port is %d\r\n", addr, ntohs(sa->sin_port));
- socket_id = socket(ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
- if (socket_id < 0) {
- LOG_E("create socket error");
- continue;
- }
- if (0 == connect(socket_id, ainfo->ai_addr, ainfo->ai_addrlen)) {
- break;
- }
- close(socket_id);
- }
- }
- freeaddrinfo(res);
- return socket_id;
- #undef NETWORK_ADDR_LEN
- }
- void HAL_UDP_close(intptr_t p_socket)
- {
- long socket_id = -1;
- socket_id = p_socket;
- close(socket_id);
- }
- int HAL_UDP_write(intptr_t p_socket,
- const unsigned char *p_data,
- unsigned int datalen)
- {
- int rc = -1;
- long socket_id = -1;
- socket_id = (long)p_socket;
- rc = send(socket_id, (char *)p_data, (int)datalen, 0);
- if (-1 == rc) {
- return -1;
- }
- return rc;
- }
- int HAL_UDP_readTimeout(intptr_t p_socket,
- unsigned char *p_data,
- unsigned int datalen,
- unsigned int timeout)
- {
- int ret;
- struct timeval tv;
- fd_set read_fds;
- long socket_id = -1;
- if (0 == p_socket || NULL == p_data) {
- return -1;
- }
- socket_id = (long)p_socket;
- if (socket_id < 0) {
- return -1;
- }
- FD_ZERO(&read_fds);
- FD_SET(socket_id, &read_fds);
- tv.tv_sec = timeout / 1000;
- tv.tv_usec = (timeout % 1000) * 1000;
- ret = select(socket_id + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv);
- /* Zero fds ready means we timed out */
- if (ret == 0) {
- return -2; /* receive timeout */
- }
- if (ret < 0) {
- if (errno == EINTR) {
- return -3; /* want read */
- }
- return -4; /* receive failed */
- }
- /* This call will not block */
- return read(p_socket, p_data, datalen);
- }
- intptr_t HAL_UDP_create_without_connect(const char *host, unsigned short port)
- {
- struct sockaddr_in addr;
- long sockfd;
- int opt_val = 1;
- struct hostent *hp;
- struct in_addr in;
- uint32_t ip;
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- if (sockfd < 0) {
- LOG_E("socket error");
- return -1;
- }
- if (0 == port) {
- return (intptr_t)sockfd;
- }
- memset(&addr, 0, sizeof(struct sockaddr_in));
- if (0 != setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST, &opt_val, sizeof(opt_val))) {
- LOG_E("setsockopt error");
- close(sockfd);
- return -1;
- }
- if (NULL == host) {
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
- } else {
- if (inet_aton(host, &in)) {
- ip = *(uint32_t *)∈
- } else {
- hp = gethostbyname(host);
- if (!hp) {
- LOG_E("can't resolute the host address");
- close(sockfd);
- return -1;
- }
- ip = *(uint32_t *)(hp->h_addr);
- }
- addr.sin_addr.s_addr = ip;
- }
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- if (-1 == bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) {
- close(sockfd);
- return -1;
- }
- LOG_D("success to establish udp, fd=%d", (int)sockfd);
- return (intptr_t)sockfd;
- }
- int HAL_UDP_connect(intptr_t sockfd,
- const char *host,
- unsigned short port)
- {
- int rc = -1;
- char port_ptr[6] = {0};
- struct addrinfo hints;
- struct addrinfo *res, *ainfo;
- if (NULL == host) {
- return -1;
- }
- LOG_D("HAL_UDP_connect, host=%s, port=%d", host, port);
- sprintf(port_ptr, "%u", port);
- memset((char *)&hints, 0x00, sizeof(hints));
- hints.ai_socktype = SOCK_DGRAM;
- hints.ai_family = AF_INET;
- hints.ai_protocol = IPPROTO_UDP;
- rc = getaddrinfo(host, port_ptr, &hints, &res);
- if (0 != rc) {
- LOG_E("getaddrinfo error");
- return -1;
- }
- for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) {
- if (AF_INET == ainfo->ai_family) {
- if (0 == connect(sockfd, ainfo->ai_addr, ainfo->ai_addrlen)) {
- freeaddrinfo(res);
- return 0;
- }
- }
- }
- freeaddrinfo(res);
- return -1;
- }
- int HAL_UDP_close_without_connect(intptr_t sockfd)
- {
- return close((int)sockfd);
- }
- int HAL_UDP_joinmulticast(intptr_t sockfd,
- char *p_group)
- {
- int err = -1;
- int socket_id = -1;
- int loop = 0;
- struct ip_mreq mreq;
- if (NULL == p_group) {
- return -1;
- }
- /*set loopback*/
- socket_id = (int)sockfd;
- err = setsockopt(socket_id, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
- if (err < 0) {
- LOG_E("setsockopt error");
- return err;
- }
- mreq.imr_multiaddr.s_addr = inet_addr(p_group);
- mreq.imr_interface.s_addr = htonl(INADDR_ANY); /*default networt interface*/
- /*join to the multicast group*/
- err = setsockopt(socket_id, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
- if (err < 0) {
- LOG_E("setsockopt error");
- return err;
- }
- return 0;
- }
- int HAL_UDP_recvfrom(intptr_t sockfd,
- NetworkAddr *p_remote,
- unsigned char *p_data,
- unsigned int datalen,
- unsigned int timeout_ms)
- {
- int ret;
- struct sockaddr_in addr;
- socklen_t addr_len = sizeof(addr);
- fd_set read_fds;
- struct timeval timeout = {timeout_ms / 1000, (timeout_ms % 1000) * 1000};
- FD_ZERO(&read_fds);
- FD_SET(sockfd, &read_fds);
- ret = select(sockfd + 1, &read_fds, NULL, NULL, &timeout);
- if (ret == 0) {
- return 0; /* receive timeout */
- }
- if (ret < 0) {
- if (errno == EINTR) {
- return -3; /* want read */
- }
- return -4; /* receive failed */
- }
- ret = recvfrom(sockfd, p_data, datalen, 0, (struct sockaddr *)&addr, &addr_len);
- if (ret > 0) {
- if (NULL != p_remote) {
- p_remote->port = ntohs(addr.sin_port);
- strcpy((char *)p_remote->addr, inet_ntoa(addr.sin_addr));
- }
- return ret;
- }
- return -1;
- }
- int HAL_UDP_sendto(intptr_t sockfd,
- const NetworkAddr *p_remote,
- const unsigned char *p_data,
- unsigned int datalen,
- unsigned int timeout_ms)
- {
- int ret;
- uint32_t ip;
- struct in_addr in;
- struct hostent *hp;
- struct sockaddr_in addr;
- fd_set write_fds;
- struct timeval timeout = {timeout_ms / 1000, (timeout_ms % 1000) * 1000};
- if (inet_aton((char *)p_remote->addr, &in)) {
- ip = *(uint32_t *)∈
- } else {
- hp = gethostbyname((char *)p_remote->addr);
- if (!hp) {
- LOG_E("can't resolute the host address");
- return -1;
- }
- ip = *(uint32_t *)(hp->h_addr);
- }
- FD_ZERO(&write_fds);
- FD_SET(sockfd, &write_fds);
- ret = select(sockfd + 1, NULL, &write_fds, NULL, &timeout);
- if (ret == 0) {
- return 0; /* write timeout */
- }
- if (ret < 0) {
- if (errno == EINTR) {
- return -3; /* want write */
- }
- return -4; /* write failed */
- }
- addr.sin_addr.s_addr = ip;
- addr.sin_family = AF_INET;
- addr.sin_port = htons(p_remote->port);
- ret = sendto(sockfd, p_data, datalen, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
- if (ret < 0) {
- LOG_E("sendto error");
- }
- return (ret) > 0 ? ret : -1;
- }
- #endif /* #if defined(HAL_UDP) */
|