public inbox for gwml@vger.gnuweeb.org
 help / color / mirror / Atom feed
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 v3 1/6] dnslookup: Split common functionality and struct into net.h and net.c
Date: Tue,  5 Aug 2025 20:04:43 +0700	[thread overview]
Message-ID: <20250805130451.146549-2-reyuki@gnuweeb.org> (raw)
In-Reply-To: <20250805130451.146549-1-reyuki@gnuweeb.org>

List of extracted stuff:
- convert_str_to_ssaddr function to net.c.
- convert_ssaddr_to_str function to net.c.
- struct gwp_sockaddr to net.h
- FULL_ADDRSTRLEN constant to net.h

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 | 89 --------------------------------------
 src/gwproxy/net.c     | 99 +++++++++++++++++++++++++++++++++++++++++++
 src/gwproxy/net.h     | 34 +++++++++++++++
 5 files changed, 135 insertions(+), 97 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..40fb5ece21cc 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)
 {
diff --git a/src/gwproxy/net.c b/src/gwproxy/net.c
new file mode 100644
index 000000000000..12c6c0b1275d
--- /dev/null
+++ b/src/gwproxy/net.c
@@ -0,0 +1,99 @@
+#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)
+{
+	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
+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..627a70f926f9
--- /dev/null
+++ b/src/gwproxy/net.h
@@ -0,0 +1,34 @@
+/*
+ * 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
+ * @return zero on success and a negative integer on failure.
+ */
+int convert_str_to_ssaddr(const char *str, struct gwp_sockaddr *gs);
+
+/*
+ * 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);
-- 
Ahmad Gani


  reply	other threads:[~2025-08-05 13:05 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-05 13:04 [PATCH gwproxy v3 0/6] Initial work for DNS lookup implementation Ahmad Gani
2025-08-05 13:04 ` Ahmad Gani [this message]
2025-08-05 13:04 ` [PATCH gwproxy v3 2/6] dnslookup: Add a new parameter default_port Ahmad Gani
2025-08-06  3:25   ` Ammar Faizi
2025-08-05 13:04 ` [PATCH gwproxy v3 3/6] dnslookup: Allow only port string number Ahmad Gani
2025-08-05 13:04 ` [PATCH gwproxy v3 4/6] dnslookup: Initial work for implementation of C-ares-like getaddrinfo function Ahmad Gani
2025-08-05 13:04 ` [PATCH gwproxy v3 5/6] dnsparser: Transaction id creation is delegated to caller Ahmad Gani
2025-08-05 13:04 ` [PATCH gwproxy v3 6/6] dnslookup: Make gw_ares_getaddrinfo asynchronous Ahmad Gani
2025-08-06  3:31 ` [PATCH gwproxy v3 0/6] Initial work for DNS lookup implementation Ammar Faizi
2025-08-06  3:43   ` Ahmad Gani
  -- strict thread matches above, loose matches on Subject: below --
2025-08-05 12:40 Ahmad Gani
2025-08-05 12:40 ` [PATCH gwproxy v3 1/6] dnslookup: Split common functionality and struct into net.h and net.c Ahmad Gani

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=20250805130451.146549-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