From 60f55f34eeec70842b21accdd7fc0dc5d89493a2 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 24 Oct 2026 17:21:47 +0200 Subject: Add an ip address parser and formatter for all formats opentracker needs --- ot_ip.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 ot_ip.h (limited to 'ot_ip.h') diff --git a/ot_ip.h b/ot_ip.h new file mode 100644 index 0000000..ac8964d --- /dev/null +++ b/ot_ip.h @@ -0,0 +1,33 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. + + $id$ */ + +#ifndef OT_IP_H__ +#define OT_IP_H__ + +#include +#include + +#include "trackerlogic.h" + +/* opentracker holds every address as sixteen bytes, writing IPv4 as the v4 + mapped form of RFC 4291 section 2.5.5.2. */ +extern const char ip_v4mapped_prefix[12]; + +static inline int ip_is_v4mapped(const void *ip) { return !memcmp(ip, ip_v4mapped_prefix, sizeof(ip_v4mapped_prefix)); } + +/* Read an address and return how many characters it took, or zero if src + does not start with one. A plain IPv4 address is accepted and stored in + its v4 mapped form. + + The count lets the caller carry on where the address ended, which is how + "address:port" and "address/bits" are read. */ +size_t ip_scan(const char *src, ot_ip6 ip); + +/* Write an address and return how many characters it took. Nothing is + terminated, so a caller may keep appending. A v4 mapped address is written + as the plain dotted quad rather than "::ffff:1.2.3.4". */ +size_t ip_format(char *dest, const ot_ip6 ip); + +#endif -- cgit v1.3