GNUnet 0.28.0-dev.2-27-gc87478450
 
Loading...
Searching...
No Matches
dnsparser.c File Reference

helper library to parse DNS packets. More...

#include "platform.h"
#include "gnunet_util_lib.h"
Include dependency graph for dnsparser.c:

Go to the source code of this file.

Functions

enum GNUNET_GenericReturnValue GNUNET_DNSPARSER_check_label (const char *label)
 Check if a label in UTF-8 format can be coded into valid IDNA.
 
enum GNUNET_GenericReturnValue GNUNET_DNSPARSER_check_name (const char *name)
 Check if a label in UTF-8 format can be coded into valid IDNA.
 
void GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
 Free SOA information record.
 
void GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert)
 Free CERT information record.
 
void GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
 Free SRV information record.
 
void GNUNET_DNSPARSER_free_uri (struct GNUNET_DNSPARSER_UriRecord *uri)
 Free URI information record.
 
void GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
 Free MX information record.
 
void GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
 Free the given DNS record.
 
static char * parse_name (const char *udp_payload, size_t udp_payload_length, size_t *off, unsigned int depth)
 Parse name inside of a DNS query or record.
 
char * GNUNET_DNSPARSER_parse_name (const char *udp_payload, size_t udp_payload_length, size_t *off)
 Parse name inside of a DNS query or record.
 
int GNUNET_DNSPARSER_parse_query (const char *udp_payload, size_t udp_payload_length, size_t *off, struct GNUNET_DNSPARSER_Query *q)
 Parse a DNS query entry.
 
struct GNUNET_DNSPARSER_SoaRecordGNUNET_DNSPARSER_parse_soa (const char *udp_payload, size_t udp_payload_length, size_t *off)
 Parse a DNS SOA record.
 
struct GNUNET_DNSPARSER_MxRecordGNUNET_DNSPARSER_parse_mx (const char *udp_payload, size_t udp_payload_length, size_t *off)
 Parse a DNS MX record.
 
struct GNUNET_DNSPARSER_SrvRecordGNUNET_DNSPARSER_parse_srv (const char *udp_payload, size_t udp_payload_length, size_t *off)
 Parse a DNS SRV record.
 
struct GNUNET_DNSPARSER_UriRecordGNUNET_DNSPARSER_parse_uri (const char *udp_payload, size_t udp_payload_length, size_t *off)
 Parse a DNS URI record.
 
struct GNUNET_DNSPARSER_CertRecordGNUNET_DNSPARSER_parse_cert (const char *udp_payload, size_t udp_payload_length, size_t *off)
 Parse a DNS CERT record.
 
int GNUNET_DNSPARSER_parse_record (const char *udp_payload, size_t udp_payload_length, size_t *off, struct GNUNET_DNSPARSER_Record *r)
 Parse a DNS record entry.
 
struct GNUNET_DNSPARSER_PacketGNUNET_DNSPARSER_parse (const char *udp_payload, size_t udp_payload_length)
 Parse a UDP payload of a DNS packet in to a nice struct for further processing and manipulation.
 
struct GNUNET_DNSPARSER_RecordGNUNET_DNSPARSER_duplicate_record (const struct GNUNET_DNSPARSER_Record *r)
 Duplicate (deep-copy) the given DNS record.
 
struct GNUNET_DNSPARSER_SoaRecordGNUNET_DNSPARSER_duplicate_soa_record (const struct GNUNET_DNSPARSER_SoaRecord *r)
 Duplicate (deep-copy) the given DNS record.
 
struct GNUNET_DNSPARSER_CertRecordGNUNET_DNSPARSER_duplicate_cert_record (const struct GNUNET_DNSPARSER_CertRecord *r)
 Duplicate (deep-copy) the given DNS record.
 
struct GNUNET_DNSPARSER_MxRecordGNUNET_DNSPARSER_duplicate_mx_record (const struct GNUNET_DNSPARSER_MxRecord *r)
 Duplicate (deep-copy) the given DNS record.
 
struct GNUNET_DNSPARSER_SrvRecordGNUNET_DNSPARSER_duplicate_srv_record (const struct GNUNET_DNSPARSER_SrvRecord *r)
 Duplicate (deep-copy) the given DNS record.
 
struct GNUNET_DNSPARSER_UriRecordGNUNET_DNSPARSER_duplicate_uri_record (const struct GNUNET_DNSPARSER_UriRecord *r)
 Duplicate (deep-copy) the given DNS record.
 
void GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
 Free memory taken by a packet.
 
int GNUNET_DNSPARSER_builder_add_name (char *dst, size_t dst_len, size_t *off, const char *name)
 Add a DNS name to the UDP packet at the given location, converting the name to IDNA notation as necessary.
 
int GNUNET_DNSPARSER_builder_add_query (char *dst, size_t dst_len, size_t *off, const struct GNUNET_DNSPARSER_Query *query)
 Add a DNS query to the UDP packet at the given location.
 
int GNUNET_DNSPARSER_builder_add_mx (char *dst, size_t dst_len, size_t *off, const struct GNUNET_DNSPARSER_MxRecord *mx)
 Add an MX record to the UDP packet at the given location.
 
int GNUNET_DNSPARSER_builder_add_cert (char *dst, size_t dst_len, size_t *off, const struct GNUNET_DNSPARSER_CertRecord *cert)
 Add a CERT record to the UDP packet at the given location.
 
int GNUNET_DNSPARSER_builder_add_soa (char *dst, size_t dst_len, size_t *off, const struct GNUNET_DNSPARSER_SoaRecord *soa)
 Add an SOA record to the UDP packet at the given location.
 
int GNUNET_DNSPARSER_builder_add_srv (char *dst, size_t dst_len, size_t *off, const struct GNUNET_DNSPARSER_SrvRecord *srv)
 Add an SRV record to the UDP packet at the given location.
 
int GNUNET_DNSPARSER_builder_add_uri (char *dst, size_t dst_len, size_t *off, const struct GNUNET_DNSPARSER_UriRecord *uri)
 Add an URI record to the UDP packet at the given location.
 
static int add_record (char *dst, size_t dst_len, size_t *off, const struct GNUNET_DNSPARSER_Record *record)
 Add a DNS record to the UDP packet at the given location.
 
int GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p, uint16_t max, char **buf, size_t *buf_length)
 Given a DNS packet p, generate the corresponding UDP payload.
 
char * GNUNET_DNSPARSER_bin_to_hex (const void *data, size_t data_size)
 Convert a block of binary data to HEX.
 
size_t GNUNET_DNSPARSER_hex_to_bin (const char *hex, void *data)
 Convert a HEX string to block of binary data.
 

Detailed Description

helper library to parse DNS packets.

Author
Philipp Toelke
Christian Grothoff

Definition in file dnsparser.c.

Function Documentation

◆ parse_name()

static char * parse_name ( const char *  udp_payload,
size_t  udp_payload_length,
size_t *  off,
unsigned int  depth 
)
static

Parse name inside of a DNS query or record.

Parameters
udp_payloadentire UDP payload
udp_payload_lengthlength of udp_payload
offpointer to the offset of the name to parse in the udp_payload (to be incremented by the size of the name)
depthcurrent depth of our recursion (to prevent stack overflow)
Returns
name as 0-terminated C string on success, NULL if the payload is malformed

Definition at line 250 of file dnsparser.c.

