2017-11-14 03:16:20 +00:00
|
|
|
// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "lwip/sockets.h"
|
|
|
|
#include "lwip/dns.h"
|
|
|
|
#include "lwip/netdb.h"
|
|
|
|
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
|
2018-09-25 08:34:04 +00:00
|
|
|
#include "esp_transport_utils.h"
|
|
|
|
#include "esp_transport.h"
|
2017-11-14 03:16:20 +00:00
|
|
|
|
|
|
|
static const char *TAG = "TRANS_TCP";
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int sock;
|
|
|
|
} transport_tcp_t;
|
|
|
|
|
|
|
|
static int resolve_dns(const char *host, struct sockaddr_in *ip) {
|
|
|
|
|
2020-03-19 03:24:06 +00:00
|
|
|
const struct addrinfo hints = {
|
|
|
|
.ai_family = AF_INET,
|
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
};
|
|
|
|
struct addrinfo *res;
|
|
|
|
|
|
|
|
int err = getaddrinfo(host, NULL, &hints, &res);
|
|
|
|
if(err != 0 || res == NULL) {
|
|
|
|
ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res);
|
2017-11-14 03:16:20 +00:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
ip->sin_family = AF_INET;
|
2020-03-19 03:24:06 +00:00
|
|
|
memcpy(&ip->sin_addr, &((struct sockaddr_in *)(res->ai_addr))->sin_addr, sizeof(ip->sin_addr));
|
|
|
|
freeaddrinfo(res);
|
2017-11-14 03:16:20 +00:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
static int tcp_connect(esp_transport_handle_t t, const char *host, int port, int timeout_ms)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in remote_ip;
|
|
|
|
struct timeval tv;
|
2018-09-26 09:56:47 +00:00
|
|
|
transport_tcp_t *tcp = esp_transport_get_context_data(t);
|
2017-11-14 03:16:20 +00:00
|
|
|
|
|
|
|
bzero(&remote_ip, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
//if stream_host is not ip address, resolve it AF_INET,servername,&serveraddr.sin_addr
|
|
|
|
if (inet_pton(AF_INET, host, &remote_ip.sin_addr) != 1) {
|
|
|
|
if (resolve_dns(host, &remote_ip) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tcp->sock = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
if (tcp->sock < 0) {
|
|
|
|
ESP_LOGE(TAG, "Error create socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
remote_ip.sin_family = AF_INET;
|
|
|
|
remote_ip.sin_port = htons(port);
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_utils_ms_to_timeval(timeout_ms, &tv);
|
2017-11-14 03:16:20 +00:00
|
|
|
|
|
|
|
setsockopt(tcp->sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
2019-09-12 05:55:47 +00:00
|
|
|
setsockopt(tcp->sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
2017-11-14 03:16:20 +00:00
|
|
|
|
|
|
|
ESP_LOGD(TAG, "[sock=%d],connecting to server IP:%s,Port:%d...",
|
|
|
|
tcp->sock, ipaddr_ntoa((const ip_addr_t*)&remote_ip.sin_addr.s_addr), port);
|
|
|
|
if (connect(tcp->sock, (struct sockaddr *)(&remote_ip), sizeof(struct sockaddr)) != 0) {
|
|
|
|
close(tcp->sock);
|
|
|
|
tcp->sock = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return tcp->sock;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
static int tcp_write(esp_transport_handle_t t, const char *buffer, int len, int timeout_ms)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
int poll;
|
2018-09-26 09:56:47 +00:00
|
|
|
transport_tcp_t *tcp = esp_transport_get_context_data(t);
|
|
|
|
if ((poll = esp_transport_poll_write(t, timeout_ms)) <= 0) {
|
2017-11-14 03:16:20 +00:00
|
|
|
return poll;
|
|
|
|
}
|
|
|
|
return write(tcp->sock, buffer, len);
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
static int tcp_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ms)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
2018-09-26 09:56:47 +00:00
|
|
|
transport_tcp_t *tcp = esp_transport_get_context_data(t);
|
2017-11-14 03:16:20 +00:00
|
|
|
int poll = -1;
|
2018-09-26 09:56:47 +00:00
|
|
|
if ((poll = esp_transport_poll_read(t, timeout_ms)) <= 0) {
|
2017-11-14 03:16:20 +00:00
|
|
|
return poll;
|
|
|
|
}
|
|
|
|
int read_len = read(tcp->sock, buffer, len);
|
|
|
|
if (read_len == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return read_len;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
static int tcp_poll_read(esp_transport_handle_t t, int timeout_ms)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
2018-09-26 09:56:47 +00:00
|
|
|
transport_tcp_t *tcp = esp_transport_get_context_data(t);
|
2019-09-12 05:55:47 +00:00
|
|
|
int ret = -1;
|
2017-11-14 03:16:20 +00:00
|
|
|
fd_set readset;
|
2019-09-12 05:55:47 +00:00
|
|
|
fd_set errset;
|
2017-11-14 03:16:20 +00:00
|
|
|
FD_ZERO(&readset);
|
2019-09-12 05:55:47 +00:00
|
|
|
FD_ZERO(&errset);
|
2017-11-14 03:16:20 +00:00
|
|
|
FD_SET(tcp->sock, &readset);
|
2019-09-12 05:55:47 +00:00
|
|
|
FD_SET(tcp->sock, &errset);
|
2017-11-14 03:16:20 +00:00
|
|
|
struct timeval timeout;
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_utils_ms_to_timeval(timeout_ms, &timeout);
|
2019-09-12 05:55:47 +00:00
|
|
|
ret = select(tcp->sock + 1, &readset, NULL, &errset, &timeout);
|
|
|
|
if (ret > 0 && FD_ISSET(tcp->sock, &errset)) {
|
|
|
|
int sock_errno = 0;
|
|
|
|
uint32_t optlen = sizeof(sock_errno);
|
|
|
|
getsockopt(tcp->sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
|
|
|
|
ESP_LOGE(TAG, "tcp_poll_read select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), tcp->sock);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
return ret;
|
2017-11-14 03:16:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
static int tcp_poll_write(esp_transport_handle_t t, int timeout_ms)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
2018-09-26 09:56:47 +00:00
|
|
|
transport_tcp_t *tcp = esp_transport_get_context_data(t);
|
2019-09-12 05:55:47 +00:00
|
|
|
int ret = -1;
|
2017-11-14 03:16:20 +00:00
|
|
|
fd_set writeset;
|
2019-09-12 05:55:47 +00:00
|
|
|
fd_set errset;
|
2017-11-14 03:16:20 +00:00
|
|
|
FD_ZERO(&writeset);
|
2019-09-12 05:55:47 +00:00
|
|
|
FD_ZERO(&errset);
|
2017-11-14 03:16:20 +00:00
|
|
|
FD_SET(tcp->sock, &writeset);
|
2019-09-12 05:55:47 +00:00
|
|
|
FD_SET(tcp->sock, &errset);
|
2017-11-14 03:16:20 +00:00
|
|
|
struct timeval timeout;
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_utils_ms_to_timeval(timeout_ms, &timeout);
|
2019-09-12 05:55:47 +00:00
|
|
|
ret = select(tcp->sock + 1, NULL, &writeset, &errset, &timeout);
|
|
|
|
if (ret > 0 && FD_ISSET(tcp->sock, &errset)) {
|
|
|
|
int sock_errno = 0;
|
|
|
|
uint32_t optlen = sizeof(sock_errno);
|
|
|
|
getsockopt(tcp->sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
|
|
|
|
ESP_LOGE(TAG, "tcp_poll_write select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), tcp->sock);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
return ret;
|
2017-11-14 03:16:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
static int tcp_close(esp_transport_handle_t t)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
2018-09-26 09:56:47 +00:00
|
|
|
transport_tcp_t *tcp = esp_transport_get_context_data(t);
|
2017-11-14 03:16:20 +00:00
|
|
|
int ret = -1;
|
|
|
|
if (tcp->sock >= 0) {
|
|
|
|
ret = close(tcp->sock);
|
|
|
|
tcp->sock = -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
static esp_err_t tcp_destroy(esp_transport_handle_t t)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
2018-09-26 09:56:47 +00:00
|
|
|
transport_tcp_t *tcp = esp_transport_get_context_data(t);
|
|
|
|
esp_transport_close(t);
|
2017-11-14 03:16:20 +00:00
|
|
|
free(tcp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_handle_t esp_transport_tcp_init()
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_handle_t t = esp_transport_init();
|
2017-11-14 03:16:20 +00:00
|
|
|
transport_tcp_t *tcp = calloc(1, sizeof(transport_tcp_t));
|
2018-09-26 09:56:47 +00:00
|
|
|
ESP_TRANSPORT_MEM_CHECK(TAG, tcp, return NULL);
|
2017-11-14 03:16:20 +00:00
|
|
|
tcp->sock = -1;
|
2018-10-02 13:19:46 +00:00
|
|
|
esp_transport_set_func(t, tcp_connect, tcp_read, tcp_write, tcp_close, tcp_poll_read, tcp_poll_write, tcp_destroy);
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_set_context_data(t, tcp);
|
2017-11-14 03:16:20 +00:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|