dns.c revision 4ba1a4b0b4488e5058af67b789187735b529075e
1ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering#include <stdlib.h>
2ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering#include <string.h>
3ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering#include <stdio.h>
4ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering
5ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering#include "dns.h"
6ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering
7ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart PoetteringflxDnsPacket* flx_dns_packet_new(void) {
8ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering    flxDnsPacket *p;
9ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering    p = g_new(flxDnsPacket, 1);
10ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering    p->size = p->rindex = 2*6;
11ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering    memset(p->data, 0, p->size);
12ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering    return p;
13ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering}
14ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering
15ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poetteringvoid flx_dns_packet_free(flxDnsPacket *p) {
16ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering    g_assert(p);
17ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering    g_free(p);
18ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering}
19ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering
20ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poetteringvoid flx_dns_packet_set_field(flxDnsPacket *p, guint index, guint16 v) {
21ab84bbc4bef07413f95c3237e29f2287319e0f4cLennart Poettering    g_assert(p);
22eca309458f9521a2e3276dc11a1792d5f2e227e4Lennart Poettering    g_assert(index < 2*6);
23eca309458f9521a2e3276dc11a1792d5f2e227e4Lennart Poettering
24eca309458f9521a2e3276dc11a1792d5f2e227e4Lennart Poettering    ((guint16*) p->data)[index] = g_htons(v);
25eca309458f9521a2e3276dc11a1792d5f2e227e4Lennart Poettering}
26a20c01bd12216d409e0dfc5d3bbacc940352bfceLennart Poettering
27a20c01bd12216d409e0dfc5d3bbacc940352bfceLennart Poetteringguint16 flx_dns_packet_get_field(flxDnsPacket *p, guint index) {
284ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    g_assert(p);
294ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    g_assert(index < 2*6);
304ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering
314f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    return g_ntohs(((guint16*) p->data)[index]);
324ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering}
335ebf655c85076f200955458673a8bbf0dd927407Lennart Poettering
344f0a5e7572a4257894b4bfede42c26d65152609eLennart Poetteringguint8* flx_dns_packet_append_name(flxDnsPacket *p, const gchar *name) {
354f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    guint8 *d, *f = NULL;
364ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering
374f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    g_assert(p);
384ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    g_assert(name);
394f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
407dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poettering    for (;;) {
414f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering        guint n = strcspn(name, ".");
42c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering        if (!n || n > 63)
43602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering            return NULL;
447dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poettering
45b6820898d317c29a31f97018ede6da5195d16bfbLennart Poettering        d = flx_dns_packet_extend(p, n+1);
46b6820898d317c29a31f97018ede6da5195d16bfbLennart Poettering        if (!f)
47602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering            f = d;
48602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering        d[0] = n;
49602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering        memcpy(d+1, name, n);
50602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering
517dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poettering        name += n;
52c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering
534f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering        /* no trailing dot */
544f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering        if (!*name)
554f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering            break;
567dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poettering
57c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering        name ++;
584ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering
59c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering        /* trailing dot */
607dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poettering        if (!*name)
61c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering            break;
62c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering    }
63c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering
644f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    d = flx_dns_packet_extend(p, 1);
657dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poettering    d[0] = 0;
66c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering
674f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    return f;
684f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering}
694f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
707dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poetteringguint8* flx_dns_packet_append_uint16(flxDnsPacket *p, guint16 v) {
71c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering    guint8 *d;
72c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering
73c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering    g_assert(p);
744f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
757dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poettering    d = flx_dns_packet_extend(p, sizeof(guint16));
76c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering    *((guint16*) d) = g_htons(v);
774f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
784f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    return d;
794f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering}
808d8c0255f0f0242a067b577747740bab1b1021eaLennart Poettering
814ba1a4b0b4488e5058af67b789187735b529075eLennart Poetteringguint8 *flx_dns_packet_extend(flxDnsPacket *p, guint l) {
824ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    guint8 *d;
834ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering
844f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    g_assert(p);
85602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    g_assert(p->size+l <= sizeof(p->data));
864f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
87602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    d = p->data + p->size;
884f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    p->size += l;
894f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
90602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    return d;
91602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering}
924f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
934f0a5e7572a4257894b4bfede42c26d65152609eLennart Poetteringguint8 *flx_dns_packet_append_name_compressed(flxDnsPacket *p, const gchar *name, guint8 *prev) {
94602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    guint16 *d;
95602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    signed long k;
96602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    g_assert(p);
97602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering
98602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    if (!prev)
99602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering        return flx_dns_packet_append_name(p, name);
1004f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
101602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    k = prev - p->data;
102602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    if (k < 0 || k >= 0x4000 || (guint) k >= p->size)
103602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering        return flx_dns_packet_append_name(p, name);
104602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering
105602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    d = (guint16*) flx_dns_packet_extend(p, sizeof(guint16));
106602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    *d = g_htons((0xC000 | k));
107602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering
108602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    return prev;
109602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering}
110602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering
111602a2b6481587b7da2594db39151ec9380f276dfLennart Poetteringgint flx_dns_packet_check_valid(flxDnsPacket *p) {
112602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    guint16 flags;
113602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    g_assert(p);
114602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering
115602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    if (p->size < 12)
116602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering        return -1;
1178d8c0255f0f0242a067b577747740bab1b1021eaLennart Poettering
118602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    flags = flx_dns_packet_get_field(p, DNS_FIELD_FLAGS);
119602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering
120602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering    if (flags & DNS_FLAG_OPCODE || flags & DNS_FLAG_RCODE)
121602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering        return -1;
122602a2b6481587b7da2594db39151ec9380f276dfLennart Poettering
1237dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poettering    return 0;
1244f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering}
1254ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering
1264ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poetteringgint flx_dns_packet_check_valid_response(flxDnsPacket *p) {
1274f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    guint16 flags;
1284ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering    g_assert(p);
1294f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
1304ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    if (flx_dns_packet_check_valid(p) < 0)
1314ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering        return -1;
1324f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
1334f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    flags = flx_dns_packet_get_field(p, DNS_FIELD_FLAGS);
1344f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
1354ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    if (!(flags & DNS_FLAG_QR))
1364f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering        return -1;
1374ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering
1384ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) > 0)
1394f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering        return -1;
1404f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
1414f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    return 0;
1424ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering}
1434f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
1444ba1a4b0b4488e5058af67b789187735b529075eLennart Poetteringstatic gint consume_labels(flxDnsPacket *p, guint index, gchar *ret_name, guint l) {
1454ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    gint ret = 0;
1464f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    int compressed = 0;
1474f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    int first_label = 1;
1484f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    g_assert(p && ret_name && l);
149af1ce5fe2245fba0045d2c0d70b58b700938bd2fLennart Poettering
15066142b071a2497d7e3cf58d7bf5159bb1c970d84Lennart Poettering    for (;;) {
151af1ce5fe2245fba0045d2c0d70b58b700938bd2fLennart Poettering        guint8 n;
152af1ce5fe2245fba0045d2c0d70b58b700938bd2fLennart Poettering
1534f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering        if (index+1 > p->size)
1544f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering            return -1;
1554f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
1564ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering        n = p->data[index];
1574f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
1584f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering        if (!n) {
1594ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering            index++;
160c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering            if (!compressed)
1617dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poettering                ret++;
1624c8155fa9c85c7ece92182d0ab53f99a982a4abfLennart Poettering
1634e2d82b18f7e099cbce933b383c45efa40306161Lennart Poettering            if (l < 1)
1644f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering                return -1;
1654f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering            *ret_name = 0;
1664f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
1674f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering            return ret;
1684ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering
1694ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering        } else if (n <= 63) {
1704ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering            /* Uncompressed label */
1714f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering            index++;
1724f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering            if (!compressed)
1734ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering                ret++;
1744f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
1754f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering            if (index + n > p->size)
1764ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering                return -1;
1774f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
1784ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering            if ((guint) n + 1 > l)
17966142b071a2497d7e3cf58d7bf5159bb1c970d84Lennart Poettering                return -1;
1804f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
1814f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering            if (!first_label) {
1824ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering                *(ret_name++) = '.';
1834ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering                l--;
1844f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering            } else
1854c8155fa9c85c7ece92182d0ab53f99a982a4abfLennart Poettering                first_label = 0;
1864ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering
1874ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering            memcpy(ret_name, p->data + index, n);
1884e2d82b18f7e099cbce933b383c45efa40306161Lennart Poettering            index += n;
1894e2d82b18f7e099cbce933b383c45efa40306161Lennart Poettering            ret_name += n;
1904ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering            l -= n;
191a3596a5e3ec4937a220e6e60218639e2aba82701Lennart Poettering
192c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering            if (!compressed)
1934ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering                ret += n;
1947dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poettering        } else if ((n & 0xC0) == 0xC0) {
1954e2d82b18f7e099cbce933b383c45efa40306161Lennart Poettering            /* Compressed label */
1964c8155fa9c85c7ece92182d0ab53f99a982a4abfLennart Poettering
1974ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering            if (index+2 > p->size)
198a3596a5e3ec4937a220e6e60218639e2aba82701Lennart Poettering                return -1;
1994f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
2004ccd2cdd5ae567dc2cb6c05ca5e5a9537a9dc1c4Lennart Poettering            index = ((guint) (p->data[index] & ~0xC0)) << 8 | p->data[index+1];
2014f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
2024f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering            if (!compressed)
2034f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering                ret += 2;
2044f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
2054ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering            compressed = 1;
2064ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering        } else
2077dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poettering            return -1;
208c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering    }
209c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering}
2104e2d82b18f7e099cbce933b383c45efa40306161Lennart Poettering
2114ba1a4b0b4488e5058af67b789187735b529075eLennart Poetteringgint flx_dns_packet_consume_name(flxDnsPacket *p, gchar *ret_name, guint l) {
2124c8155fa9c85c7ece92182d0ab53f99a982a4abfLennart Poettering    gint r;
213c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering
214c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering    if ((r = consume_labels(p, p->rindex, ret_name, l)) < 0)
215c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering        return -1;
216c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering
2174ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    p->rindex += r;
2184ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    return 0;
2194f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering}
2204f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
2214f0a5e7572a4257894b4bfede42c26d65152609eLennart Poetteringgint flx_dns_packet_consume_uint16(flxDnsPacket *p, guint16 *ret_v) {
2224ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    g_assert(p);
2234f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    g_assert(ret_v);
224c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering
2254ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    if (p->rindex + sizeof(guint16) > p->size)
2264f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering        return -1;
2274ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering
2284ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    *ret_v = g_ntohs(*((guint16*) (p->data + p->rindex)));
2294ba1a4b0b4488e5058af67b789187735b529075eLennart Poettering    p->rindex += sizeof(guint16);
2304f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
2314f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    return 0;
2324f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering}
233c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering
2344f0a5e7572a4257894b4bfede42c26d65152609eLennart Poetteringgint flx_dns_packet_consume_uint32(flxDnsPacket *p, guint32 *ret_v) {
235c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering    g_assert(p);
236c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering    g_assert(ret_v);
2374f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
23854247ce93dc6f048c0ffea546f454653ce0e25a8Lennart Poettering    if (p->rindex + sizeof(guint32) > p->size)
23954247ce93dc6f048c0ffea546f454653ce0e25a8Lennart Poettering        return -1;
24054247ce93dc6f048c0ffea546f454653ce0e25a8Lennart Poettering
24154247ce93dc6f048c0ffea546f454653ce0e25a8Lennart Poettering    *ret_v = g_ntohl(*((guint32*) (p->data + p->rindex)));
2424f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    p->rindex += sizeof(guint32);
2434f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
24454247ce93dc6f048c0ffea546f454653ce0e25a8Lennart Poettering    return 0;
2454f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering}
2464f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
2474f0a5e7572a4257894b4bfede42c26d65152609eLennart Poetteringgint flx_dns_packet_consume_bytes(flxDnsPacket *p, gpointer ret_data, guint l) {
2484aa744ffac20c7b5e18cb3b23e5dbac8221c0043Lennart Poettering    g_assert(p);
2497dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poettering    g_assert(ret_data);
250c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering    g_assert(l > 0);
25154247ce93dc6f048c0ffea546f454653ce0e25a8Lennart Poettering
252c18626ad35cdf94edbff196070ccbb6ae825abd0Lennart Poettering    if (p->rindex + l > p->size)
25354247ce93dc6f048c0ffea546f454653ce0e25a8Lennart Poettering        return -1;
25454247ce93dc6f048c0ffea546f454653ce0e25a8Lennart Poettering
25554247ce93dc6f048c0ffea546f454653ce0e25a8Lennart Poettering    memcpy(ret_data, p->data + p->rindex, l);
2564f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    p->rindex += l;
2574f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
2584f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering    return 0;
259a20c01bd12216d409e0dfc5d3bbacc940352bfceLennart Poettering}
2604f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering
2614f0a5e7572a4257894b4bfede42c26d65152609eLennart Poetteringgint flx_dns_packet_skip(flxDnsPacket *p, guint length) {
262a20c01bd12216d409e0dfc5d3bbacc940352bfceLennart Poettering    g_assert(p);
263a20c01bd12216d409e0dfc5d3bbacc940352bfceLennart Poettering
264a20c01bd12216d409e0dfc5d3bbacc940352bfceLennart Poettering    if (p->rindex + length > p->size)
265a20c01bd12216d409e0dfc5d3bbacc940352bfceLennart Poettering        return -1;
2667dce450bdc23ea306a61e00f914481e29ebcb176Lennart Poettering
267a20c01bd12216d409e0dfc5d3bbacc940352bfceLennart Poettering    p->rindex += length;
268a20c01bd12216d409e0dfc5d3bbacc940352bfceLennart Poettering    return 0;
2694f0a5e7572a4257894b4bfede42c26d65152609eLennart Poettering}
270a20c01bd12216d409e0dfc5d3bbacc940352bfceLennart Poettering