GNUnet  0.10.x
Functions
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

int GNUNET_DNSPARSER_check_label (const char *label)
 Check if a label in UTF-8 format can be coded into valid IDNA. More...
 
int GNUNET_DNSPARSER_check_name (const char *name)
 Check if a label in UTF-8 format can be coded into valid IDNA. More...
 
void GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
 Free SOA information record. More...
 
void GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert)
 Free CERT information record. More...
 
void GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
 Free SRV information record. More...
 
void GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
 Free MX information record. More...
 
void GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
 Free the given DNS record. More...
 
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. More...
 
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. More...
 
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. More...
 
struct GNUNET_DNSPARSER_SoaRecordGNUNET_DNSPARSER_parse_soa (const char *udp_payload, size_t udp_payload_length, size_t *off)
 Parse a DNS SOA record. More...
 
struct GNUNET_DNSPARSER_MxRecordGNUNET_DNSPARSER_parse_mx (const char *udp_payload, size_t udp_payload_length, size_t *off)
 Parse a DNS MX record. More...
 
struct GNUNET_DNSPARSER_SrvRecordGNUNET_DNSPARSER_parse_srv (const char *udp_payload, size_t udp_payload_length, size_t *off)
 Parse a DNS SRV record. More...
 
struct GNUNET_DNSPARSER_CertRecordGNUNET_DNSPARSER_parse_cert (const char *udp_payload, size_t udp_payload_length, size_t *off)
 Parse a DNS CERT record. More...
 
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. More...
 
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. More...
 
struct GNUNET_DNSPARSER_RecordGNUNET_DNSPARSER_duplicate_record (const struct GNUNET_DNSPARSER_Record *r)
 Duplicate (deep-copy) the given DNS record. More...
 
struct GNUNET_DNSPARSER_SoaRecordGNUNET_DNSPARSER_duplicate_soa_record (const struct GNUNET_DNSPARSER_SoaRecord *r)
 Duplicate (deep-copy) the given DNS record. More...
 
struct GNUNET_DNSPARSER_CertRecordGNUNET_DNSPARSER_duplicate_cert_record (const struct GNUNET_DNSPARSER_CertRecord *r)
 Duplicate (deep-copy) the given DNS record. More...
 
struct GNUNET_DNSPARSER_MxRecordGNUNET_DNSPARSER_duplicate_mx_record (const struct GNUNET_DNSPARSER_MxRecord *r)
 Duplicate (deep-copy) the given DNS record. More...
 
struct GNUNET_DNSPARSER_SrvRecordGNUNET_DNSPARSER_duplicate_srv_record (const struct GNUNET_DNSPARSER_SrvRecord *r)
 Duplicate (deep-copy) the given DNS record. More...
 
void GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
 Free memory taken by a packet. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
char * GNUNET_DNSPARSER_bin_to_hex (const void *data, size_t data_size)
 Convert a block of binary data to HEX. More...
 
size_t GNUNET_DNSPARSER_hex_to_bin (const char *hex, void *data)
 Convert a HEX string to block of binary data. More...
 

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 214 of file dnsparser.c.

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

Referenced by GNUNET_DNSPARSER_parse_name().

