Hi Willy,

thank you for your interest in that question.

I want to track requests in a stick table. The key should be a combination of
source ip and a hash of the host header.

After asking here on the mailing list and trying it on my own
I used the following snippet on a stick table with type binary:

capture request header Host len 64
http-request set-var-fmt(req.schluessel) 
"%[src]@%[capture.req.hdr(0),lower,crc32c]"
http-request track-sc0 var(req.schluessel)

When I looked at the generated keys I assumed that I will find the
src address at the very first four bytes. But after some digging I found out
that I got the binary representation of the string representing that
ip address. I my case 127.0.0.1. But this uses 9 Bytes and not 4.

I need something like base32+src but the base32 part only built
by the host header without the path part of a query.
I was wondering why this is not a part of a standard implementation.
But probably I just overlooked something.

Best regards
Andreas




-----Ursprüngliche Nachricht-----
Von: Willy Tarreau <w...@1wt.eu> 
Gesendet: Samstag, 15. Februar 2025 15:45
An: Andreas Mock <andreas.m...@drumedar.de>
Cc: haproxy@formilux.org
Betreff: Re: Is there a converter from type IP to binary?

Hi Andreas,

On Sat, Feb 15, 2025 at 02:30:48PM +0000, Andreas Mock wrote:
> Hi all,
> 
> the fetch method 'src' is returning an IP address with type 'ip'.
> As soon as I use it with %[src] I get a human readable string 
> representation of that ip address. That means I have something like 
> ip2str converter.
> 
> Is there a converter in HAProxy to get the binary representation of 
> that ip address?
> 
> IPv4 is 4 bytes. IPv6 is 16 bytes.
> 
> Something like src,ip2bin which would be a 4 byte binary for IPv4 and 
> a 16 byte binary for IPv6?

Interesting. We indeed don't have anything explicit to do this. Actually the IP 
address (IPv4 or IPv6) *is* internally in binary formay and there's an implicit 
cast from address to binary that does nothing more than a memcpy(). If you pass 
the address to something that consumes a binary type on input, it will be 
automatically cast to this type. For example, if used with a binary stick-table 
it should automatically cast. Could you please provide an example of how you're 
trying to use it where you'd like to get the binary format that it does not 
propose ? That might help to figure if we could for example insert a dummy 
conversion in between to force the cast, or any such a thing.

Willy


Reply via email to