]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: net - Change net_ipport2str() API to assert fail on invalid IP.
authorStephan Bosch <stephan.bosch@open-xchange.com>
Sat, 20 Nov 2021 23:08:49 +0000 (00:08 +0100)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 17 Jan 2022 11:52:10 +0000 (13:52 +0200)
Before, it returned an error result which was either handled with i_unreached()
or ignored.

src/lib-http/test-http-server.c
src/lib-oauth2/oauth2-request.c
src/lib-program-client/program-client-remote.c
src/lib/net.c
src/lib/net.h
src/old-stats/stats-carbon.c

index ceec5ce2667615ccd1cebe9671def0fb185808e0..c2432435b44de9a3c98454f19e6d41764caa12bb 100644 (file)
@@ -66,7 +66,6 @@ client_http_handle_request(void *context,
        struct client *client = (struct client *)context;
        const struct http_request *http_req = http_server_request_get(req);
        struct http_server_response *http_resp;
-       const char *ipport;
        string_t *content;
 
        if (strcmp(http_req->method, "GET") != 0) {
@@ -79,10 +78,10 @@ client_http_handle_request(void *context,
 
        /* Compose response payload */
        content = t_str_new(1024);
-       (void)net_ipport2str(&client->server_ip, client->server_port, &ipport);
-       str_printfa(content, "Server: %s\r\n", ipport);
-       (void)net_ipport2str(&client->ip, client->port, &ipport);
-       str_printfa(content, "Client: %s\r\n", ipport);
+       str_printfa(content, "Server: %s\r\n",
+                   net_ipport2str(&client->server_ip, client->server_port));
+       str_printfa(content, "Client: %s\r\n",
+                   net_ipport2str(&client->ip, client->port));
        str_printfa(content, "Host: %s", http_req->target.url->host.name);
        if (http_req->target.url->port != 0)
                str_printfa(content, ":%u", http_req->target.url->port);
index 032eff1630f2f894ef819bf334943e7e0245f96d..138780d257e2919e43065da08d2ce532c9e3b1ca 100644 (file)
@@ -170,20 +170,16 @@ oauth2_request_set_headers(struct oauth2_request *req,
                        req->req, "X-Dovecot-Auth-Service", input->service);
        }
        if (input->local_ip.family != 0) {
-               const char *addr;
-               if (net_ipport2str(&input->local_ip, input->local_port,
-                                  &addr) == 0)  {
-                       http_client_request_add_header(
-                               req->req, "X-Dovecot-Auth-Local", addr);
-               }
+               http_client_request_add_header(
+                       req->req, "X-Dovecot-Auth-Local",
+                       net_ipport2str(&input->local_ip,
+                                      input->local_port));
        }
        if (input->remote_ip.family != 0) {
-               const char *addr;
-               if (net_ipport2str(&input->remote_ip, input->remote_port,
-                                  &addr) == 0) {
-                       http_client_request_add_header(
-                               req->req, "X-Dovecot-Auth-Remote", addr);
-               }
+               http_client_request_add_header(
+                       req->req, "X-Dovecot-Auth-Remote",
+                       net_ipport2str(&input->remote_ip,
+                                      input->remote_port));
        }
 }
 
index 858abe656973678b7df64e0f23ecf9554a7d46b8..3892ed15bde746324ee35d14500a6ebecea488bb 100644 (file)
@@ -421,8 +421,7 @@ program_client_net_connect_real(struct program_client_remote *prclient)
 
        i_assert(prclient->ips_count > 0);
 
-       if (net_ipport2str(prclient->ips, prclient->port, &address) < 0)
-               i_unreached();
+       address = net_ipport2str(prclient->ips, prclient->port);
        label = t_strconcat("tcp:", address, NULL);
        program_client_set_label(pclient, label);
 
@@ -680,9 +679,7 @@ program_client_net_create_ips(const struct ip_addr *ips, size_t ips_count,
 
        i_assert(ips != NULL && ips_count > 0);
 
-       if (net_ipport2str(ips, port, &label) < 0)
-               i_unreached();
-       label = t_strconcat("tcp:", label, NULL);
+       label = t_strconcat("tcp:", net_ipport2str(ips, port), NULL);
 
        pool = pool_alloconly_create("program client net", 1024);
        prclient = p_new(pool, struct program_client_remote, 1);
index fe69faf99a105356bda60ed61db9c2df1fef5e31..d10ca6c334c2bb5fcf4daced0bf003a21bd2753f 100644 (file)
@@ -1025,16 +1025,13 @@ int net_str2hostport(const char *str, in_port_t default_port,
        return 0;
 }
 
-int net_ipport2str(const struct ip_addr *ip, in_port_t port, const char **str_r)
+const char *net_ipport2str(const struct ip_addr *ip, in_port_t port)
 {
-       if (!IPADDR_IS_V4(ip) && !IPADDR_IS_V6(ip)) return -1;
+       i_assert(IPADDR_IS_V4(ip) || IPADDR_IS_V6(ip));
 
-       *str_r = t_strdup_printf("%s%s%s:%u",
-                                IPADDR_IS_V6(ip) ? "[" : "",
-                                net_ip2addr(ip),
-                                IPADDR_IS_V6(ip) ? "]" : "",
-                                port);
-       return 0;
+       return t_strdup_printf("%s%s%s:%u",
+                              (IPADDR_IS_V6(ip) ? "[" : ""), net_ip2addr(ip),
+                              (IPADDR_IS_V6(ip) ? "]" : ""), port);
 }
 
 int net_ipv6_mapped_ipv4_convert(const struct ip_addr *src,
index 7f8abb7181315da2dfc794c392998bcbc0a19ef3..ec5f38387a9362cb38040a37eaccc759956b1ffa 100644 (file)
@@ -168,9 +168,9 @@ int net_str2port_zero(const char *str, in_port_t *port_r);
    through. */
 int net_str2hostport(const char *str, in_port_t default_port,
                     const char **host_r, in_port_t *port_r);
-/* Converts ip and port to ipv4:port or [ipv6]:port. Returns -1 if
-   ip is not valid IPv4 or IPv6 address. */
-int net_ipport2str(const struct ip_addr *ip, in_port_t port, const char **str_r);
+/* Converts ip and port to ipv4:port or [ipv6]:port. Triggers an assert crash
+   when IP is not valid. */
+const char *net_ipport2str(const struct ip_addr *ip, in_port_t port);
 
 /* Convert IPv6 mapped IPv4 address to an actual IPv4 address. Returns 0 if
    successful, -1 if the source address isn't IPv6 mapped IPv4 address. */
index 7e984cf5f6102d542a5e4438ea5c5586ee0096c4..8ef3b234035845bca35e44e274c4273fe2a0dab0 100644 (file)
@@ -113,9 +113,7 @@ stats_carbon_send(const char *endpoint, const char *data,
        ctx->to = timeout_add(ctx->to_msecs,
                              stats_carbon_timeout,
                              ctx);
-       if (net_ipport2str(&ip, port, &host) < 0)
-               i_unreached();
-       ctx->endpoint = p_strdup(ctx->pool, host);
+       ctx->endpoint = p_strdup(ctx->pool, net_ipport2str(&ip, port));
        ctx->callback = callback;
        ctx->ctx = cb_ctx;