aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs6dns/s6dns_message_get_domain_nodecode.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2021-01-14 23:24:53 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2021-01-14 23:24:53 +0000
commitf3d8514681a6a9a0664d3ad0b6b72eb87346f92f (patch)
treeaa4c9c1110ab1e39b320f8e11e67196b70529f00 /src/libs6dns/s6dns_message_get_domain_nodecode.c
parent033e162e95fb13196b3df93b708c535a3977e201 (diff)
downloads6-dns-f3d8514681a6a9a0664d3ad0b6b72eb87346f92f.tar.gz
Prepare for 2.3.5.0, s6dns_message_get_domain_nodecode() now public
Diffstat (limited to 'src/libs6dns/s6dns_message_get_domain_nodecode.c')
-rw-r--r--src/libs6dns/s6dns_message_get_domain_nodecode.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/libs6dns/s6dns_message_get_domain_nodecode.c b/src/libs6dns/s6dns_message_get_domain_nodecode.c
new file mode 100644
index 0000000..9774527
--- /dev/null
+++ b/src/libs6dns/s6dns_message_get_domain_nodecode.c
@@ -0,0 +1,49 @@
+/* ISC license. */
+
+#include <string.h>
+#include <errno.h>
+
+#include <skalibs/posixishard.h>
+
+#include <s6-dns/s6dns-message.h>
+
+size_t s6dns_message_get_domain_nodecode (char *out, size_t outmax, char const *s, unsigned int len, unsigned int *pos)
+{
+ size_t w = 0 ; /* writing head */
+ unsigned int r = *pos ; /* reading head */
+ unsigned int jumps = 0 ;
+ int hasjumped = 0 ;
+ for (;;)
+ {
+ unsigned char c ;
+ if (r >= len) return (errno = EPROTO, 0) ;
+ c = s[r] ;
+ if (c < 64) /* normal label */
+ {
+ if (r + ++c > len) return (errno = EPROTO, 0) ;
+ if (out)
+ {
+ if (w + c > outmax) return (errno = ENAMETOOLONG, 0) ;
+ memcpy(out + w, s + r, c) ;
+ }
+ w += c ; r += c ; if (!hasjumped) *pos += c ;
+ if (c == 1) break ;
+ }
+ else if (c >= 192) /* pointer */
+ {
+ if (r + 1 >= len) return (errno = EPROTO, 0) ;
+ if (hasjumped)
+ {
+ if (++jumps > 1000) return (errno = EPROTO, 0) ;
+ }
+ else
+ {
+ *pos += 2 ;
+ hasjumped = 1 ;
+ }
+ r = (((unsigned int)c & 63) << 8) | (unsigned char)(s[r + 1]) ;
+ }
+ else return (errno = EPROTONOSUPPORT, 0) ; /* unsupported extension */
+ }
+ return w ;
+}