wks_11.c 8.9 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
5
6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8
9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
Mark Andrews's avatar
Mark Andrews committed
10
11
 */

12
13
#ifndef RDATA_IN_1_WKS_11_C
#define RDATA_IN_1_WKS_11_C
Mark Andrews's avatar
Mark Andrews committed
14
15
16
17

#include <limits.h>
#include <stdlib.h>

18
19
#include <isc/net.h>
#include <isc/netdb.h>
20
#include <isc/once.h>
Mark Andrews's avatar
Mark Andrews committed
21

22
23
24
25
26
27
28
29
30
31
/*
 * Redefine CHECK here so cppcheck "sees" the define.
 */
#ifndef CHECK
#define CHECK(op)						\
	do { result = (op);					\
		if (result != ISC_R_SUCCESS) goto cleanup;	\
	} while (0)
#endif

32
33
#define RRTYPE_WKS_ATTRIBUTES (0)

34
35
36
static isc_mutex_t wks_lock;

static void init_lock(void) {
Automatic Updater's avatar
Automatic Updater committed
37
	RUNTIME_CHECK(isc_mutex_init(&wks_lock) == ISC_R_SUCCESS);
38
39
}

40
static bool
41
42
43
44
45
46
47
48
mygetprotobyname(const char *name, long *proto) {
	struct protoent *pe;

	LOCK(&wks_lock);
	pe = getprotobyname(name);
	if (pe != NULL)
		*proto = pe->p_proto;
	UNLOCK(&wks_lock);
49
	return (pe != NULL);
50
51
}

52
static bool
53
54
55
56
57
58
59
60
mygetservbyname(const char *name, const char *proto, long *port) {
	struct servent *se;

	LOCK(&wks_lock);
	se = getservbyname(name, proto);
	if (se != NULL)
		*port = ntohs(se->s_port);
	UNLOCK(&wks_lock);
61
	return (se != NULL);
62
63
}

64
65
66
67
68
69
#ifdef _WIN32
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

70
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
71
fromtext_in_wks(ARGS_FROMTEXT) {
72
	static isc_once_t once = ISC_ONCE_INIT;
Mark Andrews's avatar
Mark Andrews committed
73
74
75
76
77
	isc_token_t token;
	isc_region_t region;
	struct in_addr addr;
	char *e;
	long proto;
78
	unsigned char bm[8*1024]; /* 64k bits */
Mark Andrews's avatar
Mark Andrews committed
79
80
	long port;
	long maxport = -1;
David Lawrence's avatar
David Lawrence committed
81
	const char *ps = NULL;
Mark Andrews's avatar
Mark Andrews committed
82
	unsigned int n;
83
84
	char service[32];
	int i;
85
	isc_result_t result;
Mark Andrews's avatar
Mark Andrews committed
86

87
88
	REQUIRE(type == dns_rdatatype_wks);
	REQUIRE(rdclass == dns_rdataclass_in);
89

90
	UNUSED(type);
91
	UNUSED(origin);
92
	UNUSED(options);
93
	UNUSED(rdclass);
94
	UNUSED(callbacks);
95

96
97
	RUNTIME_CHECK(isc_once_do(&once, init_lock) == ISC_R_SUCCESS);

98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifdef _WIN32
	{
		WORD wVersionRequested;
		WSADATA wsaData;
		int err;

		wVersionRequested = MAKEWORD(2, 0);

		err = WSAStartup(wVersionRequested, &wsaData );
		if (err != 0)
			return (ISC_R_FAILURE);
	}
#endif

David Lawrence's avatar
David Lawrence committed
112
113
114
	/*
	 * IPv4 dotted quad.
	 */
115
	CHECK(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
116
				      false));
Mark Andrews's avatar
Mark Andrews committed
117

118
	isc_buffer_availableregion(target, &region);
119
	if (inet_pton(AF_INET, DNS_AS_STR(token), &addr) != 1)
120
		CHECKTOK(DNS_R_BADDOTTEDQUAD);
Mark Andrews's avatar
Mark Andrews committed
121
	if (region.length < 4)
122
		return (ISC_R_NOSPACE);
123
	memmove(region.base, &addr, 4);
Mark Andrews's avatar
Mark Andrews committed
124
125
	isc_buffer_add(target, 4);

