unsigned int dns_packet_getname(const char *buf,unsigned int len,unsigned int pos,char **d) { unsigned int loop = 0; unsigned int state = 0; unsigned int firstcompress = 0; unsigned int where; unsigned char ch; char name[255]; unsigned int namelen = 0; for (;;) { if (pos >= len) goto PROTO; ch = buf[pos++]; if (++loop >= 1000) goto PROTO; if (state) { if (namelen + 1 > sizeof name) goto PROTO; name[namelen++] = ch; --state; } else { while (ch >= 192) { where = ch; where -= 192; where <<= 8; if (pos >= len) goto PROTO; ch = buf[pos++]; if (!firstcompress) firstcompress = pos; pos = where + ch; if (pos >= len) goto PROTO; ch = buf[pos++]; if (++loop >= 1000) goto PROTO; } if (ch >= 64) goto PROTO; if (namelen + 1 > sizeof name) goto PROTO; name[namelen++] = ch; if (!ch) break; state = ch; } } if (!dns_domain_copy(d,name)) return 0; if (firstcompress) return firstcompress; return pos; PROTO: errno = EINVAL; return 0; }
int main () { stralloc out ={0}; stralloc sa ={0}; char ip[4]; char *dn =0; stralloc_copys(&sa, "abcdefg"); dns_ip4(&out, &sa); dns_ip4_qualify(&out, &sa, &sa); dns_name4(&out, ip); dns_mx(&out, &sa); dns_txt(&out, &sa); dns_domain_length(sa.s); dns_domain_equal(sa.s, sa.s); dns_domain_copy(&dn, sa.s); dns_domain_fromdot(&dn, sa.s, sa.len); return 0; }