| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- /*
- * Copyright (C) 2015-2018 Alibaba Group Holding Limited
- */
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/time.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <netinet/tcp.h>
- #include <netdb.h>
- #include "infra_config.h"
- static uint64_t _linux_get_time_ms(void)
- {
- struct timeval tv = { 0 };
- uint64_t time_ms;
- gettimeofday(&tv, NULL);
- time_ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
- return time_ms;
- }
- static uint64_t _linux_time_left(uint64_t t_end, uint64_t t_now)
- {
- uint64_t t_left;
- if (t_end > t_now) {
- t_left = t_end - t_now;
- } else {
- t_left = 0;
- }
- return t_left;
- }
- uintptr_t HAL_TCP_Establish(const char *host, uint16_t port)
- {
- struct addrinfo hints;
- struct addrinfo *addrInfoList = NULL;
- struct addrinfo *cur = NULL;
- int fd = 0;
- int rc = 0;
- char service[6];
- uint8_t dns_retry = 0;
- memset(&hints, 0, sizeof(hints));
- printf("establish tcp connection with server(host='%s', port=[%u])\n", host, port);
- hints.ai_family = AF_INET; /* only IPv4 */
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- sprintf(service, "%u", port);
- while(dns_retry++ < 8) {
- rc = getaddrinfo(host, service, &hints, &addrInfoList);
- if (rc != 0) {
- printf("getaddrinfo error[%d], res: %s, host: %s, port: %s\n", dns_retry, gai_strerror(rc), host, service);
- sleep(1);
- continue;
- }else{
- break;
- }
- }
- if (rc != 0) {
- printf("getaddrinfo error(%d), host = '%s', port = [%d]\n", rc, host, port);
- return (uintptr_t)(-1);
- }
- for (cur = addrInfoList; cur != NULL; cur = cur->ai_next) {
- if (cur->ai_family != AF_INET) {
- printf("socket type error\n");
- rc = -1;
- continue;
- }
- fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
- if (fd < 0) {
- printf("create socket error\n");
- rc = -1;
- continue;
- }
- if (connect(fd, cur->ai_addr, cur->ai_addrlen) == 0) {
- rc = fd;
- break;
- }
- close(fd);
- printf("connect error\n");
- rc = -1;
- }
- if (-1 == rc) {
- printf("fail to establish tcp\n");
- } else {
- printf("success to establish tcp, fd=%d\n", rc);
- }
- freeaddrinfo(addrInfoList);
- return (uintptr_t)rc;
- }
- int HAL_TCP_Destroy(uintptr_t fd)
- {
- int rc;
- /* Shutdown both send and receive operations. */
- rc = shutdown((int) fd, 2);
- if (0 != rc) {
- printf("shutdown error\n");
- return -1;
- }
- rc = close((int) fd);
- if (0 != rc) {
- printf("closesocket error\n");
- return -1;
- }
- return 0;
- }
- int32_t HAL_TCP_Write(uintptr_t fd, const char *buf, uint32_t len, uint32_t timeout_ms)
- {
- int ret,tcp_fd;
- uint32_t len_sent;
- uint64_t t_end, t_left;
- fd_set sets;
- int net_err = 0;
- t_end = _linux_get_time_ms() + timeout_ms;
- len_sent = 0;
- ret = 1; /* send one time if timeout_ms is value 0 */
- if (fd >= FD_SETSIZE) {
- return -1;
- }
- tcp_fd = (int)fd;
- do {
- t_left = _linux_time_left(t_end, _linux_get_time_ms());
- if (0 != t_left) {
- struct timeval timeout;
- FD_ZERO(&sets);
- FD_SET(tcp_fd, &sets);
- timeout.tv_sec = t_left / 1000;
- timeout.tv_usec = (t_left % 1000) * 1000;
- ret = select(tcp_fd + 1, NULL, &sets, NULL, &timeout);
- if (ret > 0) {
- if (0 == FD_ISSET(tcp_fd, &sets)) {
- printf("Should NOT arrive\n");
- /* If timeout in next loop, it will not sent any data */
- ret = 0;
- continue;
- }
- } else if (0 == ret) {
- printf("select-write timeout %d\n", tcp_fd);
- break;
- } else {
- if (EINTR == errno) {
- printf("EINTR be caught\n");
- continue;
- }
- printf("select-write fail, ret = select() = %d\n", ret);
- net_err = 1;
- break;
- }
- }
- if (ret > 0) {
- ret = send(tcp_fd, buf + len_sent, len - len_sent, 0);
- if (ret > 0) {
- len_sent += ret;
- } else if (0 == ret) {
- printf("No data be sent\n");
- } else {
- if (EINTR == errno) {
- printf("EINTR be caught\n");
- continue;
- }
- printf("send fail, ret = send() = %d\n", ret);
- net_err = 1;
- break;
- }
- }
- } while (!net_err && (len_sent < len) && (_linux_time_left(t_end, _linux_get_time_ms()) > 0));
- if (net_err) {
- return -1;
- } else {
- return len_sent;
- }
- }
- int32_t HAL_TCP_Read(uintptr_t fd, char *buf, uint32_t len, uint32_t timeout_ms)
- {
- int ret, err_code, tcp_fd;
- uint32_t len_recv;
- uint64_t t_end, t_left;
- fd_set sets;
- struct timeval timeout;
- t_end = _linux_get_time_ms() + timeout_ms;
- len_recv = 0;
- err_code = 0;
- if (fd >= FD_SETSIZE) {
- return -1;
- }
- tcp_fd = (int)fd;
- do {
- t_left = _linux_time_left(t_end, _linux_get_time_ms());
- if (0 == t_left) {
- break;
- }
- FD_ZERO(&sets);
- FD_SET(tcp_fd, &sets);
- timeout.tv_sec = t_left / 1000;
- timeout.tv_usec = (t_left % 1000) * 1000;
- ret = select(tcp_fd + 1, &sets, NULL, NULL, &timeout);
- if (ret > 0) {
- ret = recv(tcp_fd, buf + len_recv, len - len_recv, 0);
- if (ret > 0) {
- len_recv += ret;
- } else if (0 == ret) {
- printf("connection is closed\n");
- err_code = -1;
- break;
- } else {
- if (EINTR == errno) {
- continue;
- }
- printf("recv fail\n");
- err_code = -2;
- break;
- }
- } else if (0 == ret) {
- break;
- } else {
- if (EINTR == errno) {
- continue;
- }
- printf("select-recv fail\n");
- err_code = -2;
- break;
- }
- } while ((len_recv < len));
- /* priority to return data bytes if any data be received from TCP connection. */
- /* It will get error code on next calling */
- return (0 != len_recv) ? len_recv : err_code;
- }
|