From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server-vie001.gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,URIBL_DBL_BLOCKED_OPENDNS, URIBL_ZEN_BLOCKED_OPENDNS autolearn=ham autolearn_force=no version=3.4.6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=new2025; t=1754013306; bh=XsbDXf92xkXxm3dKt8ZTPLZJCq1nshwTDemqAvGxTq8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Message-ID:Date:From: Reply-To:Subject:To:Cc:In-Reply-To:References:Resent-Date: Resent-From:Resent-To:Resent-Cc:User-Agent:Content-Type: Content-Transfer-Encoding; b=Y3f1Jl4QcHlb0763XXZAc01dmWVLtcsrF8QdTj/2q5j/4DIDvFB1gkzRuYDs7rHEd T6zpw5dda9s3ralj3XGZ/dck/VXeqAtdLVIkiIsJUu13FLclNs50F6wS4PiBulLLis 2bDLwiMMFYty4oExWhJo68OK0w4tdsAd8cVizfcFc5hNcbx5yLGs+hlzPp1kAHxImI vYLzGVg+ylLYYbD1HmXHpyEZxQjXWTR+nlyAAsLMdw1i1jtagYAoI2IkJVDWS15Nao 4xaorY6rusrWMqvkMWToXCNK/Jqp/HfDFUI0drXp3ji3CQix8+q71/QeU3146825BM rWCfa2E7VW0rg== Received: from zero (unknown [182.253.151.159]) by server-vie001.gnuweeb.org (Postfix) with ESMTPSA id 4E7263126F0E; Fri, 1 Aug 2025 01:55:05 +0000 (UTC) From: Ahmad Gani To: Ammar Faizi Cc: Ahmad Gani , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH gwproxy v2 1/3] dnslookup: split common functionality and struct into net.c Date: Fri, 1 Aug 2025 08:54:22 +0700 Message-ID: <20250801015427.439511-2-reyuki@gnuweeb.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250801015427.439511-1-reyuki@gnuweeb.org> References: <20250801015427.439511-1-reyuki@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: It's better to split these common function that used in more than one place. Signed-off-by: Ahmad Gani --- 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 #include #include +#include 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 +#include +#include +#include +#include +#include +#include +#include +#include + +__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 + +#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