summaryrefslogtreecommitdiff
path: root/minecproxy/misc.c
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-28 14:58:32 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-28 14:58:32 +0200
commitd3352b997ca59a336a40fe6660c6e5b7079864aa (patch)
treef365d6c0304e512057223e9a3cf37357da44a443 /minecproxy/misc.c
parent99b2c70137fef05a5a18f439b9010ddba455f5cb (diff)
Run clang-format on source tree (excl igmp.c)
Diffstat (limited to 'minecproxy/misc.c')
-rw-r--r--minecproxy/misc.c124
1 files changed, 59 insertions, 65 deletions
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);
}
-