File: domainfn.cpp

package info (click to toggle)
dibbler 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,352 kB
  • sloc: cpp: 60,323; ansic: 12,235; sh: 11,951; yacc: 3,418; lex: 969; makefile: 940; perl: 319; xml: 116; python: 74
file content (471 lines) | stat: -rw-r--r-- 11,977 bytes parent folder | download | duplicates (3)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
    Posadis - A DNS Server
    Domain functions
    Copyright (C) 2002  Meilof Veeningen <meilof@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "dnsmessage.h"
#include "domainfn.h"
#include "lexfn.h"
#include "exception.h"
#include "sysstl.h"
#include "sysstring.h"

void *memdup(const void *src, int len) {
  if (len == 0) return NULL;
  void *ret = malloc(len);
  memcpy(ret, src, len);
  return ret;
}

#define DOM_RECLEVEL 10
int dom_comprlen(message_buff &buff, int ix) {
  int len = 0;
  unsigned char x;
  unsigned char *ptr = buff.msg + ix,
                *end = buff.msg + buff.len;
  
  while (true) {
    if (ptr >= end) throw PException("Domain name exceeds message borders");
    
    if (*ptr == 0)
      /* we're at the end! */
      return len + 1;
    
    if ((*ptr & 192) == 192) {
      if (ptr + 1 >= end) throw PException("Compression offset exceeds message borders");
      return len + 2;
    }
    x = *ptr & 192;
    if (x != 0) throw PException("Unknown domain label type");
    len += *ptr + 1;
    ptr += *ptr + 1;
    if (len >= 255) throw PException("Domain name too long");
  }
}

_domain dom_uncompress(message_buff &buff, int ix) {
  int reclevel = 0, len = 0, val;
  unsigned char *ptr = buff.msg + ix, *end = buff.msg + buff.len;
  unsigned char dbuff[255];

  while (true) {
    if (ptr >= end) throw PException("Domain name exceeds message borders");

    if (*ptr == 0) {
      /* we're at the end! */
      dbuff[len] = '\0';
      return domdup(dbuff);
    }

    if ((*ptr & 192) == 192) {
      if (++reclevel >= DOM_RECLEVEL) throw PException("Max dom recursion level reached");
      if (ptr + 1 >= end) throw PException("Compression offset exceeds message borders");
      val = (ptr[0] & 63) * 256 + ptr[1];
      if (val >= (ptr - buff.msg)) throw PException("Bad compression offset");
      ptr = buff.msg + val;
      continue;
    }

    if ((*ptr & 192) != 0) throw PException("Unknown domain label type");
    if (len + *ptr + 1 >= 255) throw PException("Domain name too long");
    if (ptr + *ptr + 1 >= end) throw PException("Domain name exceeds message borders");
    memcpy(dbuff + len, ptr, *ptr + 1);
    len += *ptr + 1;
    ptr += *ptr + 1;
  }

//  return domdup(dbuff);
}

dom_compr_info::dom_compr_info(_cdomain _dom, int _ix, int _nl, int _nul) {
  dom = _dom;
  ix = _ix;
  nl = _nl;
  nul = _nul;
}

int dom_partiallength(_cdomain _dom, int n) {
  _domain dom = (_domain) _dom;
  int len = 0;
  while (--n >= 0) {
    len += *dom + 1;
    dom += *dom + 1;
  }
  return len;
}

void dom_write(stl_string &ret, _cdomain dom, stl_slist(dom_compr_info) *comprinfo) {
  if (!comprinfo) {
    ret.append((char *)dom, domlen(dom));
    return;
  }
  
  stl_slist(dom_compr_info)::iterator it = comprinfo->begin(), best = comprinfo->end();
  int nlabels = dom_nlabels(dom) - 1, x;
  int ix = ret.size();

  while (it != comprinfo->end()) {
    if (nlabels >= it->nl && (best == comprinfo->end() || best->nul < it->nul)) {
      if (domcmp(domfrom(dom, nlabels - it->nl), it->dom)) {
        /* the same! */
        best = it;
        if (nlabels == best->nl) break; /* perfect match */
      }
    }
    ++it;
  }

  /* let's go! */
  if (best == comprinfo->end()) {
    ret.append((char *)dom, domlen(dom));
    x = nlabels; /* number of stored labels */
  } else {
    unsigned char val;
    ret.append((char *)dom, dom_partiallength(dom, nlabels - best->nl));
    val = (best->ix / 256) | 192; ret.append((char *)&val, 1);
    val = best->ix; ret.append((char*)&val, 1);
    x = nlabels - best->nl; /* number of stored labels */
  }

  /* add our information to the list */
  _cdomain pdom = dom;
  for (int z = 0; z < x; z++) {
    if (ix + (pdom - dom) >= 16384) break;
    comprinfo->push_front(dom_compr_info(pdom, ix + (pdom - dom), nlabels - z, x - z));
    pdom += *pdom + 1;
  }
}