218 {
219  const uint8_t *input = (const uint8_t *)udp_payload;
220  char *ret;
221  char *tmp;
222  char *xstr;
223  uint8_t len;
224  size_t xoff;
225  char *utf8;
226  Idna_rc rc;
227 
228  ret = GNUNET_strdup("");
229  while (1)
230  {
231  if (*off >= udp_payload_length)
232  {
233  GNUNET_break_op(0);
234  goto error;
235  }
236  len = input[*off];
237  if (0 == len)
238  {
239  (*off)++;
240  break;
241  }
242  if (len < 64)
243  {
244  if (*off + 1 + len > udp_payload_length)
245  {
246  GNUNET_break_op(0);
247  goto error;
248  }
249  GNUNET_asprintf(&tmp, "%.*s", (int)len, &udp_payload[*off + 1]);
250  if (IDNA_SUCCESS !=
251  (rc = idna_to_unicode_8z8z(tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
252  {
254  _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
255  tmp,
256  idna_strerror(rc));
257  GNUNET_free(tmp);
258  GNUNET_asprintf(&tmp,
259  "%s%.*s.",
260  ret,
261  (int)len,
262  &udp_payload[*off + 1]);
263  }
264  else
265  {
266  GNUNET_free(tmp);
267  GNUNET_asprintf(&tmp, "%s%s.", ret, utf8);
268  free(utf8);
269  }
270  GNUNET_free(ret);
271  ret = tmp;
272  *off += 1 + len;
273  }
274  else if ((64 | 128) == (len & (64 | 128)))
275  {
276  if (depth > 32)
277  {
278  GNUNET_break_op(0);
279  goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
280  }
281  /* pointer to string */
282  if (*off + 1 > udp_payload_length)
283  {
284  GNUNET_break_op(0);
285  goto error;
286  }
287  xoff = ((len - (64 | 128)) << 8) + input[*off + 1];
288  xstr = parse_name(udp_payload, udp_payload_length, &xoff, depth + 1);
289  if (NULL == xstr)
290  {
291  GNUNET_break_op(0);
292  goto error;
293  }
294  GNUNET_asprintf(&tmp, "%s%s.", ret, xstr);
295  GNUNET_free(ret);
296  GNUNET_free(xstr);
297  ret = tmp;
298  if (strlen(ret) > udp_payload_length)
299  {
300  GNUNET_break_op(0);
301  goto error; /* we are looping (building an infinite string) */
302  }
303  *off += 2;
304  /* pointers always terminate names */
305  break;
306  }
307  else
308  {
309  /* neither pointer nor inline string, not supported... */
310  GNUNET_break_op(0);
311  goto error;
312  }
313  }
314  if (0 < strlen(ret))
315  ret[strlen(ret) - 1] = '\0'; /* eat tailing '.' */
316  return ret;
317 error:
318  GNUNET_break_op(0);
319  GNUNET_free(ret);
320  return NULL;
321 }
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:214
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_log(kind,...)
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
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 1153 of file dnsparser.c.

References GNUNET_DNSPARSER_Record::cert, GNUNET_DNSPARSER_RawRecord::data, GNUNET_DNSPARSER_Record::data, GNUNET_DNSPARSER_RawRecord::data_len, GNUNET_TUN_DnsRecordLine::data_len, GNUNET_DNSPARSER_Record::dns_traffic_class, GNUNET_TUN_DnsRecordLine::dns_traffic_class, GNUNET_DNSPARSER_Record::expiration_time, 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_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_memcpy, GNUNET_NO, GNUNET_OK, GNUNET_TIME_absolute_get_remaining(), GNUNET_DNSPARSER_Record::hostname, GNUNET_DNSPARSER_Record::mx, GNUNET_DNSPARSER_Record::name, GNUNET_DNSPARSER_Record::raw, GNUNET_TIME_Relative::rel_value_us, ret, GNUNET_DNSPARSER_Record::soa, GNUNET_DNSPARSER_Record::srv, start, GNUNET_TUN_DnsRecordLine::ttl, GNUNET_DNSPARSER_Record::type, and GNUNET_TUN_DnsRecordLine::type.

Referenced by GNUNET_DNSPARSER_pack().

1157 {
1158  int ret;
1159  size_t start;
1160  size_t pos;
1161  struct GNUNET_TUN_DnsRecordLine rl;
1162 
1163  start = *off;
1165  dst_len -
1166  sizeof(
1167  struct GNUNET_TUN_DnsRecordLine),
1168  off,
1169  record->name);
1170  if (GNUNET_OK != ret)
1171  return ret;
1172  /* '*off' is now the position where we will need to write the record line */
1173 
1174  pos = *off + sizeof(struct GNUNET_TUN_DnsRecordLine);
1175  switch (record->type)
1176  {
1178  ret = GNUNET_DNSPARSER_builder_add_mx(dst, dst_len, &pos, record->data.mx);
1179  break;
1180 
1182  ret =
1183  GNUNET_DNSPARSER_builder_add_cert(dst, dst_len, &pos, record->data.cert);
1184  break;
1185 
1187  ret =
1188  GNUNET_DNSPARSER_builder_add_soa(dst, dst_len, &pos, record->data.soa);
1189  break;
1190 
1195  dst_len,
1196  &pos,
1197  record->data.hostname);
1198  break;
1199 
1201  ret =
1202  GNUNET_DNSPARSER_builder_add_srv(dst, dst_len, &pos, record->data.srv);
1203  break;
1204 
1205  default:
1206  if (pos + record->data.raw.data_len > dst_len)
1207  {
1208  ret = GNUNET_NO;
1209  break;
1210  }
1211  GNUNET_memcpy(&dst[pos], record->data.raw.data, record->data.raw.data_len);
1212  pos += record->data.raw.data_len;
1213  ret = GNUNET_OK;
1214  break;
1215  }
1216  if (GNUNET_OK != ret)
1217  {
1218  *off = start;
1219  return GNUNET_NO;
1220  }
1221 
1222  if (pos - (*off + sizeof(struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
1223  {
1224  /* record data too long */
1225  *off = start;
1226  return GNUNET_NO;
1227  }
1228  rl.type = htons(record->type);
1229  rl.dns_traffic_class = htons(record->dns_traffic_class);
1230  rl.ttl = htonl(
1232  1000LL / 1000LL); /* in seconds */
1233  rl.data_len = htons(
1234  (uint16_t)(pos - (*off + sizeof(struct GNUNET_TUN_DnsRecordLine))));
1235  GNUNET_memcpy(&dst[*off], &rl, sizeof(struct GNUNET_TUN_DnsRecordLine));
1236  *off = pos;
1237  return GNUNET_OK;
1238 }
uint16_t type
See GNUNET_DNSPARSER_TYPE_*.
uint64_t rel_value_us
The actual value.
#define GNUNET_DNSPARSER_TYPE_CNAME
struct GNUNET_DNSPARSER_SrvRecord * srv
SRV data for SRV records.
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
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:1117
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_DNSPARSER_TYPE_MX
General DNS record prefix.
#define GNUNET_DNSPARSER_TYPE_PTR
#define GNUNET_DNSPARSER_TYPE_SOA
char * name
Name of the record that the query is for (0-terminated).
char * hostname
For NS, CNAME and PTR records, this is the uncompressed 0-terminated hostname.
struct GNUNET_DNSPARSER_SoaRecord * soa
SOA data for SOA records.
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:1076
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:1029
size_t data_len
Number of bytes in data.
void * data
Binary record data.
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:1000
struct GNUNET_TIME_Absolute expiration_time
When does the record expire?
struct GNUNET_DNSPARSER_MxRecord * mx
MX data for MX records.
#define GNUNET_DNSPARSER_TYPE_NS
uint16_t dns_traffic_class
See GNUNET_TUN_DNS_CLASS_*.
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:331
union GNUNET_DNSPARSER_Record::@27 data
Payload of the record (which one of these is valid depends on the &#39;type&#39;).
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:888
#define GNUNET_DNSPARSER_TYPE_SRV
struct GNUNET_DNSPARSER_RawRecord raw
Raw data for all other types.
#define GNUNET_DNSPARSER_TYPE_CERT
struct GNUNET_DNSPARSER_CertRecord * cert
CERT data for CERT records.
Here is the call graph for this function:
Here is the caller graph for this function: