-
Notifications
You must be signed in to change notification settings - Fork 26
/
loc.c
201 lines (181 loc) · 6.03 KB
/
loc.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
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
/*
* Part of DNS zone file validator `validns`.
*
* Copyright 2011-2014 Anton Berezin <tobez@tobez.org>
* Modified BSD license.
* (See LICENSE file in the distribution.)
*
*/
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "common.h"
#include "textparse.h"
#include "mempool.h"
#include "carp.h"
#include "rr.h"
static uint8_t double2loc_format(double val)
{
if (val > 1000000000) {
return (((uint8_t)(val / 1000000000)) << 4) | 9;
} else if (val > 100000000) {
return (((uint8_t)(val / 100000000)) << 4) | 8;
} else if (val > 10000000) {
return (((uint8_t)(val / 10000000)) << 4) | 7;
} else if (val > 1000000) {
return (((uint8_t)(val / 1000000)) << 4) | 6;
} else if (val > 100000) {
return (((uint8_t)(val / 100000)) << 4) | 5;
} else if (val > 10000) {
return (((uint8_t)(val / 10000)) << 4) | 4;
} else if (val > 1000) {
return (((uint8_t)(val / 1000)) << 4) | 3;
} else if (val > 100) {
return (((uint8_t)(val / 100)) << 4) | 2;
} else if (val > 10) {
return (((uint8_t)(val / 10)) << 4) | 1;
} else {
return (((uint8_t)(val)) << 4);
}
}
static struct rr *loc_parse(char *name, long ttl, int type, char *s)
{
struct rr_loc *rr = getmem(sizeof(*rr));
long long i;
int deg;
int min;
double sec, val;
rr->version = 0;
/* latitude block */
i = extract_integer(&s, "degrees latitude", NULL);
if (i < 0)
return NULL;
if (i > 90)
return bitch("degrees latitude not in the range 0..90");
deg = i;
min = 0;
sec = 0;
if (isdigit(*s)) {
i = extract_integer(&s, "minutes latitude", NULL);
if (i < 0)
return NULL;
if (i > 59)
return bitch("minutes latitude not in the range 0..59");
min = i;
if (isdigit(*s)) { /* restricted floating point, starting with a digit */
if (extract_double(&s, "seconds latitude", &sec, 0) < 0)
return NULL;
if (sec < 0 || sec > 59.999)
return bitch("seconds latitude not in the range 0..59.999");
}
}
rr->latitude = sec*1000 + .5 + min*1000*60 + deg*1000*60*60;
if (*s == 'n' || *s == 'N') {
s++;
rr->latitude = 2147483648u + rr->latitude;
} else if (*s == 's' || *s == 'S') {
s++;
rr->latitude = 2147483648u - rr->latitude;
} else {
return bitch("latitude: N or S is expected");
}
if (*s && !isspace(*s) && *s != ';' && *s != ')') {
return bitch("latitude: N or S is expected");
}
s = skip_white_space(s);
if (!s) return NULL;
/* longitude block */
i = extract_integer(&s, "degrees longitude", NULL);
if (i < 0)
return NULL;
if (i > 180)
return bitch("degrees longitude not in the range 0..90");
deg = i;
min = 0;
sec = 0;
if (isdigit(*s)) {
i = extract_integer(&s, "minutes longitude", NULL);
if (i < 0)
return NULL;
if (i > 59)
return bitch("minutes longitude not in the range 0..59");
min = i;
if (isdigit(*s)) { /* restricted floating point, starting with a digit */
if (extract_double(&s, "seconds longitude", &sec, 0) < 0)
return NULL;
if (sec < 0 || sec > 59.999)
return bitch("seconds longitude not in the range 0..59.999");
}
}
rr->longitude = sec*1000 + .5 + min*1000*60 + deg*1000*60*60;
if (*s == 'e' || *s == 'E') {
s++;
rr->longitude = 2147483648u + rr->longitude;
} else if (*s == 'w' || *s == 'W') {
s++;
rr->longitude = 2147483648u - rr->longitude;
} else {
return bitch("longitude: E or W is expected");
}
if (*s && !isspace(*s) && *s != ';' && *s != ')') {
return bitch("longitude: E or W is expected");
}
s = skip_white_space(s);
if (!s) return NULL;
if (extract_double(&s, "altitude", &val, 1) < 0)
return NULL;
if (val < -100000.00 || val > 42849672.95)
return bitch("altitude is out of supported range");
rr->altitude = (val + 100000.00) * 100 + 0.5;
if (*s) {
if (extract_double(&s, "sphere size", &val, 1) < 0)
return NULL;
if (val < 0 || val > 90000000.00)
return bitch("sphere size is out of supported range");
rr->size = double2loc_format(val * 100 + 0.5);
if (*s) {
if (extract_double(&s, "horizontal precision", &val, 1) < 0)
return NULL;
if (val < 0 || val > 90000000.00)
return bitch("horizontal precision is out of supported range");
rr->horiz_pre = double2loc_format(val * 100 + 0.5);
if (*s) {
if (extract_double(&s, "vertical precision", &val, 1) < 0)
return NULL;
if (val < 0 || val > 90000000.00)
return bitch("vertical precision is out of supported range");
rr->vert_pre = double2loc_format(val * 100 + 0.5);
} else {
rr->vert_pre = double2loc_format(10 * 100 + 0.5);
}
} else {
rr->horiz_pre = double2loc_format(10000 * 100 + 0.5);
}
} else {
rr->size = double2loc_format(1 * 100 + 0.5);
}
if (*s) {
return bitch("garbage after valid LOC data");
}
return store_record(type, name, ttl, rr);
}
static char* loc_human(struct rr *rrv)
{
// struct rr_loc *rr = (struct rr_loc *)rrv;
// char s[1024];
// snprintf(s, 1024, "\"%s\" \"%s\"", rr->cpu.data, rr->os.data);
// return quickstrdup_temp(s);
return "meow";
}
static struct binary_data loc_wirerdata(struct rr *rrv)
{
RRCAST(loc);
return compose_binary_data("1111444", 1,
rr->version, rr->size,
rr->horiz_pre, rr->vert_pre,
rr->latitude, rr->longitude, rr->altitude);
}
struct rr_methods loc_methods = { loc_parse, loc_human, loc_wirerdata, NULL, NULL };