David Lawrence's avatar
David Lawrence committed
126
127
128
	/*
	 * Protocol.
	 */
129
	CHECK(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
130
				      false));
Mark Andrews's avatar
Mark Andrews committed
131

132
	proto = strtol(DNS_AS_STR(token), &e, 10);
133
	if (*e == 0)
134
		;
135
	else if (!mygetprotobyname(DNS_AS_STR(token), &proto))
136
		CHECKTOK(DNS_R_UNKNOWNPROTO);
137

138
	if (proto < 0 || proto > 0xff)
139
		CHECKTOK(ISC_R_RANGE);
Mark Andrews's avatar
Mark Andrews committed
140
141
142
143
144
145

	if (proto == IPPROTO_TCP)
		ps = "tcp";
	else if (proto == IPPROTO_UDP)
		ps = "udp";

146
	CHECK(uint8_tobuffer(proto, target));
Mark Andrews's avatar
Mark Andrews committed
147

Andreas Gustafsson's avatar
Andreas Gustafsson committed
148
	memset(bm, 0, sizeof(bm));
149
	do {
150
		CHECK(isc_lex_getmastertoken(lexer, &token,
151
					      isc_tokentype_string, true));
152
		if (token.type != isc_tokentype_string)
Mark Andrews's avatar
Mark Andrews committed
153
			break;
154
155
156
157
158

		/*
		 * Lowercase the service string as some getservbyname() are
		 * case sensitive and the database is usually in lowercase.
		 */
159
		strlcpy(service, DNS_AS_STR(token), sizeof(service));
160
161
		for (i = strlen(service) - 1; i >= 0; i--)
			if (isupper(service[i]&0xff))
Mark Andrews's avatar
Mark Andrews committed
162
				service[i] = tolower(service[i]&0xff);
163

164
		port = strtol(DNS_AS_STR(token), &e, 10);
165
		if (*e == 0)
166
			;
167
168
		else if (!mygetservbyname(service, ps, &port) &&
			 !mygetservbyname(DNS_AS_STR(token), ps, &port))
169
			CHECKTOK(DNS_R_UNKNOWNSERVICE);
Mark Andrews's avatar
Mark Andrews committed
170
		if (port < 0 || port > 0xffff)
171
			CHECKTOK(ISC_R_RANGE);
Mark Andrews's avatar
Mark Andrews committed
172
173
		if (port > maxport)
			maxport = port;
174
		bm[port / 8] |= (0x80 >> (port % 8));
175
	} while (1);
176

David Lawrence's avatar
David Lawrence committed
177
178
179
	/*
	 * Let upper layer handle eol/eof.
	 */
Mark Andrews's avatar
Mark Andrews committed
180
	isc_lex_ungettoken(lexer, &token);
181

Mark Andrews's avatar
Mark Andrews committed
182
	n = (maxport + 8) / 8;
183
184
185
186
187
188
189
190
	result = mem_tobuffer(target, bm, n);

 cleanup:
#ifdef _WIN32
	WSACleanup();
#endif

	return (result);
Mark Andrews's avatar
Mark Andrews committed
191
192
}

193
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
194
totext_in_wks(ARGS_TOTEXT) {
Mark Andrews's avatar
Mark Andrews committed
195
196
	isc_region_t sr;
	unsigned short proto;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
197
	char buf[sizeof("65535")];
Mark Andrews's avatar
Mark Andrews committed
198
199
	unsigned int i, j;

200
201
	UNUSED(tctx);

202
203
	REQUIRE(rdata->type == dns_rdatatype_wks);
	REQUIRE(rdata->rdclass == dns_rdataclass_in);
204
	REQUIRE(rdata->length >= 5);
Mark Andrews's avatar
Mark Andrews committed
205
206

	dns_rdata_toregion(rdata, &sr);
207
	RETERR(inet_totext(AF_INET, &sr, target));
Mark Andrews's avatar
Mark Andrews committed
208
209
	isc_region_consume(&sr, 4);

210
	proto = uint8_fromregion(&sr);
211
	snprintf(buf, sizeof(buf), "%u", proto);
212
213
	RETERR(str_totext(" ", target));
	RETERR(str_totext(buf, target));
214
	isc_region_consume(&sr, 1);
215

Mark Andrews's avatar
Mark Andrews committed
216
	INSIST(sr.length <= 8*1024);
217
	for (i = 0; i < sr.length; i++) {
Mark Andrews's avatar
Mark Andrews committed
218
		if (sr.base[i] != 0)
219
			for (j = 0; j < 8; j++)
220
				if ((sr.base[i] & (0x80 >> j)) != 0) {
221
222
					snprintf(buf, sizeof(buf),
						 "%u", i * 8 + j);
223
224
					RETERR(str_totext(" ", target));
					RETERR(str_totext(buf, target));
Mark Andrews's avatar
Mark Andrews committed
225
226
				}
	}
227

228
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
229
230
}

