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 v1 1/3] dnslookup: split common functionality and struct into net.c
Date: Thu, 31 Jul 2025 10:07:44 +0700	[thread overview]
Message-ID: <20250731030856.366368-2-reyuki@gnuweeb.org> (raw)
In-Reply-To: <20250731030856.366368-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 |  29 ------------
 src/gwproxy/net.c     | 108 ++++++++++++++++++++++++++++++++++++++++++
 src/gwproxy/net.h     |  27 +++++++++++
 5 files changed, 137 insertions(+), 37 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..5bc4f3ff62bb 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)
 {
diff --git a/src/gwproxy/net.c b/src/gwproxy/net.c
new file mode 100644
index 000000000000..7427f87ae300
--- /dev/null
+++ b/src/gwproxy/net.c
@@ -0,0 +1,108 @@
+#include <stdio.h>
+#include <stddef.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <gwproxy/net.h>
+
+int init_addr(const char *addr, struct gwp_sockaddr *addr_st, uint16_t port)
+{
+	char *separator, *ipstr, *port_str;
+	char tmp[FULL_ADDRSTRLEN];
+	unsigned short nport, af;
+	struct sockaddr_in6 *i6;
+	struct sockaddr_in *i4;
+	size_t addrlen;
+	int i, hport;
+
+	i6 = (void *)addr_st;
+	i4 = (void *)addr_st;
+	separator = NULL;
+	addrlen = strlen(addr) + 1;
+	if (addrlen > FULL_ADDRSTRLEN)
+		return -EINVAL;
+
+	strncpy(tmp, addr, FULL_ADDRSTRLEN);
+	for (i = addrlen - 1; i > 0; i--) {
+		if (tmp[i] == ':') {
+			separator = &tmp[i];
+			break;
+		}
+	}
+
+	if (separator) {
+		*separator = '\0';
+
+		port_str = separator + 1;
+		hport = atoi(port_str);
+		if (!hport)
+			return -EINVAL;
+		if (hport > 65535 || hport < 0)
+			return -EINVAL;
+		nport = htons(hport);
+	} else {
+		nport = htons(port);
+	}
+
+	if (*tmp == '[') {
+		af = AF_INET6;
+		/* replace ']' with null-terminated byte */
+		if (separator) {
+			if (*(separator - 1) != ']')
+				return -EINVAL;
+			*(separator - 1) = '\0';
+		} else {
+			tmp[addrlen - 1] = '\0';
+		}
+		ipstr = tmp + 1;
+	} else {
+		af = AF_INET;
+		ipstr = tmp;
+	}
+
+	addr_st->sa.sa_family = af;
+	switch (af) {
+	case AF_INET:
+		i4->sin_port = nport;
+		if (!inet_pton(AF_INET, ipstr, &i4->sin_addr))
+			return -EINVAL;
+
+		break;
+	case AF_INET6:
+		i6->sin6_port = nport;
+		if (!inet_pton(AF_INET6, ipstr, &i6->sin6_addr))
+			return -EINVAL;
+
+		break;
+	}
+
+	return 0;
+}
+
+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..ebfebca5d840
--- /dev/null
+++ b/src/gwproxy/net.h
@@ -0,0 +1,27 @@
+/*
+ * 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;
+	};
+};
+
+/*
+ * Initialize address structure
+ *
+ * @param addr
+ * @param addr_st
+ * @param port in host byte order when the string does not contain port
+ */
+int init_addr(const char *addr, struct gwp_sockaddr *addr_st, uint16_t port);
+
+int convert_ssaddr_to_str(char buf[FULL_ADDRSTRLEN],
+			const struct gwp_sockaddr *gs);

base-commit: 0753f2d766e85fcbffc1f83dfd4e67d6206591cb
-- 
Ahmad Gani


  reply	other threads:[~2025-07-31  3:09 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-31  3:07 [PATCH gwproxy v1 0/3] Initial work for DNS lookup implementation Ahmad Gani
2025-07-31  3:07 ` Ahmad Gani [this message]
2025-07-31 14:01   ` [PATCH gwproxy v1 1/3] dnslookup: split common functionality and struct into net.c Ammar Faizi
2025-07-31 18:28     ` Alviro Iskandar Setiawan
2025-07-31 18:36       ` Ammar Faizi
2025-07-31 18:42         ` Alviro Iskandar Setiawan
2025-07-31 18:53           ` Ammar Faizi
2025-07-31 19:03             ` Alviro Iskandar Setiawan
2025-07-31  3:07 ` [PATCH gwproxy v1 2/3] dnslookup: Allow only port string number Ahmad Gani
2025-07-31  3:07 ` [PATCH gwproxy v1 3/3] dnslookup: Initial work for implementation of C-ares-like getaddrinfo function Ahmad Gani
2025-07-31 18:19   ` Alviro Iskandar Setiawan
2025-07-31 19:14   ` Alviro Iskandar Setiawan
2025-08-01  1:51     ` reyuki
2025-08-01 23:32       ` Alviro Iskandar Setiawan
2025-07-31 13:39 ` [PATCH gwproxy v1 0/3] Initial work for DNS lookup implementation Ammar Faizi
2025-08-01  1:49   ` reyuki
2025-08-01  2:19     ` Ammar Faizi
2025-08-05  6:28       ` 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=20250731030856.366368-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