From c1a8d33a0ad48c06d808f536f686da917e8a49ab Mon Sep 17 00:00:00 2001 From: hexian000 Date: Wed, 29 Jun 2022 20:00:24 +0800 Subject: [PATCH] multiple fixes --- d.sh | 8 -------- src/CMakeLists.txt | 10 ++++++++++ src/conf.c | 8 -------- src/conf.h | 1 - src/event_kcp.c | 4 ++++ src/event_tcp.c | 4 ++-- src/event_udp.c | 6 +++--- src/proxy.c | 11 +++++------ src/server.c | 8 +++----- src/sockutil.c | 39 +++++++++++++++++++-------------------- src/sockutil.h | 5 +++-- 11 files changed, 49 insertions(+), 55 deletions(-) delete mode 100755 d.sh diff --git a/d.sh b/d.sh deleted file mode 100755 index 3c1f85d..0000000 --- a/d.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -set -ex - -: r -c server.json -: r -c client.json - -gdb build/src/kcptun-libev diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4ce5e90..7f4a258 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,7 +5,12 @@ add_subdirectory(libbloom) add_subdirectory(murmur3) include(CheckSymbolExists) +if(CMAKE_SYSTEM_NAME STREQUAL "Linux") list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE") +else() +message(STATUS "POSIX System: ${CMAKE_SYSTEM_NAME}") +list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_POSIX_C_SOURCE=199309L") +endif() check_symbol_exists(sendmmsg "sys/socket.h" HAVE_SENDMMSG) check_symbol_exists(recvmmsg "sys/socket.h" HAVE_RECVMMSG) check_symbol_exists(clock_gettime "time.h" HAVE_CLOCK_GETTIME) @@ -26,6 +31,11 @@ add_executable(kcptun-libev main.c nonce.c nonce.h event.h event_impl.h event_tcp.c event_kcp.c event_udp.c event_timer.c) +if(CMAKE_SYSTEM_NAME STREQUAL "Linux") +target_compile_definitions(kcptun-libev PRIVATE _GNU_SOURCE) +else() +target_compile_definitions(kcptun-libev PRIVATE _POSIX_C_SOURCE=199309L) +endif() target_compile_definitions(kcptun-libev PRIVATE _GNU_SOURCE) target_compile_options(kcptun-libev PRIVATE -include "${CMAKE_CURRENT_BINARY_DIR}/config.h") diff --git a/src/conf.c b/src/conf.c index f798cb0..7218c1a 100644 --- a/src/conf.c +++ b/src/conf.c @@ -193,9 +193,6 @@ static bool tcp_scope_cb(struct config *conf, const json_object_entry *entry) if (strcmp(name, "nodelay") == 0) { return parse_bool_json(&conf->tcp_nodelay, value); } - if (strcmp(name, "lingertime") == 0) { - return parse_int_json(&conf->tcp_lingertime, value); - } if (strcmp(name, "sndbuf") == 0) { return parse_int_json(&conf->tcp_sndbuf, value); } @@ -419,12 +416,7 @@ static struct config conf_default() .tcp_reuseport = false, .tcp_keepalive = false, .tcp_nodelay = true, - .tcp_lingertime = 30, - .tcp_sndbuf = 65536, - .tcp_rcvbuf = 65536, .udp_reuseport = false, - .udp_sndbuf = 262144, - .udp_rcvbuf = 262144, .log_level = LOG_LEVEL_INFO, }; } diff --git a/src/conf.h b/src/conf.h index 7b00654..37c80df 100644 --- a/src/conf.h +++ b/src/conf.h @@ -30,7 +30,6 @@ struct config { /* socket options */ bool tcp_reuseport, tcp_keepalive, tcp_nodelay; - int tcp_lingertime; int tcp_sndbuf, tcp_rcvbuf; bool udp_reuseport; int udp_sndbuf, udp_rcvbuf; diff --git a/src/event_kcp.c b/src/event_kcp.c index 226e898..838b5e6 100644 --- a/src/event_kcp.c +++ b/src/event_kcp.c @@ -1,5 +1,6 @@ #include "event.h" #include "event_impl.h" +#include "packet.h" #include "server.h" #include "session.h" #include "util.h" @@ -160,6 +161,9 @@ static void kcp_update(struct session *restrict ss) return; } struct server *restrict s = ss->server; + if (s->udp.packets->mq_send_len == MQ_SEND_SIZE) { + return; + } const uint32_t now_ms = tstamp2ms(ev_now(s->loop)); if (!ss->kcp_checked || (int32_t)(now_ms - ss->kcp_next) >= 0) { ikcp_update(ss->kcp, now_ms); diff --git a/src/event_tcp.c b/src/event_tcp.c index f674688..9372381 100644 --- a/src/event_tcp.c +++ b/src/event_tcp.c @@ -55,7 +55,7 @@ void accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) LOGE_PERROR("accept"); return; } - if (socket_set_nonblock(client_fd)) { + if (socket_setup(client_fd)) { LOGE_PERROR("fcntl"); close(client_fd); return; @@ -65,7 +65,7 @@ void accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) struct config *restrict cfg = s->conf; socket_set_tcp( client_fd, cfg->tcp_nodelay, - cfg->tcp_lingertime, cfg->tcp_keepalive); + cfg->tcp_keepalive); socket_set_buffer( client_fd, cfg->tcp_sndbuf, cfg->tcp_rcvbuf); } diff --git a/src/event_udp.c b/src/event_udp.c index 25a83c9..240e335 100644 --- a/src/event_udp.c +++ b/src/event_udp.c @@ -81,7 +81,7 @@ static size_t udp_recv(struct server *restrict s) struct msgframe *restrict msg = msgframe_new(p, NULL); if (msg == NULL) { - break; + return 0; } const ssize_t nbrecv = recvmsg(s->udp.fd, &msg->hdr, MSG_DONTWAIT); if (nbrecv < 0) { @@ -89,10 +89,10 @@ static size_t udp_recv(struct server *restrict s) /* temporary errors */ if ((errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == EINTR)) { - break; + return 0; } LOGE_PERROR("recvmsg"); - break; + return 0; } s->udp.last_recv_time = ev_now(s->loop); diff --git a/src/proxy.c b/src/proxy.c index d9a5c81..f99005f 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -13,21 +13,20 @@ struct session * proxy_dial(struct server *restrict s, struct sockaddr *addr, const int32_t conv) { const struct sockaddr *sa = s->conf->connect.sa; - int fd; + int fd = socket( + sa->sa_family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); // Create socket - if ((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) { + if (fd < 0) { LOGE_PERROR("socket"); return NULL; } - if (socket_set_nonblock(fd)) { + if (socket_setup(fd)) { LOGE_PERROR("fcntl"); return NULL; } { struct config *restrict cfg = s->conf; - socket_set_tcp( - fd, cfg->tcp_nodelay, cfg->tcp_lingertime, - cfg->tcp_keepalive); + socket_set_tcp(fd, cfg->tcp_nodelay, cfg->tcp_keepalive); socket_set_buffer(fd, cfg->tcp_sndbuf, cfg->tcp_rcvbuf); } struct session *ss = session_new(s, fd, addr, conv); diff --git a/src/server.c b/src/server.c index 765a7ce..421c43b 100644 --- a/src/server.c +++ b/src/server.c @@ -29,16 +29,14 @@ listener_start(struct server *restrict s, const struct sockaddr *addr) LOGE_PERROR("socket error"); return false; } - if (socket_set_nonblock(l->fd)) { + if (socket_setup(l->fd)) { LOGE_PERROR("fcntl"); return false; } { struct config *restrict cfg = s->conf; socket_set_reuseport(l->fd, cfg->tcp_reuseport); - socket_set_tcp( - l->fd, cfg->tcp_nodelay, cfg->tcp_lingertime, - cfg->tcp_keepalive); + socket_set_tcp(l->fd, cfg->tcp_nodelay, cfg->tcp_keepalive); socket_set_buffer(l->fd, cfg->tcp_sndbuf, cfg->tcp_rcvbuf); } @@ -87,7 +85,7 @@ static bool udp_start(struct server *restrict s, struct config *restrict conf) LOGE_PERROR("udp socket"); return false; } - if (socket_set_nonblock(udp->fd)) { + if (socket_setup(udp->fd)) { LOGE_PERROR("fcntl"); return NULL; } diff --git a/src/sockutil.c b/src/sockutil.c index 63260de..47c05ef 100644 --- a/src/sockutil.c +++ b/src/sockutil.c @@ -18,10 +18,10 @@ #include #include -int socket_set_nonblock(int fd) +int socket_setup(int fd) { const int flags = fcntl(fd, F_GETFL, 0); - return fcntl(fd, F_SETFL, flags | O_NONBLOCK); + return fcntl(fd, F_SETFL, flags | O_CLOEXEC | O_NONBLOCK); } void socket_set_reuseport(const int fd, const int reuseport) @@ -37,36 +37,35 @@ void socket_set_reuseport(const int fd, const int reuseport) #endif } -void socket_set_tcp( - const int fd, const int nodelay, const int linger, const int keepalive) +void socket_set_tcp(const int fd, const bool nodelay, const bool keepalive) { if (setsockopt( - fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay))) { + fd, IPPROTO_TCP, TCP_NODELAY, &(int){ nodelay ? 1 : 0 }, + sizeof(int))) { LOGW_PERROR("TCP_NODELAY"); } if (setsockopt( - fd, SOL_SOCKET, SO_LINGER, - &(struct linger){ - .l_onoff = 0, - .l_linger = linger, - }, - sizeof(struct linger))) { - LOGW_PERROR("SO_LINGER"); - } - if (setsockopt( - fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive, - sizeof(keepalive))) { + fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ keepalive ? 1 : 0 }, + sizeof(int))) { LOGW_PERROR("SO_KEEPALIVE"); } } void socket_set_buffer(int fd, size_t send, size_t recv) { - if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &(int){ send }, sizeof(int))) { - LOGW_PERROR("SO_SNDBUF"); + if (send > 0) { + if (setsockopt( + fd, SOL_SOCKET, SO_SNDBUF, &(int){ send }, + sizeof(int))) { + LOGW_PERROR("SO_SNDBUF"); + } } - if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &(int){ recv }, sizeof(int))) { - LOGW_PERROR("SO_RCVBUF"); + if (recv > 0) { + if (setsockopt( + fd, SOL_SOCKET, SO_RCVBUF, &(int){ recv }, + sizeof(int))) { + LOGW_PERROR("SO_RCVBUF"); + } } } diff --git a/src/sockutil.h b/src/sockutil.h index f1718b2..65b8f5c 100644 --- a/src/sockutil.h +++ b/src/sockutil.h @@ -3,6 +3,7 @@ #include "hashtable.h" +#include #include #include @@ -10,9 +11,9 @@ typedef struct { uint32_t b[7]; } sockaddr_max_t; -int socket_set_nonblock(int fd); +int socket_setup(int fd); void socket_set_reuseport(int fd, int reuseport); -void socket_set_tcp(int fd, int nodelay, int linger, int keepalive); +void socket_set_tcp(int fd, bool nodelay, bool keepalive); void socket_set_buffer(int fd, size_t send, size_t recv); void conv_make_key(hashkey_t *key, const struct sockaddr *sa, uint32_t conv);