From: Ahmad Gani <reyuki@gnuweeb.org>
To: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Cc: Ahmad Gani <reyuki@gnuweeb.org>,
Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>,
GNU/Weeb Mailing List <gwml@vger.gnuweeb.org>
Subject: [PATCH gwproxy v2 1/3] dnslookup: split common functionality and struct into net.c
Date: Fri, 1 Aug 2025 08:54:22 +0700 [thread overview]
Message-ID: <20250801015427.439511-2-reyuki@gnuweeb.org> (raw)
In-Reply-To: <20250801015427.439511-1-reyuki@gnuweeb.org>
It's better to split these common function that used in more
than one place.
Signed-off-by: Ahmad Gani <reyuki@gnuweeb.org>
---
Makefile | 1 +
src/gwproxy/dns.h | 9 +---
src/gwproxy/gwproxy.c | 93 +-------------------------------------
src/gwproxy/net.c | 103 ++++++++++++++++++++++++++++++++++++++++++
src/gwproxy/net.h | 35 ++++++++++++++
5 files changed, 142 insertions(+), 99 deletions(-)
create mode 100644 src/gwproxy/net.c
create mode 100644 src/gwproxy/net.h
diff --git a/Makefile b/Makefile
index d9952a12b2d2..76e12e0ae739 100644
--- a/Makefile
+++ b/Makefile
@@ -37,6 +37,7 @@ GWPROXY_TARGET = gwproxy
GWPROXY_CC_SOURCES = \
$(GWPROXY_DIR)/gwproxy.c \
$(GWPROXY_DIR)/log.c \
+ $(GWPROXY_DIR)/net.c \
$(GWPROXY_DIR)/ev/epoll.c
diff --git a/src/gwproxy/dns.h b/src/gwproxy/dns.h
index 7d62d3f9b89e..290325d4f989 100644
--- a/src/gwproxy/dns.h
+++ b/src/gwproxy/dns.h
@@ -9,17 +9,10 @@
#include <stdatomic.h>
#include <stdbool.h>
#include <netinet/in.h>
+#include <gwproxy/net.h>
struct gwp_dns_wrk;
-struct gwp_sockaddr {
- union {
- struct sockaddr sa;
- struct sockaddr_in i4;
- struct sockaddr_in6 i6;
- };
-};
-
struct gwp_dns_entry {
char *name;
char *service;
diff --git a/src/gwproxy/gwproxy.c b/src/gwproxy/gwproxy.c
index f465f82446ec..9de460c53751 100644
--- a/src/gwproxy/gwproxy.c
+++ b/src/gwproxy/gwproxy.c
@@ -270,35 +270,6 @@ static inline ssize_t gwp_eventfd_write(int fd, uint64_t val)
return 0;
}
-__hot
-static int convert_ssaddr_to_str(char buf[FULL_ADDRSTRLEN],
- const struct gwp_sockaddr *gs)
-{
- int f = gs->sa.sa_family;
- uint16_t port = 0;
- size_t l;
-
- if (f == AF_INET) {
- if (!inet_ntop(f, &gs->i4.sin_addr, buf, INET_ADDRSTRLEN))
- return -EINVAL;
- l = strlen(buf);
- port = ntohs(gs->i4.sin_port);
- } else if (f == AF_INET6) {
- buf[0] = '[';
- if (!inet_ntop(f, &gs->i6.sin6_addr, buf + 1, INET6_ADDRSTRLEN))
- return -EINVAL;
- l = strlen(buf);
- buf[l++] = ']';
- port = ntohs(gs->i6.sin6_port);
- } else {
- return -EINVAL;
- }
-
- buf[l++] = ':';
- snprintf(buf + l, FULL_ADDRSTRLEN - l, "%hu", port);
- return 0;
-}
-
__hot
const char *ip_to_str(const struct gwp_sockaddr *gs)
{
@@ -309,66 +280,6 @@ const char *ip_to_str(const struct gwp_sockaddr *gs)
return convert_ssaddr_to_str(bp, gs) ? NULL : bp;
}
-__cold
-static int convert_str_to_ssaddr(const char *str, struct gwp_sockaddr *gs)
-{
- static const struct addrinfo hints = {
- .ai_family = AF_UNSPEC,
- .ai_socktype = SOCK_STREAM,
- };
- char host[NI_MAXHOST], port[6], *p;
- struct addrinfo *res, *ai;
- bool found = false;
- size_t l;
- int r;
-
- if (*str == '[') {
- p = strchr(++str, ']');
- if (!p)
- return -EINVAL;
- l = p - str;
- if (l >= sizeof(host))
- return -EINVAL;
- p++;
- if (*p != ':')
- return -EINVAL;
- } else {
- p = strchr(str, ':');
- if (!p)
- return -EINVAL;
- l = p - str;
- if (l >= sizeof(host))
- return -EINVAL;
- }
-
- strncpy(host, str, l);
- host[l] = '\0';
- strncpy(port, p + 1, sizeof(port) - 1);
- port[sizeof(port) - 1] = '\0';
-
- r = getaddrinfo(host, port, &hints, &res);
- if (r)
- return -EINVAL;
- if (!res)
- return -EINVAL;
-
- memset(gs, 0, sizeof(*gs));
- for (ai = res; ai; ai = ai->ai_next) {
- if (ai->ai_family == AF_INET) {
- gs->i4 = *(struct sockaddr_in *)ai->ai_addr;
- found = true;
- break;
- } else if (ai->ai_family == AF_INET6) {
- gs->i6 = *(struct sockaddr_in6 *)ai->ai_addr;
- found = true;
- break;
- }
- }
-
- freeaddrinfo(res);
- return found ? 0 : -EINVAL;
-}
-
__cold
static int gwp_ctx_init_log(struct gwp_ctx *ctx)
{
@@ -629,7 +540,7 @@ static int gwp_ctx_init_threads(struct gwp_ctx *ctx)
return -EINVAL;
}
- r = convert_str_to_ssaddr(cfg->bind, &bind_addr);
+ r = convert_str_to_ssaddr(cfg->bind, &bind_addr, 0);
if (r) {
pr_err(&ctx->lh, "Invalid bind address '%s'\n", cfg->bind);
return r;
@@ -848,7 +759,7 @@ static int gwp_ctx_init(struct gwp_ctx *ctx)
if (!ctx->cfg.as_socks5) {
const char *t = ctx->cfg.target;
- r = convert_str_to_ssaddr(t, &ctx->target_addr);
+ r = convert_str_to_ssaddr(t, &ctx->target_addr, 0);
if (r) {
pr_err(&ctx->lh, "Invalid target address '%s'", t);
goto out_free_log;
diff --git a/src/gwproxy/net.c b/src/gwproxy/net.c
new file mode 100644
index 000000000000..c65f124a30af
--- /dev/null
+++ b/src/gwproxy/net.c
@@ -0,0 +1,103 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stddef.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <netdb.h>
+#include <gwproxy/net.h>
+#include <gwproxy/common.h>
+
+__cold
+int convert_str_to_ssaddr(const char *str, struct gwp_sockaddr *gs, uint16_t prt)
+{
+ static const struct addrinfo hints = {
+ .ai_family = AF_UNSPEC,
+ .ai_socktype = SOCK_STREAM,
+ };
+ char host[NI_MAXHOST], port[6], *p;
+ struct addrinfo *res, *ai;
+ bool found = false;
+ size_t l;
+ int r;
+
+ l = strlen(str);
+ if (*str == '[') {
+ p = strchr(++str, ']');
+ if (!p)
+ return -EINVAL;
+ l = p - str;
+ p++;
+ if (*p != ':' && !prt)
+ return -EINVAL;
+ } else if (!prt) {
+ p = strchr(str, ':');
+ if (!p)
+ return -EINVAL;
+ l = p - str;
+ }
+
+ if (l >= sizeof(host))
+ return -EINVAL;
+
+ strncpy(host, str, l);
+ host[l] = '\0';
+ if (prt) {
+ snprintf(port, 6, "%u", prt);
+ } else {
+ strncpy(port, p + 1, sizeof(port) - 1);
+ port[sizeof(port) - 1] = '\0';
+ }
+
+ r = getaddrinfo(host, port, &hints, &res);
+ if (r)
+ return -EINVAL;
+ if (!res)
+ return -EINVAL;
+
+ memset(gs, 0, sizeof(*gs));
+ for (ai = res; ai; ai = ai->ai_next) {
+ if (ai->ai_family == AF_INET) {
+ gs->i4 = *(struct sockaddr_in *)ai->ai_addr;
+ found = true;
+ break;
+ } else if (ai->ai_family == AF_INET6) {
+ gs->i6 = *(struct sockaddr_in6 *)ai->ai_addr;
+ found = true;
+ break;
+ }
+ }
+
+ freeaddrinfo(res);
+ return found ? 0 : -EINVAL;
+}
+
+__cold
+int convert_ssaddr_to_str(char buf[FULL_ADDRSTRLEN],
+ const struct gwp_sockaddr *gs)
+{
+ int f = gs->sa.sa_family;
+ uint16_t port = 0;
+ size_t l;
+
+ if (f == AF_INET) {
+ if (!inet_ntop(f, &gs->i4.sin_addr, buf, INET_ADDRSTRLEN))
+ return -EINVAL;
+ l = strlen(buf);
+ port = ntohs(gs->i4.sin_port);
+ } else if (f == AF_INET6) {
+ buf[0] = '[';
+ if (!inet_ntop(f, &gs->i6.sin6_addr, buf + 1, INET6_ADDRSTRLEN))
+ return -EINVAL;
+ l = strlen(buf);
+ buf[l++] = ']';
+ port = ntohs(gs->i6.sin6_port);
+ } else {
+ return -EINVAL;
+ }
+
+ buf[l++] = ':';
+ snprintf(buf + l, FULL_ADDRSTRLEN - l, "%hu", port);
+ return 0;
+}
diff --git a/src/gwproxy/net.h b/src/gwproxy/net.h
new file mode 100644
index 000000000000..113b176def74
--- /dev/null
+++ b/src/gwproxy/net.h
@@ -0,0 +1,35 @@
+/*
+ * utility for network-related stuff
+ */
+
+#include <arpa/inet.h>
+
+#define FULL_ADDRSTRLEN (INET6_ADDRSTRLEN + sizeof(":65535[]") - 1)
+
+struct gwp_sockaddr {
+ union {
+ struct sockaddr sa;
+ struct sockaddr_in i4;
+ struct sockaddr_in6 i6;
+ };
+};
+
+/*
+ * Convert address string to network address
+ *
+ * @param str source
+ * @param gs destination
+ * @param prt default port, fill it with zero if unspecified
+ * @return zero on success and a negative integer on failure.
+ */
+int convert_str_to_ssaddr(const char *str, struct gwp_sockaddr *gs, uint16_t prt);
+
+/*
+ * Convert network address to string format
+ *
+ * @param buf destination
+ * @param gs source
+ * @return zero on success and a negative integer on failure.
+ */
+int convert_ssaddr_to_str(char buf[FULL_ADDRSTRLEN],
+ const struct gwp_sockaddr *gs);
base-commit: 0753f2d766e85fcbffc1f83dfd4e67d6206591cb
--
Ahmad Gani
next prev parent reply other threads:[~2025-08-01 1:55 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-01 1:54 [PATCH gwproxy v2 0/3] Initial work for DNS lookup implementation Ahmad Gani
2025-08-01 1:54 ` Ahmad Gani [this message]
2025-08-01 23:43 ` [PATCH gwproxy v2 1/3] dnslookup: split common functionality and struct into net.c Alviro Iskandar Setiawan
2025-08-01 23:45 ` Alviro Iskandar Setiawan
2025-08-02 1:08 ` reyuki
2025-08-02 1:34 ` Alviro Iskandar Setiawan
2025-08-01 1:54 ` [PATCH gwproxy v2 2/3] dnslookup: Allow only port string number Ahmad Gani
2025-08-01 1:54 ` [PATCH gwproxy v2 3/3] dnslookup: Initial work for implementation of C-ares-like getaddrinfo function Ahmad Gani
2025-08-02 1:37 ` Alviro Iskandar Setiawan
2025-08-02 2:06 ` reyuki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250801015427.439511-2-reyuki@gnuweeb.org \
--to=reyuki@gnuweeb.org \
--cc=alviro.iskandar@gnuweeb.org \
--cc=ammarfaizi2@gnuweeb.org \
--cc=gwml@vger.gnuweeb.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox