From db1cd8f8819b59f599b6e5f407816db52c8e7ae6 Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Mon, 18 Mar 2024 15:18:08 +0100 Subject: [PATCH] OPTIM: http_ext: avoid useless copy in http_7239_extract_{ipv4,ipv6} In http_7239_extract_{ipv4,ipv6}, we declare a local buffer in order to use inet_pton() since it requires a valid destination argument (cannot be NULL). Then, if the caller provided argument, we copy inet_pton() result (from local buffer to ). In fact when the caller provides , we may directly use as inet_pton() dst argument to avoid an useless copy. Thus the local buffer is only relevant when the user doesn't provide . While at it, let's add a missing testcase for the rfc7239_n2nn converter (to check that http_7239_extract_ipv4() with provided works properly) This could be backported in 2.8 with b2bb925 ("MINOR: proxy/http_ext: introduce proxy forwarded option") --- reg-tests/http-rules/forwarded-header-7239.vtc | 6 ++++++ src/http_ext.c | 16 ++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/reg-tests/http-rules/forwarded-header-7239.vtc b/reg-tests/http-rules/forwarded-header-7239.vtc index a894113e7..dd2c6e3fd 100644 --- a/reg-tests/http-rules/forwarded-header-7239.vtc +++ b/reg-tests/http-rules/forwarded-header-7239.vtc @@ -151,6 +151,12 @@ client c1 -connect ${h1_fe1_sock} { expect resp.status == 200 expect resp.http.nodename == "::1" expect resp.http.nodeport == "_id" + + txreq -req GET -url /req4 \ + -hdr "forwarded: for=127.9.0.1" + rxresp + expect resp.status == 200 + expect resp.http.nodename == "127.9.0.1" } -run client c2 -connect ${h2_fe1h2_sock} { diff --git a/src/http_ext.c b/src/http_ext.c index 269a97f5e..3367e3884 100644 --- a/src/http_ext.c +++ b/src/http_ext.c @@ -114,8 +114,12 @@ static inline int http_7239_extract_ipv4(struct ist *input, struct in_addr *ip) { char ip4[INET_ADDRSTRLEN]; unsigned char buf[sizeof(struct in_addr)]; + void *dst = buf; int it = 0; + if (ip) + dst = ip; + /* extract ipv4 addr */ while (it < istlen(*input) && it < (sizeof(ip4) - 1)) { if (!isdigit((unsigned char)istptr(*input)[it]) && @@ -125,11 +129,9 @@ static inline int http_7239_extract_ipv4(struct ist *input, struct in_addr *ip) it += 1; } ip4[it] = 0; - if (inet_pton(AF_INET, ip4, buf) != 1) + if (inet_pton(AF_INET, ip4, dst) != 1) return 0; /* invalid ip4 addr */ /* ok */ - if (ip) - memcpy(ip, buf, sizeof(buf)); *input = istadv(*input, it); return 1; } @@ -146,8 +148,12 @@ static inline int http_7239_extract_ipv6(struct ist *input, struct in6_addr *ip) { char ip6[INET6_ADDRSTRLEN]; unsigned char buf[sizeof(struct in6_addr)]; + void *dst = buf; int it = 0; + if (ip) + dst = ip; + *input = istnext(*input); /* skip '[' leading char */ /* extract ipv6 addr */ while (it < istlen(*input) && @@ -162,11 +168,9 @@ static inline int http_7239_extract_ipv6(struct ist *input, struct in6_addr *ip) if ((istlen(*input)-it) < 1 || istptr(*input)[it] != ']') return 0; /* missing ending "]" char */ it += 1; - if (inet_pton(AF_INET6, ip6, buf) != 1) + if (inet_pton(AF_INET6, ip6, dst) != 1) return 0; /* invalid ip6 addr */ /* ok */ - if (ip) - memcpy(ip, buf, sizeof(buf)); *input = istadv(*input, it); return 1; }