wks_11.c 8.89 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
94
	UNUSED(rdclass);

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

97
98
99
100
101
102
103
104
105
106
107
108
109
110
#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
111
112
113
	/*
	 * IPv4 dotted quad.
	 */
114
	CHECK(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
115
				      false));
Mark Andrews's avatar
Mark Andrews committed
116

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

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

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

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

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

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

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

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

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

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

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

 cleanup:
#ifdef _WIN32
	WSACleanup();
#endif

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

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

199
200
	UNUSED(tctx);

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

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

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

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

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

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

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

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

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

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

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

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

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

264
265
	UNUSED(cctx);

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

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

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

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

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

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

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

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

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

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

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

323
324
325
326
327
328
329
330
	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);
331
332
	wks->protocol = uint8_fromregion(&region);
	isc_region_consume(&region, 1);
333
	wks->map_len = region.length;
334
335
336
	wks->map = mem_maybedup(mctx, region.base, region.length);
	if (wks->map == NULL)
		return (ISC_R_NOMEMORY);
337
338
	wks->mctx = mctx;
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
339
}
340

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

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

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

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

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

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

366
	return (ISC_R_SUCCESS);
367
368
}

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

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

	dns_rdata_toregion(rdata, &r);

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

381
static inline bool
382
383
checkowner_in_wks(ARGS_CHECKOWNER) {

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

	UNUSED(type);
	UNUSED(rdclass);

	return (dns_name_ishostname(name, wildcard));
}

393
static inline bool
394
395
checknames_in_wks(ARGS_CHECKNAMES) {

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

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

403
	return (true);
404
405
}

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

411
#endif	/* RDATA_IN_1_WKS_11_C */