_domain domfrom(_cdomain dom, int ix) {
  while (ix > 0) {
    if (*dom == 0) throw PException("Domain label index out of bounds");
    dom += *dom + 1;
    ix--;
  }
  return (_domain) dom;
}

bool domisparent(_cdomain parent, _cdomain child) {
  int x = domlen(parent), y = domlen(child);
  if (x > y) return false;
  return domcmp(parent, child + y - x);
}

int domlen(_cdomain dom) {
  int len = 1;
  while (*dom) {
    if (*dom > 63) throw PException(true, "Unknown domain nibble %d", *dom);
    len += *dom + 1;
    dom += *dom + 1;
    if (len > 255) throw PException("Length too long");
  }
  return len;
}

_domain domdup(_cdomain dom) {
  return (_domain) memdup(dom, domlen(dom));
}

bool domlcmp(_cdomain dom1, _cdomain dom2) {
  _domain a = (_domain) dom1;
  _domain b = (_domain) dom2;
  if (*a != *b) return false;
  for (int t = 1; t <= *a; t++)
    if (tolower(a[t]) != tolower(b[t])) return false;
  return true;
}

bool domcmp(_cdomain _dom1, _cdomain _dom2) {
  _domain dom1 = (_domain)_dom1, dom2 = (_domain)_dom2;
  if (*dom1 != *dom2) return false;

  int x = domlen(dom1), y = domlen(dom2);
  if (x != y) return false;

  while (*dom1) {
    if (*dom1 != *dom2) return false;
    for (int t = 1; t <= *dom1; t++)
      if (tolower(dom1[t]) != tolower(dom2[t])) return false;
    dom1 += *dom1 + 1;
    dom2 += *dom2 + 1;
  }

  return true;
}

domainname::domainname() {
  domain = (unsigned char *)strdup("");
}

domainname::domainname(const char *string, const domainname& origin) {
  unsigned char tmp[DOM_LEN];

  txt_to_email(tmp, string, origin.domain);
  domain = domdup(tmp);
}

domainname::domainname(const char *string, _cdomain origin) {
  unsigned char tmp[DOM_LEN];

  txt_to_email(tmp, string, origin);
  domain = domdup(tmp);
}

domainname::domainname(message_buff &buff, int ix) {
  domain = dom_uncompress(buff, ix);
}


domainname::domainname(bool val, const unsigned char* dom) {
  domain = domdup(dom);
}

domainname::domainname(const domainname& nam) {
  domain = domdup(nam.domain);
}

domainname::~domainname() {
  free(domain);
}

domainname& domainname::operator=(const domainname& nam) {
  if (this != &nam) {
    if (domain) free(domain);
    domain = domdup(nam.domain);
  }
  return *this;
}

// cppcheck-suppress operatorEqToSelf
domainname& domainname::operator=(const char *buff) {
  unsigned char tmp[DOM_LEN];

  if (domain) {
    free(domain);
    domain = NULL;
  }
  
  txt_to_dname(tmp, buff, (unsigned char *)"");
  domain = domdup(tmp);
  return *this;
}


bool domainname::operator==(const domainname& nam) const {
  return domcmp(domain, nam.domain);
}

bool domainname::operator!=(const domainname& nam) const {
  return !domcmp(domain, nam.domain);
}

domainname& domainname::operator+=(const domainname& nam) {
  int lenres = domlen(domain),
      lensrc = domlen(nam.domain);

  if (lenres + lensrc - 1 > DOM_LEN) throw PException("Domain name too long");
  unsigned char * tmp = (unsigned char *)realloc(domain, lenres + lensrc - 1);
  if (tmp != NULL) {
      domain = tmp;
  }
  memcpy(domain + lenres - 1, nam.domain, lensrc);
  return *this;
}

domainname& domainname::operator+(const domainname& nam) {
  domainname *ret = new domainname(*this);
  ret->operator+=(nam);
  return *ret;
}

bool domainname::operator>=(const domainname& dom) const {
  return domisparent(dom.domain, domain);
}

bool domainname::operator>(const domainname& dom) const {
  return !domcmp(dom.domain, domain) && domisparent(dom.domain, domain);
}

_domain domainname::c_str() const {
  if (domain == NULL) throw PException("Domain name is empty");
  return domain;
}

