Utility functions for IP address conversion, CIDR parsing, and binary encoding. Supports both IPv4 and IPv6 addresses with unified 16-byte internal format.
Utility functions for IP address conversion, CIDR parsing, and binary encoding. Supports both IPv4 and IPv6 addresses with unified 16-byte internal format.
(address-family ip-str)Determine address family from IP string. Returns :ipv4, :ipv6, or nil if invalid.
Determine address family from IP string. Returns :ipv4, :ipv6, or nil if invalid.
(bytes->ip b)Convert 4-byte array to IP u32.
Convert 4-byte array to IP u32.
(bytes->ipv6-string b)Convert 16-byte array to IPv6 string. Example: [32 1 13 184 0 0 0 0 0 0 0 0 0 0 0 1] => "2001:db8::1"
Convert 16-byte array to IPv6 string. Example: [32 1 13 184 0 0 0 0 0 0 0 0 0 0 0 1] => "2001:db8::1"
(bytes16->address-family b)Determine address family from unified 16-byte format. Returns :ipv4 if first 12 bytes are zero, :ipv6 otherwise.
Determine address family from unified 16-byte format. Returns :ipv4 if first 12 bytes are zero, :ipv6 otherwise.
(bytes16->ip-string b)Convert unified 16-byte format back to IP string. Detects IPv4 (zero-prefixed) vs IPv6.
Convert unified 16-byte format back to IP string. Detects IPv4 (zero-prefixed) vs IPv6.
(bytes16-zero? b)Check if unified 16-byte address is all zeros.
Check if unified 16-byte address is all zeros.
(cidr->string {:keys [ip prefix-len]})Convert {:ip <u32> :prefix-len <int>} back to CIDR string.
Convert {:ip <u32> :prefix-len <int>} back to CIDR string.
(cidr-unified->string {:keys [ip prefix-len]})Convert unified CIDR {:ip <bytes16> :prefix-len :af} back to string.
Convert unified CIDR {:ip <bytes16> :prefix-len :af} back to string.
(decode-conntrack-key b)Decode connection tracking key from byte array.
Decode connection tracking key from byte array.
(decode-conntrack-key-unified b)Decode unified connection tracking key from byte array.
Decode unified connection tracking key from byte array.
(decode-conntrack-value b)Decode connection tracking value from byte array (64 bytes).
Decode connection tracking value from byte array (64 bytes).
(decode-conntrack-value-unified b)Decode unified connection tracking value from byte array (96 bytes).
Decode unified connection tracking value from byte array (96 bytes).
(decode-listen-key b)Decode listen map key from byte array.
Decode listen map key from byte array.
(decode-listen-key-unified b)Decode unified listen map key from byte array.
Decode unified listen map key from byte array.
(decode-lpm-key b)Decode LPM trie key from byte array.
Decode LPM trie key from byte array.
(decode-lpm-key-unified b)Decode unified LPM trie key from byte array. Returns {:prefix-len :ip (16-byte array) :af}.
Decode unified LPM trie key from byte array.
Returns {:prefix-len :ip (16-byte array) :af}.(decode-route-value b)Decode route value from byte array.
Decode route value from byte array.
(decode-sni-key b)Decode SNI map key from byte array.
Decode SNI map key from byte array.
(decode-stats-event b)Decode stats event from ring buffer.
Decode stats event from ring buffer.
(decode-weighted-route-value b)Decode weighted route value from byte array (72 bytes). Returns {:target-count :flags :targets} where targets is a vector of {:ip :port :cumulative-weight} maps.
Decode weighted route value from byte array (72 bytes).
Returns {:target-count :flags :targets} where targets is a vector of
{:ip :port :cumulative-weight} maps.(decode-weighted-route-value-unified b)Decode unified weighted route value from byte array (168 bytes). Returns {:target-count :flags :targets} where targets is a vector of {:ip <16-byte-array> :port :cumulative-weight} maps.
Decode unified weighted route value from byte array (168 bytes).
Returns {:target-count :flags :targets} where targets is a vector of
{:ip <16-byte-array> :port :cumulative-weight} maps.(encode-conntrack-key {:keys [src-ip dst-ip src-port dst-port protocol]})Encode connection tracking 5-tuple key. {src_ip (4) + dst_ip (4) + src_port (2) + dst_port (2) + protocol (1) + padding (3)} Total: 16 bytes (aligned). XDP stores packet values (network byte order) directly, so we use big-endian to match packet byte layout.
Encode connection tracking 5-tuple key.
{src_ip (4) + dst_ip (4) + src_port (2) + dst_port (2) + protocol (1) + padding (3)}
Total: 16 bytes (aligned).
XDP stores packet values (network byte order) directly, so we use big-endian
to match packet byte layout.(encode-conntrack-key-unified {:keys [src-ip dst-ip src-port dst-port
protocol]})Encode unified connection tracking 5-tuple key. {src_ip (16) + dst_ip (16) + src_port (2) + dst_port (2) + protocol (1) + padding (3)} Total: 40 bytes (aligned). src-ip and dst-ip must be 16-byte arrays.
Encode unified connection tracking 5-tuple key.
{src_ip (16) + dst_ip (16) + src_port (2) + dst_port (2) + protocol (1) + padding (3)}
Total: 40 bytes (aligned).
src-ip and dst-ip must be 16-byte arrays.(encode-conntrack-value {:keys [orig-dst-ip orig-dst-port nat-dst-ip
nat-dst-port created-ns last-seen packets-fwd
packets-rev bytes-fwd bytes-rev]})Encode connection tracking value. {orig_dst_ip (4) + orig_dst_port (2) + padding (2) + nat_dst_ip (4) + nat_dst_port (2) + padding (2) + created_ns (8) + last_seen_ns (8) + packets_fwd (8) + packets_rev (8) + bytes_fwd (8) + bytes_rev (8)} Total: 64 bytes. IPs and ports in network byte order (from packet), counters in native order.
Encode connection tracking value.
{orig_dst_ip (4) + orig_dst_port (2) + padding (2) + nat_dst_ip (4) + nat_dst_port (2) + padding (2) +
created_ns (8) + last_seen_ns (8) + packets_fwd (8) + packets_rev (8) + bytes_fwd (8) + bytes_rev (8)}
Total: 64 bytes.
IPs and ports in network byte order (from packet), counters in native order.(encode-conntrack-value-unified
{:keys [orig-dst-ip orig-dst-port nat-dst-ip nat-dst-port created-ns last-seen
packets-fwd packets-rev bytes-fwd bytes-rev]})Encode unified connection tracking value. {orig_dst_ip (16) + orig_dst_port (2) + padding (2) + nat_dst_ip (16) + nat_dst_port (2) + padding (2) + created_ns (8) + last_seen_ns (8) + packets_fwd (8) + packets_rev (8) + bytes_fwd (8) + bytes_rev (8)} Total: 96 bytes. IPs in network byte order, counters in native order.
Encode unified connection tracking value.
{orig_dst_ip (16) + orig_dst_port (2) + padding (2) + nat_dst_ip (16) + nat_dst_port (2) + padding (2) +
created_ns (8) + last_seen_ns (8) + packets_fwd (8) + packets_rev (8) + bytes_fwd (8) + bytes_rev (8)}
Total: 96 bytes.
IPs in network byte order, counters in native order.(encode-listen-key ifindex port)Encode listen map key: {ifindex (4 bytes) + port (2 bytes) + padding (2 bytes)}. Total: 8 bytes (aligned). Uses native byte order for ifindex, but BIG_ENDIAN for port to match raw packet bytes that XDP loads with ldx :h.
Encode listen map key: {ifindex (4 bytes) + port (2 bytes) + padding (2 bytes)}.
Total: 8 bytes (aligned).
Uses native byte order for ifindex, but BIG_ENDIAN for port to match
raw packet bytes that XDP loads with ldx :h.(encode-listen-key-unified ifindex port af)Encode unified listen map key: {ifindex (4 bytes) + port (2 bytes) + af (1 byte) + pad (1 byte)}. Total: 8 bytes (same size as before, but includes address family). af is :ipv4 or :ipv6.
Encode unified listen map key: {ifindex (4 bytes) + port (2 bytes) + af (1 byte) + pad (1 byte)}.
Total: 8 bytes (same size as before, but includes address family).
af is :ipv4 or :ipv6.(encode-lpm-key prefix-len ip-u32)Encode LPM trie key: {prefix_len (4 bytes) + ip (4 bytes)}. Total: 8 bytes.
Encode LPM trie key: {prefix_len (4 bytes) + ip (4 bytes)}.
Total: 8 bytes.(encode-lpm-key-unified prefix-len ip-bytes16)Encode unified LPM trie key: {prefix_len (4 bytes) + ip (16 bytes)}. Total: 20 bytes. ip-bytes16 must be a 16-byte array in unified format.
Encode unified LPM trie key: {prefix_len (4 bytes) + ip (16 bytes)}.
Total: 20 bytes.
ip-bytes16 must be a 16-byte array in unified format.(encode-route-value target-ip target-port flags)Encode route value: {target_ip (4 bytes) + target_port (2 bytes) + flags (2 bytes)}. Total: 8 bytes. IP and port are stored in NETWORK byte order (big-endian) because XDP writes them directly to packet headers which are in network byte order. Flags remain in native order since they're only used internally.
Encode route value: {target_ip (4 bytes) + target_port (2 bytes) + flags (2 bytes)}.
Total: 8 bytes.
IP and port are stored in NETWORK byte order (big-endian) because XDP writes
them directly to packet headers which are in network byte order.
Flags remain in native order since they're only used internally.(encode-sni-key hostname-hash)Encode SNI map key from hostname hash. Key: hostname_hash (8 bytes) = 8 bytes total. Uses native byte order for efficient lookup.
Encode SNI map key from hostname hash. Key: hostname_hash (8 bytes) = 8 bytes total. Uses native byte order for efficient lookup.
(encode-stats-event {:keys [event-type timestamp src-ip dst-ip src-port dst-port
target-ip target-port packets-fwd bytes-fwd
packets-rev bytes-rev]})Encode a stats event for ring buffer. {event_type (1) + padding (3) + timestamp (8) + src_ip (4) + dst_ip (4) + src_port (2) + dst_port (2) + target_ip (4) + target_port (2) + padding (2) + packets_fwd (8) + bytes_fwd (8) + packets_rev (8) + bytes_rev (8)} Total: 64 bytes.
Encode a stats event for ring buffer.
{event_type (1) + padding (3) + timestamp (8) + src_ip (4) + dst_ip (4) +
src_port (2) + dst_port (2) + target_ip (4) + target_port (2) + padding (2) +
packets_fwd (8) + bytes_fwd (8) + packets_rev (8) + bytes_rev (8)}
Total: 64 bytes.(encode-weighted-route-value target-group)(encode-weighted-route-value target-group flags)Encode weighted route value for BPF map. target-group is a TargetGroup record with :targets and :cumulative-weights. flags is optional (default 0).
Format (max 72 bytes):
IP and port are stored in network byte order for direct packet writes.
Encode weighted route value for BPF map. target-group is a TargetGroup record with :targets and :cumulative-weights. flags is optional (default 0). Format (max 72 bytes): - Header (8 bytes): target_count(1) + reserved(3) + flags(2) + reserved(2) - Per target (8 bytes each): ip(4) + port(2) + cumulative_weight(2) IP and port are stored in network byte order for direct packet writes.
(encode-weighted-route-value-unified target-group)(encode-weighted-route-value-unified target-group flags)Encode unified weighted route value for BPF map. target-group has :targets with :ip as 16-byte arrays, and :cumulative-weights. flags is optional (default 0).
Format (max 168 bytes):
IP bytes are stored in network byte order (big-endian).
Encode unified weighted route value for BPF map. target-group has :targets with :ip as 16-byte arrays, and :cumulative-weights. flags is optional (default 0). Format (max 168 bytes): - Header (8 bytes): target_count(1) + reserved(3) + flags(2) + reserved(2) - Per target (20 bytes each): ip(16) + port(2) + cumulative_weight(2) IP bytes are stored in network byte order (big-endian).
(fnv1a-64 data)Compute FNV-1a 64-bit hash of a byte array. This is a fast, non-cryptographic hash suitable for hash table lookups.
Compute FNV-1a 64-bit hash of a byte array. This is a fast, non-cryptographic hash suitable for hash table lookups.
(get-interface-index iface-name)Get interface index by name using /sys/class/net. Returns nil if interface not found.
Get interface index by name using /sys/class/net. Returns nil if interface not found.
(hostname->hash hostname)Hash a hostname for SNI map lookup using FNV-1a 64-bit. Hostname is lowercased before hashing for case-insensitive matching.
Hash a hostname for SNI map lookup using FNV-1a 64-bit. Hostname is lowercased before hashing for case-insensitive matching.
(ip->bytes ip-u32)Convert IP u32 to byte array (4 bytes, big-endian). Handles unsigned u32 values that may exceed Integer/MAX_VALUE.
Convert IP u32 to byte array (4 bytes, big-endian). Handles unsigned u32 values that may exceed Integer/MAX_VALUE.
(ip-in-cidr? ip-u32 {:keys [ip prefix-len]})Check if an IP address (u32) falls within a CIDR range.
Check if an IP address (u32) falls within a CIDR range.
(ip-string->bytes16 ip-str)Parse IPv4 or IPv6 string to unified 16-byte format. IPv4: padded with 12 zero bytes prefix IPv6: native 16 bytes
Parse IPv4 or IPv6 string to unified 16-byte format. IPv4: padded with 12 zero bytes prefix IPv6: native 16 bytes
(ip-string->u32 ip-str)Convert dotted-decimal IP string to network byte order u32. Example: "192.168.1.1" => 0xC0A80101 (3232235777)
Convert dotted-decimal IP string to network byte order u32. Example: "192.168.1.1" => 0xC0A80101 (3232235777)
(ipv4-bytes->bytes16 ipv4-bytes)Pad 4-byte IPv4 address to 16 bytes with zero prefix. Format: 00:00:00:00:00:00:00:00:00:00:00:00:AA:BB:CC:DD
Pad 4-byte IPv4 address to 16 bytes with zero prefix. Format: 00:00:00:00:00:00:00:00:00:00:00:00:AA:BB:CC:DD
(ipv4? ip-str)Check if string is an IPv4 address (dotted decimal, no colons).
Check if string is an IPv4 address (dotted decimal, no colons).
(ipv6-string->bytes ipv6-str)Convert IPv6 string to 16-byte array. Uses Java's InetAddress for robust parsing (handles all IPv6 formats). Example: "2001:db8::1" => [32 1 13 184 0 0 0 0 0 0 0 0 0 0 0 1]
Convert IPv6 string to 16-byte array. Uses Java's InetAddress for robust parsing (handles all IPv6 formats). Example: "2001:db8::1" => [32 1 13 184 0 0 0 0 0 0 0 0 0 0 0 1]
(ipv6? ip-str)Check if string is an IPv6 address (contains colon).
Check if string is an IPv6 address (contains colon).
(is-ip-string? s)Check if a string looks like an IPv4 address.
Check if a string looks like an IPv4 address.
(list-interfaces)List all network interface names.
List all network interface names.
(parse-cidr cidr-str)Parse CIDR notation string to {:ip <u32> :prefix-len <int>}. Supports both CIDR (e.g., "192.168.1.0/24") and single IP (e.g., "192.168.1.1"). Single IPs are treated as /32.
Parse CIDR notation string to {:ip <u32> :prefix-len <int>}.
Supports both CIDR (e.g., "192.168.1.0/24") and single IP (e.g., "192.168.1.1").
Single IPs are treated as /32.(parse-cidr-unified cidr-str)Parse CIDR notation string for IPv4 or IPv6. Returns {:ip <16-byte-array> :prefix-len <int> :af <:ipv4|:ipv6>}
Examples: "192.168.1.0/24" => {:ip <bytes16> :prefix-len 24 :af :ipv4} "2001:db8::/32" => {:ip <bytes16> :prefix-len 32 :af :ipv6} "192.168.1.1" => {:ip <bytes16> :prefix-len 32 :af :ipv4} "2001:db8::1" => {:ip <bytes16> :prefix-len 128 :af :ipv6}
Parse CIDR notation string for IPv4 or IPv6.
Returns {:ip <16-byte-array> :prefix-len <int> :af <:ipv4|:ipv6>}
Examples:
"192.168.1.0/24" => {:ip <bytes16> :prefix-len 24 :af :ipv4}
"2001:db8::/32" => {:ip <bytes16> :prefix-len 32 :af :ipv6}
"192.168.1.1" => {:ip <bytes16> :prefix-len 32 :af :ipv4}
"2001:db8::1" => {:ip <bytes16> :prefix-len 128 :af :ipv6}(port->u16 port)Convert port to unsigned 16-bit value.
Convert port to unsigned 16-bit value.
(port-valid? port)Check if port number is valid (1-65535).
Check if port number is valid (1-65535).
(resolve-hostname hostname)Resolve hostname to IP address (u32). Returns nil if resolution fails.
Resolve hostname to IP address (u32). Returns nil if resolution fails.
(resolve-hostname-all hostname)Resolve hostname to ALL A records (not just first). Returns vector of u32 IPs, or nil if resolution fails.
This is useful for DNS-based load balancing where a hostname may resolve to multiple backend IPs.
Resolve hostname to ALL A records (not just first). Returns vector of u32 IPs, or nil if resolution fails. This is useful for DNS-based load balancing where a hostname may resolve to multiple backend IPs.
(resolve-hostname-all-bytes16 hostname)Resolve hostname to ALL records in unified 16-byte format. Returns vector of 16-byte arrays, or nil if resolution fails.
Resolve hostname to ALL records in unified 16-byte format. Returns vector of 16-byte arrays, or nil if resolution fails.
(resolve-hostname-bytes16 hostname)Resolve hostname to 16-byte unified format. Works for both IPv4 and IPv6 hostnames. Returns nil if resolution fails.
Resolve hostname to 16-byte unified format. Works for both IPv4 and IPv6 hostnames. Returns nil if resolution fails.
(resolve-to-ip source-spec)Resolve a source specification to IP. Accepts: IP string, CIDR string, or hostname. Returns {:ip <u32> :prefix-len <int>} or nil on failure.
Resolve a source specification to IP.
Accepts: IP string, CIDR string, or hostname.
Returns {:ip <u32> :prefix-len <int>} or nil on failure.(u32->bytes16 ip-u32)Convert IPv4 u32 to 16-byte unified format.
Convert IPv4 u32 to 16-byte unified format.
(u32->ip-string n)Convert network byte order u32 to dotted-decimal IP string. Example: 0xC0A80101 => "192.168.1.1"
Convert network byte order u32 to dotted-decimal IP string. Example: 0xC0A80101 => "192.168.1.1"
(weighted-route-unified-value-size)Return the fixed size of unified weighted route values (168 bytes).
Return the fixed size of unified weighted route values (168 bytes).
(weighted-route-value-size)Return the fixed size of weighted route values (72 bytes). All weighted routes use the same size for BPF map compatibility.
Return the fixed size of weighted route values (72 bytes). All weighted routes use the same size for BPF map compatibility.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |