Skip to content

Commit

Permalink
multiple fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hexian000 committed Jun 29, 2022
1 parent f401653 commit c1a8d33
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 55 deletions.
8 changes: 0 additions & 8 deletions d.sh

This file was deleted.

10 changes: 10 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")

Expand Down
8 changes: 0 additions & 8 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
};
}
Expand Down
1 change: 0 additions & 1 deletion src/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/event_kcp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "event.h"
#include "event_impl.h"
#include "packet.h"
#include "server.h"
#include "session.h"
#include "util.h"
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/event_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/event_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ 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) {
msgframe_delete(p, msg);
/* 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);

Expand Down
11 changes: 5 additions & 6 deletions src/proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 3 additions & 5 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down
39 changes: 19 additions & 20 deletions src/sockutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
#include <string.h>
#include <inttypes.h>

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)
Expand All @@ -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");
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/sockutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@

#include "hashtable.h"

#include <stdbool.h>
#include <stdint.h>
#include <sys/socket.h>

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);
Expand Down

0 comments on commit c1a8d33

Please sign in to comment.