254{
255 const uint8_t *input = (const uint8_t *) udp_payload;
256 char *ret;
257 char *tmp;
258 char *xstr;
259 uint8_t len;
260 size_t xoff;
261 char *utf8;
262 Idna_rc rc;
263
264 ret = GNUNET_strdup ("");
265 while (1)
266 {
267 if (*off >= udp_payload_length)
268 {
269 GNUNET_break_op (0);
270 goto error;
271 }
272 len = input[*off];
273 if (0 == len)
274 {
275 (*off)++;
276 break;
277 }
278 if (len < 64)
279 {
280 if (*off + 1 + len > udp_payload_length)
281 {
282 GNUNET_break_op (0);
283 goto error;
284 }
285 GNUNET_asprintf (&tmp, "%.*s", (int) len, &udp_payload[*off + 1]);
286 if (IDNA_SUCCESS !=
287 (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
288 {
290 _ ("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
291 tmp,
292 idna_strerror (rc));
293 GNUNET_free (tmp);
294 GNUNET_asprintf (&tmp,
295 "%s%.*s.",
296 ret,
297 (int) len,
298 &udp_payload[*off + 1]);
299 }
300 else
301 {
302 GNUNET_free (tmp);
303 GNUNET_asprintf (&tmp, "%s%s.", ret, utf8);
304 free (utf8);
305 }
307 ret = tmp;
308 *off += 1 + len;
309 }
310 else if ((64 | 128) == (len & (64 | 128)))
311 {
312 if (depth > 32)
313 {
314 GNUNET_break_op (0);
315 goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
316 }
317 /* pointer to string */
318 if (*off + 1 > udp_payload_length)
319 {
320 GNUNET_break_op (0);
321 goto error;
322 }
323 xoff = ((len - (64 | 128)) << 8) + input[*off + 1];
324 xstr = parse_name (udp_payload, udp_payload_length, &xoff, depth + 1);
325 if (NULL == xstr)
326 {
327 GNUNET_break_op (0);
328 goto error;
329 }
330 GNUNET_asprintf (&tmp, "%s%s.", ret, xstr);
332 GNUNET_free (xstr);
333 ret = tmp;
334 if (strlen (ret) > udp_payload_length)
335 {
336 GNUNET_break_op (0);
337 goto error; /* we are looping (building an infinite string) */
338 }
339 *off += 2;
340 /* pointers always terminate names */
341 break;
342 }
343 else
344 {
345 /* neither pointer nor inline string, not supported... */
346 GNUNET_break_op (0);
347 goto error;
348 }
349 }
350 if (0 < strlen (ret))
351 ret[strlen (ret) - 1] = '\0'; /* eat tailing '.' */
352 return ret;
353error:
354 GNUNET_break_op (0);
356 return NULL;
357}
static char * parse_name(const char *udp_payload, size_t udp_payload_length, size_t *off, unsigned int depth)
Parse name inside of a DNS query or record.
Definition dnsparser.c:250
static int ret
Final status code.
Definition gnunet-arm.c:93
#define GNUNET_log(kind,...)
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
@ GNUNET_ERROR_TYPE_INFO
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_free(ptr)
Wrapper around free.
#define _(String)
GNU gettext support macro.
Definition platform.h:179

References _, GNUNET_asprintf(), GNUNET_break_op, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_log, GNUNET_strdup, parse_name(), and ret.

Referenced by GNUNET_DNSPARSER_parse_name(), and parse_name().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_record()

static int add_record ( char *  dst,
size_t  dst_len,
size_t *  off,
const struct GNUNET_DNSPARSER_Record record 
)
static

Add a DNS record to the UDP packet at the given location.

Parameters
dstwhere to write the query
dst_lennumber of bytes in dst
offpointer to offset where to write the query (increment by bytes used) must not be changed if there is an error
recordrecord to write
Returns
GNUNET_SYSERR if record is invalid GNUNET_NO if record did not fit GNUNET_OK if record was added to dst

Definition at line 1310 of file dnsparser.c.

1314{
1315 int ret;
1316 size_t start;
1317 size_t pos;
1318 struct GNUNET_TUN_DnsRecordLine rl;
1319
1320 if (dst_len < sizeof(struct GNUNET_TUN_DnsRecordLine))
1321 return GNUNET_NO;
1322 start = *off;
1324 dst,
1325 dst_len - sizeof(struct GNUNET_TUN_DnsRecordLine),
1326 off,
1327 record->name);
1328 if (GNUNET_OK != ret)
1329 return ret;
1330 /* '*off' is now the position where we will need to write the record line */
1331
1332 pos = *off + sizeof(struct GNUNET_TUN_DnsRecordLine);
1333 switch (record->type)
1334 {
1336 ret = GNUNET_DNSPARSER_builder_add_mx (dst, dst_len, &pos, record->data.mx);
1337 break;
1338
1340 ret =
1341 GNUNET_DNSPARSER_builder_add_cert (dst, dst_len, &pos, record->data.cert);
1342 break;
1343
1345 ret =
1346 GNUNET_DNSPARSER_builder_add_soa (dst, dst_len, &pos, record->data.soa);
1347 break;
1348
1353 dst_len,
1354 &pos,
1355 record->data.hostname);
1356 break;
1358 ret =
1359 GNUNET_DNSPARSER_builder_add_srv (dst, dst_len, &pos, record->data.srv);
1360 break;
1362 ret =
1363 GNUNET_DNSPARSER_builder_add_uri (dst, dst_len, &pos, record->data.uri);
1364 break;
1365 default:
1366 if ( (pos + record->data.raw.data_len < pos) ||
1367 (pos + record->data.raw.data_len > dst_len) )
1368 {
1369 ret = GNUNET_NO;
1370 break;
1371 }
1372 GNUNET_memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
1373 pos += record->data.raw.data_len;
1374 ret = GNUNET_OK;
1375 break;
1376 }
1377 if (GNUNET_OK != ret)
1378 {
1379 *off = start;
1380 return GNUNET_NO;
1381 }
1382
1383 if (pos - (*off + sizeof(struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1384 {
1385 /* record data too long */
1386 *off = start;
1387 return GNUNET_NO;
1388 }
1389 rl.type = htons (record->type);
1390 rl.dns_traffic_class = htons (record->dns_traffic_class);
1391 rl.ttl = htonl (
1393 / 1000LL / 1000LL); /* in seconds */
1394 rl.data_len = htons (
1395 (uint16_t) (pos - (*off + sizeof(struct GNUNET_TUN_DnsRecordLine))));
1396 GNUNET_memcpy (&dst[*off], &rl, sizeof(struct GNUNET_TUN_DnsRecordLine));
1397 *off = pos;
1398 return GNUNET_OK;
1399}
static int start
Set if we are to start default services (including ARM).
Definition gnunet-arm.c:38
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
#define GNUNET_DNSPARSER_TYPE_URI
int GNUNET_DNSPARSER_builder_add_name(char *dst, size_t dst_len, size_t *off, const char *name)
Add a DNS name to the UDP packet at the given location, converting the name to IDNA notation as neces...
Definition dnsparser.c:1002
int GNUNET_DNSPARSER_builder_add_cert(char *dst, size_t dst_len, size_t *off, const struct GNUNET_DNSPARSER_CertRecord *cert)
Add a CERT record to the UDP packet at the given location.
Definition dnsparser.c:1146
#define GNUNET_DNSPARSER_TYPE_SRV
#define GNUNET_DNSPARSER_TYPE_SOA
#define GNUNET_DNSPARSER_TYPE_CERT
#define GNUNET_DNSPARSER_TYPE_PTR
#define GNUNET_DNSPARSER_TYPE_NS
int GNUNET_DNSPARSER_builder_add_soa(char *dst, size_t dst_len, size_t *off, const struct GNUNET_DNSPARSER_SoaRecord *soa)
Add an SOA record to the UDP packet at the given location.
Definition dnsparser.c:1194
#define GNUNET_DNSPARSER_TYPE_CNAME
int GNUNET_DNSPARSER_builder_add_mx(char *dst, size_t dst_len, size_t *off, const struct GNUNET_DNSPARSER_MxRecord *mx)
Add an MX record to the UDP packet at the given location.
Definition dnsparser.c:1116
int GNUNET_DNSPARSER_builder_add_srv(char *dst, size_t dst_len, size_t *off, const struct GNUNET_DNSPARSER_SrvRecord *srv)
Add an SRV record to the UDP packet at the given location.
Definition dnsparser.c:1235
int GNUNET_DNSPARSER_builder_add_uri(char *dst, size_t dst_len, size_t *off, const struct GNUNET_DNSPARSER_UriRecord *uri)
Add an URI record to the UDP packet at the given location.
Definition dnsparser.c:1272
#define GNUNET_DNSPARSER_TYPE_MX
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_NO
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition time.c:406
uint64_t rel_value_us
The actual value.
General DNS record prefix.

References GNUNET_TUN_DnsRecordLine::data_len, GNUNET_TUN_DnsRecordLine::dns_traffic_class, GNUNET_DNSPARSER_builder_add_cert(), GNUNET_DNSPARSER_builder_add_mx(), GNUNET_DNSPARSER_builder_add_name(), GNUNET_DNSPARSER_builder_add_soa(), GNUNET_DNSPARSER_builder_add_srv(), GNUNET_DNSPARSER_builder_add_uri(), GNUNET_DNSPARSER_TYPE_CERT, GNUNET_DNSPARSER_TYPE_CNAME, GNUNET_DNSPARSER_TYPE_MX, GNUNET_DNSPARSER_TYPE_NS, GNUNET_DNSPARSER_TYPE_PTR, GNUNET_DNSPARSER_TYPE_SOA, GNUNET_DNSPARSER_TYPE_SRV, GNUNET_DNSPARSER_TYPE_URI, GNUNET_memcpy, GNUNET_NO, GNUNET_OK, GNUNET_TIME_absolute_get_remaining(), record(), GNUNET_TIME_Relative::rel_value_us, ret, start, GNUNET_TUN_DnsRecordLine::ttl, and GNUNET_TUN_DnsRecordLine::type.

Referenced by GNUNET_DNSPARSER_pack().

Here is the call graph for this function:
Here is the caller graph for this function: