summaryrefslogtreecommitdiff
path: root/src/libs6dns/s6dns_domain_decode.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs6dns/s6dns_domain_decode.c')
-rw-r--r--src/libs6dns/s6dns_domain_decode.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/libs6dns/s6dns_domain_decode.c b/src/libs6dns/s6dns_domain_decode.c
new file mode 100644
index 0000000..db4ccf5
--- /dev/null
+++ b/src/libs6dns/s6dns_domain_decode.c
@@ -0,0 +1,30 @@
+/* ISC license. */
+
+#include <errno.h>
+#include <skalibs/error.h>
+#include <skalibs/bytestr.h>
+#include <s6-dns/s6dns-domain.h>
+
+static inline unsigned int s6dns_domain_label_decode (char *s, unsigned int max)
+{
+ unsigned int len = *(unsigned char *)s ;
+ if ((len > 63) || (len >= max)) return (errno = EPROTO, 0) ;
+ *s = '.' ;
+ case_lowerb(s+1, len) ;
+ return len + 1 ;
+}
+
+int s6dns_domain_decode (s6dns_domain_t *d)
+{
+ unsigned int max = 255 ;
+ unsigned int pos = 0 ;
+ for (;;)
+ {
+ register unsigned int r = s6dns_domain_label_decode(d->s + pos, max - pos) ;
+ if (!r) return 0 ;
+ pos += r ;
+ if (r == 1) break ;
+ }
+ d->len = pos ;
+ return 1 ;
+}