231
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
232
fromwire_in_wks(ARGS_FROMWIRE) {
Mark Andrews's avatar
Mark Andrews committed
233
234
235
	isc_region_t sr;
	isc_region_t tr;

236
237
	REQUIRE(type == dns_rdatatype_wks);
	REQUIRE(rdclass == dns_rdataclass_in);
Mark Andrews's avatar
Mark Andrews committed
238

239
	UNUSED(type);
240
	UNUSED(dctx);
241
	UNUSED(options);
242
243
	UNUSED(rdclass);

244
245
	isc_buffer_activeregion(source, &sr);
	isc_buffer_availableregion(target, &tr);
Mark Andrews's avatar
Mark Andrews committed
246

247
	if (sr.length < 5)
248
		return (ISC_R_UNEXPECTEDEND);
249
	if (sr.length > 8 * 1024 + 5)
Mark Andrews's avatar
Mark Andrews committed
250
251
		return (DNS_R_EXTRADATA);
	if (tr.length < sr.length)
252
		return (ISC_R_NOSPACE);
Mark Andrews's avatar
Mark Andrews committed
253

254
	memmove(tr.base, sr.base, sr.length);
Mark Andrews's avatar
Mark Andrews committed
255
256
	isc_buffer_add(target, sr.length);
	isc_buffer_forward(source, sr.length);
257

258
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
259
260
}

261
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
262
towire_in_wks(ARGS_TOWIRE) {
Mark Andrews's avatar
Mark Andrews committed
263
264
	isc_region_t sr;

265
266
	UNUSED(cctx);

267
268
	REQUIRE(rdata->type == dns_rdatatype_wks);
	REQUIRE(rdata->rdclass == dns_rdataclass_in);
269
	REQUIRE(rdata->length != 0);
Mark Andrews's avatar
Mark Andrews committed
270
271
272
273
274

	dns_rdata_toregion(rdata, &sr);
	return (mem_tobuffer(target, sr.base, sr.length));
}

275
static inline int
David Lawrence's avatar
David Lawrence committed
276
compare_in_wks(ARGS_COMPARE) {
Mark Andrews's avatar
Mark Andrews committed
277
278
279
280
	isc_region_t r1;
	isc_region_t r2;

	REQUIRE(rdata1->type == rdata2->type);
281
	REQUIRE(rdata1->rdclass == rdata2->rdclass);
282
283
	REQUIRE(rdata1->type == dns_rdatatype_wks);
	REQUIRE(rdata1->rdclass == dns_rdataclass_in);
284
285
	REQUIRE(rdata1->length != 0);
	REQUIRE(rdata2->length != 0);
Mark Andrews's avatar
Mark Andrews committed
286
287
288

	dns_rdata_toregion(rdata1, &r1);
	dns_rdata_toregion(rdata2, &r2);
289
	return (isc_region_compare(&r1, &r2));
Mark Andrews's avatar
Mark Andrews committed
290
291
}

292
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
293
fromstruct_in_wks(ARGS_FROMSTRUCT) {
294
	dns_rdata_in_wks_t *wks = source;
295
	uint32_t a;
Mark Andrews's avatar
Mark Andrews committed
296

297
298
	REQUIRE(type == dns_rdatatype_wks);
	REQUIRE(rdclass == dns_rdataclass_in);
299
300
301
	REQUIRE(source != NULL);
	REQUIRE(wks->common.rdtype == type);
	REQUIRE(wks->common.rdclass == rdclass);
Mark Andrews's avatar
Mark Andrews committed
302
303
	REQUIRE((wks->map != NULL && wks->map_len <= 8*1024) ||
		 wks->map_len == 0);
304

305
	UNUSED(type);
306
307
	UNUSED(rdclass);

308
309
	a = ntohl(wks->in_addr.s_addr);
	RETERR(uint32_tobuffer(a, target));
310
	RETERR(uint8_tobuffer(wks->protocol, target));
311
	return (mem_tobuffer(target, wks->map, wks->map_len));
Mark Andrews's avatar
Mark Andrews committed
312
313
}

314
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
315
tostruct_in_wks(ARGS_TOSTRUCT) {
316
	dns_rdata_in_wks_t *wks = target;
317
	uint32_t n;
318
	isc_region_t region;
Mark Andrews's avatar
Mark Andrews committed
319

320
321
	REQUIRE(rdata->type == dns_rdatatype_wks);
	REQUIRE(rdata->rdclass == dns_rdataclass_in);
322
	REQUIRE(rdata->length != 0);
Mark Andrews's avatar
Mark Andrews committed
323

324
325
326
327
328
329
330
331
	wks->common.rdclass = rdata->rdclass;
	wks->common.rdtype = rdata->type;
	ISC_LINK_INIT(&wks->common, link);

	dns_rdata_toregion(rdata, &region);
	n = uint32_fromregion(&region);
	wks->in_addr.s_addr = htonl(n);
	isc_region_consume(&region, 4);
332
333
	wks->protocol = uint8_fromregion(&region);
	isc_region_consume(&region, 1);
334
	wks->map_len = region.length;
335
336
337
	wks->map = mem_maybedup(mctx, region.base, region.length);
	if (wks->map == NULL)
		return (ISC_R_NOMEMORY);
338
339
	wks->mctx = mctx;
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
340
}
341

342
static inline void
David Lawrence's avatar
David Lawrence committed
343
freestruct_in_wks(ARGS_FREESTRUCT) {
344
345
	dns_rdata_in_wks_t *wks = source;

346
	REQUIRE(source != NULL);
347
348
	REQUIRE(wks->common.rdtype == dns_rdatatype_wks);
	REQUIRE(wks->common.rdclass == dns_rdataclass_in);
349

350
351
352
353
354
355
	if (wks->mctx == NULL)
		return;

	if (wks->map != NULL)
		isc_mem_free(wks->mctx, wks->map);
	wks->mctx = NULL;
356
}
357

358
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
359
additionaldata_in_wks(ARGS_ADDLDATA) {
360
	UNUSED(rdata);
361
362
363
	UNUSED(add);
	UNUSED(arg);

364
365
	REQUIRE(rdata->type == dns_rdatatype_wks);
	REQUIRE(rdata->rdclass == dns_rdataclass_in);
366

367
	return (ISC_R_SUCCESS);
368
369
}

370
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
371
digest_in_wks(ARGS_DIGEST) {
372
373
	isc_region_t r;

374
375
	REQUIRE(rdata->type == dns_rdatatype_wks);
	REQUIRE(rdata->rdclass == dns_rdataclass_in);
376
377
378
379
380
381

	dns_rdata_toregion(rdata, &r);

	return ((digest)(arg, &r));
}

382
static inline bool
383
384
checkowner_in_wks(ARGS_CHECKOWNER) {

385
386
	REQUIRE(type == dns_rdatatype_wks);
	REQUIRE(rdclass == dns_rdataclass_in);
387
388
389
390
391
392
393

	UNUSED(type);
	UNUSED(rdclass);

	return (dns_name_ishostname(name, wildcard));
}

394
static inline bool
395
396
checknames_in_wks(ARGS_CHECKNAMES) {

397
398
	REQUIRE(rdata->type == dns_rdatatype_wks);
	REQUIRE(rdata->rdclass == dns_rdataclass_in);
399
400
401
402
403

	UNUSED(rdata);
	UNUSED(owner);
	UNUSED(bad);

404
	return (true);
405
406
}

407
408
409
410
411
static inline int
casecompare_in_wks(ARGS_COMPARE) {
	return (compare_in_wks(rdata1, rdata2));
}

412
#endif	/* RDATA_IN_1_WKS_11_C */