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=1754399114; bh=oelgNBngHEMB0Kf4Msr01Kfbr+sr0Jm7/xsd9WotLYw=; 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=UexbgrI7vhtcZa+3bDQaxIPZBZ0pZQPctaR4AVQErGnYumx5zZQjW/idp6/ZA5EA9 jvwmkpOtgmBf398WQ+9v6Pc8ZN8Up5QmCAAO7QAtrPeNOQ0kVHwzWT5S1RXISpl9eM rWTOq6FSFqCKfHigIsWQ00gTWuNHF+jehSF07WvKuxL0RIlw01QCiuck77q/LoPQqh lKBWEkqxzcrw8m94qvr1l3FAF2N1rFBlPz9El8yUV+G09slK9QDL6BNR08yLkNPusF nOk3A9tGC8gyDTlnLlU/c7Zdi75MakjtCzDmLNlc1oTBTrz/z0F91RFW6RWqPoDDsR ouSdXAiZeUweQ== Received: from zero (unknown [182.253.151.158]) by server-vie001.gnuweeb.org (Postfix) with ESMTPSA id 9CE9E312805C; Tue, 5 Aug 2025 13:05:13 +0000 (UTC) From: Ahmad Gani To: Ammar Faizi Cc: Ahmad Gani , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH gwproxy v3 2/6] dnslookup: Add a new parameter default_port Date: Tue, 5 Aug 2025 20:04:44 +0700 Message-ID: <20250805130451.146549-3-reyuki@gnuweeb.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250805130451.146549-1-reyuki@gnuweeb.org> References: <20250805130451.146549-1-reyuki@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: default_port as parameter in convert_str_to_ssaddr function is needed when separator ':' does not exists in the provided address string. Signed-off-by: Ahmad Gani --- src/gwproxy/gwproxy.c | 4 ++-- src/gwproxy/net.c | 24 +++++++++++++++--------- src/gwproxy/net.h | 4 +++- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/gwproxy/gwproxy.c b/src/gwproxy/gwproxy.c index 40fb5ece21cc..9de460c53751 100644 --- a/src/gwproxy/gwproxy.c +++ b/src/gwproxy/gwproxy.c @@ -540,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; @@ -759,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 index 12c6c0b1275d..366fdff5a64d 100644 --- a/src/gwproxy/net.c +++ b/src/gwproxy/net.c @@ -10,7 +10,8 @@ #include __cold -int convert_str_to_ssaddr(const char *str, struct gwp_sockaddr *gs) +int convert_str_to_ssaddr(const char *str, + struct gwp_sockaddr *gs, uint16_t default_port) { static const struct addrinfo hints = { .ai_family = AF_UNSPEC, @@ -22,29 +23,34 @@ int convert_str_to_ssaddr(const char *str, struct gwp_sockaddr *gs) size_t l; int r; + l = strlen(str); + p = NULL; if (*str == '[') { p = strchr(++str, ']'); if (!p) return -EINVAL; l = p - str; - if (l >= sizeof(host)) - return -EINVAL; p++; - if (*p != ':') + if (*p != ':' && !default_port) return -EINVAL; - } else { + } else if (!default_port) { p = strchr(str, ':'); if (!p) return -EINVAL; l = p - str; - if (l >= sizeof(host)) - return -EINVAL; } + 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'; + if (default_port) { + snprintf(port, 6, "%u", default_port); + } else { + strncpy(port, p + 1, sizeof(port) - 1); + port[sizeof(port) - 1] = '\0'; + } r = getaddrinfo(host, port, &hints, &res); if (r) diff --git a/src/gwproxy/net.h b/src/gwproxy/net.h index 627a70f926f9..ff4ca1fc1965 100644 --- a/src/gwproxy/net.h +++ b/src/gwproxy/net.h @@ -19,9 +19,11 @@ struct gwp_sockaddr { * * @param str source * @param gs destination + * @param 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); +int convert_str_to_ssaddr(const char *str, + struct gwp_sockaddr *gs, uint16_t default_port); /* * Convert network address to string format -- Ahmad Gani