diff options
-rw-r--r-- | announce.c | 9 | ||||
-rw-r--r-- | cfgdir.c | 24 | ||||
-rw-r--r-- | config.c | 130 | ||||
-rw-r--r-- | config.h | 2 | ||||
-rw-r--r-- | idle.c | 6 | ||||
-rw-r--r-- | igmp.c | 6 | ||||
-rw-r--r-- | main.h | 2 | ||||
-rw-r--r-- | proxy.c | 14 | ||||
-rw-r--r-- | proxy.h | 2 | ||||
-rw-r--r-- | server.c | 79 | ||||
-rw-r--r-- | server.h | 6 | ||||
-rw-r--r-- | uring.c | 16 | ||||
-rw-r--r-- | uring.h | 4 | ||||
-rw-r--r-- | utils.c | 57 | ||||
-rw-r--r-- | utils.h | 18 |
15 files changed, 189 insertions, 186 deletions
@@ -178,10 +178,11 @@ announce_init(struct cfg *cfg) uring_task_init(&aev->mcast_task, "aev_mcast", &aev->task, mcast_free); uring_task_set_fd(&aev->mcast_task, sfd); - aev->mcast_task.addr.in4.sin_family = AF_INET; - aev->mcast_task.addr.in4.sin_addr.s_addr = inet_addr("224.0.2.60"); - aev->mcast_task.addr.in4.sin_port = htons(4445); - aev->mcast_task.addr.addrlen = sizeof(aev->mcast_task.addr.in4); + aev->mcast_task.saddr.in4.sin_family = AF_INET; + aev->mcast_task.saddr.in4.sin_addr.s_addr = inet_addr("224.0.2.60"); + aev->mcast_task.saddr.in4.sin_port = htons(4445); + aev->mcast_task.saddr.addrlen = sizeof(aev->mcast_task.saddr.in4); + saddr_set_str(&aev->mcast_task.saddr); cfg->aev = aev; uring_read(cfg, &aev->task, &aev->value, sizeof(aev->value), announce_cb); @@ -140,21 +140,21 @@ scfg_parse(struct cfg *cfg, struct server *scfg) break; case SCFG_KEY_LOCAL: { - struct sockaddr_in46 *addr, *tmp; + struct saddr *saddr, *tmp; - list_for_each_entry_safe(addr, tmp, &value.addrs, list) { - list_del(&addr->list); - server_add_local(cfg, scfg, addr); + list_for_each_entry_safe(saddr, tmp, &value.saddrs, list) { + list_del(&saddr->list); + server_add_local(cfg, scfg, saddr); } break; } case SCFG_KEY_REMOTE: { - struct sockaddr_in46 *addr, *tmp; + struct saddr *saddr, *tmp; - list_for_each_entry_safe(addr, tmp, &value.addrs, list) { - list_del(&addr->list); - server_add_remote(cfg, scfg, addr); + list_for_each_entry_safe(saddr, tmp, &value.saddrs, list) { + list_del(&saddr->list); + server_add_remote(cfg, scfg, saddr); } break; } @@ -198,11 +198,11 @@ scfg_parse(struct cfg *cfg, struct server *scfg) break; case SCFG_KEY_RCON: { - struct sockaddr_in46 *addr, *tmp; + struct saddr *saddr, *tmp; - list_for_each_entry_safe(addr, tmp, &value.addrs, list) { - list_del(&addr->list); - server_add_rcon(cfg, scfg, addr); + list_for_each_entry_safe(saddr, tmp, &value.saddrs, list) { + list_del(&saddr->list); + server_add_rcon(cfg, scfg, saddr); } break; } @@ -67,7 +67,7 @@ strtosockaddrs(const char *str, struct list_head *list) char *tmp; uint16_t port; int r; - struct sockaddr_in46 *addr; + struct saddr *saddr; if (!str || *str == '\0' || !list) return false; @@ -83,14 +83,16 @@ strtosockaddrs(const char *str, struct list_head *list) goto out; *tmp = '\0'; - addr = zmalloc(sizeof(*addr)); - if (!addr) + saddr = zmalloc(sizeof(*saddr)); + if (!saddr) goto out; - list_add(&addr->list, list); + + /* early list_add to make sure saddr is free():d on error */ + list_add(&saddr->list, list); if (!strcmp(str, "*")) - addr->in6.sin6_addr = in6addr_any; - else if (inet_pton(AF_INET6, str, &addr->in6.sin6_addr) <= 0) + saddr->in6.sin6_addr = in6addr_any; + else if (inet_pton(AF_INET6, str, &saddr->in6.sin6_addr) <= 0) goto out; tmp++; @@ -101,9 +103,10 @@ strtosockaddrs(const char *str, struct list_head *list) if (strtou16_strict(tmp, &port) < 0) goto out; - addr->in6.sin6_family = AF_INET6; - addr->in6.sin6_port = htons(port); - addr->addrlen = sizeof(addr->in6); + saddr->in6.sin6_family = AF_INET6; + saddr->in6.sin6_port = htons(port); + saddr->addrlen = sizeof(saddr->in6); + saddr_set_str(saddr); } else if (*str == '*') { /* IPv4, *:p */ @@ -117,15 +120,16 @@ strtosockaddrs(const char *str, struct list_head *list) if (strtou16_strict(str, &port) < 0) goto out; - addr = zmalloc(sizeof(*addr)); - if (!addr) + saddr = zmalloc(sizeof(*saddr)); + if (!saddr) goto out; - list_add(&addr->list, list); - addr->in4.sin_family = AF_INET; - addr->in4.sin_addr.s_addr = INADDR_ANY; - addr->in4.sin_port = htons(port); - addr->addrlen = sizeof(addr->in4); + saddr->in4.sin_family = AF_INET; + saddr->in4.sin_addr.s_addr = INADDR_ANY; + saddr->in4.sin_port = htons(port); + saddr->addrlen = sizeof(saddr->in4); + saddr_set_str(saddr); + list_add(&saddr->list, list); } else if ((tmp = strchr(str, ':'))) { /* IPv4, a.b.c.d:p or IPv4/6 hostname:p */ @@ -136,19 +140,20 @@ strtosockaddrs(const char *str, struct list_head *list) if (strtou16_strict(tmp, &port) < 0) goto out; - addr = zmalloc(sizeof(*addr)); - if (!addr) + saddr = zmalloc(sizeof(*saddr)); + if (!saddr) goto out; - if (inet_pton(AF_INET, str, &addr->in4.sin_addr) > 0) { + if (inet_pton(AF_INET, str, &saddr->in4.sin_addr) > 0) { debug(DBG_CFG, "got an IPv4:port (%s)\n", str); - addr->in4.sin_family = AF_INET; - addr->in4.sin_port = htons(port); - addr->addrlen = sizeof(addr->in4); - list_add(&addr->list, list); + saddr->in4.sin_family = AF_INET; + saddr->in4.sin_port = htons(port); + saddr->addrlen = sizeof(saddr->in4); + saddr_set_str(saddr); + list_add(&saddr->list, list); goto success; } else { - xfree(addr); + xfree(saddr); } struct addrinfo hints = { @@ -168,8 +173,8 @@ strtosockaddrs(const char *str, struct list_head *list) debug(DBG_CFG, "got a hostname:port (%s)\n", str); for (ai = results; ai; ai = ai->ai_next) { - addr = zmalloc(sizeof(*addr)); - if (!addr) { + saddr = zmalloc(sizeof(*saddr)); + if (!saddr) { freeaddrinfo(results); goto out; } @@ -178,27 +183,29 @@ strtosockaddrs(const char *str, struct list_head *list) case AF_INET: { struct sockaddr_in *naddr = (struct sockaddr_in *)ai->ai_addr; - addr->in4.sin_family = AF_INET; - addr->in4.sin_addr = naddr->sin_addr; - addr->in4.sin_port = naddr->sin_port; - addr->addrlen = sizeof(addr->in4); - list_add(&addr->list, list); + saddr->in4.sin_family = AF_INET; + saddr->in4.sin_addr = naddr->sin_addr; + saddr->in4.sin_port = naddr->sin_port; + saddr->addrlen = sizeof(saddr->in4); + saddr_set_str(saddr); + list_add(&saddr->list, list); break; } case AF_INET6: { struct sockaddr_in6 *naddr = (struct sockaddr_in6 *)ai->ai_addr; - addr->in6.sin6_family = AF_INET6; - addr->in6.sin6_addr = naddr->sin6_addr; - addr->in6.sin6_port = naddr->sin6_port; - addr->addrlen = sizeof(addr->in6); - list_add(&addr->list, list); + saddr->in6.sin6_family = AF_INET6; + saddr->in6.sin6_addr = naddr->sin6_addr; + saddr->in6.sin6_port = naddr->sin6_port; + saddr->addrlen = sizeof(saddr->in6); + saddr_set_str(saddr); + list_add(&saddr->list, list); break; } default: error("getaddrinfo(%s): unknown address family (%i)\n", str, ai->ai_family); - xfree(addr); + xfree(saddr); continue; } } @@ -209,23 +216,28 @@ strtosockaddrs(const char *str, struct list_head *list) /* Port */ debug(DBG_CFG, "attempting to parse a port number (%s)\n", str); - addr = zmalloc(sizeof(*addr)); - if (!addr) + saddr = zmalloc(sizeof(*saddr)); + if (!saddr) goto out; - addr->in6.sin6_family = AF_INET6; - addr->in6.sin6_addr = in6addr_any; - addr->in6.sin6_port = htons(port); - addr->addrlen = sizeof(addr->in6); - list_add(&addr->list, list); - - addr = zmalloc(sizeof(*addr)); - if (!addr) + + saddr->in6.sin6_family = AF_INET6; + saddr->in6.sin6_addr = in6addr_any; + saddr->in6.sin6_port = htons(port); + saddr->addrlen = sizeof(saddr->in6); + saddr_set_str(saddr); + list_add(&saddr->list, list); + + saddr = zmalloc(sizeof(*saddr)); + if (!saddr) goto out; - addr->in4.sin_family = AF_INET; - addr->in4.sin_addr.s_addr = INADDR_ANY; - addr->in4.sin_port = htons(port); - addr->addrlen = sizeof(addr->in4); - list_add(&addr->list, list); + + saddr->in4.sin_family = AF_INET; + saddr->in4.sin_addr.s_addr = INADDR_ANY; + saddr->in4.sin_port = htons(port); + saddr->addrlen = sizeof(saddr->in4); + saddr_set_str(saddr); + list_add(&saddr->list, list); + } else { /* Unknown */ error("unable to parse address: %s\n", str); @@ -239,7 +251,7 @@ success: } else { int i = 0; - list_for_each_entry(addr, list, list) + list_for_each_entry(saddr, list, list) i++; debug(DBG_CFG, "parsed to %i addresses\n", i); @@ -249,11 +261,11 @@ success: out: if (!list_empty(list)) { - struct sockaddr_in46 *tmp; + struct saddr *tmp; - list_for_each_entry_safe(addr, tmp, list, list) { - list_del(&addr->list); - xfree(addr); + list_for_each_entry_safe(saddr, tmp, list, list) { + list_del(&saddr->list); + xfree(saddr); } } return false; @@ -328,9 +340,9 @@ config_parse_line(struct cfg *cfg, const char *filename, char **buf, } case CFG_VAL_TYPE_ADDRS: { - if (!strtosockaddrs(tmp, &rvalue->addrs)) + if (!strtosockaddrs(tmp, &rvalue->saddrs)) goto out; - if (list_empty(&rvalue->addrs)) { + if (list_empty(&rvalue->saddrs)) { error("empty address list\n"); goto out; } @@ -18,7 +18,7 @@ struct cfg_key_value_map { union cfg_value { const char *str; uint16_t uint16; - struct list_head addrs; + struct list_head saddrs; bool boolean; }; @@ -303,10 +303,10 @@ idle_check_connected_cb(struct cfg *cfg, struct connection *conn, bool connected return; } - debug(DBG_IDLE, "connected to remote %s\n", idle->conn.remotestr); + debug(DBG_IDLE, "connected to remote %s\n", idle->conn.remote.addrstr); - port = sockaddr_port(&conn->remote); - sockaddr_addr(&conn->remote, hostname, sizeof(hostname)); + port = saddr_port(&conn->remote); + saddr_addr(&conn->remote, hostname, sizeof(hostname)); pos = buf; write_byte(&pos, MC_HELO); @@ -396,8 +396,8 @@ igmp_read_cb(struct cfg *cfg, struct uring_task *task, int res) task->tbuf->len = res; - if (task->addr.storage.ss_family == AF_PACKET || - task->addr.ll.sll_protocol == htons(ETH_P_IP)) + if (task->saddr.storage.ss_family == AF_PACKET || + task->saddr.ll.sll_protocol == htons(ETH_P_IP)) igmp_parse(cfg, igmp); else debug(DBG_IGMP, "invalid packet type received\n"); @@ -556,7 +556,7 @@ igmp_init(struct cfg *cfg) uring_task_init(&igmp->task, "igmp", uring_parent(cfg), igmp_free); uring_task_set_fd(&igmp->task, sfd); uring_task_set_buf(&igmp->task, &igmp->tbuf); - igmp->task.addr.addrlen = sizeof(igmp->task.addr.ll); + igmp->task.saddr.addrlen = sizeof(igmp->task.saddr.ll); uring_tbuf_recvmsg(cfg, &igmp->task, igmp_read_cb); cfg->igmp = igmp; @@ -83,7 +83,7 @@ struct uring_task { callback_t callback; rcallback_t complete_callback; /* to check if tbuf processing is done */ callback_t final_callback; /* once tbuf processing is done */ - struct sockaddr_in46 addr; /* used for recvmsg/sendmsg */ + struct saddr saddr; /* used for recvmsg/sendmsg */ void *priv; }; @@ -69,8 +69,8 @@ proxy_free(struct uring_task *task) debug(DBG_PROXY, "server: %s, src: %s, dst: %s\n", proxy->scfg->name, - proxy->client_conn.remotestr, - proxy->server_conn.remotestr); + proxy->client_conn.remote.addrstr, + proxy->server_conn.remote.addrstr); if (proxy->begin > 0) { format_time(duration, sizeof(duration), time(NULL) - proxy->begin); @@ -80,8 +80,8 @@ proxy_free(struct uring_task *task) info("%s: proxy connection %s -> %s closed " "(CtS: %s, StC: %s), duration %s\n", proxy->scfg->name, - proxy->client_conn.remotestr, - proxy->server_conn.remotestr, + proxy->client_conn.remote.addrstr, + proxy->server_conn.remote.addrstr, cts, stc, duration); } @@ -200,8 +200,8 @@ proxy_connected_cb(struct cfg *cfg, struct connection *conn, bool connected) proxy->sfd = proxy->servertask.fd; verbose("%s: proxy connection %s -> %s opened\n", proxy->scfg->name, - proxy->client_conn.remotestr, - proxy->server_conn.remotestr); + proxy->client_conn.remote.addrstr, + proxy->server_conn.remote.addrstr); proxy->begin = time(NULL); uring_tbuf_read(cfg, &proxy->clienttask, proxy_client_data_in); @@ -209,7 +209,7 @@ proxy_connected_cb(struct cfg *cfg, struct connection *conn, bool connected) } struct server_proxy * -proxy_new(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *client, int fd) +proxy_new(struct cfg *cfg, struct server *scfg, struct saddr *client, int fd) { struct server_proxy *proxy; @@ -25,6 +25,6 @@ void proxy_refdump(struct server_proxy *proxy); void proxy_delete(struct cfg *cfg, struct server_proxy *proxy); -struct server_proxy *proxy_new(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *client, int fd); +struct server_proxy *proxy_new(struct cfg *cfg, struct server *scfg, struct saddr *client, int fd); #endif @@ -24,9 +24,8 @@ #include "systemd.h" struct server_local { - struct sockaddr_in46 addr; - char addrstr[ADDRSTRLEN]; - struct sockaddr_in46 peer; + struct saddr local; + struct saddr client; struct uring_task task; struct list_head list; }; @@ -83,8 +82,9 @@ server_delete(struct cfg *cfg, struct server *scfg) { struct server_local *local, *ltmp; struct server_proxy *proxy, *ptmp; - struct sockaddr_in46 *remote, *rtmp; - struct sockaddr_in46 *rcon, *rctmp; + struct saddr *remote; + struct saddr *rcon; + struct saddr *tmp; verbose("Removing server %s\n", scfg->name); @@ -97,12 +97,12 @@ server_delete(struct cfg *cfg, struct server *scfg) list_for_each_entry_safe(proxy, ptmp, &scfg->proxys, list) proxy_delete(cfg, proxy); - list_for_each_entry_safe(rcon, rctmp, &scfg->rcons, list) { + list_for_each_entry_safe(rcon, tmp, &scfg->rcons, list) { list_del(&rcon->list); xfree(rcon); } - list_for_each_entry_safe(remote, rtmp, &scfg->remotes, list) { + list_for_each_entry_safe(remote, tmp, &scfg->remotes, list) { list_del(&remote->list); xfree(remote); } @@ -132,9 +132,8 @@ static void server_dump(struct server *scfg) { struct server_local *local; - struct sockaddr_in46 *remote; - struct sockaddr_in46 *rcon; - char abuf[ADDRSTRLEN]; + struct saddr *remote; + struct saddr *rcon; verbose("Server %s:\n", scfg->name); switch (scfg->type) { @@ -152,21 +151,21 @@ server_dump(struct server *scfg) verbose(" * Announce port: %" PRIu16 "\n", scfg->announce_port); if (!list_empty(&scfg->locals)) { - verbose(" * Listening:\n"); + verbose(" * Local:\n"); list_for_each_entry(local, &scfg->locals, list) - verbose(" * %s\n", local->addrstr); + verbose(" * %s\n", local->local.addrstr); } if (!list_empty(&scfg->remotes)) { verbose(" * Remote:\n"); list_for_each_entry(remote, &scfg->remotes, list) - verbose(" * %s\n", sockaddr_to_str(remote, abuf, sizeof(abuf))); + verbose(" * %s\n", remote->addrstr); } if (!list_empty(&scfg->rcons)) { verbose(" * RCon:\n"); list_for_each_entry(rcon, &scfg->rcons, list) - verbose(" * %s\n", sockaddr_to_str(rcon, abuf, sizeof(abuf))); + verbose(" * %s\n", rcon->addrstr); } verbose("\n"); @@ -188,9 +187,6 @@ server_local_accept(struct cfg *cfg, struct uring_task *task, int res) struct server_local *local = container_of(task, struct server_local, task); struct server *scfg = container_of(task->parent, struct server, task); struct server_proxy *proxy; - char pbuf[ADDRSTRLEN]; - struct sockaddr_in46 laddr = { .addrlen = sizeof(laddr) } ; - char lbuf[ADDRSTRLEN]; debug(DBG_SRV, "task %p, res %i, scfg %s\n", task, res, scfg->name); @@ -204,15 +200,10 @@ server_local_accept(struct cfg *cfg, struct uring_task *task, int res) goto out; } - if (getsockname(res, (struct sockaddr *)&laddr.storage, &laddr.addrlen) < 0) - sprintf(lbuf, "<unknown>"); - else - sockaddr_to_str(&laddr, lbuf, sizeof(lbuf)); - - sockaddr_to_str(&local->peer, pbuf, sizeof(pbuf)); + saddr_set_str(&local->client); - verbose("%s: incoming proxy connection: %s -> %s (%s)\n", - scfg->name, pbuf, lbuf, local->addrstr); + verbose("%s: incoming proxy connection: %s -> %s\n", + scfg->name, local->client.addrstr, local->local.addrstr); if (list_empty(&scfg->remotes)) { /* This shouldn't be possible, checked before opening local */ @@ -221,12 +212,12 @@ server_local_accept(struct cfg *cfg, struct uring_task *task, int res) goto out; } - proxy = proxy_new(cfg, scfg, &local->peer, res); + proxy = proxy_new(cfg, scfg, &local->client, res); if (!proxy) uring_close(cfg, &local->task, res); out: - uring_accept(cfg, &local->task, &local->peer, server_local_accept); + uring_accept(cfg, &local->task, &local->client, server_local_accept); } static bool @@ -236,7 +227,7 @@ server_local_open(struct cfg *cfg, struct server *scfg, struct server_local *loc int option; int r; - sfd = socket(local->addr.storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0); + sfd = socket(local->local.storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0); if (sfd < 0) { error("socket: %m"); goto out; @@ -261,7 +252,7 @@ server_local_open(struct cfg *cfg, struct server *scfg, struct server_local *loc socket_set_low_latency(cfg, sfd); - r = bind(sfd, (struct sockaddr *)&local->addr.storage, local->addr.addrlen); + r = bind(sfd, (struct sockaddr *)&local->local.storage, local->local.addrlen); if (r < 0) { error("bind: %m"); goto out; @@ -274,7 +265,7 @@ server_local_open(struct cfg *cfg, struct server *scfg, struct server_local *loc } uring_task_set_fd(&local->task, sfd); - uring_accept(cfg, &local->task, &local->peer, server_local_accept); + uring_accept(cfg, &local->task, &local->client, server_local_accept); return true; out: @@ -508,7 +499,7 @@ server_commit(struct cfg *cfg, struct server *scfg) } list_for_each_entry(local, &scfg->locals, list) { - port = sockaddr_port(&local->addr); + port = saddr_port(&local->local); if (port == 0) { error("%s: invalid local port\n", scfg->name); @@ -570,21 +561,21 @@ server_commit(struct cfg *cfg, struct server *scfg) } bool -server_add_remote(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *addr) +server_add_remote(struct cfg *cfg, struct server *scfg, struct saddr *remote) { - if (!scfg || !addr) + if (!scfg || !remote) return false; - list_add(&addr->list, &scfg->remotes); + list_add(&remote->list, &scfg->remotes); return true; } bool -server_add_local(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *addr) +server_add_local(struct cfg *cfg, struct server *scfg, struct saddr *saddr) { struct server_local *local; - if (!scfg || !addr) { + if (!scfg || !saddr) { error("missing arguments\n"); return false; } @@ -595,23 +586,23 @@ server_add_local(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *add return false; } - local->addr.storage = addr->storage; - local->addr.addrlen = addr->addrlen; + local->local.storage = saddr->storage; + local->local.addrlen = saddr->addrlen; + saddr_set_str(&local->local); uring_task_init(&local->task, "local", &scfg->task, server_local_free); - sockaddr_to_str(&local->addr, local->addrstr, sizeof(local->addrstr)); + list_add(&local->list, &scfg->locals); - xfree(addr); + xfree(saddr); return true; } bool -server_add_rcon(struct cfg *cfg, struct server *scfg, - struct sockaddr_in46 *addr) +server_add_rcon(struct cfg *cfg, struct server *scfg, struct saddr *rcon) { - if (!scfg || !addr) + if (!scfg || !rcon) return false; - list_add(&addr->list, &scfg->rcons); + list_add(&rcon->list, &scfg->rcons); return true; } @@ -75,13 +75,13 @@ bool server_stop(struct cfg *cfg, struct server *scfg); bool server_commit(struct cfg *cfg, struct server *scfg); bool server_add_remote(struct cfg *cfg, struct server *scfg, - struct sockaddr_in46 *addr); + struct saddr *remote); bool server_add_local(struct cfg *cfg, struct server *scfg, - struct sockaddr_in46 *addr); + struct saddr *saddr); bool server_add_rcon(struct cfg *cfg, struct server *scfg, - struct sockaddr_in46 *addr); + struct saddr *rcon); bool server_set_rcon_password(struct cfg *cfg, struct server *scfg, const char *password); @@ -162,7 +162,7 @@ uring_task_set_buf(struct uring_task *task, struct uring_task_buf *tbuf) if (tbuf) { /* iov_len and msg_namelen are set at send/receive time */ tbuf->iov.iov_base = tbuf->buf; - tbuf->msg.msg_name = &task->addr.storage; + tbuf->msg.msg_name = &task->saddr.storage; tbuf->msg.msg_iov = &tbuf->iov; tbuf->msg.msg_iovlen = 1; tbuf->msg.msg_control = NULL; @@ -470,7 +470,7 @@ uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, callback_t callback task->tbuf->done = 0; task->tbuf->len = 0; task->tbuf->iov.iov_len = sizeof(task->tbuf->buf); - task->tbuf->msg.msg_namelen = task->addr.addrlen; + task->tbuf->msg.msg_namelen = task->saddr.addrlen; task->callback = callback; io_uring_prep_recvmsg(sqe, task->fd, &task->tbuf->msg, 0); io_uring_sqe_set_data(sqe, task); @@ -492,14 +492,14 @@ uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, callback_t callback sqe = get_sqe(cfg, task); task->tbuf->done = 0; task->tbuf->iov.iov_len = task->tbuf->len; - task->tbuf->msg.msg_namelen = task->addr.addrlen; + task->tbuf->msg.msg_namelen = task->saddr.addrlen; task->callback = callback; io_uring_prep_sendmsg(sqe, task->fd, &task->tbuf->msg, 0); io_uring_sqe_set_data(sqe, task); } void -uring_connect(struct cfg *cfg, struct uring_task *task, struct sockaddr_in46 *addr, callback_t callback) +uring_connect(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, callback_t callback) { struct io_uring_sqe *sqe; @@ -513,12 +513,12 @@ uring_connect(struct cfg *cfg, struct uring_task *task, struct sockaddr_in46 *ad sqe = get_sqe(cfg, task); task->callback = callback; - io_uring_prep_connect(sqe, task->fd, (struct sockaddr *)&addr->storage, addr->addrlen); + io_uring_prep_connect(sqe, task->fd, (struct sockaddr *)&saddr->storage, saddr->addrlen); io_uring_sqe_set_data(sqe, task); } void -uring_accept(struct cfg *cfg, struct uring_task *task, struct sockaddr_in46 *addr, callback_t callback) +uring_accept(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, callback_t callback) { struct io_uring_sqe *sqe; @@ -531,9 +531,9 @@ uring_accept(struct cfg *cfg, struct uring_task *task, struct sockaddr_in46 *add task->name, task, task->fd, task->refcount); sqe = get_sqe(cfg, task); - addr->addrlen = sizeof(addr->storage); + saddr->addrlen = sizeof(saddr->storage); task->callback = callback; - io_uring_prep_accept(sqe, task->fd, (struct sockaddr *)&addr->storage, &addr->addrlen, SOCK_CLOEXEC); + io_uring_prep_accept(sqe, task->fd, (struct sockaddr *)&saddr->storage, &saddr->addrlen, SOCK_CLOEXEC); io_uring_sqe_set_data(sqe, task); } @@ -56,10 +56,10 @@ void uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, callback_t cal void uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, callback_t callback); void uring_connect(struct cfg *cfg, struct uring_task *task, - struct sockaddr_in46 *addr, callback_t callback); + struct saddr *saddr, callback_t callback); void uring_accept(struct cfg *cfg, struct uring_task *task, - struct sockaddr_in46 *addr, callback_t callback); + struct saddr *saddr, callback_t callback); void uring_poll(struct cfg *cfg, struct uring_task *task, short poll_mask, callback_t callback); @@ -190,19 +190,20 @@ socket_set_low_latency(struct cfg *cfg, int sfd) void connection_set_local(struct cfg *cfg, struct connection *conn, int fd) { + conn->local.addrlen = sizeof(conn->local.storage); if (fd < 0 || getsockname(fd, (struct sockaddr *)&conn->local.storage, &conn->local.addrlen) < 0) - sprintf(conn->localstr, "<unknown>"); + sprintf(conn->local.addrstr, "<unknown>"); else - sockaddr_to_str(&conn->local, conn->localstr, sizeof(conn->localstr)); + saddr_set_str(&conn->local); } void -connection_set_remote(struct cfg *cfg, struct connection *conn, struct sockaddr_in46 *remote) +connection_set_remote(struct cfg *cfg, struct connection *conn, struct saddr *remote) { conn->remote = *remote; - sockaddr_to_str(&conn->remote, conn->remotestr, sizeof(conn->remotestr)); + saddr_set_str(&conn->remote); } static void connect_next(struct cfg *cfg, struct uring_task *task, struct connection *conn); @@ -214,7 +215,7 @@ connect_cb(struct cfg *cfg, struct uring_task *task, int res) if (res < 0) { debug(DBG_UR, "%s: connection to %s failed\n", - task->name, conn->remotestr); + task->name, conn->remote.addrstr); uring_task_close_fd(cfg, task); connect_next(cfg, task, conn); return; @@ -223,7 +224,7 @@ connect_cb(struct cfg *cfg, struct uring_task *task, int res) connection_set_local(cfg, conn, task->fd); debug(DBG_UR, "%s: connection established %s -> %s\n", - task->name, conn->localstr, conn->remotestr); + task->name, conn->local.addrstr, conn->remote.addrstr); conn->callback(cfg, conn, true); } @@ -231,7 +232,7 @@ connect_cb(struct cfg *cfg, struct uring_task *task, int res) static void connect_next(struct cfg *cfg, struct uring_task *task, struct connection *conn) { - struct sockaddr_in46 *remote, *tmp; + struct saddr *remote, *tmp; int sfd; unsigned i; @@ -256,7 +257,7 @@ again: conn->next_addr++; connection_set_remote(cfg, conn, remote); debug(DBG_MALLOC, "%s: attempting to connect to %s\n", - task->name, conn->remotestr); + task->name, conn->remote.addrstr); sfd = socket(conn->remote.storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0); if (sfd < 0) { @@ -288,28 +289,28 @@ connect_any(struct cfg *cfg, struct uring_task *task, } uint16_t -sockaddr_port(struct sockaddr_in46 *addr) +saddr_port(struct saddr *saddr) { - switch (addr->storage.ss_family) { + switch (saddr->storage.ss_family) { case AF_INET: - return ntohs(addr->in4.sin_port); + return ntohs(saddr->in4.sin_port); case AF_INET6: - return ntohs(addr->in6.sin6_port); + return ntohs(saddr->in6.sin6_port); default: return 0; } } char * -sockaddr_addr(struct sockaddr_in46 *addr, char *buf, size_t len) +saddr_addr(struct saddr *saddr, char *buf, size_t len) { - switch (addr->storage.ss_family) { + switch (saddr->storage.ss_family) { case AF_INET: - if (inet_ntop(addr->in4.sin_family, &addr->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(addr->in6.sin6_family, &addr->in6.sin6_addr, buf, len)) + if (inet_ntop(saddr->in6.sin6_family, &saddr->in6.sin6_addr, buf, len)) return buf; break; default: @@ -320,28 +321,28 @@ sockaddr_addr(struct sockaddr_in46 *addr, char *buf, size_t len) return buf; } -char * -sockaddr_to_str(struct sockaddr_in46 *addr, char *buf, size_t len) +void +saddr_set_str(struct saddr *saddr) { char abuf[ADDRSTRLEN]; - switch (addr->storage.ss_family) { + switch (saddr->storage.ss_family) { case AF_INET: - snprintf(buf, len, "AF_INET4 %s %" PRIu16, - sockaddr_addr(addr, abuf, sizeof(abuf)), - sockaddr_port(addr)); + snprintf(saddr->addrstr, sizeof(saddr->addrstr), + "AF_INET4 %s %" PRIu16, + saddr_addr(saddr, abuf, sizeof(abuf)), + saddr_port(saddr)); break; case AF_INET6: - snprintf(buf, len, "AF_INET6 %s %" PRIu16, - sockaddr_addr(addr, abuf, sizeof(abuf)), - sockaddr_port(addr)); + snprintf(saddr->addrstr, sizeof(saddr->addrstr), + "AF_INET6 %s %" PRIu16, + saddr_addr(saddr, abuf, sizeof(abuf)), + saddr_port(saddr)); break; default: - snprintf(buf, len, "AF_UNKNOWN"); + snprintf(saddr->addrstr, sizeof(saddr->addrstr), "AF_UNKNOWN"); break; } - - return buf; } int @@ -26,7 +26,7 @@ void __xfree(const char *fn, int line, void *ptr); void debug_resource_usage(); #define ADDRSTRLEN (9 /*strlen("AF_INETX ")*/ + INET6_ADDRSTRLEN + 6 /*strlen(" 65535")*/ + 1) -struct sockaddr_in46 { +struct saddr { union { struct sockaddr_storage storage; struct sockaddr_in in4; @@ -34,15 +34,13 @@ struct sockaddr_in46 { struct sockaddr_ll ll; }; socklen_t addrlen; + char addrstr[ADDRSTRLEN]; struct list_head list; }; struct connection { - struct sockaddr_in46 remote; - char remotestr[ADDRSTRLEN]; - - struct sockaddr_in46 local; - char localstr[ADDRSTRLEN]; + struct saddr remote; + struct saddr local; struct list_head *addrs; unsigned next_addr; @@ -57,17 +55,17 @@ void socket_set_low_latency(struct cfg *cfg, int sfd); void connection_set_local(struct cfg *cfg, struct connection *conn, int fd); void connection_set_remote(struct cfg *cfg, struct connection *conn, - struct sockaddr_in46 *remote); + struct saddr *remote); void connect_any(struct cfg *cfg, struct uring_task *task, struct list_head *addrs, struct connection *conn, void (*callback)(struct cfg *, struct connection *, bool)); -char *sockaddr_addr(struct sockaddr_in46 *addr, char *buf, size_t len); +char *saddr_addr(struct saddr *saddr, char *buf, size_t len); -uint16_t sockaddr_port(struct sockaddr_in46 *addr); +uint16_t saddr_port(struct saddr *saddr); -char *sockaddr_to_str(struct sockaddr_in46 *addr, char *buf, size_t len); +void saddr_set_str(struct saddr *saddr); int strtou16_strict(const char *str, uint16_t *result); |