uint8_t* dns_packet_append_name(struct dns_packet *p, const char *name) { uint8_t *d, *f = NULL; assert(p); for (;;) { size_t n = strcspn(name, "."); if (!n || n > 63) return NULL; d = dns_packet_extend(p, n+1); if (!f) f = d; d[0] = (uint8_t)n; memcpy(d+1, name, n); name += n; /* no trailing dot */ if (!*name) break; name ++; /* trailing dot */ if (!*name) break; } d = dns_packet_extend(p, 1); d[0] = 0; return f; }
uint8_t* dns_packet_append_text(struct dns_packet *p, const char *text) { uint8_t *d, *f = NULL; assert(p); for (;;) { size_t n = strcspn(text, "\n"); if (!n || n > 63) return NULL; d = dns_packet_extend(p, n+1); if (!f) f = d; d[0] = (uint8_t)n; memcpy(d+1, text, n); text += n; /* no trailing dot */ if (!*text) break; text ++; /* trailing dot */ if (!*text) break; } d = dns_packet_extend(p, 1); d[0] = 0; return f; }
uint8_t* dns_packet_append_uint16(struct dns_packet *p, uint16_t v) { uint8_t *d; assert(p); d = dns_packet_extend(p, sizeof(uint16_t)); SET_16_P(d, htons(v)); return d; }
uint8_t *dns_packet_append_ipv6(struct dns_packet *p, struct ipv6_address_struct_t *v) { int n; uint8_t *d; assert(p); d = dns_packet_extend(p, sizeof(v->address)); for(n=0; n<sizeof(v->address); n++) d[n]=v->address[n]; return d; }
uint8_t* dns_packet_append_uint32(struct dns_packet *p, uint32_t v) { uint8_t *d; uint32_t u; assert(p); d = dns_packet_extend(p, sizeof(uint32_t)); u=htonl(v); memcpy(d,&u,sizeof(u)); return d; }
uint8_t *dns_packet_append_name_compressed(struct dns_packet *p, const char *name, uint8_t *prev) { uint8_t *d; signed long k; assert(p); if (!prev) return dns_packet_append_name(p, name); k = prev - p->data; if (k < 0 || k >= 0x4000 || (size_t) k >= p->size) return dns_packet_append_name(p, name); d = dns_packet_extend(p, sizeof(uint16_t)); SET_16_P(d, htons((0xC000 | k))); return prev; }
uint8_t *dns_packet_append_name_compressed(struct dns_packet *p, const char *name, uint8_t *prev) { int8_t *d; ptrdiff_t k; uint16_t u; assert(p); if (!prev) return dns_packet_append_name(p, name); k = prev - p->data; if (k < 0 || k >= 0x4000 || (size_t) k >= p->size) return dns_packet_append_name(p, name); d = dns_packet_extend(p, sizeof(uint16_t)); u=htons((0xC000 | (u_short)k)); memcpy(d,&u,sizeof(u)); return prev; }