Utility functions for IP address conversion, CIDR parsing, and binary encoding.
Utility functions for IP address conversion, CIDR parsing, and binary encoding.
(bytes->ip b)Convert 4-byte array to IP u32.
Convert 4-byte array to IP u32.
(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.
(decode-conntrack-key b)Decode connection tracking key from byte array.
Decode 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-listen-key b)Decode listen map key from byte array.
Decode 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-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.(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-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-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-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-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.
(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).
Convert IP u32 to byte array (4 bytes, big-endian).
(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->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)
(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.(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-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->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-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 |