int domainname::len() const {
  return domlen(domain);
}

stl_string domainname::tostring() const {
  return dom_tostring(domain);
}

int domainname::nlabels() const {
  return dom_nlabels(domain);
}

stl_string domainname::label(int ix) const {
  return dom_label(domain, ix);
}

domainname domainname::from(int ix) const {
  stl_string ret;
  unsigned char *dom = domain;
  while (ix > 0) {
    if (*dom == 0) throw PException("Domain label index out of bounds");
    dom += *dom + 1;
    ix--;
  }
  return domainname(true, dom);
}

domainname domainname::to(int labels) const {
  unsigned char ptr[DOM_LEN];
  domto(ptr, domain, labels);
  return domainname(true, ptr);
}

stl_string domainname::torelstring(const domainname &root) const {
  if (*this == root) {
    return "@";
  } else if (*this >= root) {
    stl_string str = to(nlabels() - root.nlabels()).tostring();
    str.resize(str.size() - 1);
    return str;
  } else return tostring();
}

stl_string domainname::canonical () const {
  unsigned char val[DOM_LEN];
  int len = domlen (domain);
  memcpy (val, domain, len);
  unsigned char *lenlabel = val;
  for (int t = 0; t < len; t++) {
    if (val + t == lenlabel)
      lenlabel += *lenlabel + 1;
    else
      val[t] = tolower (val[t]);
  }
  return std::string ((char*)val, len);
}


int domainname::ncommon(const domainname &dom) const {
  return domncommon(domain, dom.domain);
}

void domcat(_domain res, _cdomain src) {
  int lenres = domlen(res),
      lensrc = domlen(src);
  if (lenres + lensrc - 1 > DOM_LEN) throw PException("Domain name too long");
  memcpy(res + lenres - 1, src, lensrc);
}

void domcpy(_domain res, _cdomain src) {
  memcpy(res, src, domlen(src));
}
 
void domfromlabel(_domain dom, const char *label, int len) {
  if (len == -1) len = strlen(label);
  if (len > DOMLABEL_LEN) throw PException(true, "Domain name label %s too long", label);
  dom[0] = len;
  memcpy(dom + 1, label, len);
  dom[len + 1] = '\0';
}

int dom_nlabels(_cdomain dom) {
  int n_labels = 1;
  while (*dom) {
    dom += *dom + 1;
    n_labels++;
  }
  return n_labels;
}

stl_string dom_label(_cdomain dom, int label) {
  stl_string ret;
  while (label > 0) {
    if (*dom == 0) return "";
    dom += *dom + 1;
    label--;
  }

  ret.append((char*)dom + 1, (int)*dom);
  return ret;
}

_domain dom_plabel(_cdomain dom, int label) {
  unsigned char *ret = (unsigned char *)dom;
  if (label < 0) throw PException("Negative label accessed");
  while (label--) {
    if (*ret == 0) throw PException("Label not in domain name");
    ret += *ret + 1;
  }
  return (_domain) ret;
}

stl_string dom_tostring(_cdomain dom) {
  if (*dom == '\0') return ".";

  stl_string x;

  while (*dom != '\0') {
    x.append((char*)dom + 1, (int)*dom);
    x.append(".");

    dom += *dom + 1;
  }

  return x;
}

int domncommon(_cdomain _dom1, _cdomain _dom2) {
  _domain dom1 = (_domain)_dom1,
          dom2 = (_domain)_dom2;

  int a = dom_nlabels(dom1), b = dom_nlabels(dom2);
  if (a > b)
    dom1 = dom_plabel(dom1, a - b);
  else
    dom2 = dom_plabel(dom2, b - a);

  int x = 0;
  while (*dom1) {
    if (domlcmp(dom1, dom2)) x++; else x = 0;
    dom1 += *dom1 + 1;
    dom2 += *dom2 + 1;
  }
  return x;
}

int domccmp(_cdomain _dom1, _cdomain _dom2) {
  _domain dom1 = (_domain)_dom1,
          dom2 = (_domain)_dom2;

  int x = domncommon(dom1, dom2), y = dom_nlabels(dom1), z = dom_nlabels(dom2);
  if (x == y - 1) { if (x == z - 1) return 0; else return -1; }
  else if (x == z - 1) { return 1; } else {
    /* check the last label */
    return strcmpi(dom_label(dom1, y - x - 2).c_str(), dom_label(dom2, z - x - 2).c_str());
  }
}

void domto(_domain ret, _cdomain src, int labels) {
  _domain ptr = dom_plabel(src, labels);
  memcpy(ret, src, ptr - src);
  ret[ptr - src] = '\0';
}