diff options
author | David Härdeman <david@hardeman.nu> | 2020-06-28 14:58:32 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2020-06-28 14:58:32 +0200 |
commit | d3352b997ca59a336a40fe6660c6e5b7079864aa (patch) | |
tree | f365d6c0304e512057223e9a3cf37357da44a443 | |
parent | 99b2c70137fef05a5a18f439b9010ddba455f5cb (diff) |
Run clang-format on source tree (excl igmp.c)
43 files changed, 1408 insertions, 1332 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..32ca6ad --- /dev/null +++ b/.clang-format @@ -0,0 +1,190 @@ +# +# Configuration for clang-format 11.0. +# +UseTab: Always +TabWidth: 8 +IndentWidth: 8 +ContinuationIndentWidth: 8 +ColumnLimit: 80 + +# C Language specifics +Language: Cpp + +# Align parameters on the open bracket +# someLongFunction(argument1, +# argument2); +AlignAfterOpenBracket: Align + +# Don't align consecutive assignments +# int aaaa = 12; +# int b = 14; +AlignConsecutiveAssignments: false + +# Don't align consecutive declarations +# int aaaa = 12; +# double b = 3.14; +AlignConsecutiveDeclarations: false + +# Align consecutive macros +# #define SHORT_NAME 42 +# #define LONGER_NAME 0x007f +# #define EVEN_LONGER_NAME (2) +# #define foo(x) (x * x) +# #define bar(y, z) (y + z) +AlignConsecutiveMacros: true + +# Align escaped newlines as far right as possible +# #define A \ +# int aaaa; \ +# int b; \ +# int dddddddddd; +AlignEscapedNewlines: Right + +# Align operands of binary and ternary expressions +# int aaa = bbbbbbbbbbb + +# cccccc; +AlignOperands: true + +# Don't align trailing comments +# int a; // Comment a +# int b = 2; // Comment b +AlignTrailingComments: false + +# By default don't allow putting parameters onto the next line +# myFunction(foo, bar, baz); +AllowAllParametersOfDeclarationOnNextLine: false + +# Don't allow short braced statements to be on a single line +# if (a) not if (a) return; +# return; +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false + +# By default don't add a line break after the return type of top-level functions +# int foo(); +AlwaysBreakAfterReturnType: None + +# Pack as many parameters or arguments onto the same line as possible +# int myFunction(int aaaaaaaaaaaa, int bbbbbbbb, +# int cccc); +BinPackArguments: true +BinPackParameters: true + +# Attach braces to surrounding context except break before braces on function +# definitions. +# void foo() +# { +# if (true) { +# } else { +# } +# }; +BreakBeforeBraces: Linux + +# Break after operators +# int valuve = aaaaaaaaaaaaa + +# bbbbbb - +# ccccccccccc; +BreakBeforeBinaryOperators: None +BreakBeforeTernaryOperators: false + +# Don't break string literals +BreakStringLiterals: false + +# Use the same indentation level as for the switch statement. +# Switch statement body is always indented one level more than case labels. +IndentCaseLabels: false + +# Don't indent a function definition or declaration if it is wrapped after the +# type +IndentWrappedFunctionNames: false + +# Align pointer to the right +# int *a; +PointerAlignment: Right + +# Don't insert a space after a cast +# x = (int32)y; not x = (int32) y; +SpaceAfterCStyleCast: false + +# Insert spaces before and after assignment operators +# int a = 5; not int a=5; +# a += 42; a+=42; +SpaceBeforeAssignmentOperators: true + +# Put a space before opening parentheses only after control statement keywords. +# void f() { +# if (true) { +# f(); +# } +# } +SpaceBeforeParens: ControlStatementsExceptForEachMacros + +# Don't insert spaces inside empty '()' +SpaceInEmptyParentheses: false + +# The number of spaces before trailing line comments (// - comments). +# This does not affect trailing block comments (/* - comments). +SpacesBeforeTrailingComments: 1 + +# Don't insert spaces in casts +# x = (int32) y; not x = ( int32 ) y; +SpacesInCStyleCastParentheses: false + +# Don't insert spaces inside container literals +# var arr = [1, 2, 3]; not var arr = [ 1, 2, 3 ]; +SpacesInContainerLiterals: false + +# Don't insert spaces after '(' or before ')' +# f(arg); not f( arg ); +SpacesInParentheses: false + +# Don't insert spaces after '[' or before ']' +# int a[5]; not int a[ 5 ]; +SpacesInSquareBrackets: false + +# Insert a space after '{' and before '}' in struct initializers +Cpp11BracedListStyle: false + +# A list of macros that should be interpreted as foreach loops instead of as +# function calls. Taken from: +# git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \ +# | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$, - '\1'," \ +# | sort | uniq +ForEachMacros: + - 'list_for_each' + - 'list_for_each_continue' + - 'list_for_each_prev' + - 'list_for_each_safe' + - 'list_for_each_prev_safe' + - 'list_for_each_entry' + - 'list_for_each_entry_reverse' + - 'list_for_each_entry_continue' + - 'list_for_each_entry_continue_reverse' + - 'list_for_each_entry_from' + - 'list_for_each_entry_from_reverse' + - 'list_for_each_entry_safe' + - 'list_for_each_entry_safe_continue' + - 'list_for_each_entry_safe_from' + - 'list_for_each_entry_safe_reverse' + +# The maximum number of consecutive empty lines to keep. +MaxEmptyLinesToKeep: 1 + +# No empty line at the start of a block. +KeepEmptyLinesAtTheStartOfBlocks: false + +# Penalties +# This decides what order things should be done if a line is too long +PenaltyBreakAssignment: 10 +PenaltyBreakBeforeFirstCallParameter: 30 +PenaltyBreakComment: 10 +PenaltyBreakFirstLessLess: 0 +PenaltyBreakString: 10 +PenaltyExcessCharacter: 100 +PenaltyReturnTypeOnItsOwnLine: 60 + +# Don't sort #include's +SortIncludes: false diff --git a/minecctl/mc-commands.c b/minecctl/mc-commands.c index 0ac20d0..d4d2846 100644 --- a/minecctl/mc-commands.c +++ b/minecctl/mc-commands.c @@ -7,8 +7,7 @@ #include "misc.h" #include "mc-protocol.h" -bool -do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max) +bool do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max) { struct server *server; struct saddr *saddr; @@ -24,7 +23,7 @@ do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max) if (fd < 0) { error("%s: unable to connect", server->name); return false; - } + } /* FIXME: connect_any needs to indicate the address it used */ saddr = list_first_entry(&server->mc_addrs, struct saddr, list); @@ -33,7 +32,8 @@ do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max) goto out; } - if (!mc_protocol_create_status_request(buf, sizeof(buf), &plen, saddr)) { + if (!mc_protocol_create_status_request(buf, sizeof(buf), &plen, + saddr)) { error("Failed to create req"); goto out; } @@ -64,7 +64,7 @@ do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max) break; } } - + if (!mc_protocol_parse_status_reply(buf, off, online, max)) { error("Failed to get player count"); return false; @@ -74,4 +74,3 @@ out: close(fd); return rv; } - diff --git a/minecctl/minecctl-commands.h b/minecctl/minecctl-commands.h index 444204f..2805a51 100644 --- a/minecctl/minecctl-commands.h +++ b/minecctl/minecctl-commands.h @@ -16,35 +16,41 @@ enum commands { static struct command_list { const char *name; enum commands cmd; -} command_list[] = { - { - .name = "list", - .cmd = CMD_LIST, - }, { - .name = "status", - .cmd = CMD_STATUS, - }, { - .name = "ping", - .cmd = CMD_PING, - }, { - .name = "stop", - .cmd = CMD_STOP, - }, { - .name = "stopall", - .cmd = CMD_STOPALL, - }, { - .name = "pcount", - .cmd = CMD_PCOUNT, - }, { - .name = "cmd", - .cmd = CMD_COMMAND, - }, { - .name = "console", - .cmd = CMD_CONSOLE, - }, { - .name = NULL, - .cmd = CMD_INVALID, - } -}; +} command_list[] = { { + .name = "list", + .cmd = CMD_LIST, + }, + { + .name = "status", + .cmd = CMD_STATUS, + }, + { + .name = "ping", + .cmd = CMD_PING, + }, + { + .name = "stop", + .cmd = CMD_STOP, + }, + { + .name = "stopall", + .cmd = CMD_STOPALL, + }, + { + .name = "pcount", + .cmd = CMD_PCOUNT, + }, + { + .name = "cmd", + .cmd = CMD_COMMAND, + }, + { + .name = "console", + .cmd = CMD_CONSOLE, + }, + { + .name = NULL, + .cmd = CMD_INVALID, + } }; #endif diff --git a/minecctl/minecctl.c b/minecctl/minecctl.c index b93f87b..5ac3942 100644 --- a/minecctl/minecctl.c +++ b/minecctl/minecctl.c @@ -22,8 +22,7 @@ #include "config-parser.h" #include "config.h" -static void -dump_config(struct cfg *cfg) +static void dump_config(struct cfg *cfg) { /* FIXME: return unless debugging enabled */ struct server *server; @@ -44,7 +43,8 @@ dump_config(struct cfg *cfg) info(" name : %s", server->name); info(" filename : %s", server->filename); info(" rcon_password : %s", server->rcon_password); - info(" file_read : %s", server->file_read ? "yes" : "no"); + info(" file_read : %s", + server->file_read ? "yes" : "no"); list_for_each_entry(saddr, &server->rcon_addrs, list) info(" * rcon addr : %s", saddr->addrstr); list_for_each_entry(saddr, &server->mc_addrs, list) @@ -54,8 +54,7 @@ dump_config(struct cfg *cfg) info("============="); } -_noreturn_ static void -usage(bool no_error) +_noreturn_ static void usage(bool no_error) { info("Usage: %s [OPTION...] COMMAND\n" "\n" @@ -92,8 +91,7 @@ usage(bool no_error) exit(no_error ? EXIT_FAILURE : EXIT_SUCCESS); } -static bool -str_to_addrs(const char *str, struct list_head *list) +static bool str_to_addrs(const char *str, struct list_head *list) { struct cfg_value value; char *tmp = NULL; @@ -124,8 +122,7 @@ out: return rv; } -static bool -create_server_from_cmdline_args(struct cfg *cfg) +static bool create_server_from_cmdline_args(struct cfg *cfg) { struct server *server; @@ -167,8 +164,8 @@ error: return false; } -static inline void -get_optional_server_arg(struct cfg *cfg, char * const **argv, bool more) +static inline void get_optional_server_arg(struct cfg *cfg, char *const **argv, + bool more) { if (!cfg->rcon_addrstr) { if (!**argv) { @@ -177,7 +174,8 @@ get_optional_server_arg(struct cfg *cfg, char * const **argv, bool more) } if (!server_set_default(cfg, **argv)) { - error("\"%s\" is not a known server or command", **argv); + error("\"%s\" is not a known server or command", + **argv); usage(false); } @@ -190,8 +188,7 @@ get_optional_server_arg(struct cfg *cfg, char * const **argv, bool more) } } -static void -parse_command(struct cfg *cfg, char * const *argv) +static void parse_command(struct cfg *cfg, char *const *argv) { enum commands cmd = CMD_INVALID; @@ -280,8 +277,7 @@ parse_command(struct cfg *cfg, char * const *argv) } } -static void -parse_cmdline(struct cfg *cfg, int argc, char * const *argv) +static void parse_cmdline(struct cfg *cfg, int argc, char *const *argv) { int c; char *e; @@ -299,6 +295,7 @@ parse_cmdline(struct cfg *cfg, int argc, char * const *argv) while (true) { int option_index = 0; + /* clang-format off */ static struct option long_options[] = { { "rcon-password", required_argument, 0, 'p' }, { "rcon-address", required_argument, 0, 'r' }, @@ -309,9 +306,10 @@ parse_cmdline(struct cfg *cfg, int argc, char * const *argv) { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 } }; + /* clang-format on */ - c = getopt_long(argc, argv, ":p:r:m:c:vfh", - long_options, &option_index); + c = getopt_long(argc, argv, ":p:r:m:c:vfh", long_options, + &option_index); if (c == -1) break; @@ -362,8 +360,7 @@ parse_cmdline(struct cfg *cfg, int argc, char * const *argv) } } -int -main(int argc, char **argv) +int main(int argc, char **argv) { struct cfg cfg = { .servers = LIST_HEAD_INIT(cfg.servers), @@ -401,4 +398,3 @@ out: xfree(cfg.cmdstr); exit(success ? EXIT_SUCCESS : EXIT_FAILURE); } - diff --git a/minecctl/minecctl.h b/minecctl/minecctl.h index 60b71b4..259ac52 100644 --- a/minecctl/minecctl.h +++ b/minecctl/minecctl.h @@ -16,4 +16,3 @@ struct cfg { }; #endif - diff --git a/minecctl/misc-commands.c b/minecctl/misc-commands.c index c64a005..52797da 100644 --- a/minecctl/misc-commands.c +++ b/minecctl/misc-commands.c @@ -5,8 +5,7 @@ #include "rcon-commands.h" #include "mc-commands.h" -bool -do_list(struct cfg *cfg) +bool do_list(struct cfg *cfg) { struct server *server; @@ -18,8 +17,7 @@ do_list(struct cfg *cfg) return true; } -bool -do_pcount(struct cfg *cfg) +bool do_pcount(struct cfg *cfg) { unsigned x, y; diff --git a/minecctl/misc-commands.h b/minecctl/misc-commands.h index 8270601..1a4363f 100644 --- a/minecctl/misc-commands.h +++ b/minecctl/misc-commands.h @@ -6,4 +6,3 @@ bool do_list(struct cfg *cfg); bool do_pcount(struct cfg *cfg); #endif - diff --git a/minecctl/misc.c b/minecctl/misc.c index 72eb03c..8f0761f 100644 --- a/minecctl/misc.c +++ b/minecctl/misc.c @@ -11,8 +11,7 @@ bool use_colors = false; /* FIXME: Can be shared */ -void -set_use_colors() +void set_use_colors() { int fd; const char *e; @@ -37,8 +36,7 @@ set_use_colors() use_colors = true; } -char * -strv_join(char * const *strv) +char *strv_join(char *const *strv) { size_t len = 0; char *r, *to; @@ -61,8 +59,7 @@ strv_join(char * const *strv) return r; } -int -connect_any(struct list_head *addrs, bool may_fail) +int connect_any(struct list_head *addrs, bool may_fail) { struct saddr *saddr; bool connected = false; @@ -78,13 +75,15 @@ connect_any(struct list_head *addrs, bool may_fail) list_for_each_entry(saddr, addrs, list) { verbose("Attempting connection to %s", saddr->addrstr); - sfd = socket(saddr->storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0); + sfd = socket(saddr->st.ss_family, SOCK_STREAM | SOCK_CLOEXEC, + 0); if (sfd < 0) die("socket: %m"); socket_set_low_latency(sfd, true, true, true); - if (connect(sfd, (struct sockaddr *)&saddr->storage, saddr->addrlen) < 0) { + if (connect(sfd, (struct sockaddr *)&saddr->st, + saddr->addrlen) < 0) { close(sfd); continue; } @@ -101,8 +100,7 @@ connect_any(struct list_head *addrs, bool may_fail) return sfd; } -char * -ask_password() +char *ask_password() { struct termios old, new; char *password = NULL; @@ -139,8 +137,7 @@ ask_password() return password; } -void -free_password(char **password) +void free_password(char **password) { if (!password || !*password) return; @@ -150,8 +147,7 @@ free_password(char **password) *password = NULL; } -void -__debug(_unused_ enum debug_lvl lvl, const char *fmt, ...) +void __debug(_unused_ enum debug_lvl lvl, const char *fmt, ...) { va_list ap; @@ -170,8 +166,7 @@ __debug(_unused_ enum debug_lvl lvl, const char *fmt, ...) fprintf(stderr, ANSI_NORMAL); } -_noreturn_ void -__die(const char *fmt, ...) +_noreturn_ void __die(const char *fmt, ...) { va_list ap; @@ -182,8 +177,7 @@ __die(const char *fmt, ...) exit(EXIT_FAILURE); } -void * -__zmalloc(const char *fn, int line, size_t size) +void *__zmalloc(const char *fn, int line, size_t size) { void *ptr; @@ -195,12 +189,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) @@ -208,12 +202,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) @@ -221,11 +215,9 @@ __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) { assert_die(!empty_str(fn) && line > 0, "invalid arguments"); free(ptr); } - diff --git a/minecctl/misc.h b/minecctl/misc.h index 155681e..6abed5e 100644 --- a/minecctl/misc.h +++ b/minecctl/misc.h @@ -5,7 +5,7 @@ extern bool use_colors; void set_use_colors(); -char *strv_join(char * const *strv); +char *strv_join(char *const *strv); int connect_any(struct list_head *addrs, bool may_fail); diff --git a/minecctl/rcon-commands.c b/minecctl/rcon-commands.c index cf43c5e..b6a216f 100644 --- a/minecctl/rcon-commands.c +++ b/minecctl/rcon-commands.c @@ -16,8 +16,7 @@ #include "rcon-protocol.h" #include "misc.h" -static void -send_packet(int sfd, const char *buf, size_t len) +static void send_packet(int sfd, const char *buf, size_t len) { size_t off = 0; ssize_t r; @@ -38,8 +37,8 @@ send_packet(int sfd, const char *buf, size_t len) } /* Note: msg is null-terminated due to the mc protocol trailer */ -static void -read_packet(int sfd, char *buf, size_t len, int32_t *id, int32_t *type, const char **msg) +static void read_packet(int sfd, char *buf, size_t len, int32_t *id, + int32_t *type, const char **msg) { size_t off = 0; ssize_t r; @@ -69,16 +68,16 @@ read_packet(int sfd, char *buf, size_t len, int32_t *id, int32_t *type, const ch die("Failed to parse response: %s", error); } -static void -send_msg(int sfd, char *buf, size_t len, enum rcon_packet_type type, - const char *msg, enum rcon_packet_type *rtype, const char **reply) +static void send_msg(int sfd, char *buf, size_t len, enum rcon_packet_type type, + const char *msg, enum rcon_packet_type *rtype, + const char **reply) { static uint32_t rcon_packet_id = 1; size_t plen; int32_t id; - if (!rcon_protocol_create_packet(buf, len, &plen, - rcon_packet_id, type, msg)) + if (!rcon_protocol_create_packet(buf, len, &plen, rcon_packet_id, type, + msg)) die("Failed to create rcon packet"); send_packet(sfd, buf, plen); @@ -102,8 +101,7 @@ send_msg(int sfd, char *buf, size_t len, enum rcon_packet_type type, rcon_packet_id++; } -static int -rcon_login(struct cfg *cfg, struct server *server) +static int rcon_login(struct cfg *cfg, struct server *server) { char buf[4096]; int32_t rtype; @@ -121,7 +119,7 @@ rcon_login(struct cfg *cfg, struct server *server) if (fd < 0) { error("%s: unable to connect", server->name); goto error; - } + } if (!server->rcon_password) server->rcon_password = ask_password(); @@ -154,21 +152,20 @@ error: return -1; } -static bool -send_cmd(int sfd, const char *cmd) +static bool send_cmd(int sfd, const char *cmd) { char buf[4096]; int32_t rtype; const char *reply; - send_msg(sfd, buf, sizeof(buf), RCON_PACKET_COMMAND, cmd, &rtype, &reply); + send_msg(sfd, buf, sizeof(buf), RCON_PACKET_COMMAND, cmd, &rtype, + &reply); if (rtype != RCON_PACKET_RESPONSE) { die("Invalid return code: %" PRIi32, rtype); return false; } - if (use_colors) info("%s%s%s", ANSI_GREY, reply, ANSI_NORMAL); else @@ -177,13 +174,12 @@ send_cmd(int sfd, const char *cmd) return true; } -static void -eat_whitespace(char **pos) +static void eat_whitespace(char **pos) { char *end; size_t len; - while(isspace(**pos)) + while (isspace(**pos)) (*pos)++; len = strlen(*pos); @@ -203,24 +199,24 @@ eat_whitespace(char **pos) #define MCTIME_PER_HOUR 1000 #define MIN_PER_HOUR 60 -static inline unsigned -mctime_days(unsigned mctime) { +static inline unsigned mctime_days(unsigned mctime) +{ return (mctime / MCTIME_PER_DAY); } -static inline unsigned -mctime_hh(unsigned mctime) { +static inline unsigned mctime_hh(unsigned mctime) +{ return (mctime % MCTIME_PER_DAY) / MCTIME_PER_HOUR; } -static inline unsigned -mctime_mm(unsigned mctime) { +static inline unsigned mctime_mm(unsigned mctime) +{ return ((mctime % MCTIME_PER_HOUR) * MIN_PER_HOUR) / MCTIME_PER_HOUR; } -static bool -get_one_status(int fd, char *buf, size_t len, const char *cmd, - size_t argc, const char *replyscan, const char **reply, ...) +static bool get_one_status(int fd, char *buf, size_t len, const char *cmd, + size_t argc, const char *replyscan, + const char **reply, ...) { int32_t rtype; va_list ap; @@ -240,8 +236,8 @@ get_one_status(int fd, char *buf, size_t len, const char *cmd, return false; } -bool -do_status(struct cfg *cfg) { +bool do_status(struct cfg *cfg) +{ char buf[4096]; char tbuf[4096]; const char *reply; @@ -256,17 +252,17 @@ do_status(struct cfg *cfg) { if (fd < 0) return false; - if (get_one_status(fd, buf, sizeof(buf), "seed", 1, - "Seed : [ %[^]]]", &reply, tbuf)) + if (get_one_status(fd, buf, sizeof(buf), "seed", 1, "Seed : [ %[^]]]", + &reply, tbuf)) info("Seed: %s", tbuf); - + if (get_one_status(fd, buf, sizeof(buf), "difficulty", 1, "The difficulty is %s", &reply, tbuf)) info("Difficulty: %s", tbuf); if (get_one_status(fd, buf, sizeof(buf), "list", 2, - "There are %u of a max %u players online", - &reply, &cplayers, &maxplayers)) + "There are %u of a max %u players online", &reply, + &cplayers, &maxplayers)) info("Players: %u/%u", cplayers, maxplayers); if (get_one_status(fd, buf, sizeof(buf), "time query day", 1, @@ -275,20 +271,23 @@ do_status(struct cfg *cfg) { if (get_one_status(fd, buf, sizeof(buf), "time query gametime", 1, "The time is %u", &reply, >ime)) - info("World age: %ud:%02uh:%02um", - mctime_days(gtime), mctime_hh(gtime), mctime_mm(gtime)); + info("World age: %ud:%02uh:%02um", mctime_days(gtime), + mctime_hh(gtime), mctime_mm(gtime)); if (get_one_status(fd, buf, sizeof(buf), "time query daytime", 1, "The time is %u", &reply, >ime)) info("Current in-game time: %02uh:%02um", - mctime_hh(gtime + MCTIME_OFFSET), mctime_mm(gtime + MCTIME_OFFSET)); + mctime_hh(gtime + MCTIME_OFFSET), + mctime_mm(gtime + MCTIME_OFFSET)); if (get_one_status(fd, buf, sizeof(buf), "datapack list enabled", 2, - "There are %u data packs enabled: %[^\n]", &reply, &epacks, tbuf)) + "There are %u data packs enabled: %[^\n]", &reply, + &epacks, tbuf)) info("Enabled data packs (%u): %s", epacks, tbuf); if (get_one_status(fd, buf, sizeof(buf), "datapack list available", 2, - "There are %u data packs available : %[^\n]", &reply, &apacks, tbuf)) + "There are %u data packs available : %[^\n]", &reply, + &apacks, tbuf)) info("Available data packs (%u): %s", apacks, tbuf); else if (streq(reply, "There are no more data packs available")) info("Available data packs: none"); @@ -308,22 +307,21 @@ do_status(struct cfg *cfg) { return true; } -bool -do_ping(_unused_ struct cfg *cfg) { +bool do_ping(_unused_ struct cfg *cfg) +{ die("Not implemented"); return false; } -static bool -get_player_count(int fd, unsigned *current, unsigned *max) +static bool get_player_count(int fd, unsigned *current, unsigned *max) { char buf[4096]; const char *reply; unsigned c, m; if (!get_one_status(fd, buf, sizeof(buf), "list", 2, - "There are %u of a max %u players online", - &reply, &c, &m)) + "There are %u of a max %u players online", &reply, + &c, &m)) return false; if (current) @@ -335,8 +333,7 @@ get_player_count(int fd, unsigned *current, unsigned *max) return true; } -static bool -stop_one_server(struct cfg *cfg, struct server *server) +static bool stop_one_server(struct cfg *cfg, struct server *server) { int fd; bool rv; @@ -366,16 +363,16 @@ stop_one_server(struct cfg *cfg, struct server *server) return rv; } -bool -do_stop(struct cfg *cfg) { +bool do_stop(struct cfg *cfg) +{ struct server *server; server = server_get_default(cfg); return stop_one_server(cfg, server); } -bool -do_stop_all(struct cfg *cfg) { +bool do_stop_all(struct cfg *cfg) +{ struct server *server; list_for_each_entry(server, &cfg->servers, list) { @@ -386,8 +383,7 @@ do_stop_all(struct cfg *cfg) { return true; } -bool -do_rcon_pcount(struct cfg *cfg, unsigned *online, unsigned *max) +bool do_rcon_pcount(struct cfg *cfg, unsigned *online, unsigned *max) { struct server *server; bool rv; @@ -399,14 +395,13 @@ do_rcon_pcount(struct cfg *cfg, unsigned *online, unsigned *max) return false; rv = get_player_count(fd, online, max); - + close(fd); return rv; } -bool -do_console(struct cfg *cfg) +bool do_console(struct cfg *cfg) { char *prompt; char *cmd; @@ -418,8 +413,8 @@ do_console(struct cfg *cfg) if (fd < 0) return false; - prompt = alloca(strlen(program_invocation_short_name) + - STRLEN(" (") + strlen(server->name) + STRLEN("): ") + 1); + prompt = alloca(strlen(program_invocation_short_name) + STRLEN(" (") + + strlen(server->name) + STRLEN("): ") + 1); sprintf(prompt, "%s (%s): ", program_invocation_short_name, server->name); @@ -437,8 +432,8 @@ do_console(struct cfg *cfg) continue; } - if (streq(tmp, "q") || streq(tmp, "quit") || - streq(tmp, "/q") || streq(tmp, "/quit")) + if (streq(tmp, "q") || streq(tmp, "quit") || streq(tmp, "/q") || + streq(tmp, "/quit")) break; send_cmd(fd, tmp); @@ -454,8 +449,8 @@ do_console(struct cfg *cfg) return true; } -bool -do_command(struct cfg *cfg) { +bool do_command(struct cfg *cfg) +{ int fd; struct server *server; @@ -466,4 +461,3 @@ do_command(struct cfg *cfg) { return send_cmd(fd, cfg->cmdstr); } - diff --git a/minecctl/server.c b/minecctl/server.c index da59c8f..aca69a0 100644 --- a/minecctl/server.c +++ b/minecctl/server.c @@ -10,8 +10,7 @@ #include "config-parser.h" #include "server-config-options.h" -void -server_read_config(struct cfg *cfg, struct server *server) +void server_read_config(struct cfg *cfg, struct server *server) { char buf[4096]; size_t off = 0; @@ -44,7 +43,8 @@ server_read_config(struct cfg *cfg, struct server *server) off += r; if (off == sizeof(buf) - 1) - die("Failed to read %s: file too large", server->filename); + die("Failed to read %s: file too large", + server->filename); } buf[off] = '\0'; @@ -97,8 +97,7 @@ server_read_config(struct cfg *cfg, struct server *server) verbose("mc server address not found in %s", server->filename); } -struct server * -server_get_default(struct cfg *cfg) +struct server *server_get_default(struct cfg *cfg) { struct server *server; @@ -111,8 +110,7 @@ server_get_default(struct cfg *cfg) return server; } -bool -server_set_default(struct cfg *cfg, const char *name) +bool server_set_default(struct cfg *cfg, const char *name) { struct server *server; @@ -128,8 +126,7 @@ server_set_default(struct cfg *cfg, const char *name) return false; } -void -server_load_all_known(struct cfg *cfg) +void server_load_all_known(struct cfg *cfg) { struct dirent *dent; DIR *dir; @@ -161,8 +158,7 @@ server_load_all_known(struct cfg *cfg) closedir(dir); } -void -server_free_all(struct cfg *cfg) +void server_free_all(struct cfg *cfg) { struct server *server, *tmp; @@ -170,8 +166,7 @@ server_free_all(struct cfg *cfg) server_free(server); } -void -server_free(struct server *server) +void server_free(struct server *server) { struct saddr *saddr, *tmp; @@ -192,8 +187,7 @@ server_free(struct server *server) xfree(server); } -struct server * -server_new() +struct server *server_new() { struct server *server; @@ -204,4 +198,3 @@ server_new() return server; } - 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); diff --git a/shared/ansi-colors.h b/shared/ansi-colors.h index ba89c46..8294f2d 100644 --- a/shared/ansi-colors.h +++ b/shared/ansi-colors.h @@ -1,12 +1,12 @@ #ifndef fooansicolorshfoo #define fooansicolorshfoo -#define ANSI_RED "\x1B[0;31m" -#define ANSI_GREEN "\x1B[0;32m" -#define ANSI_YELLOW "\x1B[0;33m" -#define ANSI_BLUE "\x1B[0;34m" -#define ANSI_MAGENTA "\x1B[0;35m" -#define ANSI_GREY "\x1B[0;38;5;245m" -#define ANSI_NORMAL "\x1B[0m" +#define ANSI_RED "\x1B[0;31m" +#define ANSI_GREEN "\x1B[0;32m" +#define ANSI_YELLOW "\x1B[0;33m" +#define ANSI_BLUE "\x1B[0;34m" +#define ANSI_MAGENTA "\x1B[0;35m" +#define ANSI_GREY "\x1B[0;38;5;245m" +#define ANSI_NORMAL "\x1B[0m" #endif diff --git a/shared/config-parser.c b/shared/config-parser.c index 386d5a2..13b26e1 100644 --- a/shared/config-parser.c +++ b/shared/config-parser.c @@ -13,8 +13,7 @@ #include "config-parser.h" #include "config.h" -static void -eat_whitespace_and_comments(char **pos) +static void eat_whitespace_and_comments(char **pos) { assert_return(pos && *pos); @@ -30,8 +29,7 @@ eat_whitespace_and_comments(char **pos) } } -static char * -get_line(char **pos) +static char *get_line(char **pos) { char *begin, *end; @@ -63,9 +61,8 @@ get_line(char **pos) return begin; } -static bool -dnslookup(const char *name, uint16_t port, struct cfg_value *rvalue, - unsigned *naddrs, bool async) +static bool dnslookup(const char *name, uint16_t port, struct cfg_value *rvalue, + unsigned *naddrs, bool async) { struct sockaddr_in *in4; struct sockaddr_in6 *in6; @@ -77,7 +74,9 @@ dnslookup(const char *name, uint16_t port, struct cfg_value *rvalue, bool rv = false; int r; - assert_return(!empty_str(name) && strlen(name) < sizeof(dns->name) && port > 0 && rvalue, false); + assert_return(!empty_str(name) && strlen(name) < sizeof(dns->name) && + port > 0 && rvalue, + false); if (async) { rvalue->type = CFG_VAL_TYPE_ASYNC_ADDRS; @@ -94,7 +93,7 @@ dnslookup(const char *name, uint16_t port, struct cfg_value *rvalue, debug(DBG_DNS, "doing sync DNS lookup of %s", name); } - sprintf(dns->name, "%s", name); + sprintf(dns->name, "%s", name); sprintf(dns->port, "%" PRIu16, port); dns->req.ai_family = AF_UNSPEC; @@ -114,7 +113,8 @@ dnslookup(const char *name, uint16_t port, struct cfg_value *rvalue, r = getaddrinfo_a(mode, gcbs, ARRAY_SIZE(gcbs), &dns->sev); if (r != 0) { - error("getaddrinfo(%s:%" PRIu16 "): %s", name, port, gai_strerror(r)); + error("getaddrinfo(%s:%" PRIu16 "): %s", name, port, + gai_strerror(r)); goto out; } @@ -136,7 +136,8 @@ dnslookup(const char *name, uint16_t port, struct cfg_value *rvalue, 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); debug(DBG_CFG, "addrstr: %s", saddr->addrstr); list_add(&saddr->list, &rvalue->saddrs); (*naddrs)++; @@ -165,8 +166,7 @@ out: return rv; } -bool -strtosockaddrs(const char *str, struct cfg_value *rvalue, bool async) +bool strtosockaddrs(const char *str, struct cfg_value *rvalue, bool async) { struct saddr *saddr; uint16_t port; @@ -235,7 +235,8 @@ strtosockaddrs(const char *str, struct cfg_value *rvalue, bool async) } else if ((tmp = strchr(str, ':'))) { /* IPv4, a.b.c.d:p or IPv4/6 hostname:p */ - debug(DBG_CFG, "attempting to parse IPv4 addr or hostname (%s)", str); + debug(DBG_CFG, "attempting to parse IPv4 addr or hostname (%s)", + str); *tmp = '\0'; tmp++; @@ -247,15 +248,18 @@ strtosockaddrs(const char *str, struct cfg_value *rvalue, bool async) goto error; if (inet_pton(AF_INET, str, &saddr->in4.sin_addr) > 0) { - debug(DBG_CFG, "got an IPv4:port (%s:%" PRIu16 ")", str, port); - saddr_set_ipv4(saddr, saddr->in4.sin_addr.s_addr, htons(port)); + debug(DBG_CFG, "got an IPv4:port (%s:%" PRIu16 ")", str, + port); + saddr_set_ipv4(saddr, saddr->in4.sin_addr.s_addr, + htons(port)); list_add(&saddr->list, list); naddrs++; goto success; - } + } xfree(saddr); - debug(DBG_CFG, "maybe got a hostname:port (%s:%" PRIu16 ")", str, port); + debug(DBG_CFG, "maybe got a hostname:port (%s:%" PRIu16 ")", + str, port); if (!dnslookup(str, port, rvalue, &naddrs, async)) goto error; @@ -318,16 +322,16 @@ error: } /* Returns true if there's data left to parse in buf */ -bool -config_parse_line(const char *filename, char **buf, - struct cfg_key_value_map *kvmap, int *rkey, - const char **rkeyname, struct cfg_value *rvalue, - bool async_dns) +bool config_parse_line(const char *filename, char **buf, + struct cfg_key_value_map *kvmap, int *rkey, + const char **rkeyname, struct cfg_value *rvalue, + bool async_dns) { char *line, *tmp, *key; int i; - assert_return(buf && *buf && kvmap && rkey && rkeyname && rvalue, false); + assert_return(buf && *buf && kvmap && rkey && rkeyname && rvalue, + false); line = get_line(buf); if (!line) @@ -370,7 +374,6 @@ config_parse_line(const char *filename, char **buf, continue; switch (kvmap[i].value_type) { - case CFG_VAL_TYPE_STRING: rvalue->type = CFG_VAL_TYPE_STRING; rvalue->str = tmp; @@ -425,7 +428,8 @@ config_parse_line(const char *filename, char **buf, if (strcaseeq(tmp, "yes") || strcaseeq(tmp, "true")) { rvalue->type = CFG_VAL_TYPE_BOOL; rvalue->boolean = true; - } else if (strcaseeq(tmp, "no") || strcaseeq(tmp, "false")) { + } else if (strcaseeq(tmp, "no") || + strcaseeq(tmp, "false")) { rvalue->type = CFG_VAL_TYPE_BOOL; rvalue->boolean = false; } else { @@ -454,7 +458,8 @@ config_parse_line(const char *filename, char **buf, } error: - /* FIXME: the line is already mangled here, a line number would be nice */ + /* FIXME: the line is already mangled here, a line number would be nice + */ error("%s: invalid config line: %s", filename, line); rvalue->type = CFG_VAL_TYPE_INVALID; *rkey = 0; @@ -462,8 +467,7 @@ error: return true; } -bool -config_parse_header(const char *title, char **buf) +bool config_parse_header(const char *title, char **buf) { char *line; @@ -481,8 +485,7 @@ config_parse_header(const char *title, char **buf) return false; } -bool -is_valid_server_config_filename(struct dirent *dent, const char *filename) +bool is_valid_server_config_filename(struct dirent *dent, const char *filename) { const char *suffix; @@ -512,4 +515,3 @@ is_valid_server_config_filename(struct dirent *dent, const char *filename) return true; } - diff --git a/shared/config-parser.h b/shared/config-parser.h index 7c5703e..771ece1 100644 --- a/shared/config-parser.h +++ b/shared/config-parser.h @@ -18,7 +18,7 @@ enum cfg_value_type { struct dns_async; -typedef void (dns_cb_t)(struct dns_async *); +typedef void(dns_cb_t)(struct dns_async *); struct dns_async { char name[FQDN_STR_LEN + 1]; @@ -51,9 +51,9 @@ struct cfg_value { bool strtosockaddrs(const char *str, struct cfg_value *rvalue, bool async); bool config_parse_line(const char *filename, char **buf, - struct cfg_key_value_map *kvmap, - int *rkey, const char **rkeyname, - struct cfg_value *rvalue, bool async_dns); + struct cfg_key_value_map *kvmap, int *rkey, + const char **rkeyname, struct cfg_value *rvalue, + bool async_dns); bool config_parse_header(const char *title, char **buf); diff --git a/shared/debug.h b/shared/debug.h index 6f1a054..2b1eba0 100644 --- a/shared/debug.h +++ b/shared/debug.h @@ -2,6 +2,7 @@ #define foodebughfoo /* FIXME: Should these be shared? */ +/* clang-format off */ enum debug_lvl { DBG_ERROR = (0x1 << 1), DBG_INFO = (0x1 << 2), @@ -21,70 +22,70 @@ enum debug_lvl { DBG_DNS = (0x1 << 16), DBG_TIMER = (0x1 << 17), }; +/* clang-format on */ -static inline bool -debug_enabled(enum debug_lvl lvl) +static inline bool debug_enabled(enum debug_lvl lvl) { return !!(lvl & debug_mask); } -#define __ifdebug(lvl, fmt, ...) \ - do { \ - if (debug_enabled((lvl))) \ - __debug((lvl), fmt "\n"__VA_OPT__(,) __VA_ARGS__); \ +#define __ifdebug(lvl, fmt, ...) \ + do { \ + if (debug_enabled((lvl))) \ + __debug((lvl), fmt "\n"__VA_OPT__(, )__VA_ARGS__); \ } while (0) -#define debug(lvl, fmt, ...) __ifdebug((lvl), "%s:%s:%i: " fmt, \ - __FILE__, __func__, __LINE__ \ - __VA_OPT__(,) __VA_ARGS__) -#define verbose(fmt, ...) __ifdebug(DBG_VERBOSE, fmt, __VA_ARGS__) -#define info(fmt, ...) __ifdebug(DBG_INFO, fmt, __VA_ARGS__) -#define error(fmt, ...) __ifdebug(DBG_ERROR, "%s:%s:%i: " fmt, \ - __FILE__, __func__, __LINE__ \ - __VA_OPT__(,) __VA_ARGS__) +#define debug(lvl, fmt, ...) \ + __ifdebug((lvl), "%s:%s:%i: " fmt, __FILE__, __func__, \ + __LINE__ __VA_OPT__(, ) __VA_ARGS__) +#define verbose(fmt, ...) __ifdebug(DBG_VERBOSE, fmt, __VA_ARGS__) +#define info(fmt, ...) __ifdebug(DBG_INFO, fmt, __VA_ARGS__) +#define error(fmt, ...) \ + __ifdebug(DBG_ERROR, "%s:%s:%i: " fmt, __FILE__, __func__, \ + __LINE__ __VA_OPT__(, ) __VA_ARGS__) -#define die(fmt, ...) \ - __die("%s:%s:%i: " fmt "\n", \ - __FILE__, __func__, __LINE__ \ - __VA_OPT__(,) __VA_ARGS__) +#define die(fmt, ...) \ + __die("%s:%s:%i: " fmt "\n", __FILE__, __func__, \ + __LINE__ __VA_OPT__(, ) __VA_ARGS__) -#define assert_log(expr, msg) \ - ((expr) ? \ - (true) : \ - (__debug(DBG_ERROR, "%s:%s:%i: assertion \"" msg "\" failed\n", \ - __FILE__, __func__, __LINE__), false)) +#define assert_log(expr, msg) \ + ((expr) ? (true) : \ + (__debug(DBG_ERROR, \ + "%s:%s:%i: assertion \"" msg "\" failed\n", \ + __FILE__, __func__, __LINE__), \ + false)) -#define assert_return(expr, ...) \ - do { \ - if (!assert_log(expr, #expr)) \ - return __VA_ARGS__; \ +#define assert_return(expr, ...) \ + do { \ + if (!assert_log(expr, #expr)) \ + return __VA_ARGS__; \ } while (0) -#define assert_return_silent(expr, ...) \ - do { \ - if (!(expr)) \ - return __VA_ARGS__; \ +#define assert_return_silent(expr, ...) \ + do { \ + if (!(expr)) \ + return __VA_ARGS__; \ } while (0) -#define assert_die(expr, msg) \ - do { \ - if (!assert_log(expr, #expr)) \ - die(msg); \ +#define assert_die(expr, msg) \ + do { \ + if (!assert_log(expr, #expr)) \ + die(msg); \ } while (0) -#define assert_task_alive_or(lvl, t, cmd) \ -do { \ - if (!(t)) { \ - error("invalid task"); \ - cmd; \ - } \ - \ - if ((t)->dead) { \ - debug((lvl), "task dead"); \ - cmd; \ - } \ -} while(0) +#define assert_task_alive_or(lvl, t, cmd) \ + do { \ + if (!(t)) { \ + error("invalid task"); \ + cmd; \ + } \ + \ + if ((t)->dead) { \ + debug((lvl), "task dead"); \ + cmd; \ + } \ + } while (0) -#define assert_task_alive(lvl, t) assert_task_alive_or((lvl), (t), return) +#define assert_task_alive(lvl, t) assert_task_alive_or((lvl), (t), return ) #endif diff --git a/shared/external.h b/shared/external.h index beba93a..738e460 100644 --- a/shared/external.h +++ b/shared/external.h @@ -9,7 +9,8 @@ void *__zmalloc(const char *fn, int line, size_t s) _malloc_ _alloc_(3); char *__xstrdup(const char *fn, int line, const char *s) _malloc_; #define xstrndup(s, n) __xstrndup(__func__, __LINE__, s, n) -char *__xstrndup(const char *fn, int line, const char *s, size_t n) _malloc_ _alloc_(4); +char *__xstrndup(const char *fn, int line, const char *s, size_t n) _malloc_ + _alloc_(4); #define xfree(s) __xfree(__func__, __LINE__, s) void __xfree(const char *fn, int line, void *ptr); diff --git a/shared/list.h b/shared/list.h index fcd005a..25f4219 100644 --- a/shared/list.h +++ b/shared/list.h @@ -17,15 +17,17 @@ struct list_head { }; #define WRITE_ONCE(a, b) ((a) = (b)) -#define READ_ONCE(a) (a) -#define LIST_POISON1 ((void *) 0x100) -#define LIST_POISON2 ((void *) 0x122) +#define READ_ONCE(a) (a) +#define LIST_POISON1 ((void *)0x100) +#define LIST_POISON2 ((void *)0x122) -#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) +#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER) -#define container_of(ptr, type, member) __extension__ ({ \ - const typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offsetof(type,member) );}) +#define container_of(ptr, type, member) \ + __extension__({ \ + const typeof(((type *)0)->member) *__mptr = (ptr); \ + (type *)((char *)__mptr - offsetof(type, member)); \ + }) /* End of added stuff */ @@ -39,10 +41,12 @@ struct list_head { * using the generic single-entry routines. */ -#define LIST_HEAD_INIT(name) { &(name), &(name) } +#define LIST_HEAD_INIT(name) \ + { \ + &(name), &(name) \ + } -#define LIST_HEAD(name) \ - struct list_head name = LIST_HEAD_INIT(name) +#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name) /** * INIT_LIST_HEAD - Initialize a list_head structure @@ -58,14 +62,13 @@ static inline void INIT_LIST_HEAD(struct list_head *list) } #ifdef CONFIG_DEBUG_LIST -extern bool __list_add_valid(struct list_head *new, - struct list_head *prev, - struct list_head *next); +extern bool __list_add_valid(struct list_head *new, struct list_head *prev, + struct list_head *next); extern bool __list_del_entry_valid(struct list_head *entry); #else static inline bool __list_add_valid(_unused_ struct list_head *new, - _unused_ struct list_head *prev, - _unused_ struct list_head *next) + _unused_ struct list_head *prev, + _unused_ struct list_head *next) { return true; } @@ -81,8 +84,7 @@ static inline bool __list_del_entry_valid(_unused_ struct list_head *entry) * This is only for internal list manipulation where we know * the prev/next entries already! */ -static inline void __list_add(struct list_head *new, - struct list_head *prev, +static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next) { if (!__list_add_valid(new, prev, next)) @@ -107,7 +109,6 @@ static inline void list_add(struct list_head *new, struct list_head *head) __list_add(new, head, head->next); } - /** * list_add_tail - add a new entry * @new: new entry to be added @@ -128,7 +129,7 @@ static inline void list_add_tail(struct list_head *new, struct list_head *head) * This is only for internal list manipulation where we know * the prev/next entries already! */ -static inline void __list_del(struct list_head * prev, struct list_head * next) +static inline void __list_del(struct list_head *prev, struct list_head *next) { next->prev = prev; WRITE_ONCE(prev->next, next); @@ -176,8 +177,7 @@ static inline void list_del(struct list_head *entry) * * If @old was empty, it will be overwritten. */ -static inline void list_replace(struct list_head *old, - struct list_head *new) +static inline void list_replace(struct list_head *old, struct list_head *new) { new->next = old->next; new->next->prev = new; @@ -204,8 +204,7 @@ static inline void list_replace_init(struct list_head *old, * @entry1: the location to place entry2 * @entry2: the location to place entry1 */ -static inline void list_swap(struct list_head *entry1, - struct list_head *entry2) +static inline void list_swap(struct list_head *entry1, struct list_head *entry2) { struct list_head *pos = entry2->prev; @@ -278,7 +277,7 @@ static inline void list_bulk_move_tail(struct list_head *head, * @head: the head of the list */ static inline int list_is_first(const struct list_head *list, - const struct list_head *head) + const struct list_head *head) { return list->prev == head; } @@ -289,7 +288,7 @@ static inline int list_is_first(const struct list_head *list, * @head: the head of the list */ static inline int list_is_last(const struct list_head *list, - const struct list_head *head) + const struct list_head *head) { return list->next == head; } @@ -364,7 +363,8 @@ static inline int list_is_singular(const struct list_head *head) } static inline void __list_cut_position(struct list_head *list, - struct list_head *head, struct list_head *entry) + struct list_head *head, + struct list_head *entry) { struct list_head *new_first = entry->next; list->next = head->next; @@ -390,12 +390,12 @@ static inline void __list_cut_position(struct list_head *list, * */ static inline void list_cut_position(struct list_head *list, - struct list_head *head, struct list_head *entry) + struct list_head *head, + struct list_head *entry) { if (list_empty(head)) return; - if (list_is_singular(head) && - (head->next != entry && head != entry)) + if (list_is_singular(head) && (head->next != entry && head != entry)) return; if (entry == head) INIT_LIST_HEAD(list); @@ -434,8 +434,7 @@ static inline void list_cut_before(struct list_head *list, } static inline void __list_splice(const struct list_head *list, - struct list_head *prev, - struct list_head *next) + struct list_head *prev, struct list_head *next) { struct list_head *first = list->next; struct list_head *last = list->prev; @@ -453,7 +452,7 @@ static inline void __list_splice(const struct list_head *list, * @head: the place to add it in the first list. */ static inline void list_splice(const struct list_head *list, - struct list_head *head) + struct list_head *head) { if (!list_empty(list)) __list_splice(list, head, head->next); @@ -465,7 +464,7 @@ static inline void list_splice(const struct list_head *list, * @head: the place to add it in the first list. */ static inline void list_splice_tail(struct list_head *list, - struct list_head *head) + struct list_head *head) { if (!list_empty(list)) __list_splice(list, head->prev, head); @@ -510,8 +509,7 @@ static inline void list_splice_tail_init(struct list_head *list, * @type: the type of the struct this is embedded in. * @member: the name of the list_head within the struct. */ -#define list_entry(ptr, type, member) \ - container_of(ptr, type, member) +#define list_entry(ptr, type, member) container_of(ptr, type, member) /** * list_first_entry - get the first element from a list @@ -521,7 +519,7 @@ static inline void list_splice_tail_init(struct list_head *list, * * Note, that list is expected to be not empty. */ -#define list_first_entry(ptr, type, member) \ +#define list_first_entry(ptr, type, member) \ list_entry((ptr)->next, type, member) /** @@ -532,8 +530,7 @@ static inline void list_splice_tail_init(struct list_head *list, * * Note, that list is expected to be not empty. */ -#define list_last_entry(ptr, type, member) \ - list_entry((ptr)->prev, type, member) +#define list_last_entry(ptr, type, member) list_entry((ptr)->prev, type, member) /** * list_first_entry_or_null - get the first element from a list @@ -543,18 +540,19 @@ static inline void list_splice_tail_init(struct list_head *list, * * Note that if the list is empty, it returns NULL. */ -#define list_first_entry_or_null(ptr, type, member) ({ \ - struct list_head *head__ = (ptr); \ - struct list_head *pos__ = READ_ONCE(head__->next); \ - pos__ != head__ ? list_entry(pos__, type, member) : NULL; \ -}) +#define list_first_entry_or_null(ptr, type, member) \ + ({ \ + struct list_head *head__ = (ptr); \ + struct list_head *pos__ = READ_ONCE(head__->next); \ + pos__ != head__ ? list_entry(pos__, type, member) : NULL; \ + }) /** * list_next_entry - get the next element in list * @pos: the type * to cursor * @member: the name of the list_head within the struct. */ -#define list_next_entry(pos, member) \ +#define list_next_entry(pos, member) \ list_entry((pos)->member.next, typeof(*(pos)), member) /** @@ -562,7 +560,7 @@ static inline void list_splice_tail_init(struct list_head *list, * @pos: the type * to cursor * @member: the name of the list_head within the struct. */ -#define list_prev_entry(pos, member) \ +#define list_prev_entry(pos, member) \ list_entry((pos)->member.prev, typeof(*(pos)), member) /** @@ -570,7 +568,7 @@ static inline void list_splice_tail_init(struct list_head *list, * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. */ -#define list_for_each(pos, head) \ +#define list_for_each(pos, head) \ for (pos = (head)->next; pos != (head); pos = pos->next) /** @@ -580,7 +578,7 @@ static inline void list_splice_tail_init(struct list_head *list, * * Continue to iterate over a list, continuing after the current position. */ -#define list_for_each_continue(pos, head) \ +#define list_for_each_continue(pos, head) \ for (pos = pos->next; pos != (head); pos = pos->next) /** @@ -588,7 +586,7 @@ static inline void list_splice_tail_init(struct list_head *list, * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. */ -#define list_for_each_prev(pos, head) \ +#define list_for_each_prev(pos, head) \ for (pos = (head)->prev; pos != (head); pos = pos->prev) /** @@ -597,19 +595,19 @@ static inline void list_splice_tail_init(struct list_head *list, * @n: another &struct list_head to use as temporary storage * @head: the head for your list. */ -#define list_for_each_safe(pos, n, head) \ - for (pos = (head)->next, n = pos->next; pos != (head); \ - pos = n, n = pos->next) +#define list_for_each_safe(pos, n, head) \ + for (pos = (head)->next, n = pos->next; pos != (head); \ + pos = n, n = pos->next) /** - * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry + * list_for_each_prev_safe - iterate over a list backwards safe against removal + * of list entry * @pos: the &struct list_head to use as a loop cursor. * @n: another &struct list_head to use as temporary storage * @head: the head for your list. */ -#define list_for_each_prev_safe(pos, n, head) \ - for (pos = (head)->prev, n = pos->prev; \ - pos != (head); \ +#define list_for_each_prev_safe(pos, n, head) \ + for (pos = (head)->prev, n = pos->prev; pos != (head); \ pos = n, n = pos->prev) /** @@ -618,10 +616,9 @@ static inline void list_splice_tail_init(struct list_head *list, * @head: the head for your list. * @member: the name of the list_head within the struct. */ -#define list_for_each_entry(pos, head, member) \ - for (pos = list_first_entry(head, typeof(*pos), member); \ - &pos->member != (head); \ - pos = list_next_entry(pos, member)) +#define list_for_each_entry(pos, head, member) \ + for (pos = list_first_entry(head, typeof(*pos), member); \ + &pos->member != (head); pos = list_next_entry(pos, member)) /** * list_for_each_entry_reverse - iterate backwards over list of given type. @@ -629,21 +626,22 @@ static inline void list_splice_tail_init(struct list_head *list, * @head: the head for your list. * @member: the name of the list_head within the struct. */ -#define list_for_each_entry_reverse(pos, head, member) \ - for (pos = list_last_entry(head, typeof(*pos), member); \ - &pos->member != (head); \ - pos = list_prev_entry(pos, member)) +#define list_for_each_entry_reverse(pos, head, member) \ + for (pos = list_last_entry(head, typeof(*pos), member); \ + &pos->member != (head); pos = list_prev_entry(pos, member)) /** - * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() + * list_prepare_entry - prepare a pos entry for use in + * list_for_each_entry_continue() * @pos: the type * to use as a start point * @head: the head of the list * @member: the name of the list_head within the struct. * - * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). + * Prepares a pos entry for use as a start point in + * list_for_each_entry_continue(). */ -#define list_prepare_entry(pos, head, member) \ - ((pos) ? : list_entry(head, typeof(*pos), member)) +#define list_prepare_entry(pos, head, member) \ + ((pos) ?: list_entry(head, typeof(*pos), member)) /** * list_for_each_entry_continue - continue iteration over list of given type @@ -654,9 +652,8 @@ static inline void list_splice_tail_init(struct list_head *list, * Continue to iterate over list of given type, continuing after * the current position. */ -#define list_for_each_entry_continue(pos, head, member) \ - for (pos = list_next_entry(pos, member); \ - &pos->member != (head); \ +#define list_for_each_entry_continue(pos, head, member) \ + for (pos = list_next_entry(pos, member); &pos->member != (head); \ pos = list_next_entry(pos, member)) /** @@ -668,22 +665,21 @@ static inline void list_splice_tail_init(struct list_head *list, * Start to iterate over list of given type backwards, continuing after * the current position. */ -#define list_for_each_entry_continue_reverse(pos, head, member) \ - for (pos = list_prev_entry(pos, member); \ - &pos->member != (head); \ +#define list_for_each_entry_continue_reverse(pos, head, member) \ + for (pos = list_prev_entry(pos, member); &pos->member != (head); \ pos = list_prev_entry(pos, member)) /** - * list_for_each_entry_from - iterate over list of given type from the current point + * list_for_each_entry_from - iterate over list of given type from the current + * point * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list_head within the struct. * * Iterate over list of given type, continuing from current position. */ -#define list_for_each_entry_from(pos, head, member) \ - for (; &pos->member != (head); \ - pos = list_next_entry(pos, member)) +#define list_for_each_entry_from(pos, head, member) \ + for (; &pos->member != (head); pos = list_next_entry(pos, member)) /** * list_for_each_entry_from_reverse - iterate backwards over list of given type @@ -694,25 +690,25 @@ static inline void list_splice_tail_init(struct list_head *list, * * Iterate backwards over list of given type, continuing from current position. */ -#define list_for_each_entry_from_reverse(pos, head, member) \ - for (; &pos->member != (head); \ - pos = list_prev_entry(pos, member)) +#define list_for_each_entry_from_reverse(pos, head, member) \ + for (; &pos->member != (head); pos = list_prev_entry(pos, member)) /** - * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry + * list_for_each_entry_safe - iterate over list of given type safe against + * removal of list entry * @pos: the type * to use as a loop cursor. * @n: another type * to use as temporary storage * @head: the head for your list. * @member: the name of the list_head within the struct. */ -#define list_for_each_entry_safe(pos, n, head, member) \ - for (pos = list_first_entry(head, typeof(*pos), member), \ - n = list_next_entry(pos, member); \ - &pos->member != (head); \ - pos = n, n = list_next_entry(n, member)) +#define list_for_each_entry_safe(pos, n, head, member) \ + for (pos = list_first_entry(head, typeof(*pos), member), \ + n = list_next_entry(pos, member); \ + &pos->member != (head); pos = n, n = list_next_entry(n, member)) /** - * list_for_each_entry_safe_continue - continue list iteration safe against removal + * list_for_each_entry_safe_continue - continue list iteration safe against + * removal * @pos: the type * to use as a loop cursor. * @n: another type * to use as temporary storage * @head: the head for your list. @@ -721,14 +717,14 @@ static inline void list_splice_tail_init(struct list_head *list, * Iterate over list of given type, continuing after current point, * safe against removal of list entry. */ -#define list_for_each_entry_safe_continue(pos, n, head, member) \ - for (pos = list_next_entry(pos, member), \ - n = list_next_entry(pos, member); \ - &pos->member != (head); \ - pos = n, n = list_next_entry(n, member)) +#define list_for_each_entry_safe_continue(pos, n, head, member) \ + for (pos = list_next_entry(pos, member), \ + n = list_next_entry(pos, member); \ + &pos->member != (head); pos = n, n = list_next_entry(n, member)) /** - * list_for_each_entry_safe_from - iterate over list from current point safe against removal + * list_for_each_entry_safe_from - iterate over list from current point safe + * against removal * @pos: the type * to use as a loop cursor. * @n: another type * to use as temporary storage * @head: the head for your list. @@ -737,13 +733,13 @@ static inline void list_splice_tail_init(struct list_head *list, * Iterate over list of given type from current point, safe against * removal of list entry. */ -#define list_for_each_entry_safe_from(pos, n, head, member) \ - for (n = list_next_entry(pos, member); \ - &pos->member != (head); \ +#define list_for_each_entry_safe_from(pos, n, head, member) \ + for (n = list_next_entry(pos, member); &pos->member != (head); \ pos = n, n = list_next_entry(n, member)) /** - * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal + * list_for_each_entry_safe_reverse - iterate backwards over list safe against + * removal * @pos: the type * to use as a loop cursor. * @n: another type * to use as temporary storage * @head: the head for your list. @@ -752,11 +748,10 @@ static inline void list_splice_tail_init(struct list_head *list, * Iterate backwards over list of given type, safe against removal * of list entry. */ -#define list_for_each_entry_safe_reverse(pos, n, head, member) \ - for (pos = list_last_entry(head, typeof(*pos), member), \ - n = list_prev_entry(pos, member); \ - &pos->member != (head); \ - pos = n, n = list_prev_entry(n, member)) +#define list_for_each_entry_safe_reverse(pos, n, head, member) \ + for (pos = list_last_entry(head, typeof(*pos), member), \ + n = list_prev_entry(pos, member); \ + &pos->member != (head); pos = n, n = list_prev_entry(n, member)) /** * list_safe_reset_next - reset a stale list_for_each_entry_safe loop @@ -770,7 +765,6 @@ static inline void list_splice_tail_init(struct list_head *list, * and list_safe_reset_next is called after re-taking the lock and before * completing the current iteration of the loop body. */ -#define list_safe_reset_next(pos, n, member) \ - n = list_next_entry(pos, member) +#define list_safe_reset_next(pos, n, member) n = list_next_entry(pos, member) #endif diff --git a/shared/mc-protocol.c b/shared/mc-protocol.c index afc9b38..03a2dcd 100644 --- a/shared/mc-protocol.c +++ b/shared/mc-protocol.c @@ -4,15 +4,14 @@ #include "utils.h" -#define MC_HELO 0x00 -#define MC_NEXT_STATE_STATUS 0x01 -#define MC_GET_STATUS 0x00 -#define MC_VARINT_MAX_BYTES 5 -#define MC_STATUS_REPLY 0x00 -#define MC_UNDEFINED_VERSION -1 - -static inline unsigned -write_byte(char **pos, char byte) +#define MC_HELO 0x00 +#define MC_NEXT_STATE_STATUS 0x01 +#define MC_GET_STATUS 0x00 +#define MC_VARINT_MAX_BYTES 5 +#define MC_STATUS_REPLY 0x00 +#define MC_UNDEFINED_VERSION -1 + +static inline unsigned write_byte(char **pos, char byte) { if (pos && *pos) { **pos = byte; @@ -20,8 +19,7 @@ write_byte(char **pos, char byte) } return 1; } -static inline unsigned -write_varint(char **pos, int32_t v) +static inline unsigned write_varint(char **pos, int32_t v) { uint32_t u = (uint32_t)v; unsigned rv = 0; @@ -40,8 +38,7 @@ write_varint(char **pos, int32_t v) return rv; } -static inline unsigned -write_bytes(char **pos, const char *bytes, size_t n) +static inline unsigned write_bytes(char **pos, const char *bytes, size_t n) { if (pos && *pos) { memcpy(*pos, bytes, n); @@ -51,8 +48,7 @@ write_bytes(char **pos, const char *bytes, size_t n) return n; } -static inline unsigned -write_str(char **pos, const char *str, size_t len) +static inline unsigned write_str(char **pos, const char *str, size_t len) { unsigned rv; @@ -68,8 +64,7 @@ write_str(char **pos, const char *str, size_t len) * 0 = need more bytes * < 0 = error */ -static inline int -read_varint(const char **from, size_t *remain, int32_t *res) +static inline int read_varint(const char **from, size_t *remain, int32_t *res) { unsigned consumed; uint32_t val = 0; @@ -103,8 +98,7 @@ read_varint(const char **from, size_t *remain, int32_t *res) * 0 = need more bytes * < 0 = error */ -int -mc_is_handshake_complete(const char *buf, size_t len) +int mc_is_handshake_complete(const char *buf, size_t len) { int32_t mclen; int r; @@ -123,21 +117,21 @@ mc_is_handshake_complete(const char *buf, size_t len) } if (mclen < len) { - debug(DBG_IDLE, "short MC message - len: %" PRIi32 ", remain: %zu", - mclen, len); + debug(DBG_IDLE, + "short MC message - len: %" PRIi32 ", remain: %zu", mclen, + len); return 0; - } + } debug(DBG_IDLE, "Complete message"); return 1; } -#define PLAYERS_NEEDLE "\"players\"" -#define MAX_NEEDLE "\"max\"" -#define ONLINE_NEEDLE "\"online\"" -static bool -mc_get_player_count(const char *buf, size_t remain, - unsigned *ronline, unsigned *rmax) +#define PLAYERS_NEEDLE "\"players\"" +#define MAX_NEEDLE "\"max\"" +#define ONLINE_NEEDLE "\"online\"" +static bool mc_get_player_count(const char *buf, size_t remain, + unsigned *ronline, unsigned *rmax) { /* * Example JSON (line breaks added): @@ -182,9 +176,8 @@ error: return false; } -bool -mc_protocol_parse_status_reply(const char *buf, size_t len, - unsigned *online, unsigned *max) +bool mc_protocol_parse_status_reply(const char *buf, size_t len, + unsigned *online, unsigned *max) { const char *from = buf; size_t remain = len; @@ -201,8 +194,8 @@ mc_protocol_parse_status_reply(const char *buf, size_t len, return false; } - debug(DBG_IDLE, "MC message - len: %" PRIi32 ", remain: %zu", - mclen, remain); + debug(DBG_IDLE, "MC message - len: %" PRIi32 ", remain: %zu", mclen, + remain); if (*from != MC_STATUS_REPLY) { error("unknown server reply (0x%02hhx)", *from); @@ -218,8 +211,8 @@ mc_protocol_parse_status_reply(const char *buf, size_t len, return false; } - debug(DBG_IDLE, "MC - json len: %" PRIi32 ", remain: %zu", - jsonlen, remain); + debug(DBG_IDLE, "MC - json len: %" PRIi32 ", remain: %zu", jsonlen, + remain); if (jsonlen < remain) { error("invalid JSON length"); @@ -232,9 +225,8 @@ mc_protocol_parse_status_reply(const char *buf, size_t len, return false; } -bool -mc_protocol_create_status_request(char *buf, size_t len, size_t *rlen, - struct saddr *saddr) +bool mc_protocol_create_status_request(char *buf, size_t len, size_t *rlen, + struct saddr *saddr) { uint16_t port; char hostname[INET6_ADDRSTRLEN]; @@ -248,7 +240,7 @@ mc_protocol_create_status_request(char *buf, size_t len, size_t *rlen, hostlen = strlen(hostname); /* First, a handshake, calculate body length... */ - bodylen = write_byte(NULL, MC_HELO); + bodylen = write_byte(NULL, MC_HELO); bodylen += write_varint(NULL, MC_UNDEFINED_VERSION); bodylen += write_str(NULL, hostname, hostlen); bodylen += write_byte(NULL, (port >> 8) & 0xff); @@ -279,4 +271,3 @@ mc_protocol_create_status_request(char *buf, size_t len, size_t *rlen, *rlen = packetlen; return true; } - diff --git a/shared/mc-protocol.h b/shared/mc-protocol.h index 8ecc02a..8e62aeb 100644 --- a/shared/mc-protocol.h +++ b/shared/mc-protocol.h @@ -10,4 +10,3 @@ bool mc_protocol_create_status_request(char *buf, size_t len, size_t *rlen, struct saddr *saddr); #endif - diff --git a/shared/rcon-protocol.c b/shared/rcon-protocol.c index 0ea1245..0cf73fc 100644 --- a/shared/rcon-protocol.c +++ b/shared/rcon-protocol.c @@ -5,8 +5,7 @@ #include "rcon-protocol.h" -static int32_t -read_int(const char **pos, size_t *len) +static int32_t read_int(const char **pos, size_t *len) { uint32_t val; const char *p; @@ -18,8 +17,8 @@ read_int(const char **pos, size_t *len) return 0; p = *pos; - val = ((uint8_t)p[0] << 0); - val += ((uint8_t)p[1] << 8); + val = ((uint8_t)p[0] << 0); + val += ((uint8_t)p[1] << 8); val += ((uint8_t)p[2] << 16); val += ((uint8_t)p[3] << 24); @@ -30,8 +29,7 @@ read_int(const char **pos, size_t *len) return (int32_t)val; } -static void -write_int(char **pos, size_t *len, int32_t orig) +static void write_int(char **pos, size_t *len, int32_t orig) { uint32_t val = (uint32_t)orig; char *p; @@ -40,8 +38,8 @@ write_int(char **pos, size_t *len, int32_t orig) return; p = *pos; - p[0] = (val >> 0) & 0xff; - p[1] = (val >> 8) & 0xff; + p[0] = (val >> 0) & 0xff; + p[1] = (val >> 8) & 0xff; p[2] = (val >> 16) & 0xff; p[3] = (val >> 24) & 0xff; @@ -50,8 +48,7 @@ write_int(char **pos, size_t *len, int32_t orig) *len += RCON_INT_LEN; } -static void -write_str(char **pos, size_t *len, const char *str, size_t slen) +static void write_str(char **pos, size_t *len, const char *str, size_t slen) { if (!pos || !*pos || !str || *str == '\0' || slen < 1) return; @@ -62,8 +59,7 @@ write_str(char **pos, size_t *len, const char *str, size_t slen) *len += slen; } -static void -write_end(char **pos, size_t *len) +static void write_end(char **pos, size_t *len) { char *p; @@ -79,9 +75,9 @@ write_end(char **pos, size_t *len) *len += RCON_END_LEN; } -bool -rcon_protocol_create_packet(char *buf, size_t len, size_t *rlen, int32_t reqid, - enum rcon_packet_type type, const char *msg) +bool rcon_protocol_create_packet(char *buf, size_t len, size_t *rlen, + int32_t reqid, enum rcon_packet_type type, + const char *msg) { size_t plen, msglen; char *pos; @@ -115,8 +111,7 @@ rcon_protocol_create_packet(char *buf, size_t len, size_t *rlen, int32_t reqid, * 0 = packet not complete * > 0 = packet complete (length) */ -int32_t -rcon_protocol_packet_complete(const char *buf, size_t len) +int32_t rcon_protocol_packet_complete(const char *buf, size_t len) { const char *pos; int32_t plen; @@ -140,9 +135,9 @@ rcon_protocol_packet_complete(const char *buf, size_t len) return plen; } -bool -rcon_protocol_read_packet(const char *buf, size_t len, int32_t *id, int32_t *type, - const char **rmsg, const char **error) +bool rcon_protocol_read_packet(const char *buf, size_t len, int32_t *id, + int32_t *type, const char **rmsg, + const char **error) { const char *pos; int32_t plen; diff --git a/shared/rcon-protocol.h b/shared/rcon-protocol.h index 097da1d..b3aea98 100644 --- a/shared/rcon-protocol.h +++ b/shared/rcon-protocol.h @@ -5,6 +5,7 @@ #include <stdint.h> /* FIXME: FAIL is an id, not type, LOGIN_OK should be LOGIN_RESPONSE */ +/* clang-format off */ enum rcon_packet_type { RCON_PACKET_LOGIN = 3, RCON_PACKET_LOGIN_OK = 2, @@ -12,6 +13,7 @@ enum rcon_packet_type { RCON_PACKET_COMMAND = 2, RCON_PACKET_RESPONSE = 0, }; +/* clang-format on */ #define RCON_INT_LEN 4 @@ -22,8 +24,7 @@ enum rcon_packet_type { /* header + reqid + type + end */ #define RCON_PKT_MIN_LEN (RCON_HDR_LEN + 2 * RCON_INT_LEN + RCON_END_LEN) -static inline size_t -rcon_protocol_packet_len(size_t msglen) +static inline size_t rcon_protocol_packet_len(size_t msglen) { /* header + reqid + type + msg + end */ return (RCON_PKT_MIN_LEN + msglen); diff --git a/shared/server-config-options.h b/shared/server-config-options.h index 5aee734..5600e52 100644 --- a/shared/server-config-options.h +++ b/shared/server-config-options.h @@ -25,55 +25,68 @@ struct cfg_key_value_map scfg_key_map[] = { .key_name = "type", .key_value = SCFG_KEY_TYPE, .value_type = CFG_VAL_TYPE_STRING, - }, { + }, + { .key_name = "name", .key_value = SCFG_KEY_NAME, .value_type = CFG_VAL_TYPE_STRING, - }, { + }, + { .key_name = "port", .key_value = SCFG_KEY_PORT, .value_type = CFG_VAL_TYPE_UINT16, - }, { + }, + { .key_name = "local", .key_value = SCFG_KEY_LOCAL, .value_type = CFG_VAL_TYPE_ADDRS, - }, { + }, + { .key_name = "remote", .key_value = SCFG_KEY_REMOTE, .value_type = CFG_VAL_TYPE_ADDRS, - }, { + }, + { .key_name = "idle_timeout", .key_value = SCFG_KEY_IDLE_TIMEOUT, .value_type = CFG_VAL_TYPE_UINT16, - }, { + }, + { .key_name = "stop_method", .key_value = SCFG_KEY_STOP_METHOD, .value_type = CFG_VAL_TYPE_STRING, - }, { + }, + { .key_name = "start_method", .key_value = SCFG_KEY_START_METHOD, .value_type = CFG_VAL_TYPE_STRING, - }, { + }, + { .key_name = "stop_exec", .key_value = SCFG_KEY_STOP_EXEC, .value_type = CFG_VAL_TYPE_STRING, - }, { + }, + { .key_name = "start_exec", .key_value = SCFG_KEY_START_EXEC, .value_type = CFG_VAL_TYPE_STRING, - }, { + }, + { .key_name = "rcon", .key_value = SCFG_KEY_RCON, .value_type = CFG_VAL_TYPE_ADDRS, - }, { + }, + { .key_name = "rcon_password", .key_value = SCFG_KEY_RCON_PASSWORD, .value_type = CFG_VAL_TYPE_STRING, - }, { + }, + { .key_name = "systemd_service", .key_value = SCFG_KEY_SYSTEMD_SERVICE, .value_type = CFG_VAL_TYPE_STRING, - }, { + }, + { .key_name = NULL, .key_value = SCFG_KEY_INVALID, .value_type = CFG_VAL_TYPE_INVALID, @@ -81,4 +94,3 @@ struct cfg_key_value_map scfg_key_map[] = { }; #endif - diff --git a/shared/utils.c b/shared/utils.c index c71f1f7..20d9809 100644 --- a/shared/utils.c +++ b/shared/utils.c @@ -17,8 +17,7 @@ unsigned debug_mask = 0; -void -socket_set_low_latency(int sfd, bool keepalive, bool iptos, bool nodelay) +void socket_set_low_latency(int sfd, bool keepalive, bool iptos, bool nodelay) { int option; @@ -27,31 +26,33 @@ socket_set_low_latency(int sfd, bool keepalive, bool iptos, bool nodelay) /* Probably not necessary, but can't hurt */ if (keepalive) { option = true; - if (setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option)) < 0) + if (setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &option, + sizeof(option)) < 0) error("setsockopt: %m"); } /* Doubtful if it has much effect, but can't hurt */ if (iptos) { option = IPTOS_LOWDELAY; - if (setsockopt(sfd, IPPROTO_IP, IP_TOS, &option, sizeof(option)) < 0) + if (setsockopt(sfd, IPPROTO_IP, IP_TOS, &option, + sizeof(option)) < 0) error("setsockopt: %m"); } /* Nagle's algorithm is a poor fit for gaming */ if (nodelay) { option = true; - if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &option, sizeof(option)) < 0) + if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &option, + sizeof(option)) < 0) error("setsockopt: %m"); } } -uint16_t -saddr_port(struct saddr *saddr) +uint16_t saddr_port(struct saddr *saddr) { - //assert_return(saddr, 0); + // assert_return(saddr, 0); - switch (saddr->storage.ss_family) { + switch (saddr->st.ss_family) { case AF_INET: return ntohs(saddr->in4.sin_port); case AF_INET6: @@ -61,18 +62,19 @@ saddr_port(struct saddr *saddr) } } -char * -saddr_addr(struct saddr *saddr, char *buf, size_t len) +char *saddr_addr(struct saddr *saddr, char *buf, size_t len) { - //assert_return(saddr && buf && len > 0, NULL); + // assert_return(saddr && buf && len > 0, NULL); - switch (saddr->storage.ss_family) { + switch (saddr->st.ss_family) { case AF_INET: - if (inet_ntop(saddr->in4.sin_family, &saddr->in4.sin_addr, buf, len)) + if (inet_ntop(saddr->in4.sin_family, &saddr->in4.sin_addr, buf, + len)) return buf; break; case AF_INET6: - if (inet_ntop(saddr->in6.sin6_family, &saddr->in6.sin6_addr, buf, len)) + if (inet_ntop(saddr->in6.sin6_family, &saddr->in6.sin6_addr, + buf, len)) return buf; break; default: @@ -83,10 +85,9 @@ saddr_addr(struct saddr *saddr, char *buf, size_t len) return buf; } -void -saddr_set_ipv4(struct saddr *saddr, in_addr_t ip, in_port_t port) +void saddr_set_ipv4(struct saddr *saddr, in_addr_t ip, in_port_t port) { - //assert_return(saddr); + // assert_return(saddr); memset(&saddr->in4, 0, sizeof(saddr->in4)); saddr->in4.sin_family = AF_INET; @@ -96,10 +97,10 @@ saddr_set_ipv4(struct saddr *saddr, in_addr_t ip, in_port_t port) saddr_set_addrstr(saddr); } -void -saddr_set_ipv6(struct saddr *saddr, const struct in6_addr *ip, in_port_t port) +void saddr_set_ipv6(struct saddr *saddr, const struct in6_addr *ip, + in_port_t port) { - //assert_return(saddr && ip); + // assert_return(saddr && ip); memset(&saddr->in6, 0, sizeof(saddr->in6)); saddr->in6.sin6_family = AF_INET6; @@ -110,14 +111,13 @@ saddr_set_ipv6(struct saddr *saddr, const struct in6_addr *ip, in_port_t port) saddr_set_addrstr(saddr); } -void -saddr_set_addrstr(struct saddr *saddr) +void saddr_set_addrstr(struct saddr *saddr) { - //assert_return(saddr); + // assert_return(saddr); char abuf[ADDRSTRLEN]; - switch (saddr->storage.ss_family) { + switch (saddr->st.ss_family) { case AF_INET: snprintf(saddr->addrstr, sizeof(saddr->addrstr), "AF_INET4 %s %" PRIu16, @@ -136,8 +136,7 @@ saddr_set_addrstr(struct saddr *saddr) } } -int -strtou16_strict(const char *str, uint16_t *result) +int strtou16_strict(const char *str, uint16_t *result) { char *end; long val; @@ -150,7 +149,7 @@ strtou16_strict(const char *str, uint16_t *result) if (errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) return -EINVAL; - + if (errno != 0 && val == 0) return -EINVAL; @@ -167,4 +166,3 @@ strtou16_strict(const char *str, uint16_t *result) *result = val; return 0; } - diff --git a/shared/utils.h b/shared/utils.h index 3ed1c87..9171648 100644 --- a/shared/utils.h +++ b/shared/utils.h @@ -12,14 +12,14 @@ extern unsigned debug_mask; -#define _unused_ __attribute__((__unused__)) -#define _pure_ __attribute__((__pure__)) -#define _const_ __attribute__((__const__)) -#define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__))) -#define _malloc_ __attribute__((__malloc__)) +#define _unused_ __attribute__((__unused__)) +#define _pure_ __attribute__((__pure__)) +#define _const_ __attribute__((__const__)) +#define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__))) +#define _malloc_ __attribute__((__malloc__)) #define _printf_(a, b) __attribute__((__format__(printf, a, b))) -#define _alignas_(x) __attribute__((__aligned__(__alignof(x)))) -#define _big_endian_ __attribute__((packed, scalar_storage_order("big-endian"))) +#define _alignas_(x) __attribute__((__aligned__(__alignof(x)))) +#define _big_endian_ __attribute__((packed, scalar_storage_order("big-endian"))) #if __GNUC__ >= 7 #define _fallthrough_ __attribute__((__fallthrough__)) @@ -35,7 +35,7 @@ extern unsigned debug_mask; #endif #endif -#define STRLEN(x) (sizeof(""x"") - 1) +#define STRLEN(x) (sizeof("" x "") - 1) #include "list.h" #include "debug.h" @@ -56,7 +56,7 @@ extern unsigned debug_mask; struct saddr { union { - struct sockaddr_storage storage; + struct sockaddr_storage st; struct sockaddr_in in4; struct sockaddr_in6 in6; struct sockaddr_ll ll; @@ -74,7 +74,8 @@ uint16_t saddr_port(struct saddr *saddr); void saddr_set_ipv4(struct saddr *saddr, in_addr_t ip, in_port_t port); -void saddr_set_ipv6(struct saddr *saddr, const struct in6_addr *ip, in_port_t port); +void saddr_set_ipv6(struct saddr *saddr, const struct in6_addr *ip, + in_port_t port); void saddr_set_addrstr(struct saddr *saddr); @@ -105,9 +106,11 @@ static inline bool strcaseeq(const char *a, const char *b) #endif #if __BYTE_ORDER == __LITTLE_ENDIAN -#define cinet_addr(a,b,c,d) ((uint32_t)((a)<<0|(b)<<8|(c)<<16|(d)<<24)) +#define cinet_addr(a, b, c, d) \ + ((uint32_t)((a) << 0 | (b) << 8 | (c) << 16 | (d) << 24)) #else -#define cinet_addr(a,b,c,d) ((uint32_t)((a)<<24|(b)<<16|(c)<<8|(d)<<0)) +#define cinet_addr(a, b, c, d) \ + ((uint32_t)((a) << 24 | (b) << 16 | (c) << 8 | (d) << 0)) #endif #define PIPE_RD 0 @@ -117,7 +120,6 @@ static inline bool strcaseeq(const char *a, const char *b) #define MAX(a, b) ((a) > (b) ? (a) : (b)) -#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) +#define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d)) #endif - |