|
|
|
|
@@ -74,6 +74,7 @@ static inline enum tfe_conn_dir __bev_dir(struct tfe_stream_private * _stream, s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(0);
|
|
|
|
|
return CONN_DIR_DOWNSTREAM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool __is_ssl(struct tfe_stream_private * _stream)
|
|
|
|
|
@@ -334,16 +335,6 @@ static void __conn_private_destory_with_ssl(struct event_base * ev_base,
|
|
|
|
|
return __conn_private_destory(conn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void __stream_bev_downstream_statcb(struct evbuffer * buffer, const struct evbuffer_cb_info * info, void * arg)
|
|
|
|
|
{
|
|
|
|
|
struct tfe_stream_private * _stream = (struct tfe_stream_private *) arg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void __stream_bev_upstream_statcb(struct evbuffer * buffer, const struct evbuffer_cb_info * info, void * arg)
|
|
|
|
|
{
|
|
|
|
|
struct tfe_stream_private * _stream = (struct tfe_stream_private *) arg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void __stream_bev_passthrough_readcb(struct bufferevent * bev, void * arg)
|
|
|
|
|
{
|
|
|
|
|
struct tfe_stream_private * _stream = (struct tfe_stream_private *) arg;
|
|
|
|
|
@@ -580,7 +571,6 @@ static void __stream_bev_writecb(struct bufferevent * bev, void * arg)
|
|
|
|
|
struct tfe_conn_private ** ref_peer_conn{};
|
|
|
|
|
struct ssl_stream ** ref_this_ssl_stream{};
|
|
|
|
|
|
|
|
|
|
const char * __str_dir = NULL;
|
|
|
|
|
if (__bev_dir(_stream, bev) == CONN_DIR_UPSTREAM)
|
|
|
|
|
{
|
|
|
|
|
ref_this_conn = &_stream->conn_upstream;
|
|
|
|
|
@@ -631,7 +621,6 @@ static void __stream_bev_eventcb(struct bufferevent * bev, short events, void *
|
|
|
|
|
struct ssl_stream ** ref_peer_ssl_stream{};
|
|
|
|
|
struct tfe_stream_write_ctx ** ref_this_write_ctx{};
|
|
|
|
|
|
|
|
|
|
const char * __str_dir = NULL;
|
|
|
|
|
if (__bev_dir(_stream, bev) == CONN_DIR_UPSTREAM)
|
|
|
|
|
{
|
|
|
|
|
ref_this_conn = &_stream->conn_upstream;
|
|
|
|
|
@@ -713,7 +702,6 @@ static tfe_conn_private * __conn_private_create_by_fd(struct tfe_stream_private
|
|
|
|
|
__conn_private->bev = bufferevent_socket_new(__ev_base, fd, BEV_OPT_DEFER_CALLBACKS);
|
|
|
|
|
__conn_private->fd = fd;
|
|
|
|
|
|
|
|
|
|
struct bufferevent * __bev = __conn_private->bev;
|
|
|
|
|
if (!__conn_private->bev)
|
|
|
|
|
{
|
|
|
|
|
TFE_LOG_ERROR(__STREAM_LOGGER(stream), "Failed at creating bufferevent for fd %d", fd);
|
|
|
|
|
@@ -858,6 +846,11 @@ void tfe_stream_destory(struct tfe_stream_private * stream)
|
|
|
|
|
TFE_PROXY_STAT_INCREASE(STAT_STREAM_DESTROY, 1);
|
|
|
|
|
__stream_access_log_write(stream);
|
|
|
|
|
|
|
|
|
|
if (stream->head.addr)
|
|
|
|
|
{
|
|
|
|
|
free(stream->head.addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stream->str_stream_addr)
|
|
|
|
|
{
|
|
|
|
|
free(stream->str_stream_addr);
|
|
|
|
|
@@ -910,68 +903,6 @@ void tfe_stream_destory(struct tfe_stream_private * stream)
|
|
|
|
|
thread->load--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct tfe_stream_addr * __stream_addr_create_by_fds(struct tfe_stream * stream, evutil_socket_t fd_downstream)
|
|
|
|
|
{
|
|
|
|
|
struct tfe_stream_addr * __stream_addr = NULL;
|
|
|
|
|
|
|
|
|
|
struct sockaddr_storage sk_src_storage{};
|
|
|
|
|
struct sockaddr * sk_src_ptr = (struct sockaddr *) &sk_src_storage;
|
|
|
|
|
socklen_t sk_src_len = sizeof(sk_src_storage);
|
|
|
|
|
|
|
|
|
|
struct sockaddr_storage sk_dst_storage{};
|
|
|
|
|
struct sockaddr * sk_dst_ptr = (struct sockaddr *) &sk_dst_storage;
|
|
|
|
|
socklen_t sk_dst_len = sizeof(sk_dst_storage);
|
|
|
|
|
|
|
|
|
|
int ret = getsockname(fd_downstream, sk_src_ptr, &sk_src_len);
|
|
|
|
|
if (unlikely(ret < 0))
|
|
|
|
|
{
|
|
|
|
|
TFE_STREAM_LOG_ERROR(stream, "Failed at calling getsockaddr() for fd %d : %s", fd_downstream, strerror(errno));
|
|
|
|
|
goto __errout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = getpeername(fd_downstream, sk_dst_ptr, &sk_dst_len);
|
|
|
|
|
if (unlikely(ret < 0))
|
|
|
|
|
{
|
|
|
|
|
TFE_STREAM_LOG_ERROR(stream, "Failed at calling getpeername() for fd %d : %s", fd_downstream, strerror(errno));
|
|
|
|
|
goto __errout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(sk_src_ptr->sa_family == sk_dst_ptr->sa_family);
|
|
|
|
|
if (sk_src_ptr->sa_family == AF_INET)
|
|
|
|
|
{
|
|
|
|
|
__stream_addr = (struct tfe_stream_addr *) malloc(
|
|
|
|
|
sizeof(struct tfe_stream_addr) + sizeof(struct tfe_stream_addr_tuple4_v4));
|
|
|
|
|
|
|
|
|
|
struct tfe_stream_addr_ipv4 * st_addr_v4 = __stream_addr->ipv4;
|
|
|
|
|
struct sockaddr_in * sk_v4_src_ptr = (struct sockaddr_in *) sk_src_ptr;
|
|
|
|
|
struct sockaddr_in * sk_v4_dst_ptr = (struct sockaddr_in *) sk_dst_ptr;
|
|
|
|
|
|
|
|
|
|
__stream_addr->addrtype = TFE_ADDR_STREAM_TUPLE4_V4;
|
|
|
|
|
__stream_addr->addrlen = sizeof(struct tfe_stream_addr_tuple4_v4);
|
|
|
|
|
|
|
|
|
|
st_addr_v4->saddr.s_addr = sk_v4_src_ptr->sin_addr.s_addr;
|
|
|
|
|
st_addr_v4->source = sk_v4_src_ptr->sin_port;
|
|
|
|
|
st_addr_v4->daddr.s_addr = sk_v4_dst_ptr->sin_addr.s_addr;
|
|
|
|
|
st_addr_v4->dest = sk_v4_dst_ptr->sin_port;
|
|
|
|
|
}
|
|
|
|
|
else if (sk_src_ptr->sa_family == AF_INET6)
|
|
|
|
|
{
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
TFE_STREAM_LOG_ERROR(stream, "Invalid sockaddr family for fd %d: sa_family is %d.",
|
|
|
|
|
fd_downstream, sk_src_ptr->sa_family);
|
|
|
|
|
goto __errout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return __stream_addr;
|
|
|
|
|
|
|
|
|
|
__errout:
|
|
|
|
|
if (__stream_addr != NULL) free(__stream_addr);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int __fd_ttl_option_setup(struct tfe_stream_private * _stream, evutil_socket_t fd, int ttl)
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_storage sk_storage;
|
|
|
|
|
@@ -1023,8 +954,8 @@ void __stream_fd_option_setup(struct tfe_stream_private * _stream, evutil_socket
|
|
|
|
|
{
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const void *) &tcp_options->sz_rcv_buffer, sizeof(int)) == -1)
|
|
|
|
|
{
|
|
|
|
|
TFE_STREAM_LOG_ERROR(stream, "setsockopt(SO_RCVBUF, %d) failed, ignored: %s",
|
|
|
|
|
tcp_options->sz_rcv_buffer, strerror(errno));
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "%s: setsockopt(SO_RCVBUF, %d) failed, ignored: %s",
|
|
|
|
|
stream->str_stream_info, tcp_options->sz_rcv_buffer, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -1033,8 +964,8 @@ void __stream_fd_option_setup(struct tfe_stream_private * _stream, evutil_socket
|
|
|
|
|
{
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &tcp_options->sz_snd_buffer, sizeof(int)) == -1)
|
|
|
|
|
{
|
|
|
|
|
TFE_STREAM_LOG_ERROR(stream, "setsockopt(SO_SNDBUF, %d) failed, ignored: %s",
|
|
|
|
|
tcp_options->sz_snd_buffer, strerror(errno));
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "%s: setsockopt(SO_SNDBUF, %d) failed, ignored: %s",
|
|
|
|
|
stream->str_stream_info, tcp_options->sz_snd_buffer, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -1043,8 +974,8 @@ void __stream_fd_option_setup(struct tfe_stream_private * _stream, evutil_socket
|
|
|
|
|
{
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (const void *) &tcp_options->so_keepalive, sizeof(int)) == -1)
|
|
|
|
|
{
|
|
|
|
|
TFE_STREAM_LOG_ERROR(stream, "setsockopt(SO_KEEPALIVE, %d) failed, ignored: %s",
|
|
|
|
|
tcp_options->so_keepalive, strerror(errno));
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "%s: setsockopt(SO_KEEPALIVE, %d) failed, ignored: %s",
|
|
|
|
|
stream->str_stream_info, tcp_options->so_keepalive, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -1052,8 +983,8 @@ void __stream_fd_option_setup(struct tfe_stream_private * _stream, evutil_socket
|
|
|
|
|
{
|
|
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, (const void *) &tcp_options->tcp_keepcnt, sizeof(int)) == -1)
|
|
|
|
|
{
|
|
|
|
|
TFE_STREAM_LOG_ERROR(stream, "setsockopt(TCP_KEEPCNT, %d) failed, ignored: %s",
|
|
|
|
|
tcp_options->tcp_keepcnt, strerror(errno));
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "%s: setsockopt(TCP_KEEPCNT, %d) failed, ignored: %s",
|
|
|
|
|
stream->str_stream_info, tcp_options->tcp_keepcnt, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -1061,8 +992,8 @@ void __stream_fd_option_setup(struct tfe_stream_private * _stream, evutil_socket
|
|
|
|
|
{
|
|
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, (const void *) &tcp_options->tcp_keepintvl, sizeof(int)) == -1)
|
|
|
|
|
{
|
|
|
|
|
TFE_STREAM_LOG_ERROR(stream, "setsockopt(TCP_KEEPINTVL, %d) failed, ignored: %s",
|
|
|
|
|
tcp_options->tcp_keepintvl, strerror(errno));
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "%s: setsockopt(TCP_KEEPINTVL, %d) failed, ignored: %s",
|
|
|
|
|
stream->str_stream_info, tcp_options->tcp_keepintvl, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -1070,8 +1001,8 @@ void __stream_fd_option_setup(struct tfe_stream_private * _stream, evutil_socket
|
|
|
|
|
{
|
|
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, (const void *) &tcp_options->tcp_keepidle, sizeof(int)) == -1)
|
|
|
|
|
{
|
|
|
|
|
TFE_STREAM_LOG_ERROR(stream, "setsockopt(TCP_KEEPIDLE, %d) failed, ignored: %s",
|
|
|
|
|
tcp_options->tcp_keepidle, strerror(errno));
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "%s: setsockopt(TCP_KEEPIDLE, %d) failed, ignored: %s",
|
|
|
|
|
stream->str_stream_info, tcp_options->tcp_keepidle, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -1080,18 +1011,17 @@ void __stream_fd_option_setup(struct tfe_stream_private * _stream, evutil_socket
|
|
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, (const void *) &tcp_options->tcp_user_timeout, sizeof(int))
|
|
|
|
|
== -1)
|
|
|
|
|
{
|
|
|
|
|
TFE_STREAM_LOG_ERROR(stream, "setsockopt(TCP_USER_TIMEOUT, %d) failed, ignored: %s",
|
|
|
|
|
tcp_options->tcp_user_timeout, strerror(errno));
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "%s: setsockopt(TCP_USER_TIMEOUT, %d) failed, ignored: %s",
|
|
|
|
|
stream->str_stream_info, tcp_options->tcp_user_timeout, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int __ttl = (dir == CONN_DIR_UPSTREAM) ? tcp_options->tcp_ttl_upstream : tcp_options->tcp_ttl_downstream;
|
|
|
|
|
if (__ttl > 0 && __fd_ttl_option_setup(_stream, fd, __ttl) < 0)
|
|
|
|
|
{
|
|
|
|
|
TFE_STREAM_LOG_ERROR(stream, "Failed at setup FD's ttl option, ttl = %d, fd = %d", __ttl, fd);
|
|
|
|
|
TFE_LOG_ERROR(g_default_logger, "%s: Failed at setup FD's ttl option, ttl = %d, fd = %d",
|
|
|
|
|
stream->str_stream_info, __ttl, fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int tfe_stream_init_by_fds(struct tfe_stream * stream, evutil_socket_t fd_downstream, evutil_socket_t fd_upstream)
|
|
|
|
|
|