forked from dterweij/ndjbdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns_dfd.c
120 lines (102 loc) · 3 KB
/
dns_dfd.c
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
/*
* dns_dfd.c: This file is part of the `djbdns' project, originally written
* by Dr. D J Bernstein and later released under public-domain since late
* December 2007 (http://cr.yp.to/distributors.html).
*
* Copyright (C) 2009 - 2012 Prasad J Pandit
*
* This program is a free software; you can redistribute it and/or modify
* it under the terms of GNU General Public License as published by 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
* of 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 Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include "dns.h"
#include "byte.h"
#include "error.h"
#include "alloc.h"
int
dns_domain_fromdot (char **out, const char *buf, unsigned int n)
{
char ch = 0, *x = NULL;
char label[63], name[255];
unsigned int namelen = 0; /* <= sizeof name */
unsigned int labellen = 0; /* <= sizeof label */
errno = error_proto;
for (;;)
{
if (!n)
break;
--n;
ch = *buf++;
if (ch == '.')
{
if (labellen)
{
if (namelen + labellen + 1 > sizeof name)
return 0;
name[namelen++] = labellen;
byte_copy (name + namelen, labellen, label);
namelen += labellen;
labellen = 0;
}
continue;
}
if (ch == '\\')
{
if (!n)
break;
--n;
ch = *buf++;
if ((ch >= '0') && (ch <= '7'))
{
ch -= '0';
if (n && (*buf >= '0') && (*buf <= '7'))
{
ch <<= 3;
ch += (*buf - '0');
--n;
++buf;
if (n && (*buf >= '0') && (*buf <= '7'))
{
ch <<= 3;
ch += (*buf - '0');
--n;
++buf;
}
}
}
}
if (labellen >= sizeof label)
return 0;
label[labellen++] = ch;
}
if (labellen)
{
if (namelen + labellen + 1 > sizeof name)
return 0;
name[namelen++] = labellen;
byte_copy (name + namelen, labellen, label);
namelen += labellen;
labellen = 0;
}
if (namelen + 1 > sizeof name)
return 0;
name[namelen++] = 0;
if (!(x = alloc (namelen)))
return 0;
byte_copy (x, namelen, name);
if (*out)
alloc_free (*out);
*out = x;
return 1;
}