aboutsummaryrefslogtreecommitdiff
path: root/src/gns/gns_util.c
blob: b2f5e1f38149b34af1f35e7894bcf13f7747a592 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "gns.h"

/**
 * Add a DNS name to the buffer at the given location.
 *
 * @param dst where to write the name
 * @param dst_len number of bytes in dst
 * @param off pointer to offset where to write the name (increment by bytes used)
 *            must not be changed if there is an error
 * @param name name to write
 * @return GNUNET_SYSERR if 'name' is invalid
 *         GNUNET_NO if 'name' did not fit
 *         GNUNET_OK if 'name' was added to 'dst'
 */
static int
add_name (char *dst,
	  size_t dst_len,
	  size_t *off,
	  const char *name)
{
  const char *dot;
  size_t start;
  size_t pos;
  size_t len;

  if (NULL == name)
    return GNUNET_SYSERR;
  start = *off;
  if (start + strlen (name) + 2 > dst_len)
    return GNUNET_NO;
  pos = start;
  do
  {
    dot = strchr (name, '.');
    if (NULL == dot)
      len = strlen (name);
    else
      len = dot - name;
    if ( (len >= 64) || (len == 0) )
      return GNUNET_NO; /* segment too long or empty */
    dst[pos++] = (char) (uint8_t) len;
    memcpy (&dst[pos], name, len);
    pos += len;
    name += len + 1; /* also skip dot */
  }
  while (NULL != dot);
  dst[pos++] = '\0'; /* terminator */
  *off = pos;
  return GNUNET_OK;
}

/**
 * Add an MX record to the buffer at the given location.
 *
 * @param dst where to write the mx record
 * @param dst_len number of bytes in dst
 * @param off pointer to offset where to write the mx information (increment by bytes used);
 *            can also change if there was an error
 * @param mx mx information to write
 * @return GNUNET_SYSERR if 'mx' is invalid
 *         GNUNET_NO if 'mx' did not fit
 *         GNUNET_OK if 'mx' was added to 'dst'
 */
static int
add_mx (char *dst,
	size_t dst_len,
	size_t *off,
	const struct GNUNET_DNSPARSER_MxRecord *mx)
{
  uint16_t mxpref;

  if (*off + sizeof (uint16_t) > dst_len)
    return GNUNET_NO;
  mxpref = htons (mx->preference);
  memcpy (&dst[*off], &mxpref, sizeof (mxpref));
  (*off) += sizeof (mxpref);
  return add_name (dst, dst_len, off, mx->mxhost);
}


/**
 * Add an SOA record to the buffer at the given location.
 *
 * @param dst where to write the SOA record
 * @param dst_len number of bytes in dst
 * @param off pointer to offset where to write the SOA information (increment by bytes used)
 *            can also change if there was an error
 * @param soa SOA information to write
 * @return GNUNET_SYSERR if 'soa' is invalid
 *         GNUNET_NO if 'soa' did not fit
 *         GNUNET_OK if 'soa' was added to 'dst'
 */
static int
add_soa (char *dst,
	 size_t dst_len,
	 size_t *off,
	 const struct GNUNET_DNSPARSER_SoaRecord *soa)
{
  struct soa_data sd;
  int ret;

  if ( (GNUNET_OK != (ret = add_name (dst,
				      dst_len,
				      off,
				      soa->mname))) ||
       (GNUNET_OK != (ret = add_name (dst,
				      dst_len,
				      off,
				      soa->rname)) ) )
    return ret;
  if (*off + sizeof (struct soa_data) > dst_len)
    return GNUNET_NO;
  sd.serial = htonl (soa->serial);
  sd.refresh = htonl (soa->refresh);
  sd.retry = htonl (soa->retry);
  sd.expire = htonl (soa->expire);
  sd.minimum = htonl (soa->minimum_ttl);
  memcpy (&dst[*off], &sd, sizeof (sd));
  (*off) += sizeof (sd);
  return GNUNET_OK;
}

/**
 * Add a DNS record to the buffer at the given location.
 *
 * @param dst where to write the record
 * @param dst_len number of bytes in dst
 * @param off pointer to offset where to write the query (increment by bytes used)
 *            must not be changed if there is an error
 * @param record record to write
 * @return GNUNET_SYSERR if 'record' is invalid
 *         GNUNET_NO if 'record' did not fit
 *         GNUNET_OK if 'record' was added to 'dst'
 */
static int
parse_record (char *dst,
	    size_t dst_len,
	    size_t *off,
	    const struct GNUNET_GNS_Record *record)
{
  int ret;
  size_t start;
  size_t pos;
  struct record_line rl;

  start = *off;
  ret = add_name (dst, dst_len - sizeof (struct record_line), off, record->name);
  if (ret != GNUNET_OK)
    return ret;
  /* '*off' is now the position where we will need to write the record line */

  pos = *off + sizeof (struct record_line);
  switch (record->type)
  { 
  case GNUNET_DNSPARSER_TYPE_MX:
    ret = add_mx (dst, dst_len, &pos, record->data.mx);    
    break;
  case GNUNET_DNSPARSER_TYPE_SOA:
    ret = add_soa (dst, dst_len, &pos, record->data.soa);
    break;
  case GNUNET_DNSPARSER_TYPE_NS:
  case GNUNET_DNSPARSER_TYPE_CNAME:
  case GNUNET_DNSPARSER_TYPE_PTR:
    ret = add_name (dst, dst_len, &pos, record->data.hostname);
    break;
  default:
    if (pos + record->data.raw.data_len > dst_len)
    {
      ret = GNUNET_NO;
      break;
    }
    memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
    pos += record->data.raw.data_len;
    ret = GNUNET_OK;
    break;
  }
  if (ret != GNUNET_OK)
  {
    *off = start;
    return GNUNET_NO;
  }

  if (pos - (*off + sizeof (struct record_line)) > UINT16_MAX)
  {
    /* record data too long */
    *off = start;
    return GNUNET_NO;
  }
  rl.type = htons (record->type);
  rl.class = htons (record->class);
  rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value / 1000); /* in seconds */
  rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct record_line))));
  memcpy (&dst[*off], &rl, sizeof (struct record_line));
  *off = pos;
  return GNUNET_OK;  
}