summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-19 00:57:54 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-19 00:57:54 +0200
commit3d6bdb2cc11a293f536f38a975e4ae2095d467fa (patch)
tree26e48d2e87e8f137129cdf9b95e0824f8e8350ae
parentb32800e33ec16f7c592fa09b1a8b1f30a2e9df77 (diff)
Add some helper functions for saddrs, reduce code duplication
-rw-r--r--announce.c6
-rw-r--r--config.c46
-rw-r--r--server.c6
-rw-r--r--utils.c29
-rw-r--r--utils.h6
5 files changed, 45 insertions, 48 deletions
diff --git a/announce.c b/announce.c
index a2c120d..e957ad2 100644
--- a/announce.c
+++ b/announce.c
@@ -178,11 +178,7 @@ 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.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);
+ saddr_set_ipv4(&aev->mcast_task.saddr, cinet_addr(224,0,2,60), htons(4445));
cfg->aev = aev;
uring_read(cfg, &aev->task, &aev->value, sizeof(aev->value), announce_cb);
diff --git a/config.c b/config.c
index de0522e..bb107ef 100644
--- a/config.c
+++ b/config.c
@@ -103,10 +103,7 @@ strtosockaddrs(const char *str, struct list_head *list)
if (strtou16_strict(tmp, &port) < 0)
goto out;
- saddr->in6.sin6_family = AF_INET6;
- saddr->in6.sin6_port = htons(port);
- saddr->addrlen = sizeof(saddr->in6);
- saddr_set_str(saddr);
+ saddr_set_ipv6(saddr, NULL, htons(port));
} else if (*str == '*') {
/* IPv4, *:p */
@@ -124,11 +121,7 @@ strtosockaddrs(const char *str, struct list_head *list)
if (!saddr)
goto out;
- 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);
+ saddr_set_ipv4(saddr, INADDR_ANY, htons(port));
list_add(&saddr->list, list);
} else if ((tmp = strchr(str, ':'))) {
@@ -146,10 +139,7 @@ strtosockaddrs(const char *str, struct list_head *list)
if (inet_pton(AF_INET, str, &saddr->in4.sin_addr) > 0) {
debug(DBG_CFG, "got an IPv4:port (%s)\n", str);
- saddr->in4.sin_family = AF_INET;
- saddr->in4.sin_port = htons(port);
- saddr->addrlen = sizeof(saddr->in4);
- saddr_set_str(saddr);
+ saddr_set_ipv4(saddr, saddr->in4.sin_addr.s_addr, htons(port));
list_add(&saddr->list, list);
goto success;
} else {
@@ -181,27 +171,21 @@ strtosockaddrs(const char *str, struct list_head *list)
switch (ai->ai_family) {
case AF_INET: {
- struct sockaddr_in *naddr = (struct sockaddr_in *)ai->ai_addr;
+ struct sockaddr_in *in4 = (struct sockaddr_in *)ai->ai_addr;
- 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);
+ saddr_set_ipv4(saddr, in4->sin_addr.s_addr, htons(port));
list_add(&saddr->list, list);
break;
}
+
case AF_INET6: {
- struct sockaddr_in6 *naddr = (struct sockaddr_in6 *)ai->ai_addr;
+ struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)ai->ai_addr;
- 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);
+ saddr_set_ipv6(saddr, &in6->sin6_addr, in6->sin6_port);
list_add(&saddr->list, list);
break;
}
+
default:
error("getaddrinfo(%s): unknown address family (%i)\n",
str, ai->ai_family);
@@ -220,22 +204,14 @@ strtosockaddrs(const char *str, struct list_head *list)
if (!saddr)
goto out;
- 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);
+ saddr_set_ipv6(saddr, &in6addr_any, htons(port));
list_add(&saddr->list, list);
saddr = zmalloc(sizeof(*saddr));
if (!saddr)
goto out;
- 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);
+ saddr_set_ipv4(saddr, INADDR_ANY, htons(port));
list_add(&saddr->list, list);
} else {
diff --git a/server.c b/server.c
index f0f8386..cf66001 100644
--- a/server.c
+++ b/server.c
@@ -200,7 +200,7 @@ server_local_accept(struct cfg *cfg, struct uring_task *task, int res)
goto out;
}
- saddr_set_str(&local->client);
+ saddr_set_addrstr(&local->client);
verbose("%s: incoming proxy connection: %s -> %s\n",
scfg->name, local->client.addrstr, local->local.addrstr);
@@ -586,9 +586,7 @@ server_add_local(struct cfg *cfg, struct server *scfg, struct saddr *saddr)
return false;
}
- local->local.storage = saddr->storage;
- local->local.addrlen = saddr->addrlen;
- saddr_set_str(&local->local);
+ local->local = *saddr;
uring_task_init(&local->task, "local", &scfg->task, server_local_free);
list_add(&local->list, &scfg->locals);
diff --git a/utils.c b/utils.c
index 9071cb3..8c7b663 100644
--- a/utils.c
+++ b/utils.c
@@ -196,14 +196,14 @@ connection_set_local(struct cfg *cfg, struct connection *conn, int fd)
&conn->local.addrlen) < 0)
sprintf(conn->local.addrstr, "<unknown>");
else
- saddr_set_str(&conn->local);
+ saddr_set_addrstr(&conn->local);
}
void
connection_set_remote(struct cfg *cfg, struct connection *conn, struct saddr *remote)
{
conn->remote = *remote;
- saddr_set_str(&conn->remote);
+ saddr_set_addrstr(&conn->remote);
}
static void connect_next(struct cfg *cfg, struct uring_task *task, struct connection *conn);
@@ -322,7 +322,30 @@ saddr_addr(struct saddr *saddr, char *buf, size_t len)
}
void
-saddr_set_str(struct saddr *saddr)
+saddr_set_ipv4(struct saddr *saddr, in_addr_t ip, in_port_t port)
+{
+ memset(&saddr->in4, 0, sizeof(saddr->in4));
+ saddr->in4.sin_family = AF_INET;
+ saddr->in4.sin_port = port;
+ saddr->in4.sin_addr.s_addr = ip;
+ saddr->addrlen = sizeof(saddr->in4);
+ saddr_set_addrstr(saddr);
+}
+
+void
+saddr_set_ipv6(struct saddr *saddr, const struct in6_addr *ip, in_port_t port)
+{
+ memset(&saddr->in6, 0, sizeof(saddr->in6));
+ saddr->in6.sin6_family = AF_INET6;
+ saddr->in6.sin6_port = port;
+ if (ip)
+ saddr->in6.sin6_addr = *ip;
+ saddr->addrlen = sizeof(saddr->in6);
+ saddr_set_addrstr(saddr);
+}
+
+void
+saddr_set_addrstr(struct saddr *saddr)
{
char abuf[ADDRSTRLEN];
diff --git a/utils.h b/utils.h
index 628650f..ccd4b6d 100644
--- a/utils.h
+++ b/utils.h
@@ -65,7 +65,11 @@ char *saddr_addr(struct saddr *saddr, char *buf, size_t len);
uint16_t saddr_port(struct saddr *saddr);
-void saddr_set_str(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_addrstr(struct saddr *saddr);
int strtou16_strict(const char *str, uint16_t *result);