g_assert(p);
g_assert(b);
g_assert(l);
-
+
if (!(d = flx_dns_packet_extend(p, l)))
return NULL;
}
flxRecord* flx_dns_packet_consume_record(flxDnsPacket *p, gboolean *ret_cache_flush) {
- gchar name[256];
+ gchar name[257], buf[257+6];
guint16 type, class;
guint32 ttl;
guint16 rdlength;
flx_dns_packet_consume_uint16(p, &type) < 0 ||
flx_dns_packet_consume_uint16(p, &class) < 0 ||
flx_dns_packet_consume_uint32(p, &ttl) < 0 ||
- flx_dns_packet_consume_uint16(p, &rdlength) < 0 ||
- !(data = flx_dns_packet_get_rptr(p)) ||
- flx_dns_packet_skip(p, rdlength) < 0)
+ flx_dns_packet_consume_uint16(p, &rdlength) < 0)
return NULL;
+ switch (type) {
+ case FLX_DNS_TYPE_PTR:
+ case FLX_DNS_TYPE_CNAME:
+ if (flx_dns_packet_consume_name(p, buf, sizeof(buf)) < 0)
+ return NULL;
+
+ data = buf;
+ rdlength = strlen(buf);
+ break;
+
+ case FLX_DNS_TYPE_SRV: {
+ const guint8 *t = flx_dns_packet_get_rptr(p);
+
+ if (flx_dns_packet_skip(p, 6) < 0)
+ return NULL;
+
+ memcpy(buf, t, 6);
+
+ if (flx_dns_packet_consume_name(p, buf+6, sizeof(buf)-6) < 0)
+ return NULL;
+
+ data = buf;
+ rdlength = 6 + strlen(buf+6);
+ break;
+ }
+
+ default:
+
+ if (rdlength > 0) {
+
+ if (!(data = flx_dns_packet_get_rptr(p)) ||
+ flx_dns_packet_skip(p, rdlength) < 0)
+ return NULL;
+ } else
+ data = NULL;
+
+ break;
+ }
+
*ret_cache_flush = !!(class & MDNS_CACHE_FLUSH);
class &= ~ MDNS_CACHE_FLUSH;
guint8* flx_dns_packet_append_key(flxDnsPacket *p, flxKey *k) {
guint8 *t;
+ guint size;
g_assert(p);
g_assert(k);
+ size = p->size;
+
if (!(t = flx_dns_packet_append_name(p, k->name)) ||
!flx_dns_packet_append_uint16(p, k->type) ||
- !flx_dns_packet_append_uint16(p, k->class))
+ !flx_dns_packet_append_uint16(p, k->class)) {
+ p->size = size;
return NULL;
+ }
return t;
}
guint8* flx_dns_packet_append_record(flxDnsPacket *p, flxRecord *r, gboolean cache_flush) {
guint8 *t;
+ guint size;
g_assert(p);
g_assert(r);
+ size = p->size;
+
if (!(t = flx_dns_packet_append_name(p, r->key->name)) ||
!flx_dns_packet_append_uint16(p, r->key->type) ||
!flx_dns_packet_append_uint16(p, cache_flush ? (r->key->class | MDNS_CACHE_FLUSH) : (r->key->class &~ MDNS_CACHE_FLUSH)) ||
- !flx_dns_packet_append_uint32(p, r->ttl) ||
- !flx_dns_packet_append_uint16(p, r->size) ||
- !flx_dns_packet_append_bytes(p, r->data, r->size))
- return NULL;
+ !flx_dns_packet_append_uint32(p, r->ttl))
+ goto fail;
+
+ switch (r->key->type) {
+
+ case FLX_DNS_TYPE_PTR:
+ case FLX_DNS_TYPE_CNAME: {
+ char ptr_name[257];
+
+ g_assert((size_t) r->size+1 <= sizeof(ptr_name));
+ memcpy(ptr_name, r->data, r->size);
+ ptr_name[r->size] = 0;
+
+ if (!flx_dns_packet_append_uint16(p, strlen(ptr_name)+1) ||
+ !flx_dns_packet_append_name(p, ptr_name))
+ goto fail;
+
+ break;
+ }
+
+ case FLX_DNS_TYPE_SRV: {
+ char name[257];
+
+ g_assert(r->size >= 6 && (size_t) r->size-6+1 <= sizeof(name));
+ memcpy(name, r->data+6, r->size-6);
+ name[r->size-6] = 0;
+
+ if (!flx_dns_packet_append_uint16(p, strlen(name+6)+1+6) ||
+ !flx_dns_packet_append_bytes(p, r->data, 6) ||
+ !flx_dns_packet_append_name(p, name))
+ goto fail;
+
+ break;
+ }
+
+ default:
+ if (!flx_dns_packet_append_uint16(p, r->size) ||
+ (r->size != 0 && !flx_dns_packet_append_bytes(p, r->data, r->size)))
+ goto fail;
+ }
return t;
+
+
+fail:
+ p->size = size;
+ return NULL;
+}
+
+gboolean flx_dns_packet_is_empty(flxDnsPacket *p) {
+ g_assert(p);
+
+ return p->size <= FLX_DNS_PACKET_HEADER_SIZE;
}