summaryrefslogtreecommitdiff
path: root/minecproxy
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-28 14:58:32 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-28 14:58:32 +0200
commitd3352b997ca59a336a40fe6660c6e5b7079864aa (patch)
treef365d6c0304e512057223e9a3cf37357da44a443 /minecproxy
parent99b2c70137fef05a5a18f439b9010ddba455f5cb (diff)
Run clang-format on source tree (excl igmp.c)
Diffstat (limited to 'minecproxy')
-rw-r--r--minecproxy/announce.c31
-rw-r--r--minecproxy/idle.c42
-rw-r--r--minecproxy/igmp.c115
-rw-r--r--minecproxy/main.c105
-rw-r--r--minecproxy/main.h2
-rw-r--r--minecproxy/misc.c124
-rw-r--r--minecproxy/misc.h7
-rw-r--r--minecproxy/ptimer.c59
-rw-r--r--minecproxy/ptimer.h5
-rw-r--r--minecproxy/server-config.c158
-rw-r--r--minecproxy/server-proxy.c185
-rw-r--r--minecproxy/server-proxy.h2
-rw-r--r--minecproxy/server-rcon.c64
-rw-r--r--minecproxy/server.c231
-rw-r--r--minecproxy/server.h5
-rw-r--r--minecproxy/signal-handler.c31
-rw-r--r--minecproxy/systemd.c133
-rw-r--r--minecproxy/uring.c290
-rw-r--r--minecproxy/uring.h15
19 files changed, 759 insertions, 845 deletions
diff --git a/minecproxy/announce.c b/minecproxy/announce.c
index 13ef423..78a7ccd 100644
--- a/minecproxy/announce.c
+++ b/minecproxy/announce.c
@@ -18,8 +18,7 @@ struct announce {
int mcast_fd;
};
-static void
-announce_cb(struct ptimer_task *ptask)
+static void announce_cb(struct ptimer_task *ptask)
{
struct announce *announce = container_of(ptask, struct announce, ptask);
struct server *server;
@@ -32,28 +31,25 @@ announce_cb(struct ptimer_task *ptask)
server_announce(server, announce->mcast_fd);
}
-static void
-announce_free(struct uring_task *task)
+static void announce_free(struct uring_task *task)
{
struct announce *announce = container_of(task, struct announce, task);
assert_return(task);
- debug(DBG_ANN, "task %p, announce 0x%p, mcast_fd: %i",
- task, announce, announce->mcast_fd);
+ debug(DBG_ANN, "task %p, announce 0x%p, mcast_fd: %i", task, announce,
+ announce->mcast_fd);
close(announce->mcast_fd);
xfree(announce);
}
-void
-announce_refdump()
+void announce_refdump()
{
assert_return_silent(cfg->announce);
uring_task_refdump(&cfg->announce->task);
}
-void
-announce_delete()
+void announce_delete()
{
assert_return_silent(cfg->announce);
@@ -63,8 +59,7 @@ announce_delete()
cfg->announce = NULL;
}
-void
-announce_stop()
+void announce_stop()
{
struct announce *announce = cfg->announce;
@@ -73,8 +68,7 @@ announce_stop()
ptimer_del_task(&announce->ptask);
}
-void
-announce_start(unsigned duration)
+void announce_start(unsigned duration)
{
struct announce *announce = cfg->announce;
unsigned times;
@@ -91,12 +85,11 @@ announce_start(unsigned duration)
ptimer_add_task(&announce->ptask);
}
-void
-announce_init()
+void announce_init()
{
struct announce *announce;
int sfd;
-
+
assert_return(!cfg->announce);
assert_return_silent(cfg->announce_interval > 0);
@@ -108,9 +101,9 @@ announce_init()
if (sfd < 0)
die("socket: %m");
- uring_task_init(&announce->task, "announce", uring_parent(), announce_free);
+ uring_task_init(&announce->task, "announce", uring_parent(),
+ announce_free);
ptask_init(&announce->ptask, cfg->announce_interval, 0, announce_cb);
announce->mcast_fd = sfd;
cfg->announce = announce;
}
-
diff --git a/minecproxy/idle.c b/minecproxy/idle.c
index 70d8099..ec9e2da 100644
--- a/minecproxy/idle.c
+++ b/minecproxy/idle.c
@@ -18,8 +18,8 @@ struct idle {
struct uring_task task;
};
-static int
-idle_check_handshake_complete(struct uring_task *task, _unused_ int res)
+static int idle_check_handshake_complete(struct uring_task *task,
+ _unused_ int res)
{
int r;
@@ -31,8 +31,7 @@ idle_check_handshake_complete(struct uring_task *task, _unused_ int res)
return r;
}
-static void
-idle_check_handshake_reply(struct uring_task *task, int res)
+static void idle_check_handshake_reply(struct uring_task *task, int res)
{
struct server *server = container_of(task, struct server, idle_task);
unsigned online, max;
@@ -46,8 +45,7 @@ idle_check_handshake_reply(struct uring_task *task, int res)
goto out;
if (mc_protocol_parse_status_reply(server->idle_buf.buf,
- server->idle_buf.len,
- &online, &max))
+ server->idle_buf.len, &online, &max))
player_count = online;
else
error("failed to parse reply");
@@ -58,8 +56,7 @@ out:
return;
}
-static void
-idle_check_handshake_sent(struct uring_task *task, int res)
+static void idle_check_handshake_sent(struct uring_task *task, int res)
{
assert_return(task);
assert_task_alive(DBG_IDLE, task);
@@ -70,20 +67,17 @@ idle_check_handshake_sent(struct uring_task *task, int res)
return;
}
- uring_tbuf_read_until(task,
- idle_check_handshake_complete,
+ uring_tbuf_read_until(task, idle_check_handshake_complete,
idle_check_handshake_reply);
}
-void
-idle_check_get_player_count(struct server *server, struct connection *conn)
+void idle_check_get_player_count(struct server *server, struct connection *conn)
{
assert_return(server && conn && server->idle_task.priv);
- if (!mc_protocol_create_status_request(server->idle_buf.buf,
- sizeof(server->idle_buf.buf),
- &server->idle_buf.len,
- &conn->remote)) {
+ if (!mc_protocol_create_status_request(
+ server->idle_buf.buf, sizeof(server->idle_buf.buf),
+ &server->idle_buf.len, &conn->remote)) {
error("failed to create mc request");
/* FIXME: is this enough? */
return;
@@ -94,8 +88,7 @@ idle_check_get_player_count(struct server *server, struct connection *conn)
uring_tbuf_write(&server->idle_task, idle_check_handshake_sent);
}
-static void
-idle_cb(struct ptimer_task *ptask)
+static void idle_cb(struct ptimer_task *ptask)
{
struct idle *idle = container_of(ptask, struct idle, ptask);
struct server *server;
@@ -109,8 +102,7 @@ idle_cb(struct ptimer_task *ptask)
server_idle_check(server);
}
-static void
-idle_free(struct uring_task *task)
+static void idle_free(struct uring_task *task)
{
struct idle *idle = container_of(task, struct idle, task);
@@ -119,16 +111,14 @@ idle_free(struct uring_task *task)
xfree(idle);
}
-void
-idle_refdump()
+void idle_refdump()
{
assert_return_silent(cfg->idle);
uring_task_refdump(&cfg->idle->task);
}
-void
-idle_delete()
+void idle_delete()
{
assert_return(cfg->idle);
@@ -138,8 +128,7 @@ idle_delete()
cfg->idle = NULL;
}
-void
-idle_init()
+void idle_init()
{
struct idle *idle;
@@ -154,4 +143,3 @@ idle_init()
ptimer_add_task(&idle->ptask);
cfg->idle = idle;
}
-
diff --git a/minecproxy/igmp.c b/minecproxy/igmp.c
index f1d380d..f3579f4 100644
--- a/minecproxy/igmp.c
+++ b/minecproxy/igmp.c
@@ -345,7 +345,8 @@ igmp_parse(struct igmp *igmp)
body_len -= sizeof(*record);
pos += sizeof(*record);
- if (body_len < record->nsrcs * sizeof(uint32_t) + record->auxlen) {
+ if (body_len <
+ (record->nsrcs * sizeof(uint32_t) + record->auxlen)) {
error("IGMPv3 too short");
break;
}
@@ -364,10 +365,12 @@ igmp_parse(struct igmp *igmp)
}
/* Yes, EXCL, not INCL, see RFC3376 */
+ /* clang-format off */
if ((htonl(record->addr) == cinet_addr(224,0,2,60)) &&
((record->type == IGMP_V3_REC_MODE_IS_EXCL) ||
(record->type == IGMP_V3_REC_MODE_CH_EXCL)))
igmp_match();
+ /* clang-format on */
body_len -= record->auxlen;
pos += record->auxlen;
@@ -409,7 +412,7 @@ igmp_read_cb(struct uring_task *task, int res)
task->tbuf->len = res;
- if (task->saddr.storage.ss_family == AF_PACKET ||
+ if (task->saddr.st.ss_family == AF_PACKET ||
task->saddr.ll.sll_protocol == htons(ETH_P_IP))
igmp_parse(igmp);
else
@@ -450,46 +453,74 @@ void
igmp_init()
{
static const struct sock_filter filter[] = {
- BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */
- BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(struct iphdr), 1, 0), /* A < sizeof(iphdr) */
- BPF_STMT(BPF_RET + BPF_K, 0), /* drop packet */
-
- BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 0 /* iphdr[0] */), /* A <- version + ihl */
- BPF_STMT(BPF_ALU + BPF_RSH + BPF_K, 4), /* A <- A >> 4 (version) */
- BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0x04, 1, 0), /* A != 4 */
- BPF_STMT(BPF_RET + BPF_K, 0), /* drop packet */
-
- BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(struct iphdr, protocol)), /* A <- ip protocol */
- BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_IGMP, 1, 0), /* A != IPPROTO_IGMP */
- BPF_STMT(BPF_RET + BPF_K, 0), /* drop packet */
-
- BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct iphdr, daddr)), /* A <- ip dst addr */
- BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, chtobe32(cinet_addr(224,0,2,60)), 2, 0), /* A != 224.0.2.60 */
- //BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0xe000023c, 2, 0), /* A != 224.0.2.60 */
- BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, chtobe32(cinet_addr(224,0,0,22)), 1, 0), /* A != 224.0.0.22 */
- //BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0xe0000016, 1, 0), /* A != 224.0.0.22 */
- BPF_STMT(BPF_RET + BPF_K, 0), /* drop packet */
-
- BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 0 /* iphdr[0] */), /* X <- pkt->ihl * 4 */
- BPF_STMT(BPF_LD + BPF_IMM, 20), /* A <- 20 */
- BPF_JUMP(BPF_JMP + BPF_JGT + BPF_X, 0, 0, 1), /* A > X */
- BPF_STMT(BPF_RET + BPF_K, 0), /* drop packet */
-
- BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct iphdr, tot_len)), /* A <- ip tot_len */
- BPF_JUMP(BPF_JMP + BPF_JGT + BPF_X, 0, 1, 0), /* A <= ip->ihl * 4 */
- BPF_STMT(BPF_RET + BPF_K, 0), /* drop packet */
- BPF_STMT(BPF_ALU + BPF_SUB + BPF_X, 0), /* A <- A - X (bodylen) */
- BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, 8, 1, 0), /* A < 8 */
- BPF_STMT(BPF_RET + BPF_K, 0), /* drop packet */
-
- BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct iphdr, tot_len)), /* A <- ip->tot_len */
- BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
- BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */
- BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_X, 0, 1, 0), /* A != ip->tot_len */
- BPF_STMT(BPF_RET + BPF_K, 0), /* drop packet */
-
- BPF_STMT(BPF_RET + BPF_K, (uint32_t) -1), /* accept packet */
- };
+ /* A <- packet length */
+ BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0),
+ /* A < sizeof(iphdr) */
+ BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(struct iphdr), 1, 0),
+ /* drop packet */
+ BPF_STMT(BPF_RET + BPF_K, 0),
+
+ /* A <- version + ihl */
+ BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 0 /* iphdr[0] */),
+ /* A <- A >> 4 (version) */
+ BPF_STMT(BPF_ALU + BPF_RSH + BPF_K, 4),
+ /* A != 4 */
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0x04, 1, 0),
+ /* drop packet */
+ BPF_STMT(BPF_RET + BPF_K, 0),
+
+ /* A <- ip protocol */
+ BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(struct iphdr, protocol)),
+ /* A != IPPROTO_IGMP */
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_IGMP, 1, 0),
+ /* drop packet */
+ BPF_STMT(BPF_RET + BPF_K, 0),
+
+ /* A <- ip dst addr */
+ BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct iphdr, daddr)),
+ /* A != 224.0.2.60 */
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, chtobe32(cinet_addr(224,0,2,60)), 2, 0),
+ /* A != 224.0.0.22 */
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, chtobe32(cinet_addr(224,0,0,22)), 1, 0),
+ /* drop packet */
+ BPF_STMT(BPF_RET + BPF_K, 0),
+
+ /* X <- pkt->ihl * 4 */
+ BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 0 /* iphdr[0] */),
+ /* A <- 20 */
+ BPF_STMT(BPF_LD + BPF_IMM, 20),
+ /* A > X */
+ BPF_JUMP(BPF_JMP + BPF_JGT + BPF_X, 0, 0, 1),
+ /* drop packet */
+ BPF_STMT(BPF_RET + BPF_K, 0),
+
+ /* A <- ip tot_len */
+ BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct iphdr, tot_len)),
+ /* A <= ip->ihl * 4 */
+ BPF_JUMP(BPF_JMP + BPF_JGT + BPF_X, 0, 1, 0),
+ /* drop packet */
+ BPF_STMT(BPF_RET + BPF_K, 0),
+ /* A <- A - X (bodylen) */
+ BPF_STMT(BPF_ALU + BPF_SUB + BPF_X, 0),
+ /* A < 8 */
+ BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, 8, 1, 0),
+ /* drop packet */
+ BPF_STMT(BPF_RET + BPF_K, 0),
+
+ /* A <- ip->tot_len */
+ BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct iphdr, tot_len)),
+ /* X <- A */
+ BPF_STMT(BPF_MISC + BPF_TAX, 0),
+ /* A <- packet length */
+ BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0),
+ /* A != ip->tot_len */
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_X, 0, 1, 0),
+ /* drop packet */
+ BPF_STMT(BPF_RET + BPF_K, 0),
+
+ /* accept packet */
+ BPF_STMT(BPF_RET + BPF_K, (uint32_t) -1),
+ };
static const struct sock_fprog fprog = {
.len = ARRAY_SIZE(filter),
.filter = (struct sock_filter*) filter,
diff --git a/minecproxy/main.c b/minecproxy/main.c
index f5e6c88..b5a195b 100644
--- a/minecproxy/main.c
+++ b/minecproxy/main.c
@@ -38,8 +38,7 @@ static bool daemonize = false;
static FILE *log_file = NULL;
static const char *log_file_path = NULL;
-static void
-msg(enum debug_lvl lvl, const char *fmt, va_list ap)
+static void msg(enum debug_lvl lvl, const char *fmt, va_list ap)
{
static bool first = true;
static bool use_colors = false;
@@ -136,8 +135,7 @@ msg(enum debug_lvl lvl, const char *fmt, va_list ap)
fprintf(stderr, ANSI_NORMAL);
}
-void
-__debug(enum debug_lvl lvl, const char *fmt, ...)
+void __debug(enum debug_lvl lvl, const char *fmt, ...)
{
va_list ap;
@@ -148,8 +146,7 @@ __debug(enum debug_lvl lvl, const char *fmt, ...)
va_end(ap);
}
-_noreturn_ void
-__die(const char *fmt, ...)
+_noreturn_ void __die(const char *fmt, ...)
{
va_list ap;
@@ -164,8 +161,7 @@ __die(const char *fmt, ...)
exit(EXIT_FAILURE);
}
-static void
-cfg_free(struct uring_task *task)
+static void cfg_free(struct uring_task *task)
{
struct cfg *xcfg = container_of(task, struct cfg, task);
@@ -198,51 +194,60 @@ struct cfg_key_value_map mcfg_key_map[] = {
.key_name = "igmp",
.key_value = MCFG_KEY_IGMP,
.value_type = CFG_VAL_TYPE_BOOL,
- }, {
+ },
+ {
.key_name = "igmp_iface",
.key_value = MCFG_KEY_IGMP_IFACE,
.value_type = CFG_VAL_TYPE_STRING,
- }, {
+ },
+ {
.key_name = "announce_interval",
.key_value = MCFG_KEY_ANN_INTERVAL,
.value_type = CFG_VAL_TYPE_UINT16,
- }, {
+ },
+ {
.key_name = "proxy_connection_interval",
.key_value = MCFG_KEY_PROXY_CONN_INTERVAL,
.value_type = CFG_VAL_TYPE_UINT16,
- }, {
+ },
+ {
.key_name = "proxy_connection_attempts",
.key_value = MCFG_KEY_PROXY_CONN_ATTEMPTS,
.value_type = CFG_VAL_TYPE_UINT16,
- }, {
+ },
+ {
.key_name = "socket_defer",
.key_value = MCFG_KEY_SOCKET_DEFER,
.value_type = CFG_VAL_TYPE_BOOL,
- }, {
+ },
+ {
.key_name = "socket_freebind",
.key_value = MCFG_KEY_SOCKET_FREEBIND,
.value_type = CFG_VAL_TYPE_BOOL,
- }, {
+ },
+ {
.key_name = "socket_keepalive",
.key_value = MCFG_KEY_SOCKET_KEEPALIVE,
.value_type = CFG_VAL_TYPE_BOOL,
- }, {
+ },
+ {
.key_name = "socket_iptos",
.key_value = MCFG_KEY_SOCKET_IPTOS,
.value_type = CFG_VAL_TYPE_BOOL,
- }, {
+ },
+ {
.key_name = "socket_nodelay",
.key_value = MCFG_KEY_SOCKET_NODELAY,
.value_type = CFG_VAL_TYPE_BOOL,
- }, {
+ },
+ {
.key_name = NULL,
.key_value = MCFG_KEY_INVALID,
.value_type = CFG_VAL_TYPE_INVALID,
}
};
-static void
-cfg_read()
+static void cfg_read()
{
FILE *cfgfile;
const char *path;
@@ -297,8 +302,8 @@ cfg_read()
const char *keyname;
struct cfg_value value;
- if (!config_parse_line(path, &pos, mcfg_key_map,
- &key, &keyname, &value, false))
+ if (!config_parse_line(path, &pos, mcfg_key_map, &key, &keyname,
+ &value, false))
break;
if (key == MCFG_KEY_INVALID)
@@ -307,7 +312,6 @@ cfg_read()
debug(DBG_CFG, "main cfg: key %s", keyname);
switch (key) {
-
case MCFG_KEY_IGMP:
cfg->do_igmp = value.boolean;
break;
@@ -358,6 +362,7 @@ cfg_read()
}
}
+/* clang-format off */
const struct {
const char *name;
unsigned val;
@@ -409,9 +414,9 @@ const struct {
.val = 0,
}
};
+/* clang-format on */
-_noreturn_ static void
-usage(bool invalid)
+_noreturn_ static void usage(bool invalid)
{
if (invalid)
info("Invalid option(s)");
@@ -434,8 +439,7 @@ usage(bool invalid)
exit(invalid ? EXIT_FAILURE : EXIT_SUCCESS);
}
-static void
-cfg_init(int argc, char **argv)
+static void cfg_init(int argc, char **argv)
{
int c;
unsigned i;
@@ -463,6 +467,7 @@ cfg_init(int argc, char **argv)
while (true) {
int option_index = 0;
+ /* clang-format off */
static struct option long_options[] = {
{ "cfgdir", required_argument, 0, 'c' },
{ "cfgfile", required_argument, 0, 'C' },
@@ -474,9 +479,10 @@ cfg_init(int argc, char **argv)
{ "debug", required_argument, 0, 'd' },
{ 0, 0, 0, 0 }
};
+ /* clang-format on */
- c = getopt_long(argc, argv, ":c:C:u:Dl:hvd:",
- long_options, &option_index);
+ c = getopt_long(argc, argv, ":c:C:u:Dl:hvd:", long_options,
+ &option_index);
if (c == -1)
break;
@@ -512,12 +518,12 @@ cfg_init(int argc, char **argv)
if (errno == ESRCH)
die("failed to find user %s", optarg);
else
- die("failed to find user %s (%m)", optarg);
+ die("failed to find user %s (%m)",
+ optarg);
}
debug(DBG_CFG, "asked to execute with uid %ji gid %ji",
- (intmax_t)pwd->pw_uid,
- (intmax_t)pwd->pw_gid);
+ (intmax_t)pwd->pw_uid, (intmax_t)pwd->pw_gid);
cfg->uid = pwd->pw_uid;
cfg->gid = pwd->pw_gid;
break;
@@ -531,12 +537,14 @@ cfg_init(int argc, char **argv)
info("Debug categories:");
info(" * all");
for (i = 0; debug_category_str[i].name; i++)
- info(" * %s", debug_category_str[i].name);
+ info(" * %s",
+ debug_category_str[i].name);
exit(EXIT_FAILURE);
}
for (i = 0; debug_category_str[i].name; i++) {
- if (strcaseeq(optarg, debug_category_str[i].name))
+ if (strcaseeq(optarg,
+ debug_category_str[i].name))
break;
}
@@ -553,29 +561,25 @@ cfg_init(int argc, char **argv)
default:
usage(true);
}
-
}
if (optind < argc)
usage(true);
}
-static void
-cfg_apply()
+static void cfg_apply()
{
if (cfg->uid == 0 || cfg->gid == 0)
/* This catches both -u root and running as root without -u */
die("Execution as root is not supported (use -u <someuser>)");
capng_clear(CAPNG_SELECT_BOTH);
- if (capng_updatev(CAPNG_ADD,
- CAPNG_EFFECTIVE | CAPNG_PERMITTED,
+ if (capng_updatev(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
CAP_NET_RAW, CAP_NET_BIND_SERVICE, -1))
die("capng_updatev failed");
if (geteuid() != cfg->uid) {
- if (capng_change_id(cfg->uid,
- cfg->gid,
+ if (capng_change_id(cfg->uid, cfg->gid,
CAPNG_DROP_SUPP_GRP | CAPNG_CLEAR_BOUNDING))
die("capng_change_id failed");
} else {
@@ -614,8 +618,7 @@ cfg_apply()
}
}
-void
-dump_tree()
+void dump_tree()
{
struct server *server;
@@ -639,8 +642,7 @@ dump_tree()
info("\n\n");
}
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
{
struct server *server;
unsigned server_count;
@@ -679,8 +681,7 @@ main(int argc, char **argv)
/* Drop CAP_NET_RAW (if we have it), only used for igmp */
capng_clear(CAPNG_SELECT_BOTH);
- if (capng_update(CAPNG_ADD,
- CAPNG_EFFECTIVE | CAPNG_PERMITTED,
+ if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
CAP_NET_BIND_SERVICE))
die("capng_update failed");
@@ -708,14 +709,14 @@ main(int argc, char **argv)
list_for_each_entry(server, &cfg->servers, list)
server_count++;
- sd_notifyf(0, "READY=1\n"
+ sd_notifyf(0,
+ "READY=1\n"
"STATUS=Running, %u server configurations loaded\n"
"MAINPID=%lu",
- server_count,
- (unsigned long)getpid());
+ server_count, (unsigned long)getpid());
- info("mcproxy (%s) started, %u server configurations loaded",
- VERSION, server_count);
+ info("mcproxy (%s) started, %u server configurations loaded", VERSION,
+ server_count);
uring_event_loop();
diff --git a/minecproxy/main.h b/minecproxy/main.h
index 8c8add0..c46169a 100644
--- a/minecproxy/main.h
+++ b/minecproxy/main.h
@@ -62,7 +62,7 @@ struct uring_task {
utask_cb_t cb;
/* returns: 0 = not complete; < 0 = error; > 0 = complete */
- rutask_cb_t is_complete_cb;
+ rutask_cb_t is_complete_cb;
/* called once tbuf processing is done */
utask_cb_t final_cb;
diff --git a/minecproxy/misc.c b/minecproxy/misc.c
index 152de1a..e3872c5 100644
--- a/minecproxy/misc.c
+++ b/minecproxy/misc.c
@@ -31,12 +31,14 @@ struct allocation {
struct list_head list;
};
-static void
-add_allocation(const char *allocfn, const char *callerfn, int line, void *ptr, size_t size)
+static void add_allocation(const char *allocfn, const char *callerfn, int line,
+ void *ptr, size_t size)
{
struct allocation *a;
- assert_die(!empty_str(allocfn) && !empty_str(callerfn) && line > 0 && ptr && size > 0, "invalid arguments");
+ assert_die(!empty_str(allocfn) && !empty_str(callerfn) && line > 0 &&
+ ptr && size > 0,
+ "invalid arguments");
a = malloc(sizeof(*a));
if (!a)
@@ -49,12 +51,11 @@ add_allocation(const char *allocfn, const char *callerfn, int line, void *ptr, s
list_add(&a->list, &malloc_list);
total_malloc_count++;
malloc_count++;
- debug(DBG_MALLOC, "called from %s:%i - %s(%zu) = %p (%p)",
- callerfn, line, allocfn, size, ptr, a);
+ debug(DBG_MALLOC, "called from %s:%i - %s(%zu) = %p (%p)", callerfn,
+ line, allocfn, size, ptr, a);
}
-void *
-__zmalloc(const char *fn, int line, size_t size)
+void *__zmalloc(const char *fn, int line, size_t size)
{
void *ptr;
@@ -66,12 +67,12 @@ __zmalloc(const char *fn, int line, size_t size)
return ptr;
}
-char *
-__xstrdup(const char *fn, int line, const char *s)
+char *__xstrdup(const char *fn, int line, const char *s)
{
char *ptr;
- assert_die(!empty_str(fn) && line > 0 && !empty_str(s), "invalid arguments");
+ assert_die(!empty_str(fn) && line > 0 && !empty_str(s),
+ "invalid arguments");
ptr = strdup(s);
if (ptr)
@@ -79,12 +80,12 @@ __xstrdup(const char *fn, int line, const char *s)
return ptr;
}
-char *
-__xstrndup(const char *fn, int line, const char *s, size_t n)
+char *__xstrndup(const char *fn, int line, const char *s, size_t n)
{
char *ptr;
- assert_die(!empty_str(fn) && line > 0 && !empty_str(s) && n > 0, "invalid arguments");
+ assert_die(!empty_str(fn) && line > 0 && !empty_str(s) && n > 0,
+ "invalid arguments");
ptr = strndup(s, n);
if (ptr)
@@ -92,8 +93,7 @@ __xstrndup(const char *fn, int line, const char *s, size_t n)
return ptr;
}
-void
-__xfree(const char *fn, int line, void *ptr)
+void __xfree(const char *fn, int line, void *ptr)
{
struct allocation *a, *tmp;
unsigned delete_count = 0;
@@ -121,8 +121,7 @@ __xfree(const char *fn, int line, void *ptr)
}
}
-void
-debug_resource_usage()
+void debug_resource_usage()
{
struct allocation *a;
DIR *dir;
@@ -131,11 +130,12 @@ debug_resource_usage()
ssize_t r;
unsigned file_count = 0;
- debug(DBG_MALLOC, "Still malloced %i (total %u)",
- malloc_count, total_malloc_count);
+ debug(DBG_MALLOC, "Still malloced %i (total %u)", malloc_count,
+ total_malloc_count);
list_for_each_entry(a, &malloc_list, list) {
- debug(DBG_MALLOC, "* Lost allocation - %s:%i - ptr: %p, size: %zu",
+ debug(DBG_MALLOC,
+ "* Lost allocation - %s:%i - ptr: %p, size: %zu",
a->callerfn, a->line, a->ptr, a->size);
}
@@ -152,7 +152,8 @@ debug_resource_usage()
r = readlinkat(dirfd(dir), dent->d_name, buf, sizeof(buf));
if (r < 0) {
- debug(DBG_MALLOC, "Failed to readlink %s", dent->d_name);
+ debug(DBG_MALLOC, "Failed to readlink %s",
+ dent->d_name);
continue;
}
buf[r] = '\0';
@@ -168,21 +169,19 @@ debug_resource_usage()
cqe_count, sqe_count);
}
-void
-connection_set_local(struct connection *conn, int fd)
+void connection_set_local(struct connection *conn, int fd)
{
assert_return(conn && fd >= 0);
- conn->local.addrlen = sizeof(conn->local.storage);
- if (getsockname(fd, (struct sockaddr *)&conn->local.storage,
+ conn->local.addrlen = sizeof(conn->local.st);
+ if (getsockname(fd, (struct sockaddr *)&conn->local.st,
&conn->local.addrlen) < 0)
sprintf(conn->local.addrstr, "<unknown>");
else
saddr_set_addrstr(&conn->local);
}
-void
-connection_set_remote(struct connection *conn, struct saddr *remote)
+void connection_set_remote(struct connection *conn, struct saddr *remote)
{
assert_return(conn && remote);
@@ -192,8 +191,7 @@ connection_set_remote(struct connection *conn, struct saddr *remote)
static void connect_next(struct uring_task *task, struct connection *conn);
-static void
-connect_cb(struct uring_task *task, int res)
+static void connect_cb(struct uring_task *task, int res)
{
struct connection *conn;
@@ -201,8 +199,8 @@ connect_cb(struct uring_task *task, int res)
conn = task->priv;
if (res < 0) {
- debug(DBG_SRV, "%s: connection to %s failed",
- task->name, conn->remote.addrstr);
+ debug(DBG_SRV, "%s: connection to %s failed", task->name,
+ conn->remote.addrstr);
uring_task_close_fd(task);
connect_next(task, conn);
return;
@@ -210,66 +208,63 @@ connect_cb(struct uring_task *task, int res)
connection_set_local(conn, task->fd);
- debug(DBG_SRV, "%s: connection established %s -> %s",
- task->name, conn->local.addrstr, conn->remote.addrstr);
+ debug(DBG_SRV, "%s: connection established %s -> %s", task->name,
+ conn->local.addrstr, conn->remote.addrstr);
conn->cb(conn, true);
}
-static void
-connect_next(struct uring_task *task, struct connection *conn)
+static void connect_next(struct uring_task *task, struct connection *conn)
{
- struct saddr *remote, *tmp;
- int sfd;
- unsigned i;
+ struct saddr *remote, *tmp;
+ int sfd;
+ unsigned i;
assert_return(task && conn && conn->cb);
again:
assert_task_alive_or(DBG_UR, task, goto out);
i = 0;
- remote = NULL;
- list_for_each_entry(tmp, conn->addrs, list) {
- if (i == conn->next_addr) {
- remote = tmp;
- break;
- }
- i++;
- }
-
- if (!remote) {
+ remote = NULL;
+ list_for_each_entry(tmp, conn->addrs, list) {
+ if (i == conn->next_addr) {
+ remote = tmp;
+ break;
+ }
+ i++;
+ }
+
+ if (!remote) {
debug(DBG_SRV, "%s: no more remote addresses to attempt",
task->name);
goto out;
- }
+ }
conn->next_addr++;
connection_set_remote(conn, remote);
- debug(DBG_SRV, "%s: attempting to connect to %s",
- task->name, conn->remote.addrstr);
+ debug(DBG_SRV, "%s: attempting to connect to %s", task->name,
+ conn->remote.addrstr);
- sfd = socket(conn->remote.storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
- if (sfd < 0) {
- debug(DBG_SRV, "socket: %m");
- goto again;
- }
+ sfd = socket(conn->remote.st.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ if (sfd < 0) {
+ debug(DBG_SRV, "socket: %m");
+ goto again;
+ }
- socket_set_low_latency(sfd, cfg->socket_keepalive,
- cfg->socket_iptos, cfg->socket_nodelay);
+ socket_set_low_latency(sfd, cfg->socket_keepalive, cfg->socket_iptos,
+ cfg->socket_nodelay);
task->priv = conn;
- uring_task_set_fd(task, sfd);
- uring_connect(task, &conn->remote, connect_cb);
+ uring_task_set_fd(task, sfd);
+ uring_connect(task, &conn->remote, connect_cb);
return;
out:
conn->cb(conn, false);
}
-void
-connect_any(struct uring_task *task,
- struct list_head *addrs, struct connection *conn,
- connection_cb_t cb)
+void connect_any(struct uring_task *task, struct list_head *addrs,
+ struct connection *conn, connection_cb_t cb)
{
assert_return(task && addrs && conn && cb);
@@ -278,4 +273,3 @@ connect_any(struct uring_task *task,
conn->cb = cb;
connect_next(task, conn);
}
-
diff --git a/minecproxy/misc.h b/minecproxy/misc.h
index 6627913..70b84c7 100644
--- a/minecproxy/misc.h
+++ b/minecproxy/misc.h
@@ -13,7 +13,7 @@ void debug_resource_usage();
struct connection;
-typedef void(*connection_cb_t)(struct connection *, bool);
+typedef void (*connection_cb_t)(struct connection *, bool);
struct connection {
struct saddr remote;
@@ -29,8 +29,7 @@ void connection_set_local(struct connection *conn, int fd);
void connection_set_remote(struct connection *conn, struct saddr *remote);
-void connect_any(struct uring_task *task,
- struct list_head *addrs, struct connection *conn,
- connection_cb_t cb);
+void connect_any(struct uring_task *task, struct list_head *addrs,
+ struct connection *conn, connection_cb_t cb);
#endif
diff --git a/minecproxy/ptimer.c b/minecproxy/ptimer.c
index 845fbfa..6ed405d 100644
--- a/minecproxy/ptimer.c
+++ b/minecproxy/ptimer.c
@@ -16,18 +16,11 @@ struct ptimer {
struct list_head ptasks;
};
-static void
-ptimer_set(unsigned value, unsigned interval)
+static void ptimer_set(unsigned value, unsigned interval)
{
struct itimerspec tspec = {
- .it_interval = {
- .tv_sec = interval,
- .tv_nsec = 0
- },
- .it_value = {
- .tv_sec = value,
- .tv_nsec = 0
- }
+ .it_interval = { .tv_sec = interval, .tv_nsec = 0 },
+ .it_value = { .tv_sec = value, .tv_nsec = 0 }
};
assert_return(cfg->ptimer && cfg->ptimer->task.fd >= 0);
@@ -36,16 +29,14 @@ ptimer_set(unsigned value, unsigned interval)
error("timerfd_settime: %m");
}
-static unsigned
-gcd(unsigned a, unsigned b)
+static unsigned gcd(unsigned a, unsigned b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
-static unsigned
-array_gcd(unsigned arr[], unsigned n)
+static unsigned array_gcd(unsigned arr[], unsigned n)
{
unsigned result = arr[0];
@@ -55,8 +46,7 @@ array_gcd(unsigned arr[], unsigned n)
return result;
}
-static void
-ptimer_tick(struct ptimer *ptimer)
+static void ptimer_tick(struct ptimer *ptimer)
{
time_t now = time(NULL);
unsigned diff = (unsigned)(now - ptimer->previous_time);
@@ -69,8 +59,8 @@ ptimer_tick(struct ptimer *ptimer)
continue;
}
- debug(DBG_TIMER, "triggering ptask %p (times %u)",
- ptask, ptask->times);
+ debug(DBG_TIMER, "triggering ptask %p (times %u)", ptask,
+ ptask->times);
ptask->cb(ptask);
ptask->remain = ptask->interval;
@@ -88,8 +78,7 @@ ptimer_tick(struct ptimer *ptimer)
ptimer->previous_time = now;
}
-static void
-ptimer_reconfig(struct ptimer *ptimer)
+static void ptimer_reconfig(struct ptimer *ptimer)
{
struct ptimer_task *ptask;
unsigned i = 0;
@@ -116,8 +105,7 @@ ptimer_reconfig(struct ptimer *ptimer)
ptimer_set(lowest, interval);
}
-void
-ptimer_del_task(struct ptimer_task *ptask)
+void ptimer_del_task(struct ptimer_task *ptask)
{
struct ptimer *ptimer = cfg->ptimer;
@@ -133,8 +121,7 @@ ptimer_del_task(struct ptimer_task *ptask)
uring_task_put(&ptimer->task);
}
-void
-ptimer_add_task(struct ptimer_task *ptask)
+void ptimer_add_task(struct ptimer_task *ptask)
{
struct ptimer *ptimer = cfg->ptimer;
@@ -150,16 +137,14 @@ ptimer_add_task(struct ptimer_task *ptask)
ptimer_reconfig(ptimer);
}
-void
-ptimer_refdump()
+void ptimer_refdump()
{
assert_return(cfg->ptimer);
uring_task_refdump(&cfg->ptimer->task);
}
-static void
-ptimer_free(struct uring_task *task)
+static void ptimer_free(struct uring_task *task)
{
struct ptimer *ptimer = container_of(task, struct ptimer, task);
@@ -170,8 +155,7 @@ ptimer_free(struct uring_task *task)
cfg->ptimer = NULL;
}
-void
-ptimer_delete()
+void ptimer_delete()
{
assert_return(cfg->ptimer);
@@ -179,8 +163,7 @@ ptimer_delete()
uring_task_destroy(&cfg->ptimer->task);
}
-static void
-ptimer_cb(struct uring_task *task, int res)
+static void ptimer_cb(struct uring_task *task, int res)
{
struct ptimer *ptimer = container_of(task, struct ptimer, task);
@@ -193,15 +176,15 @@ ptimer_cb(struct uring_task *task, int res)
}
ptimer_tick(ptimer);
- uring_read(&ptimer->task, &ptimer->value, sizeof(ptimer->value), ptimer_cb);
+ uring_read(&ptimer->task, &ptimer->value, sizeof(ptimer->value),
+ ptimer_cb);
}
-void
-ptimer_init()
+void ptimer_init()
{
struct ptimer *ptimer;
int tfd;
-
+
assert_return(!cfg->ptimer);
ptimer = zmalloc(sizeof(*ptimer));
@@ -218,6 +201,6 @@ ptimer_init()
uring_task_init(&ptimer->task, "ptimer", uring_parent(), ptimer_free);
uring_task_set_fd(&ptimer->task, tfd);
cfg->ptimer = ptimer;
- uring_read(&ptimer->task, &ptimer->value, sizeof(ptimer->value), ptimer_cb);
+ uring_read(&ptimer->task, &ptimer->value, sizeof(ptimer->value),
+ ptimer_cb);
}
-
diff --git a/minecproxy/ptimer.h b/minecproxy/ptimer.h
index 0b53590..3481ba4 100644
--- a/minecproxy/ptimer.h
+++ b/minecproxy/ptimer.h
@@ -10,9 +10,8 @@ struct ptimer_task {
struct list_head list;
};
-static inline void
-ptask_init(struct ptimer_task *ptask, unsigned interval,
- unsigned times, void(*cb)(struct ptimer_task *))
+static inline void ptask_init(struct ptimer_task *ptask, unsigned interval,
+ unsigned times, void (*cb)(struct ptimer_task *))
{
ptask->interval = interval;
ptask->times = times;
diff --git a/minecproxy/server-config.c b/minecproxy/server-config.c
index 92990d3..71463ee 100644
--- a/minecproxy/server-config.c
+++ b/minecproxy/server-config.c
@@ -16,8 +16,8 @@
#include "server-config.h"
#include "server-config-options.h"
-static void
-scfg_dns_cb(struct dns_async *dns, bool (*server_cb)(struct server *, struct saddr *))
+static void scfg_dns_cb(struct dns_async *dns,
+ bool (*server_cb)(struct server *, struct saddr *))
{
struct server *server;
struct sockaddr_in *in4;
@@ -29,8 +29,9 @@ scfg_dns_cb(struct dns_async *dns, bool (*server_cb)(struct server *, struct sad
assert_return(dns && dns->priv && server_cb);
server = dns->priv;
- debug(DBG_DNS, "called, dns: %p, name: %s, server: %p, server->name: %s",
- dns, dns->name, server, server->name);
+ debug(DBG_DNS,
+ "called, dns: %p, name: %s, server: %p, server->name: %s", dns,
+ dns->name, server, server->name);
r = gai_error(&dns->gcb);
if (r == EAI_INPROGRESS) {
@@ -41,8 +42,8 @@ scfg_dns_cb(struct dns_async *dns, bool (*server_cb)(struct server *, struct sad
/* The server must be in the process of going away */
goto out;
} else if (r < 0) {
- error("DNS lookup of %s:%s failed: %s",
- dns->name, dns->port, gai_strerror(r));
+ error("DNS lookup of %s:%s failed: %s", dns->name, dns->port,
+ gai_strerror(r));
goto out;
}
@@ -51,14 +52,16 @@ scfg_dns_cb(struct dns_async *dns, bool (*server_cb)(struct server *, struct sad
for (ai = results; ai; ai = ai->ai_next) {
saddr = zmalloc(sizeof(*saddr));
if (!saddr) {
- error("DNS lookup of %s:%s failed: %m", dns->name, dns->port);
+ error("DNS lookup of %s:%s failed: %m", dns->name,
+ dns->port);
goto out;
}
switch (ai->ai_family) {
case AF_INET:
in4 = (struct sockaddr_in *)ai->ai_addr;
- saddr_set_ipv4(saddr, in4->sin_addr.s_addr, in4->sin_port);
+ saddr_set_ipv4(saddr, in4->sin_addr.s_addr,
+ in4->sin_port);
server_cb(server, saddr);
break;
@@ -84,34 +87,30 @@ out:
server_commit(server);
}
-static void
-scfg_local_dns_cb(struct dns_async *dns)
+static void scfg_local_dns_cb(struct dns_async *dns)
{
assert_return(dns);
scfg_dns_cb(dns, server_add_local);
}
-static void
-scfg_remote_dns_cb(struct dns_async *dns)
+static void scfg_remote_dns_cb(struct dns_async *dns)
{
assert_return(dns);
scfg_dns_cb(dns, server_add_remote);
}
-static void
-scfg_rcon_dns_cb(struct dns_async *dns)
+static void scfg_rcon_dns_cb(struct dns_async *dns)
{
assert_return(dns);
scfg_dns_cb(dns, server_add_rcon);
}
-static bool
-handle_dns(struct server *server, const char *type,
- struct cfg_value *value, dns_cb_t *async_cb,
- bool (*sync_cb)(struct server *, struct saddr *))
+static bool handle_dns(struct server *server, const char *type,
+ struct cfg_value *value, dns_cb_t *async_cb,
+ bool (*sync_cb)(struct server *, struct saddr *))
{
struct saddr *saddr, *tmp;
struct dns_async *dns;
@@ -129,8 +128,8 @@ handle_dns(struct server *server, const char *type,
return true;
case CFG_VAL_TYPE_ASYNC_ADDRS:
- debug(DBG_DNS, "%s: doing async lookup of DNS record: %p",
- type, value->dns_async);
+ debug(DBG_DNS, "%s: doing async lookup of DNS record: %p", type,
+ value->dns_async);
dns = value->dns_async;
dns->cb = async_cb;
@@ -144,8 +143,7 @@ handle_dns(struct server *server, const char *type,
}
}
-static void
-scfg_parse(struct server *server)
+static void scfg_parse(struct server *server)
{
char *pos;
@@ -163,8 +161,8 @@ scfg_parse(struct server *server)
const char *keyname;
struct cfg_value value;
- if (!config_parse_line(server->name, &pos, scfg_key_map,
- &key, &keyname, &value, true))
+ if (!config_parse_line(server->name, &pos, scfg_key_map, &key,
+ &keyname, &value, true))
break;
if (key == SCFG_KEY_INVALID)
@@ -173,13 +171,13 @@ scfg_parse(struct server *server)
debug(DBG_CFG, "%s: key %s", server->name, keyname);
switch (key) {
-
case SCFG_KEY_TYPE:
if (streq(value.str, "proxy")) {
if (!server_set_type(server, SERVER_TYPE_PROXY))
return;
} else if (streq(value.str, "announce")) {
- if (!server_set_type(server, SERVER_TYPE_ANNOUNCE))
+ if (!server_set_type(server,
+ SERVER_TYPE_ANNOUNCE))
return;
}
break;
@@ -213,23 +211,29 @@ scfg_parse(struct server *server)
case SCFG_KEY_STOP_METHOD:
if (streq(value.str, "exec")) {
- if (server_set_stop_method(server, SERVER_STOP_METHOD_EXEC))
+ if (server_set_stop_method(
+ server, SERVER_STOP_METHOD_EXEC))
break;
} else if (streq(value.str, "rcon")) {
- if (server_set_stop_method(server, SERVER_STOP_METHOD_RCON))
+ if (server_set_stop_method(
+ server, SERVER_STOP_METHOD_RCON))
break;
} else if (streq(value.str, "systemd")) {
- if (server_set_stop_method(server, SERVER_STOP_METHOD_SYSTEMD))
+ if (server_set_stop_method(
+ server, SERVER_STOP_METHOD_SYSTEMD))
break;
}
return;
case SCFG_KEY_START_METHOD:
if (streq(value.str, "exec")) {
- if (server_set_start_method(server, SERVER_START_METHOD_EXEC))
+ if (server_set_start_method(
+ server, SERVER_START_METHOD_EXEC))
break;
} else if (streq(value.str, "systemd")) {
- if (server_set_start_method(server, SERVER_START_METHOD_SYSTEMD))
+ if (server_set_start_method(
+ server,
+ SERVER_START_METHOD_SYSTEMD))
break;
}
return;
@@ -267,8 +271,7 @@ scfg_parse(struct server *server)
}
}
-static void
-scfg_read_cb(struct uring_task *task, int res)
+static void scfg_read_cb(struct uring_task *task, int res)
{
struct server *server = container_of(task, struct server, task);
@@ -276,8 +279,8 @@ scfg_read_cb(struct uring_task *task, int res)
assert_task_alive(DBG_CFG, task);
if (res <= 0) {
- error("error reading config file for %s: %s",
- server->name, strerror(-res));
+ error("error reading config file for %s: %s", server->name,
+ strerror(-res));
server_delete(server);
}
@@ -287,8 +290,7 @@ scfg_read_cb(struct uring_task *task, int res)
server_commit(server);
}
-static void
-scfg_open_cb(struct uring_task *task, int res)
+static void scfg_open_cb(struct uring_task *task, int res)
{
struct server *server = container_of(task, struct server, task);
@@ -311,12 +313,10 @@ struct server_cfg_monitor {
char buf[4096] _alignas_(struct inotify_event);
};
-static void
-scfgm_free(struct uring_task *task)
+static void scfgm_free(struct uring_task *task)
{
- struct server_cfg_monitor *scfgm = container_of(task,
- struct server_cfg_monitor,
- task);
+ struct server_cfg_monitor *scfgm =
+ container_of(task, struct server_cfg_monitor, task);
assert_return(task);
@@ -325,8 +325,7 @@ scfgm_free(struct uring_task *task)
cfg->server_cfg_monitor = NULL;
}
-static void
-inotify_event_dump(const struct inotify_event *event)
+static void inotify_event_dump(const struct inotify_event *event)
{
assert_return(event);
@@ -338,42 +337,40 @@ inotify_event_dump(const struct inotify_event *event)
debug(DBG_CFG, " * Mask : %" PRIu32, event->mask);
if (event->mask & IN_ACCESS)
debug(DBG_CFG, "\tIN_ACCESS");
- else if(event->mask & IN_MODIFY)
+ else if (event->mask & IN_MODIFY)
debug(DBG_CFG, "\tIN_MODIFY");
- else if(event->mask & IN_ATTRIB)
+ else if (event->mask & IN_ATTRIB)
debug(DBG_CFG, "\tIN_ATTRIB");
- else if(event->mask & IN_CLOSE_WRITE)
+ else if (event->mask & IN_CLOSE_WRITE)
debug(DBG_CFG, "\tIN_CLOSE_WRITE");
- else if(event->mask & IN_CLOSE_NOWRITE)
+ else if (event->mask & IN_CLOSE_NOWRITE)
debug(DBG_CFG, "\tIN_CLOSE_NOWRITE");
- else if(event->mask & IN_OPEN)
+ else if (event->mask & IN_OPEN)
debug(DBG_CFG, "\tIN_OPEN");
- else if(event->mask & IN_MOVED_FROM)
+ else if (event->mask & IN_MOVED_FROM)
debug(DBG_CFG, "\tIN_MOVED_FROM");
- else if(event->mask & IN_MOVED_TO)
+ else if (event->mask & IN_MOVED_TO)
debug(DBG_CFG, "\tIN_MOVED_TO");
- else if(event->mask & IN_CREATE)
+ else if (event->mask & IN_CREATE)
debug(DBG_CFG, "\tIN_CREATE");
- else if(event->mask & IN_DELETE)
+ else if (event->mask & IN_DELETE)
debug(DBG_CFG, "\tIN_DELETE");
- else if(event->mask & IN_DELETE_SELF)
+ else if (event->mask & IN_DELETE_SELF)
debug(DBG_CFG, "\tIN_DELETE_SELF");
- else if(event->mask & IN_MOVE_SELF)
+ else if (event->mask & IN_MOVE_SELF)
debug(DBG_CFG, "\tIN_MOVE_SELF");
- else if(event->mask & IN_UNMOUNT)
+ else if (event->mask & IN_UNMOUNT)
debug(DBG_CFG, "\tIN_UNMOUNT");
- else if(event->mask & IN_Q_OVERFLOW)
+ else if (event->mask & IN_Q_OVERFLOW)
debug(DBG_CFG, "\tIN_Q_OVERFLOW");
- else if(event->mask & IN_IGNORED)
+ else if (event->mask & IN_IGNORED)
debug(DBG_CFG, "\tIN_IGNORED");
}
-static void
-inotify_cb(struct uring_task *task, int res)
+static void inotify_cb(struct uring_task *task, int res)
{
- struct server_cfg_monitor *scfgm = container_of(task,
- struct server_cfg_monitor,
- task);
+ struct server_cfg_monitor *scfgm =
+ container_of(task, struct server_cfg_monitor, task);
const struct inotify_event *event;
char *ptr;
struct server *server;
@@ -386,13 +383,15 @@ inotify_cb(struct uring_task *task, int res)
return;
}
- for (ptr = scfgm->buf; ptr < scfgm->buf + res; ptr += sizeof(struct inotify_event) + event->len) {
+ for (ptr = scfgm->buf; ptr < scfgm->buf + res;
+ ptr += sizeof(struct inotify_event) + event->len) {
event = (const struct inotify_event *)ptr;
if (debug_enabled(DBG_CFG))
inotify_event_dump(event);
- if (event->mask & (IN_IGNORED | IN_MOVE_SELF | IN_DELETE_SELF | IN_UNMOUNT))
+ if (event->mask &
+ (IN_IGNORED | IN_MOVE_SELF | IN_DELETE_SELF | IN_UNMOUNT))
die("configuration directory gone, exiting");
if (event->mask & IN_Q_OVERFLOW) {
@@ -405,9 +404,11 @@ inotify_cb(struct uring_task *task, int res)
if (event->mask & (IN_MOVED_FROM | IN_DELETE))
server_delete_by_name(event->name);
- else if (event->mask & (IN_MOVED_TO | IN_CREATE | IN_CLOSE_WRITE)) {
+ else if (event->mask &
+ (IN_MOVED_TO | IN_CREATE | IN_CLOSE_WRITE)) {
server = server_new(event->name);
- verbose("New server config file detected: %s", server->name);
+ verbose("New server config file detected: %s",
+ server->name);
uring_openat(&server->task, server->name, scfg_open_cb);
} else
error("inotify: unknown event: 0x%08x", event->mask);
@@ -416,16 +417,14 @@ inotify_cb(struct uring_task *task, int res)
uring_read(&scfgm->task, scfgm->buf, sizeof(scfgm->buf), inotify_cb);
}
-void
-server_cfg_monitor_refdump()
+void server_cfg_monitor_refdump()
{
assert_return_silent(cfg->server_cfg_monitor);
uring_task_refdump(&cfg->server_cfg_monitor->task);
}
-void
-server_cfg_monitor_delete()
+void server_cfg_monitor_delete()
{
assert_return(cfg->server_cfg_monitor);
@@ -434,8 +433,7 @@ server_cfg_monitor_delete()
cfg->server_cfg_monitor = NULL;
}
-void
-server_cfg_monitor_init()
+void server_cfg_monitor_init()
{
int ifd;
int iwd;
@@ -455,15 +453,16 @@ server_cfg_monitor_init()
die("inotify_init1: %m");
/* ln = IN_CREATE, cp/vi/mv = IN_CREATE, IN_OPEN, IN_CLOSE_WRITE */
- iwd = inotify_add_watch(ifd, ".",
- IN_CLOSE_WRITE | IN_DELETE | IN_CREATE |
- IN_DELETE_SELF | IN_MOVE_SELF | IN_MOVED_TO |
- IN_MOVED_FROM | IN_DONT_FOLLOW |
- IN_EXCL_UNLINK | IN_ONLYDIR );
+ iwd = inotify_add_watch(
+ ifd, ".",
+ IN_CLOSE_WRITE | IN_DELETE | IN_CREATE | IN_DELETE_SELF |
+ IN_MOVE_SELF | IN_MOVED_TO | IN_MOVED_FROM |
+ IN_DONT_FOLLOW | IN_EXCL_UNLINK | IN_ONLYDIR);
if (iwd < 0)
die("inotify_add_watch: %m");
- uring_task_init(&scfgm->task, "server-config-monitor", uring_parent(), scfgm_free);
+ uring_task_init(&scfgm->task, "server-config-monitor", uring_parent(),
+ scfgm_free);
uring_task_set_fd(&scfgm->task, ifd);
cfg->server_cfg_monitor = scfgm;
uring_read(&scfgm->task, scfgm->buf, sizeof(scfgm->buf), inotify_cb);
@@ -483,4 +482,3 @@ server_cfg_monitor_init()
closedir(dir);
}
-
diff --git a/minecproxy/server-proxy.c b/minecproxy/server-proxy.c
index 087773d..b80a220 100644
--- a/minecproxy/server-proxy.c
+++ b/minecproxy/server-proxy.c
@@ -13,8 +13,7 @@
#include "server.h"
#include "server-proxy.h"
-static void
-format_bytes(char *buf, size_t len, uint64_t val)
+static void format_bytes(char *buf, size_t len, uint64_t val)
{
uint64_t tmp;
const char *suffix = "B";
@@ -24,11 +23,11 @@ format_bytes(char *buf, size_t len, uint64_t val)
tmp = val * 10;
if (val > 1152921504606846976ULL) {
tmp = val / 115292150460684697ULL;
- suffix= "EiB";
+ suffix = "EiB";
} else if (val > 1125899906842624ULL) {
tmp /= 1125899906842624ULL;
suffix = "PiB";
- } else if (val > 1099511627776ULL) {
+ } else if (val > 1099511627776ULL) {
tmp /= 1099511627776ULL;
suffix = "TiB";
} else if (val > 1073741824ULL) {
@@ -45,8 +44,7 @@ format_bytes(char *buf, size_t len, uint64_t val)
snprintf(buf, len, "%lu.%lu %s", tmp / 10, tmp % 10, suffix);
}
-static void
-format_time(char *buf, size_t len, time_t diff)
+static void format_time(char *buf, size_t len, time_t diff)
{
unsigned hh, mm, ss;
@@ -61,62 +59,60 @@ format_time(char *buf, size_t len, time_t diff)
snprintf(buf, len, "%02u:%02u:%02u", hh, mm, ss);
}
-static void
-proxy_free(struct uring_task *task)
+static void proxy_free(struct uring_task *task)
{
- struct server_proxy *proxy = container_of(task, struct server_proxy, task);
+ struct server_proxy *proxy =
+ container_of(task, struct server_proxy, task);
char cts[100];
char stc[100];
char duration[100];
assert_return(task);
- debug(DBG_PROXY, "server: %s, src: %s, dst: %s",
- proxy->server->name,
+ debug(DBG_PROXY, "server: %s, src: %s, dst: %s", proxy->server->name,
proxy->client_conn.remote.addrstr,
proxy->server_conn.remote.addrstr);
if (proxy->begin > 0) {
- format_time(duration, sizeof(duration), time(NULL) - proxy->begin);
+ format_time(duration, sizeof(duration),
+ time(NULL) - proxy->begin);
format_bytes(cts, sizeof(cts), proxy->client_bytes);
format_bytes(stc, sizeof(stc), proxy->server_bytes);
info("%s: proxy connection %s -> %s closed "
"(CtS: %s, StC: %s), duration %s",
- proxy->server->name,
- proxy->client_conn.remote.addrstr,
- proxy->server_conn.remote.addrstr,
- cts, stc, duration);
+ proxy->server->name, proxy->client_conn.remote.addrstr,
+ proxy->server_conn.remote.addrstr, cts, stc, duration);
}
list_del(&proxy->list);
xfree(proxy);
}
-static void
-proxy_client_free(struct uring_task *task)
+static void proxy_client_free(struct uring_task *task)
{
- struct server_proxy *proxy = container_of(task, struct server_proxy, clienttask);
+ struct server_proxy *proxy =
+ container_of(task, struct server_proxy, clienttask);
assert_return(task);
debug(DBG_PROXY, "%s: client connection closed", proxy->server->name);
}
-static void
-proxy_server_free(struct uring_task *task)
+static void proxy_server_free(struct uring_task *task)
{
- struct server_proxy *proxy = container_of(task, struct server_proxy, servertask);
+ struct server_proxy *proxy =
+ container_of(task, struct server_proxy, servertask);
assert_return(task);
debug(DBG_PROXY, "%s: server connection closed", proxy->server->name);
}
-void
-proxy_delete(struct server_proxy *proxy)
+void proxy_delete(struct server_proxy *proxy)
{
- debug(DBG_PROXY, "%s: shutting down proxy %p", proxy->server->name, proxy);
+ debug(DBG_PROXY, "%s: shutting down proxy %p", proxy->server->name,
+ proxy);
assert_return(proxy);
@@ -141,10 +137,10 @@ proxy_delete(struct server_proxy *proxy)
*/
static void proxy_client_read(struct uring_task *task, int res);
-static void
-proxy_client_written(struct uring_task *task, int res)
+static void proxy_client_written(struct uring_task *task, int res)
{
- struct server_proxy *proxy = container_of(task, struct server_proxy, clienttask);
+ struct server_proxy *proxy =
+ container_of(task, struct server_proxy, clienttask);
assert_return(task);
assert_task_alive(DBG_PROXY, task);
@@ -160,10 +156,10 @@ proxy_client_written(struct uring_task *task, int res)
uring_tbuf_read(task, proxy_client_read);
}
-static void
-proxy_client_read(struct uring_task *task, int res)
+static void proxy_client_read(struct uring_task *task, int res)
{
- struct server_proxy *proxy = container_of(task, struct server_proxy, clienttask);
+ struct server_proxy *proxy =
+ container_of(task, struct server_proxy, clienttask);
assert_return(task);
assert_task_alive(DBG_PROXY, task);
@@ -180,10 +176,10 @@ proxy_client_read(struct uring_task *task, int res)
static void proxy_server_read(struct uring_task *task, int res);
-static void
-proxy_server_written(struct uring_task *task, int res)
+static void proxy_server_written(struct uring_task *task, int res)
{
- struct server_proxy *proxy = container_of(task, struct server_proxy, servertask);
+ struct server_proxy *proxy =
+ container_of(task, struct server_proxy, servertask);
assert_return(task);
assert_task_alive(DBG_PROXY, task);
@@ -199,10 +195,10 @@ proxy_server_written(struct uring_task *task, int res)
uring_tbuf_read(&proxy->servertask, proxy_server_read);
}
-static void
-proxy_server_read(struct uring_task *task, int res)
+static void proxy_server_read(struct uring_task *task, int res)
{
- struct server_proxy *proxy = container_of(task, struct server_proxy, servertask);
+ struct server_proxy *proxy =
+ container_of(task, struct server_proxy, servertask);
assert_return(task);
assert_task_alive(DBG_PROXY, task);
@@ -222,10 +218,10 @@ proxy_server_read(struct uring_task *task, int res)
*/
static void proxy_client_spliced_in(struct uring_task *task, int res);
-static void
-proxy_client_spliced_out(struct uring_task *task, int res)
+static void proxy_client_spliced_out(struct uring_task *task, int res)
{
- struct server_proxy *proxy = container_of(task, struct server_proxy, servertask);
+ struct server_proxy *proxy =
+ container_of(task, struct server_proxy, servertask);
assert_return(task);
assert_task_alive(DBG_PROXY, task);
@@ -236,13 +232,14 @@ proxy_client_spliced_out(struct uring_task *task, int res)
return;
}
- uring_splice(task, proxy->cfd, proxy->cpipe[PIPE_WR], proxy_client_spliced_in);
+ uring_splice(task, proxy->cfd, proxy->cpipe[PIPE_WR],
+ proxy_client_spliced_in);
}
-static void
-proxy_client_spliced_in(struct uring_task *task, int res)
+static void proxy_client_spliced_in(struct uring_task *task, int res)
{
- struct server_proxy *proxy = container_of(task, struct server_proxy, servertask);
+ struct server_proxy *proxy =
+ container_of(task, struct server_proxy, servertask);
assert_return(task);
assert_task_alive(DBG_PROXY, task);
@@ -253,15 +250,16 @@ proxy_client_spliced_in(struct uring_task *task, int res)
return;
}
- uring_splice(task, proxy->cpipe[PIPE_RD], proxy->sfd, proxy_client_spliced_out);
+ uring_splice(task, proxy->cpipe[PIPE_RD], proxy->sfd,
+ proxy_client_spliced_out);
}
static void proxy_server_spliced_in(struct uring_task *task, int res);
-static void
-proxy_server_spliced_out(struct uring_task *task, int res)
+static void proxy_server_spliced_out(struct uring_task *task, int res)
{
- struct server_proxy *proxy = container_of(task, struct server_proxy, servertask);
+ struct server_proxy *proxy =
+ container_of(task, struct server_proxy, servertask);
assert_return(task);
assert_task_alive(DBG_PROXY, task);
@@ -272,13 +270,14 @@ proxy_server_spliced_out(struct uring_task *task, int res)
return;
}
- uring_splice(task, proxy->sfd, proxy->spipe[PIPE_WR], proxy_server_spliced_in);
+ uring_splice(task, proxy->sfd, proxy->spipe[PIPE_WR],
+ proxy_server_spliced_in);
}
-static void
-proxy_server_spliced_in(struct uring_task *task, int res)
+static void proxy_server_spliced_in(struct uring_task *task, int res)
{
- struct server_proxy *proxy = container_of(task, struct server_proxy, servertask);
+ struct server_proxy *proxy =
+ container_of(task, struct server_proxy, servertask);
assert_return(task);
assert_task_alive(DBG_PROXY, task);
@@ -289,13 +288,14 @@ proxy_server_spliced_in(struct uring_task *task, int res)
return;
}
- uring_splice(task, proxy->spipe[PIPE_RD], proxy->cfd, proxy_server_spliced_out);
+ uring_splice(task, proxy->spipe[PIPE_RD], proxy->cfd,
+ proxy_server_spliced_out);
}
-static void
-proxy_connected_cb(struct connection *conn, bool connected)
+static void proxy_connected_cb(struct connection *conn, bool connected)
{
- struct server_proxy *proxy = container_of(conn, struct server_proxy, server_conn);
+ struct server_proxy *proxy =
+ container_of(conn, struct server_proxy, server_conn);
assert_return(conn);
assert_task_alive(DBG_PROXY, &proxy->clienttask);
@@ -313,16 +313,17 @@ proxy_connected_cb(struct connection *conn, bool connected)
ptimer_del_task(&proxy->ptask);
proxy->sfd = proxy->servertask.fd;
- verbose("%s: proxy connection %s -> %s opened",
- proxy->server->name,
+ verbose("%s: proxy connection %s -> %s opened", proxy->server->name,
proxy->client_conn.remote.addrstr,
proxy->server_conn.remote.addrstr);
proxy->begin = time(NULL);
if (cfg->splice_supported) {
debug(DBG_PROXY, "handling proxy connection with splice");
- uring_splice(&proxy->clienttask, proxy->cfd, proxy->cpipe[PIPE_WR], proxy_client_spliced_in);
- uring_splice(&proxy->servertask, proxy->sfd, proxy->spipe[PIPE_WR], proxy_server_spliced_in);
+ uring_splice(&proxy->clienttask, proxy->cfd,
+ proxy->cpipe[PIPE_WR], proxy_client_spliced_in);
+ uring_splice(&proxy->servertask, proxy->sfd,
+ proxy->spipe[PIPE_WR], proxy_server_spliced_in);
} else {
debug(DBG_PROXY, "handling proxy connection with read-write");
uring_tbuf_read(&proxy->clienttask, proxy_client_read);
@@ -330,8 +331,7 @@ proxy_connected_cb(struct connection *conn, bool connected)
}
}
-void
-proxy_refdump(struct server_proxy *proxy)
+void proxy_refdump(struct server_proxy *proxy)
{
assert_return(proxy);
@@ -340,10 +340,10 @@ proxy_refdump(struct server_proxy *proxy)
uring_task_refdump(&proxy->servertask);
}
-static void
-proxy_connect_timer_cb(struct ptimer_task *ptask)
+static void proxy_connect_timer_cb(struct ptimer_task *ptask)
{
- struct server_proxy *proxy = container_of(ptask, struct server_proxy, ptask);
+ struct server_proxy *proxy =
+ container_of(ptask, struct server_proxy, ptask);
assert_return(ptask);
@@ -355,8 +355,8 @@ proxy_connect_timer_cb(struct ptimer_task *ptask)
&proxy->server_conn, proxy_connected_cb);
}
-struct server_proxy *
-proxy_new(struct server *server, struct saddr *client, int fd)
+struct server_proxy *proxy_new(struct server *server, struct saddr *client,
+ int fd)
{
struct server_proxy *proxy;
@@ -412,8 +412,8 @@ proxy_new(struct server *server, struct saddr *client, int fd)
}
proxy->connecting = true;
- connect_any(&proxy->servertask, &server->remotes,
- &proxy->server_conn, proxy_connected_cb);
+ connect_any(&proxy->servertask, &server->remotes, &proxy->server_conn,
+ proxy_connected_cb);
return proxy;
@@ -426,10 +426,10 @@ out:
return NULL;
}
-static void
-local_accept(struct uring_task *task, int res)
+static void local_accept(struct uring_task *task, int res)
{
- struct server_local *local = container_of(task, struct server_local, task);
+ struct server_local *local =
+ container_of(task, struct server_local, task);
struct server *server = container_of(task->parent, struct server, task);
struct server_proxy *proxy;
@@ -445,8 +445,8 @@ local_accept(struct uring_task *task, int res)
saddr_set_addrstr(&local->client);
- verbose("%s: incoming proxy connection: %s -> %s",
- server->name, local->client.addrstr, local->local.addrstr);
+ verbose("%s: incoming proxy connection: %s -> %s", server->name,
+ local->client.addrstr, local->local.addrstr);
if (list_empty(&server->remotes)) {
/* This shouldn't be possible, checked before opening local */
@@ -463,8 +463,7 @@ out:
uring_accept(&local->task, &local->client, local_accept);
}
-bool
-local_open(struct server_local *local)
+bool local_open(struct server_local *local)
{
int sfd;
int option;
@@ -472,14 +471,15 @@ local_open(struct server_local *local)
assert_return(local && local->server, false);
- sfd = socket(local->local.storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ sfd = socket(local->local.st.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (sfd < 0) {
error("socket: %m");
goto error;
}
option = true;
- if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) < 0) {
+ if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) <
+ 0) {
error("setsockopt: %m");
goto error;
}
@@ -487,7 +487,8 @@ local_open(struct server_local *local)
/* The MC protocol expects the client to send data first */
if (cfg->socket_defer) {
option = true;
- if (setsockopt(sfd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &option, sizeof(option)) < 0)
+ if (setsockopt(sfd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &option,
+ sizeof(option)) < 0)
error("setsockopt: %m");
}
@@ -497,14 +498,16 @@ local_open(struct server_local *local)
*/
if (cfg->socket_freebind) {
option = true;
- if (setsockopt(sfd, IPPROTO_IP, IP_FREEBIND, &option, sizeof(option)) < 0)
+ if (setsockopt(sfd, IPPROTO_IP, IP_FREEBIND, &option,
+ sizeof(option)) < 0)
error("setsockopt: %m");
}
- socket_set_low_latency(sfd, cfg->socket_keepalive,
- cfg->socket_iptos, cfg->socket_nodelay);
+ socket_set_low_latency(sfd, cfg->socket_keepalive, cfg->socket_iptos,
+ cfg->socket_nodelay);
- r = bind(sfd, (struct sockaddr *)&local->local.storage, local->local.addrlen);
+ r = bind(sfd, (struct sockaddr *)&local->local.st,
+ local->local.addrlen);
if (r < 0) {
error("bind: %m");
goto error;
@@ -526,18 +529,17 @@ error:
return false;
}
-void
-local_refdump(struct server_local *local)
+void local_refdump(struct server_local *local)
{
assert_return(local);
uring_task_refdump(&local->task);
}
-static void
-local_free(struct uring_task *task)
+static void local_free(struct uring_task *task)
{
- struct server_local *local = container_of(task, struct server_local, task);
+ struct server_local *local =
+ container_of(task, struct server_local, task);
assert_return(task);
@@ -546,16 +548,14 @@ local_free(struct uring_task *task)
xfree(local);
}
-void
-local_delete(struct server_local *local)
+void local_delete(struct server_local *local)
{
assert_return(local);
uring_task_destroy(&local->task);
}
-struct server_local *
-local_new(struct server *server, struct saddr *saddr)
+struct server_local *local_new(struct server *server, struct saddr *saddr)
{
struct server_local *local;
@@ -574,4 +574,3 @@ local_new(struct server *server, struct saddr *saddr)
xfree(saddr);
return local;
}
-
diff --git a/minecproxy/server-proxy.h b/minecproxy/server-proxy.h
index ee3bda3..877ae9a 100644
--- a/minecproxy/server-proxy.h
+++ b/minecproxy/server-proxy.h
@@ -15,7 +15,7 @@ struct server_proxy {
uint64_t server_bytes;
int spipe[2];
int sfd;
-
+
bool connecting;
time_t begin;
struct ptimer_task ptask;
diff --git a/minecproxy/server-rcon.c b/minecproxy/server-rcon.c
index b19bdb1..9562aa9 100644
--- a/minecproxy/server-rcon.c
+++ b/minecproxy/server-rcon.c
@@ -16,8 +16,7 @@
#include "server-rcon.h"
#include "rcon-protocol.h"
-static int
-rcon_packet_complete(struct uring_task *task, _unused_ int res)
+static int rcon_packet_complete(struct uring_task *task, _unused_ int res)
{
assert_return(task, -EINVAL);
assert_task_alive_or(DBG_RCON, task, return -EINTR);
@@ -25,23 +24,22 @@ rcon_packet_complete(struct uring_task *task, _unused_ int res)
return rcon_protocol_packet_complete(task->tbuf->buf, task->tbuf->len);
}
-static bool
-rcon_check_reply(struct uring_task *task, int res, int32_t *id,
- int32_t *type, const char **msg)
+static bool rcon_check_reply(struct uring_task *task, int res, int32_t *id,
+ int32_t *type, const char **msg)
{
struct server *server = container_of(task, struct server, rcon_task);
const char *error;
if (res < 0) {
- error("rcon(%s): reading reply failed, res: %i",
- server->name, res);
+ error("rcon(%s): reading reply failed, res: %i", server->name,
+ res);
goto error;
}
- if (!rcon_protocol_read_packet(task->tbuf->buf, task->tbuf->len,
- id, type, msg, &error)) {
- error("rcon(%s): failed to parse packet: %s",
- server->name, error);
+ if (!rcon_protocol_read_packet(task->tbuf->buf, task->tbuf->len, id,
+ type, msg, &error)) {
+ error("rcon(%s): failed to parse packet: %s", server->name,
+ error);
goto error;
}
@@ -52,8 +50,7 @@ error:
return false;
}
-static void
-rcon_stop_reply(struct uring_task *task, int res)
+static void rcon_stop_reply(struct uring_task *task, int res)
{
struct server *server = container_of(task, struct server, rcon_task);
int32_t id;
@@ -82,8 +79,7 @@ out:
uring_task_close_fd(task);
}
-static void
-rcon_stop_sent(struct uring_task *task, int res)
+static void rcon_stop_sent(struct uring_task *task, int res)
{
struct server *server = container_of(task, struct server, rcon_task);
@@ -101,8 +97,7 @@ rcon_stop_sent(struct uring_task *task, int res)
uring_tbuf_read_until(task, rcon_packet_complete, rcon_stop_reply);
}
-static void
-rcon_login_reply(struct uring_task *task, int res)
+static void rcon_login_reply(struct uring_task *task, int res)
{
struct server *server = container_of(task, struct server, rcon_task);
int32_t id;
@@ -140,8 +135,7 @@ error:
uring_task_close_fd(task);
}
-static void
-rcon_login_sent(struct uring_task *task, int res)
+static void rcon_login_sent(struct uring_task *task, int res)
{
struct server *server = container_of(task, struct server, rcon_task);
@@ -149,8 +143,8 @@ rcon_login_sent(struct uring_task *task, int res)
assert_task_alive(DBG_RCON, task);
if (res != task->tbuf->len) {
- error("rcon(%s): sending login failed, res: %i",
- server->name, res);
+ error("rcon(%s): sending login failed, res: %i", server->name,
+ res);
uring_task_close_fd(task);
return;
}
@@ -159,8 +153,7 @@ rcon_login_sent(struct uring_task *task, int res)
uring_tbuf_read_until(task, rcon_packet_complete, rcon_login_reply);
}
-static void
-rcon_connected_cb(struct connection *conn, bool connected)
+static void rcon_connected_cb(struct connection *conn, bool connected)
{
struct server *server = container_of(conn, struct server, rcon_conn);
@@ -175,13 +168,11 @@ rcon_connected_cb(struct connection *conn, bool connected)
rcon_protocol_create_packet(server->rcon_tbuf.buf,
sizeof(server->rcon_tbuf.buf),
&server->rcon_tbuf.len, 1,
- RCON_PACKET_LOGIN,
- server->rcon_password);
+ RCON_PACKET_LOGIN, server->rcon_password);
uring_tbuf_write(&server->rcon_task, rcon_login_sent);
}
-static void
-rcon_free(struct uring_task *task)
+static void rcon_free(struct uring_task *task)
{
struct server *server = container_of(task, struct server, rcon_task);
@@ -190,25 +181,24 @@ rcon_free(struct uring_task *task)
debug(DBG_RCON, "task %p, server %s (%p)", task, server->name, server);
}
-void
-rcon_stop(struct server *server)
+void rcon_stop(struct server *server)
{
- assert_return(server && !list_empty(&server->rcons) && !empty_str(server->rcon_password));
+ assert_return(server && !list_empty(&server->rcons) &&
+ !empty_str(server->rcon_password));
assert_task_alive(DBG_RCON, &server->rcon_task);
- connect_any(&server->rcon_task, &server->rcons, &server->rcon_conn, rcon_connected_cb);
+ connect_any(&server->rcon_task, &server->rcons, &server->rcon_conn,
+ rcon_connected_cb);
}
-void
-rcon_refdump(struct server *server)
+void rcon_refdump(struct server *server)
{
assert_return(server);
uring_task_refdump(&server->rcon_task);
}
-void
-rcon_delete(struct server *server)
+void rcon_delete(struct server *server)
{
assert_return(server);
@@ -216,12 +206,10 @@ rcon_delete(struct server *server)
uring_task_destroy(&server->rcon_task);
}
-void
-rcon_init(struct server *server)
+void rcon_init(struct server *server)
{
assert_return(server);
uring_task_init(&server->rcon_task, "rcon", &server->task, rcon_free);
uring_task_set_buf(&server->rcon_task, &server->rcon_tbuf);
}
-
diff --git a/minecproxy/server.c b/minecproxy/server.c
index 39b4024..8c521cf 100644
--- a/minecproxy/server.c
+++ b/minecproxy/server.c
@@ -20,8 +20,8 @@
#include "idle.h"
#include "systemd.h"
-static bool
-set_property(struct server *server, char **property, const char *value)
+static bool set_property(struct server *server, char **property,
+ const char *value)
{
assert_return(server && !*property && !empty_str(value), false);
@@ -34,8 +34,7 @@ set_property(struct server *server, char **property, const char *value)
return true;
}
-void
-server_refdump(struct server *server)
+void server_refdump(struct server *server)
{
struct server_local *local;
struct server_proxy *proxy;
@@ -53,8 +52,7 @@ server_refdump(struct server *server)
rcon_refdump(server);
}
-static void
-server_free(struct uring_task *task)
+static void server_free(struct uring_task *task)
{
struct server *server = container_of(task, struct server, task);
@@ -72,8 +70,7 @@ server_free(struct uring_task *task)
xfree(server);
}
-void
-server_delete(struct server *server)
+void server_delete(struct server *server)
{
struct server_local *local, *ltmp;
struct server_proxy *proxy, *ptmp;
@@ -107,7 +104,7 @@ server_delete(struct server *server)
list_for_each_entry_safe(dns, dtmp, &server->dnslookups, list)
gai_cancel(&dns->gcb);
-
+
uring_task_destroy(&server->idle_task);
uring_poll_cancel(&server->exec_task);
uring_task_put(&server->exec_task);
@@ -115,8 +112,7 @@ server_delete(struct server *server)
uring_task_put(&server->ann_task);
}
-void
-server_delete_by_name(const char *name)
+void server_delete_by_name(const char *name)
{
struct server *server;
@@ -130,8 +126,7 @@ server_delete_by_name(const char *name)
}
}
-static void
-server_dump(struct server *server)
+static void server_dump(struct server *server)
{
struct server_local *local;
struct saddr *remote;
@@ -151,8 +146,9 @@ server_dump(struct server *server)
verbose(" * Type: unknown");
break;
}
- verbose(" * Name: %s", server->pretty_name ? server->pretty_name : "<undefined>");
- verbose(" * Announce port: %" PRIu16, server->announce_port);
+ verbose(" * Name: %s",
+ server->pretty_name ? server->pretty_name : "<undefined>");
+ verbose(" * Announce port: %" PRIu16, server->announce_port);
if (!list_empty(&server->locals)) {
verbose(" * Local:");
@@ -175,8 +171,7 @@ server_dump(struct server *server)
verbose("");
}
-static void
-server_exec_free(struct uring_task *task)
+static void server_exec_free(struct uring_task *task)
{
assert_return(task);
@@ -188,8 +183,7 @@ server_exec_free(struct uring_task *task)
#endif
/* FIXME: update states */
-static void
-server_exec_done(struct uring_task *task, int res)
+static void server_exec_done(struct uring_task *task, int res)
{
struct server *server = container_of(task, struct server, exec_task);
int r;
@@ -205,10 +199,10 @@ server_exec_done(struct uring_task *task, int res)
}
r = waitid(P_PIDFD, server->exec_task.fd, &info, WEXITED);
- if (r < 0) {
- error("waitid: %m");
+ if (r < 0) {
+ error("waitid: %m");
goto out;
- }
+ }
if (info.si_status == 0)
debug(DBG_SRV, "command successfully executed");
@@ -219,8 +213,7 @@ out:
uring_task_close_fd(&server->exec_task);
}
-static int
-server_exec_child(void *ptr)
+static int server_exec_child(void *ptr)
{
const char *cmd = ptr;
@@ -234,8 +227,7 @@ server_exec_child(void *ptr)
#define CLONE_PIDFD 0x00001000
#endif
-static bool
-server_exec(struct server *server, const char *cmd)
+static bool server_exec(struct server *server, const char *cmd)
{
char stack[4096]; /* Beautiful/horrible hack :) */
int pidfd;
@@ -243,9 +235,9 @@ server_exec(struct server *server, const char *cmd)
assert_return(server && cmd && server->exec_task.fd < 1, false);
- r = clone(server_exec_child, stack + sizeof(stack),
- CLONE_VM | CLONE_VFORK | CLONE_PIDFD | SIGCHLD,
- (void *)cmd, &pidfd);
+ r = clone(server_exec_child, stack + sizeof(stack),
+ CLONE_VM | CLONE_VFORK | CLONE_PIDFD | SIGCHLD, (void *)cmd,
+ &pidfd);
if (r < 0) {
error("clone: %m: %i", r);
return false;
@@ -256,14 +248,14 @@ server_exec(struct server *server, const char *cmd)
return true;
}
-static bool
-server_check_running(struct server *server)
+static bool server_check_running(struct server *server)
{
assert_return(server, false);
/* FIXME: other methods, rcon? */
if (server->systemd_service) {
- verbose("%s: checking if systemd service is running", server->name);
+ verbose("%s: checking if systemd service is running",
+ server->name);
if (systemd_service_running(server)) {
server->state = SERVER_STATE_RUNNING;
return true;
@@ -276,21 +268,19 @@ server_check_running(struct server *server)
return false;
}
-bool
-server_start(struct server *server)
+bool server_start(struct server *server)
{
assert_return(server, false);
assert_task_alive_or(DBG_SRV, &server->task, return false);
switch (server->start_method) {
-
case SERVER_START_METHOD_EXEC:
verbose("Starting server %s via external cmd", server->name);
return server_exec(server, server->start_exec);
case SERVER_START_METHOD_SYSTEMD:
- verbose("Starting server %s via systemd (%s)",
- server->name, server->systemd_service);
+ verbose("Starting server %s via systemd (%s)", server->name,
+ server->systemd_service);
if (systemd_service_start(server)) {
server->state = SERVER_STATE_RUNNING;
@@ -306,21 +296,19 @@ server_start(struct server *server)
return false;
}
-bool
-server_stop(struct server *server)
+bool server_stop(struct server *server)
{
assert_return(server, false);
assert_task_alive_or(DBG_SRV, &server->task, return false);
switch (server->stop_method) {
-
case SERVER_STOP_METHOD_EXEC:
verbose("Stopping server %s via external cmd", server->name);
return server_exec(server, server->stop_exec);
case SERVER_STOP_METHOD_SYSTEMD:
- verbose("Stopping server %s via systemd (%s)",
- server->name, server->systemd_service);
+ verbose("Stopping server %s via systemd (%s)", server->name,
+ server->systemd_service);
if (systemd_service_stop(server)) {
server->state = SERVER_STATE_STOPPED;
return true;
@@ -340,22 +328,19 @@ server_stop(struct server *server)
return false;
}
-static void
-server_idle_free(struct uring_task *task)
+static void server_idle_free(struct uring_task *task)
{
assert_return(task);
debug(DBG_ANN, "called");
}
-void
-server_set_active_players(struct server *server, int count)
+void server_set_active_players(struct server *server, int count)
{
assert_return(server);
assert_task_alive(DBG_IDLE, &server->idle_task);
- debug(DBG_IDLE, "%s: currently %i active players",
- server->name, count);
+ debug(DBG_IDLE, "%s: currently %i active players", server->name, count);
if (count < 0)
return;
@@ -368,12 +353,11 @@ server_set_active_players(struct server *server, int count)
if (server->idle_count > server->idle_timeout) {
verbose("stopping idle server %s", server->name);
- server_stop(server);
+ server_stop(server);
}
}
-static void
-server_idle_connected_cb(struct connection *conn, bool connected)
+static void server_idle_connected_cb(struct connection *conn, bool connected)
{
struct server *server = container_of(conn, struct server, idle_conn);
@@ -393,8 +377,7 @@ server_idle_connected_cb(struct connection *conn, bool connected)
idle_check_get_player_count(server, conn);
}
-bool
-server_idle_check(struct server *server)
+bool server_idle_check(struct server *server)
{
assert_return(server, false);
@@ -413,21 +396,19 @@ server_idle_check(struct server *server)
return true;
}
- connect_any(&server->idle_task, &server->remotes,
- &server->idle_conn, server_idle_connected_cb);
+ connect_any(&server->idle_task, &server->remotes, &server->idle_conn,
+ server_idle_connected_cb);
return true;
}
-static void
-server_announce_free(struct uring_task *task)
+static void server_announce_free(struct uring_task *task)
{
assert_return(task);
debug(DBG_ANN, "called");
}
-static void
-server_announce_cb(struct uring_task *task, int res)
+static void server_announce_cb(struct uring_task *task, int res)
{
struct server *server = container_of(task, struct server, ann_task);
@@ -443,8 +424,7 @@ server_announce_cb(struct uring_task *task, int res)
uring_task_set_fd(&server->ann_task, -1);
}
-bool
-server_announce(struct server *server, int fd)
+bool server_announce(struct server *server, int fd)
{
assert_return(server && fd >= 0, false);
@@ -458,8 +438,7 @@ server_announce(struct server *server, int fd)
return true;
}
-bool
-server_commit(struct server *server)
+bool server_commit(struct server *server)
{
struct server_local *local;
uint16_t port;
@@ -486,14 +465,14 @@ server_commit(struct server *server)
if (server->stop_method == SERVER_STOP_METHOD_RCON &&
list_empty(&server->rcons)) {
error("%s: rcon stop method missing rcon address",
- server->name);
+ server->name);
return false;
}
if (server->stop_method == SERVER_STOP_METHOD_RCON &&
!server->rcon_password) {
error("%s: rcon stop method missing rcon password",
- server->name);
+ server->name);
return false;
}
@@ -501,12 +480,13 @@ server_commit(struct server *server)
server->stop_method == SERVER_STOP_METHOD_SYSTEMD) &&
!server->systemd_service) {
error("%s: systemd start/stop method missing systemd service",
- server->name);
+ server->name);
return false;
}
if (server->systemd_service && !server->systemd_obj) {
- server->systemd_obj = systemd_object_path(server->systemd_service);
+ server->systemd_obj =
+ systemd_object_path(server->systemd_service);
if (!server->systemd_obj) {
error("%s: failed to create systemd object path (%s)",
server->name, server->systemd_service);
@@ -516,29 +496,33 @@ server_commit(struct server *server)
if (server->idle_timeout > 0 &&
server->stop_method == SERVER_STOP_METHOD_UNDEFINED) {
- error("%s: idle_timeout set but missing stop method", server->name);
+ error("%s: idle_timeout set but missing stop method",
+ server->name);
return false;
}
switch (server->type) {
case SERVER_TYPE_ANNOUNCE:
if (server->announce_port < 1) {
- error("%s: missing announce port", server->name);
+ error("%s: missing announce port", server->name);
return false;
}
if (server->start_method != SERVER_START_METHOD_UNDEFINED) {
- error("%s: can't set start_method for announce server", server->name);
+ error("%s: can't set start_method for announce server",
+ server->name);
return false;
}
if (!list_empty(&server->locals)) {
- error("%s: can't set local addresses for announce server", server->name);
+ error("%s: can't set local addresses for announce server",
+ server->name);
return false;
}
if (!list_empty(&server->remotes)) {
- error("%s: can't set remote addresses for announce server", server->name);
+ error("%s: can't set remote addresses for announce server",
+ server->name);
return false;
}
@@ -546,17 +530,20 @@ server_commit(struct server *server)
case SERVER_TYPE_PROXY:
if (server->announce_port >= 1) {
- error("%s: can't set announce port for proxy server", server->name);
+ error("%s: can't set announce port for proxy server",
+ server->name);
return false;
}
if (list_empty(&server->locals)) {
- error("%s: missing local addresses for proxy server", server->name);
+ error("%s: missing local addresses for proxy server",
+ server->name);
return false;
}
if (list_empty(&server->remotes)) {
- error("%s: missing remote addresses for proxy server", server->name);
+ error("%s: missing remote addresses for proxy server",
+ server->name);
return false;
}
@@ -578,7 +565,8 @@ server_commit(struct server *server)
}
if (server->announce_port < 1) {
- error("%s: can't determine which port to announce", server->name);
+ error("%s: can't determine which port to announce",
+ server->name);
return false;
}
@@ -591,30 +579,34 @@ server_commit(struct server *server)
if (!server->pretty_name) {
char *suffix;
-
+
suffix = strrchr(server->name, '.');
if (!suffix || suffix == server->name) {
error("invalid server name: %s", server->name);
return false;
}
- server->pretty_name = xstrndup(server->name, suffix - server->name);
+ server->pretty_name =
+ xstrndup(server->name, suffix - server->name);
if (!server->pretty_name) {
- error("failed to create display name: %s", server->name);
+ error("failed to create display name: %s",
+ server->name);
return false;
}
}
r = snprintf(server->ann_buf.buf, sizeof(server->ann_buf.buf),
- "[MOTD]%s[/MOTD][AD]%" PRIu16 "[/AD]",
- server->pretty_name, server->announce_port);
+ "[MOTD]%s[/MOTD][AD]%" PRIu16 "[/AD]", server->pretty_name,
+ server->announce_port);
if (r < 1 || r >= sizeof(server->ann_buf.buf)) {
- error("%s: unable to create announce msg: %i\n", server->name, r);
+ error("%s: unable to create announce msg: %i\n", server->name,
+ r);
return false;
}
server->ann_buf.len = r;
- /* FIXME: config, dont reread config if server running, make sure fd is available before this is called */
+ /* FIXME: config, dont reread config if server running, make sure fd is
+ * available before this is called */
server_dump(server);
list_for_each_entry(local, &server->locals, list)
@@ -628,8 +620,7 @@ server_commit(struct server *server)
return true;
}
-bool
-server_add_remote(struct server *server, struct saddr *remote)
+bool server_add_remote(struct server *server, struct saddr *remote)
{
assert_return(server && remote, false);
assert_task_alive_or(DBG_SRV, &server->task, return false);
@@ -639,8 +630,7 @@ server_add_remote(struct server *server, struct saddr *remote)
return true;
}
-bool
-server_add_local(struct server *server, struct saddr *saddr)
+bool server_add_local(struct server *server, struct saddr *saddr)
{
struct server_local *local;
@@ -655,8 +645,7 @@ server_add_local(struct server *server, struct saddr *saddr)
return true;
}
-bool
-server_add_rcon(struct server *server, struct saddr *rcon)
+bool server_add_rcon(struct server *server, struct saddr *rcon)
{
assert_return(server && rcon, false);
assert_task_alive_or(DBG_SRV, &server->task, return false);
@@ -666,21 +655,20 @@ server_add_rcon(struct server *server, struct saddr *rcon)
return true;
}
-bool
-server_set_rcon_password(struct server *server, const char *password)
+bool server_set_rcon_password(struct server *server, const char *password)
{
assert_return(server && !empty_str(password), false);
return set_property(server, &server->rcon_password, password);
}
-bool
-server_set_systemd_service(struct server *server, const char *service)
+bool server_set_systemd_service(struct server *server, const char *service)
{
const char *suffix;
char *tmp;
- assert_return(server && !empty_str(service) && !server->systemd_service, false);
+ assert_return(server && !empty_str(service) && !server->systemd_service,
+ false);
suffix = strrchr(service, '.');
if (!suffix || !streq(suffix, ".service")) {
@@ -699,55 +687,52 @@ server_set_systemd_service(struct server *server, const char *service)
return true;
}
-bool
-server_set_stop_method(struct server *server,
- enum server_stop_method stop_method)
+bool server_set_stop_method(struct server *server,
+ enum server_stop_method stop_method)
{
assert_return(server->stop_method == SERVER_STOP_METHOD_UNDEFINED &&
- stop_method != SERVER_STOP_METHOD_UNDEFINED, false);
+ stop_method != SERVER_STOP_METHOD_UNDEFINED,
+ false);
server->stop_method = stop_method;
return true;
}
-bool
-server_set_start_method(struct server *server,
- enum server_start_method start_method)
+bool server_set_start_method(struct server *server,
+ enum server_start_method start_method)
{
assert_return(server->start_method == SERVER_START_METHOD_UNDEFINED &&
- start_method != SERVER_START_METHOD_UNDEFINED, false);
+ start_method != SERVER_START_METHOD_UNDEFINED,
+ false);
server->start_method = start_method;
return true;
}
-bool
-server_set_stop_exec(struct server *server, const char *cmd)
+bool server_set_stop_exec(struct server *server, const char *cmd)
{
assert_return(server && !empty_str(cmd), false);
return set_property(server, &server->stop_exec, cmd);
}
-bool
-server_set_start_exec(struct server *server, const char *cmd)
+bool server_set_start_exec(struct server *server, const char *cmd)
{
assert_return(server && !empty_str(cmd), false);
return set_property(server, &server->start_exec, cmd);
}
-bool
-server_set_idle_timeout(struct server *server, uint16_t timeout)
+bool server_set_idle_timeout(struct server *server, uint16_t timeout)
{
- assert_return(server && timeout > 0 && server->idle_timeout == 0, false);
+ assert_return(server && timeout > 0 && server->idle_timeout == 0,
+ false);
server->idle_timeout = timeout;
return true;
}
-bool
-server_set_port(struct server *server, uint16_t port)
+bool server_set_port(struct server *server, uint16_t port)
{
assert_return(server && port > 0 && server->announce_port == 0, false);
@@ -755,8 +740,7 @@ server_set_port(struct server *server, uint16_t port)
return true;
}
-bool
-server_set_type(struct server *server, enum server_type type)
+bool server_set_type(struct server *server, enum server_type type)
{
assert_return(server && type != SERVER_TYPE_UNDEFINED, false);
@@ -774,16 +758,14 @@ server_set_type(struct server *server, enum server_type type)
return true;
}
-bool
-server_set_pretty_name(struct server *server, const char *pretty_name)
+bool server_set_pretty_name(struct server *server, const char *pretty_name)
{
assert_return(server && !empty_str(pretty_name), false);
return set_property(server, &server->pretty_name, pretty_name);
}
-struct server *
-server_new(const char *name)
+struct server *server_new(const char *name)
{
struct server *server;
@@ -810,16 +792,21 @@ server_new(const char *name)
server->start_method = SERVER_START_METHOD_UNDEFINED;
server->idle_timeout = 0;
- uring_task_init(&server->task, server->name, uring_parent(), server_free);
+ uring_task_init(&server->task, server->name, uring_parent(),
+ server_free);
uring_task_set_buf(&server->task, &server->tbuf);
- uring_task_init(&server->ann_task, "announce", &server->task, server_announce_free);
+ uring_task_init(&server->ann_task, "announce", &server->task,
+ server_announce_free);
uring_task_set_buf(&server->ann_task, &server->ann_buf);
- saddr_set_ipv4(&server->ann_task.saddr, cinet_addr(224,0,2,60), htons(4445));
+ saddr_set_ipv4(&server->ann_task.saddr, cinet_addr(224,0,2,60),
+ htons(4445));
- uring_task_init(&server->exec_task, "exec", &server->task, server_exec_free);
+ uring_task_init(&server->exec_task, "exec", &server->task,
+ server_exec_free);
- uring_task_init(&server->idle_task, "idle", &server->task, server_idle_free);
+ uring_task_init(&server->idle_task, "idle", &server->task,
+ server_idle_free);
uring_task_set_buf(&server->idle_task, &server->idle_buf);
rcon_init(server);
diff --git a/minecproxy/server.h b/minecproxy/server.h
index ff4c28e..937fa67 100644
--- a/minecproxy/server.h
+++ b/minecproxy/server.h
@@ -1,6 +1,7 @@
#ifndef fooserverhfoo
#define fooserverhfoo
+/* clang-format off */
enum server_state {
SERVER_STATE_INIT = 0,
SERVER_STATE_CFG_OK = 1,
@@ -8,6 +9,7 @@ enum server_state {
SERVER_STATE_STOPPED = 3,
SERVER_STATE_DEAD = 4,
};
+/* clang-format on */
enum server_type {
SERVER_TYPE_UNDEFINED,
@@ -39,7 +41,7 @@ struct server {
struct list_head rcons;
struct list_head dnslookups;
enum server_state state;
-
+
enum server_stop_method stop_method;
enum server_start_method start_method;
@@ -125,4 +127,3 @@ bool server_set_pretty_name(struct server *server, const char *pretty_name);
struct server *server_new(const char *name);
#endif
-
diff --git a/minecproxy/signal-handler.c b/minecproxy/signal-handler.c
index 15c3816..ba92382 100644
--- a/minecproxy/signal-handler.c
+++ b/minecproxy/signal-handler.c
@@ -21,16 +21,14 @@ struct signal_ev {
int pipe[2];
};
-static void
-signal_delete()
+static void signal_delete()
{
assert_return(cfg->signal);
uring_task_destroy(&cfg->signal->task);
}
-static void
-signalfd_read(struct uring_task *task, int res)
+static void signalfd_read(struct uring_task *task, int res)
{
struct signal_ev *signal = container_of(task, struct signal_ev, task);
struct server *server, *stmp;
@@ -60,8 +58,8 @@ signalfd_read(struct uring_task *task, int res)
goto out;
}
- debug(DBG_DNS, "DNS lookup complete, dns: %p, dns->cb: %p",
- dns, dns->cb);
+ debug(DBG_DNS, "DNS lookup complete, dns: %p, dns->cb: %p", dns,
+ dns->cb);
dns->cb(dns);
break;
}
@@ -109,8 +107,8 @@ out:
uring_tbuf_read(&signal->task, signalfd_read);
}
-static void
-hack_signal_handler(int signum, siginfo_t *si, _unused_ void *ucontext)
+static void hack_signal_handler(int signum, siginfo_t *si,
+ _unused_ void *ucontext)
{
ssize_t r;
@@ -119,11 +117,9 @@ hack_signal_handler(int signum, siginfo_t *si, _unused_ void *ucontext)
r = write(cfg->signal->pipe[PIPE_WR], si, sizeof(*si));
if (r != sizeof(*si))
error("write: %zi\n", r);
-
}
-static void
-signal_free(struct uring_task *task)
+static void signal_free(struct uring_task *task)
{
struct signal_ev *signal = container_of(task, struct signal_ev, task);
@@ -135,18 +131,16 @@ signal_free(struct uring_task *task)
xfree(signal);
}
-void
-signal_refdump()
+void signal_refdump()
{
assert_return(cfg->signal);
uring_task_refdump(&cfg->signal->task);
}
-void
-signal_init()
+void signal_init()
{
- //sigset_t mask;
+ // sigset_t mask;
struct signal_ev *signal;
assert_return(!cfg->signal);
@@ -183,12 +177,11 @@ signal_init()
action.sa_flags = 0;
sigaction(SIGPIPE, &action, NULL);
- debug(DBG_SIG, "using pipe fds %i -> %i",
- signal->pipe[PIPE_WR], signal->pipe[PIPE_RD]);
+ debug(DBG_SIG, "using pipe fds %i -> %i", signal->pipe[PIPE_WR],
+ signal->pipe[PIPE_RD]);
uring_task_init(&signal->task, "signal", uring_parent(), signal_free);
uring_task_set_fd(&signal->task, signal->pipe[PIPE_RD]);
uring_task_set_buf(&signal->task, &signal->tbuf);
cfg->signal = signal;
uring_tbuf_read(&signal->task, signalfd_read);
}
-
diff --git a/minecproxy/systemd.c b/minecproxy/systemd.c
index e9ed885..a973a55 100644
--- a/minecproxy/systemd.c
+++ b/minecproxy/systemd.c
@@ -6,12 +6,11 @@
#include "server.h"
#include "systemd.h"
-#define SYSTEMD_DBUS_SERVICE "org.freedesktop.systemd1"
-#define SYSTEMD_DBUS_INTERFACE "org.freedesktop.systemd1.Unit"
-#define SYSTEMD_DBUS_PATH_PREFIX "/org/freedesktop/systemd1/unit/"
+#define SYSTEMD_DBUS_SERVICE "org.freedesktop.systemd1"
+#define SYSTEMD_DBUS_INTERFACE "org.freedesktop.systemd1.Unit"
+#define SYSTEMD_DBUS_PATH_PREFIX "/org/freedesktop/systemd1/unit/"
-static inline char
-tohex(uint8_t val)
+static inline char tohex(uint8_t val)
{
static const char hex[] = "0123456789abcdef";
@@ -29,8 +28,7 @@ tohex(uint8_t val)
*
* Example: minecraft@world1.service -> minecraft_40world1_2eservice
*/
-char *
-systemd_object_path(const char *service)
+char *systemd_object_path(const char *service)
{
char *r;
char *d;
@@ -46,8 +44,7 @@ systemd_object_path(const char *service)
d = r + STRLEN(SYSTEMD_DBUS_PATH_PREFIX);
for (s = service; *s; s++) {
- if ((*s >= 'a' && *s <= 'z') ||
- (*s >= 'A' && *s <= 'Z') ||
+ if ((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
(*s >= '0' && *s <= '9')) {
*(d++) = *s;
continue;
@@ -62,17 +59,15 @@ systemd_object_path(const char *service)
return r;
}
-void
-systemd_delete()
+void systemd_delete()
{
- assert_return_silent(cfg->sd_bus);
+ assert_return_silent(cfg->sd_bus);
- sd_bus_unref(cfg->sd_bus);
+ sd_bus_unref(cfg->sd_bus);
cfg->sd_bus = NULL;
}
-static sd_bus *
-get_bus()
+static sd_bus *get_bus()
{
int r;
@@ -82,7 +77,8 @@ get_bus()
if (!cfg->sd_bus) {
r = sd_bus_open_user(&cfg->sd_bus);
if (r < 0) {
- error("failed to connect to user system bus: %s", strerror(-r));
+ error("failed to connect to user system bus: %s",
+ strerror(-r));
cfg->sd_bus_failed = true;
return NULL;
}
@@ -94,37 +90,36 @@ get_bus()
/*
* Check if a systemd service is running.
*
- * This is equivalent to (assuming service minecraft@world1):
- * gdbus call --session
- * --dest org.freedesktop.systemd1
- * --object-path /org/freedesktop/systemd1/unit/minecraft_40world1_2eservice
+ * This is equivalent to (assuming service mc@world1):
+ * gdbus call --session
+ * --dest org.freedesktop.systemd1
+ * --object-path /org/freedesktop/systemd1/unit/mc_40world1_2eservice
* --method org.freedesktop.DBus.Properties.Get
- * "org.freedesktop.systemd1.Unit"
+ * "org.freedesktop.systemd1.Unit"
* "ActiveState"
*/
-bool
-systemd_service_running(struct server *server)
+bool systemd_service_running(struct server *server)
{
sd_bus *bus = get_bus();
- sd_bus_error error = SD_BUS_ERROR_NULL;
+ sd_bus_error error = SD_BUS_ERROR_NULL;
char *status = NULL;
bool running = false;
int r;
- assert_return(server && bus && server->systemd_service && server->systemd_obj, false);
+ assert_return(server && bus && server->systemd_service &&
+ server->systemd_obj,
+ false);
- r = sd_bus_get_property_string(bus,
- SYSTEMD_DBUS_SERVICE,
+ r = sd_bus_get_property_string(bus, SYSTEMD_DBUS_SERVICE,
server->systemd_obj,
- SYSTEMD_DBUS_INTERFACE,
- "ActiveState",
- &error,
- &status);
- if (r < 0) {
- error("failed to get status for service %s (%s): %s",
- server->systemd_service, server->systemd_obj, error.message);
+ SYSTEMD_DBUS_INTERFACE, "ActiveState",
+ &error, &status);
+ if (r < 0) {
+ error("failed to get status for service %s (%s): %s",
+ server->systemd_service, server->systemd_obj,
+ error.message);
goto out;
- }
+ }
if (streq(status, "active")) {
running = true;
@@ -136,64 +131,58 @@ systemd_service_running(struct server *server)
out:
free(status);
- sd_bus_error_free(&error);
+ sd_bus_error_free(&error);
return running;
}
-static bool
-systemd_service_action(struct server *server, const char *action)
+static bool systemd_service_action(struct server *server, const char *action)
{
- sd_bus_error error = SD_BUS_ERROR_NULL;
- sd_bus_message *m = NULL;
+ sd_bus_error error = SD_BUS_ERROR_NULL;
+ sd_bus_message *m = NULL;
sd_bus *bus = get_bus();
const char *path;
bool performed = false;
int r;
- assert_return(server && bus && server->systemd_service && server->systemd_obj && action, false);
+ assert_return(server && bus && server->systemd_service &&
+ server->systemd_obj && action,
+ false);
- r = sd_bus_call_method(bus,
- SYSTEMD_DBUS_SERVICE,
- server->systemd_obj,
- SYSTEMD_DBUS_INTERFACE,
- action,
- &error,
- &m,
- "s",
+ r = sd_bus_call_method(bus, SYSTEMD_DBUS_SERVICE, server->systemd_obj,
+ SYSTEMD_DBUS_INTERFACE, action, &error, &m, "s",
"fail");
- if (r < 0) {
- error("failed to perform action %s on systemd service %s: %s",
+ if (r < 0) {
+ error("failed to perform action %s on systemd service %s: %s",
action, server->systemd_service, error.message);
goto out;
- }
+ }
- r = sd_bus_message_read(m, "o", &path);
- if (r < 0) {
- error("failed to parse response message: %s", strerror(-r));
+ r = sd_bus_message_read(m, "o", &path);
+ if (r < 0) {
+ error("failed to parse response message: %s", strerror(-r));
goto out;
- }
+ }
- verbose("action %s queued for service %s",
- action, server->systemd_service);
+ verbose("action %s queued for service %s", action,
+ server->systemd_service);
performed = true;
out:
- sd_bus_error_free(&error);
- sd_bus_message_unref(m);
+ sd_bus_error_free(&error);
+ sd_bus_message_unref(m);
return performed;
}
/*
* Stop systemd service.
*
- * This is equivalent to (assuming service minecraft@world1):
- * gdbus call --session
- * --dest org.freedesktop.systemd1
- * --object-path /org/freedesktop/systemd1/unit/minecraft_40world1_2eservice
+ * This is equivalent to (assuming service mc@world1):
+ * gdbus call --session
+ * --dest org.freedesktop.systemd1
+ * --object-path /org/freedesktop/systemd1/unit/mc_40world1_2eservice
* --method org.freedesktop.systemd1.Unit.Stop "fail"
*/
-bool
-systemd_service_stop(struct server *server)
+bool systemd_service_stop(struct server *server)
{
assert_return(server, false);
@@ -203,17 +192,15 @@ systemd_service_stop(struct server *server)
/*
* Start systemd service.
*
- * This is equivalent to (assuming service minecraft@world1):
- * gdbus call --session
- * --dest org.freedesktop.systemd1
- * --object-path /org/freedesktop/systemd1/unit/minecraft_40world1_2eservice
+ * This is equivalent to (assuming service mc@world1):
+ * gdbus call --session
+ * --dest org.freedesktop.systemd1
+ * --object-path /org/freedesktop/systemd1/unit/mc_40world1_2eservice
* --method org.freedesktop.systemd1.Unit.Start "fail"
*/
-bool
-systemd_service_start(struct server *server)
+bool systemd_service_start(struct server *server)
{
assert_return(server, false);
return systemd_service_action(server, "Start");
}
-
diff --git a/minecproxy/uring.c b/minecproxy/uring.c
index abea592..dd0d28a 100644
--- a/minecproxy/uring.c
+++ b/minecproxy/uring.c
@@ -19,23 +19,24 @@ struct uring_ev {
int tfd;
};
+/* clang-format off */
enum cqe_type {
CQE_TYPE_NORMAL = 0x0,
CQE_TYPE_CANCEL = 0x1,
CQE_TYPE_CLOSE = 0x2,
CQE_TYPE_POLL_CANCEL = 0x3
};
+/* clang-format on */
#define CQE_TYPE_PTR_MASK 0x3
uint64_t sqe_count = 0;
uint64_t cqe_count = 0;
-static struct io_uring_sqe *
-get_sqe(struct uring_task *task)
+static struct io_uring_sqe *get_sqe(struct uring_task *task)
{
struct io_uring_sqe *sqe;
-
+
assert_die(task, "invalid arguments");
sqe = io_uring_get_sqe(&cfg->uring->uring);
@@ -51,8 +52,7 @@ get_sqe(struct uring_task *task)
return sqe;
}
-void
-uring_task_refdump(struct uring_task *task)
+void uring_task_refdump(struct uring_task *task)
{
char buf[4096];
struct uring_task *tmp;
@@ -83,45 +83,43 @@ uring_task_refdump(struct uring_task *task)
memcpy(dst, tmp->name, strlen(tmp->name));
}
- info("%s (0x%p parent 0x%p free 0x%p fd %i ref %u)",
- buf, task, task->parent, task->free, task->fd,
- task->refcount);
+ info("%s (0x%p parent 0x%p free 0x%p fd %i ref %u)", buf, task,
+ task->parent, task->free, task->fd, task->refcount);
}
/*
* Similar to uring_task_put, but can be called from other tasks
* while the task is active.
*/
-void
-uring_task_destroy(struct uring_task *task)
+void uring_task_destroy(struct uring_task *task)
{
assert_return(task);
assert_return_silent(!task->dead);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
if (task->fd >= 0) {
struct io_uring_sqe *sqe;
-
+
sqe = get_sqe(task);
io_uring_prep_cancel(sqe, task, 0);
- io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task | CQE_TYPE_CANCEL));
+ io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task |
+ CQE_TYPE_CANCEL));
}
task->dead = true;
uring_task_put(task);
}
-void
-uring_task_put(struct uring_task *task)
+void uring_task_put(struct uring_task *task)
{
struct uring_task *parent;
assert_return(task);
- debug(DBG_REF, "task %s (%p), refcount %u -> %u",
- task->name, task, task->refcount, task->refcount - 1);
+ debug(DBG_REF, "task %s (%p), refcount %u -> %u", task->name, task,
+ task->refcount, task->refcount - 1);
task->refcount--;
@@ -147,13 +145,12 @@ uring_task_put(struct uring_task *task)
}
}
-void
-uring_task_get(struct uring_task *task)
+void uring_task_get(struct uring_task *task)
{
assert_return(task);
- debug(DBG_REF, "task %s (%p), refcount %u -> %u",
- task->name, task, task->refcount, task->refcount + 1);
+ debug(DBG_REF, "task %s (%p), refcount %u -> %u", task->name, task,
+ task->refcount, task->refcount + 1);
if (task->refcount < 0)
error("Negative refcount!");
@@ -161,17 +158,16 @@ uring_task_get(struct uring_task *task)
task->refcount++;
}
-void
-uring_task_set_buf(struct uring_task *task, struct uring_task_buf *tbuf)
+void uring_task_set_buf(struct uring_task *task, struct uring_task_buf *tbuf)
{
assert_return(task && tbuf);
- debug(DBG_UR, "task %s (%p), buf %p, refcount %u",
- task->name, task, tbuf, task->refcount);
+ debug(DBG_UR, "task %s (%p), buf %p, refcount %u", task->name, task,
+ tbuf, task->refcount);
/* iov_len and msg_namelen are set at send/receive time */
tbuf->iov.iov_base = tbuf->buf;
- tbuf->msg.msg_name = &task->saddr.storage;
+ tbuf->msg.msg_name = &task->saddr.st;
tbuf->msg.msg_iov = &tbuf->iov;
tbuf->msg.msg_iovlen = 1;
tbuf->msg.msg_control = NULL;
@@ -180,24 +176,22 @@ uring_task_set_buf(struct uring_task *task, struct uring_task_buf *tbuf)
task->tbuf = tbuf;
}
-void
-uring_task_set_fd(struct uring_task *task, int fd)
+void uring_task_set_fd(struct uring_task *task, int fd)
{
assert_return(task);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, fd,
+ task->refcount);
task->fd = fd;
}
-void
-uring_task_close_fd(struct uring_task *task)
+void uring_task_close_fd(struct uring_task *task)
{
assert_return(task);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
if (task->fd < 0)
return;
@@ -206,17 +200,16 @@ uring_task_close_fd(struct uring_task *task)
task->fd = -1;
}
-struct uring_task *
-uring_parent()
+struct uring_task *uring_parent()
{
assert_die(cfg->uring, "invalid arguments");
return &cfg->uring->task;
}
-void
-uring_task_init(struct uring_task *task, const char *name,
- struct uring_task *parent, void (*free)(struct uring_task *))
+void uring_task_init(struct uring_task *task, const char *name,
+ struct uring_task *parent,
+ void (*free)(struct uring_task *))
{
static bool first = true;
@@ -236,38 +229,37 @@ uring_task_init(struct uring_task *task, const char *name,
task->tbuf = NULL;
if (task->parent) {
- debug(DBG_REF, "task %s (%p), refcount %u, "
+ debug(DBG_REF,
+ "task %s (%p), refcount %u, "
"getting parent %s (%p), refcount %u",
- task->name, task, task->refcount,
- task->parent->name, task->parent, task->parent->refcount);
+ task->name, task, task->refcount, task->parent->name,
+ task->parent, task->parent->refcount);
uring_task_get(task->parent);
}
}
-void
-uring_close(struct uring_task *task, int fd)
+void uring_close(struct uring_task *task, int fd)
{
struct io_uring_sqe *sqe;
assert_return(task && fd >= 0);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
- sqe = get_sqe(task);
+ sqe = get_sqe(task);
io_uring_prep_close(sqe, fd);
io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task | CQE_TYPE_CLOSE));
}
-static void
-uring_tbuf_write_cb(struct uring_task *task, int res)
+static void uring_tbuf_write_cb(struct uring_task *task, int res)
{
int r;
assert_return(task && task->tbuf && task->final_cb);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
if (res < 0) {
r = res;
@@ -282,52 +274,51 @@ uring_tbuf_write_cb(struct uring_task *task, int res)
}
uring_write(task, task->tbuf->buf + task->tbuf->done,
- task->tbuf->len - task->tbuf->done,
- uring_tbuf_write_cb);
+ task->tbuf->len - task->tbuf->done, uring_tbuf_write_cb);
return;
finished:
task->final_cb(task, r);
}
-void
-uring_tbuf_write(struct uring_task *task, utask_cb_t final_cb)
+void uring_tbuf_write(struct uring_task *task, utask_cb_t final_cb)
{
- assert_return(task && task->fd >= 0 && task->tbuf && task->tbuf->len > 0);
+ assert_return(task && task->fd >= 0 && task->tbuf &&
+ task->tbuf->len > 0);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
task->tbuf->done = 0;
task->final_cb = final_cb;
- uring_write(task, &task->tbuf->buf, task->tbuf->len, uring_tbuf_write_cb);
+ uring_write(task, &task->tbuf->buf, task->tbuf->len,
+ uring_tbuf_write_cb);
}
-void
-uring_write(struct uring_task *task, void *buf, size_t len, utask_cb_t cb)
+void uring_write(struct uring_task *task, void *buf, size_t len, utask_cb_t cb)
{
struct io_uring_sqe *sqe;
assert_return(task && buf && len > 0 && cb && task->fd >= 0);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
- sqe = get_sqe(task);
+ sqe = get_sqe(task);
task->cb = cb;
io_uring_prep_write(sqe, task->fd, buf, len, 0);
io_uring_sqe_set_data(sqe, task);
}
-static void
-uring_tbuf_read_until_cb(struct uring_task *task, int res)
+static void uring_tbuf_read_until_cb(struct uring_task *task, int res)
{
int r;
- assert_return(task && task->tbuf && task->final_cb && task->is_complete_cb);
+ assert_return(task && task->tbuf && task->final_cb &&
+ task->is_complete_cb);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
if (res < 0) {
r = res;
@@ -359,14 +350,14 @@ finished:
task->final_cb(task, r);
}
-void
-uring_tbuf_read_until(struct uring_task *task,
- rutask_cb_t is_complete_cb, utask_cb_t final_cb)
+void uring_tbuf_read_until(struct uring_task *task, rutask_cb_t is_complete_cb,
+ utask_cb_t final_cb)
{
- assert_return(task && task->fd >= 0 && task->tbuf && is_complete_cb && final_cb);
+ assert_return(task && task->fd >= 0 && task->tbuf && is_complete_cb &&
+ final_cb);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
task->tbuf->len = 0;
task->is_complete_cb = is_complete_cb;
@@ -375,8 +366,7 @@ uring_tbuf_read_until(struct uring_task *task,
uring_tbuf_read_until_cb);
}
-static int
-uring_tbuf_eof(struct uring_task *task, int res)
+static int uring_tbuf_eof(struct uring_task *task, int res)
{
assert_return(task && task->tbuf, -EINVAL);
assert_task_alive_or(DBG_UR, task, return -EINTR);
@@ -392,17 +382,14 @@ uring_tbuf_eof(struct uring_task *task, int res)
return 1;
}
-void
-uring_tbuf_read_until_eof(struct uring_task *task,
- utask_cb_t final_cb)
+void uring_tbuf_read_until_eof(struct uring_task *task, utask_cb_t final_cb)
{
assert_return(task && task->tbuf && final_cb);
uring_tbuf_read_until(task, uring_tbuf_eof, final_cb);
}
-static int
-uring_tbuf_have_data(struct uring_task *task, int res)
+static int uring_tbuf_have_data(struct uring_task *task, int res)
{
assert_return(task, -EINVAL);
@@ -412,23 +399,22 @@ uring_tbuf_have_data(struct uring_task *task, int res)
return 1;
}
-void
-uring_tbuf_read(struct uring_task *task, utask_cb_t final_cb)
+void uring_tbuf_read(struct uring_task *task, utask_cb_t final_cb)
{
assert_return(task && final_cb);
uring_tbuf_read_until(task, uring_tbuf_have_data, final_cb);
}
-void
-uring_read_offset(struct uring_task *task, void *buf, size_t len, off_t offset, utask_cb_t cb)
+void uring_read_offset(struct uring_task *task, void *buf, size_t len,
+ off_t offset, utask_cb_t cb)
{
struct io_uring_sqe *sqe;
assert_return(task && buf && len > 0 && task->fd >= 0);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
sqe = get_sqe(task);
task->cb = cb;
@@ -436,15 +422,14 @@ uring_read_offset(struct uring_task *task, void *buf, size_t len, off_t offset,
io_uring_sqe_set_data(sqe, task);
}
-void
-uring_openat(struct uring_task *task, const char *path, utask_cb_t cb)
+void uring_openat(struct uring_task *task, const char *path, utask_cb_t cb)
{
struct io_uring_sqe *sqe;
-
+
assert_return(task && !empty_str(path) && cb);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
sqe = get_sqe(task);
task->cb = cb;
@@ -452,15 +437,14 @@ uring_openat(struct uring_task *task, const char *path, utask_cb_t cb)
io_uring_sqe_set_data(sqe, task);
}
-void
-uring_tbuf_recvmsg(struct uring_task *task, utask_cb_t cb)
+void uring_tbuf_recvmsg(struct uring_task *task, utask_cb_t cb)
{
struct io_uring_sqe *sqe;
assert_return(task && task->fd >= 0 && task->tbuf && cb);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
sqe = get_sqe(task);
task->tbuf->done = 0;
@@ -472,15 +456,14 @@ uring_tbuf_recvmsg(struct uring_task *task, utask_cb_t cb)
io_uring_sqe_set_data(sqe, task);
}
-void
-uring_tbuf_sendmsg(struct uring_task *task, utask_cb_t cb)
+void uring_tbuf_sendmsg(struct uring_task *task, utask_cb_t cb)
{
struct io_uring_sqe *sqe;
assert_return(task && task->fd >= 0 && task->tbuf && cb);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
sqe = get_sqe(task);
task->tbuf->done = 0;
@@ -491,44 +474,43 @@ uring_tbuf_sendmsg(struct uring_task *task, utask_cb_t cb)
io_uring_sqe_set_data(sqe, task);
}
-void
-uring_connect(struct uring_task *task, struct saddr *saddr, utask_cb_t cb)
+void uring_connect(struct uring_task *task, struct saddr *saddr, utask_cb_t cb)
{
struct io_uring_sqe *sqe;
assert_return(task && task->fd >= 0 && saddr && cb);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
sqe = get_sqe(task);
task->cb = cb;
- io_uring_prep_connect(sqe, task->fd, (struct sockaddr *)&saddr->storage, saddr->addrlen);
+ io_uring_prep_connect(sqe, task->fd, (struct sockaddr *)&saddr->st,
+ saddr->addrlen);
io_uring_sqe_set_data(sqe, task);
}
-void
-uring_accept(struct uring_task *task, struct saddr *saddr, utask_cb_t cb)
+void uring_accept(struct uring_task *task, struct saddr *saddr, utask_cb_t cb)
{
struct io_uring_sqe *sqe;
assert_return(task && task->fd >= 0 && saddr && cb);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
sqe = get_sqe(task);
- saddr->addrlen = sizeof(saddr->storage);
+ saddr->addrlen = sizeof(saddr->st);
task->cb = cb;
- io_uring_prep_accept(sqe, task->fd, (struct sockaddr *)&saddr->storage, &saddr->addrlen, SOCK_CLOEXEC);
+ io_uring_prep_accept(sqe, task->fd, (struct sockaddr *)&saddr->st,
+ &saddr->addrlen, SOCK_CLOEXEC);
io_uring_sqe_set_data(sqe, task);
}
-void
-uring_splice(struct uring_task *task, int fd_in, int fd_out, utask_cb_t cb)
+void uring_splice(struct uring_task *task, int fd_in, int fd_out, utask_cb_t cb)
{
struct io_uring_sqe *sqe;
-
+
assert_return(task && fd_in >= 0 && fd_out >= 0 && cb);
debug(DBG_UR, "task %s (%p), fd_in %i, fd_out %i, refcount %u",
@@ -540,15 +522,14 @@ uring_splice(struct uring_task *task, int fd_in, int fd_out, utask_cb_t cb)
io_uring_sqe_set_data(sqe, task);
}
-void
-uring_poll(struct uring_task *task, short poll_mask, utask_cb_t cb)
+void uring_poll(struct uring_task *task, short poll_mask, utask_cb_t cb)
{
struct io_uring_sqe *sqe;
-
+
assert_return(task && task->fd >= 0 && poll_mask && cb);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
sqe = get_sqe(task);
task->cb = cb;
@@ -556,11 +537,10 @@ uring_poll(struct uring_task *task, short poll_mask, utask_cb_t cb)
io_uring_sqe_set_data(sqe, task);
}
-void
-uring_poll_cancel(struct uring_task *task)
+void uring_poll_cancel(struct uring_task *task)
{
struct io_uring_sqe *sqe;
-
+
assert_return(task);
if (task->fd < 0) {
@@ -568,54 +548,51 @@ uring_poll_cancel(struct uring_task *task)
return;
}
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
sqe = get_sqe(task);
task->dead = true;
io_uring_prep_poll_remove(sqe, task);
- io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task | CQE_TYPE_POLL_CANCEL));
+ io_uring_sqe_set_data(sqe,
+ (void *)((uintptr_t)task | CQE_TYPE_POLL_CANCEL));
}
-static void
-uring_free(struct uring_task *task)
+static void uring_free(struct uring_task *task)
{
struct uring_ev *uring = container_of(task, struct uring_ev, task);
assert_return(task);
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
io_uring_queue_exit(&uring->uring);
cfg->uring = NULL;
xfree(uring);
}
-void
-uring_refdump()
+void uring_refdump()
{
assert_return(cfg->uring);
uring_task_refdump(&cfg->uring->task);
}
-void
-uring_delete()
+void uring_delete()
{
struct uring_task *task;
assert_return(cfg->uring);
task = &cfg->uring->task;
- debug(DBG_UR, "task %s (%p), fd %i, refcount %u",
- task->name, task, task->fd, task->refcount);
+ debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task,
+ task->fd, task->refcount);
uring_task_put(task);
}
-static void
-uring_splice_test_cb(struct uring_task *task, int res)
+static void uring_splice_test_cb(struct uring_task *task, int res)
{
struct uring_ev *uring = container_of(task, struct uring_ev, task);
@@ -634,12 +611,11 @@ uring_splice_test_cb(struct uring_task *task, int res)
debug(DBG_UR, "splice supported");
} else if (res == -EINVAL)
debug(DBG_UR, "splice not supported");
- else
+ else
error("splice check failed: %i\n", res);
}
-void
-uring_init()
+void uring_init()
{
struct uring_ev *uring;
@@ -649,7 +625,8 @@ uring_init()
if (!uring)
die("malloc: %m");
- if (io_uring_queue_init_params(4096, &uring->uring, &uring->uring_params) < 0)
+ if (io_uring_queue_init_params(4096, &uring->uring,
+ &uring->uring_params) < 0)
die("io_uring_queue_init_params");
debug(DBG_UR, "uring initialized, features: 0x%08x",
@@ -665,28 +642,23 @@ uring_init()
uring->tfd = open("/dev/null", O_RDONLY | O_CLOEXEC | O_NOCTTY);
if (uring->tfd < 0)
die("open(\"/dev/null\"): %m");
- uring_splice(&uring->task, uring->tfd, uring->pipe[PIPE_WR], uring_splice_test_cb);
+ uring_splice(&uring->task, uring->tfd, uring->pipe[PIPE_WR],
+ uring_splice_test_cb);
}
-static inline void
-uring_print_cqe(const char *type, struct uring_task *task,
- struct io_uring_cqe *cqe)
+static inline void uring_print_cqe(const char *type, struct uring_task *task,
+ struct io_uring_cqe *cqe)
{
assert_return(!empty_str(type) && task && cqe);
- debug(DBG_UR, "got CQE "
+ debug(DBG_UR,
+ "got CQE "
"(type: %s, res: %i (%s), task: %s (%p), fd: %i, cb: %p)",
- type,
- cqe->res,
- cqe->res < 0 ? strerror(-cqe->res) : "ok",
- task->name ? task->name : "<none>",
- task,
- task->fd,
- task->cb);
+ type, cqe->res, cqe->res < 0 ? strerror(-cqe->res) : "ok",
+ task->name ? task->name : "<none>", task, task->fd, task->cb);
}
-void
-uring_event_loop()
+void uring_event_loop()
{
while (true) {
struct io_uring_cqe *cqe;
@@ -703,7 +675,8 @@ uring_event_loop()
}
nr = 0;
- io_uring_for_each_cqe(&cfg->uring->uring, head, cqe) {
+ io_uring_for_each_cqe(&cfg->uring->uring, head, cqe)
+ {
struct uring_task *task = io_uring_cqe_get_data(cqe);
bool do_cb;
enum cqe_type cqe_type;
@@ -755,4 +728,3 @@ uring_event_loop()
io_uring_cq_advance(&cfg->uring->uring, nr);
}
}
-
diff --git a/minecproxy/uring.h b/minecproxy/uring.h
index 9c33104..c465904 100644
--- a/minecproxy/uring.h
+++ b/minecproxy/uring.h
@@ -30,18 +30,18 @@ void uring_tbuf_write(struct uring_task *task, utask_cb_t final_cb);
void uring_write(struct uring_task *task, void *buf, size_t len, utask_cb_t cb);
-void uring_tbuf_read_until(struct uring_task *task,
- rutask_cb_t is_complete_cb, utask_cb_t final_cb);
+void uring_tbuf_read_until(struct uring_task *task, rutask_cb_t is_complete_cb,
+ utask_cb_t final_cb);
void uring_tbuf_read_until_eof(struct uring_task *task, utask_cb_t final_cb);
void uring_tbuf_read(struct uring_task *task, utask_cb_t final_cb);
-void uring_read_offset(struct uring_task *task, void *buf,
- size_t len, off_t offset, utask_cb_t cb);
+void uring_read_offset(struct uring_task *task, void *buf, size_t len,
+ off_t offset, utask_cb_t cb);
-static inline void
-uring_read(struct uring_task *task, void *buf, size_t len, utask_cb_t cb)
+static inline void uring_read(struct uring_task *task, void *buf, size_t len,
+ utask_cb_t cb)
{
uring_read_offset(task, buf, len, 0, cb);
}
@@ -56,7 +56,8 @@ void uring_connect(struct uring_task *task, struct saddr *saddr, utask_cb_t cb);
void uring_accept(struct uring_task *task, struct saddr *saddr, utask_cb_t cb);
-void uring_splice(struct uring_task *task, int fd_in, int fd_out, utask_cb_t cb);
+void uring_splice(struct uring_task *task, int fd_in, int fd_out,
+ utask_cb_t cb);
void uring_poll(struct uring_task *task, short poll_mask, utask_cb_t cb);