a_1.c 4.85 KB
Newer Older
1
/*
Bob Halley's avatar
Bob Halley committed
2
 * Copyright (C) 1998, 1999, 2000  Internet Software Consortium.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

Brian Wellington's avatar
Brian Wellington committed
18
/* $Id: a_1.c,v 1.25 2000/03/17 01:48:29 bwelling Exp $ */
Brian Wellington's avatar
Brian Wellington committed
19
20

/* Reviewed: Thu Mar 16 16:52:50 PST 2000 by bwelling */
Mark Andrews's avatar
Mark Andrews committed
21

22
23
#ifndef RDATA_IN_1_A_1_C
#define RDATA_IN_1_A_1_C
Mark Andrews's avatar
Mark Andrews committed
24

Michael Graff's avatar
Michael Graff committed
25
26
#include <string.h>

27
#include <isc/net.h>
28

29
static inline isc_result_t
30
fromtext_in_a(dns_rdataclass_t rdclass, dns_rdatatype_t type,
31
	      isc_lex_t *lexer, dns_name_t *origin,
Mark Andrews's avatar
Mark Andrews committed
32
33
	      isc_boolean_t downcase, isc_buffer_t *target)
{
34
35
36
37
	isc_token_t token;
	struct in_addr addr;
	isc_region_t region;

Brian Wellington's avatar
Brian Wellington committed
38
39
40
	UNUSED(origin);
	UNUSED(downcase);

Mark Andrews's avatar
Mark Andrews committed
41
	REQUIRE(type == 1);
42
	REQUIRE(rdclass == 1);
Mark Andrews's avatar
Mark Andrews committed
43

44
	RETERR(gettoken(lexer, &token, isc_tokentype_string, ISC_FALSE));
45

46
	if (inet_aton(token.value.as_pointer, &addr) != 1)
47
		return (DNS_R_BADDOTTEDQUAD);
48
49
50
51
52
53
54
55
	isc_buffer_available(target, &region);
	if (region.length < 4)
		return (DNS_R_NOSPACE);
	memcpy(region.base, &addr, 4);
	isc_buffer_add(target, 4);
	return (DNS_R_SUCCESS);
}

56
static inline isc_result_t
57
58
59
totext_in_a(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx, 
	    isc_buffer_t *target) 
{
60
61
	isc_region_t region;

Brian Wellington's avatar
Brian Wellington committed
62
63
	UNUSED(tctx);

Mark Andrews's avatar
Mark Andrews committed
64
	REQUIRE(rdata->type == 1);
65
	REQUIRE(rdata->rdclass == 1);
Mark Andrews's avatar
Mark Andrews committed
66
	REQUIRE(rdata->length == 4);
67
68

	isc_buffer_available(target, &region);
69
	if (inet_ntop(AF_INET, rdata->data,
70
			  (char *)region.base, region.length) == NULL)
71
72
		return (DNS_R_NOSPACE);

73
	isc_buffer_add(target, strlen((char *)region.base));
74
75
76
	return (DNS_R_SUCCESS);
}

77
static inline isc_result_t
78
fromwire_in_a(dns_rdataclass_t rdclass, dns_rdatatype_t type,
79
	      isc_buffer_t *source, dns_decompress_t *dctx,
Mark Andrews's avatar
Mark Andrews committed
80
81
	      isc_boolean_t downcase, isc_buffer_t *target)
{
82
83
84
	isc_region_t sregion;
	isc_region_t tregion;

Brian Wellington's avatar
Brian Wellington committed
85
86
87
	UNUSED(dctx);
	UNUSED(downcase);

Mark Andrews's avatar
Mark Andrews committed
88
	REQUIRE(type == 1);
89
	REQUIRE(rdclass == 1);
90

91
	isc_buffer_active(source, &sregion);
92
	isc_buffer_available(target, &tregion);
Mark Andrews's avatar
Mark Andrews committed
93
94
	if (sregion.length < 4)
		return (DNS_R_UNEXPECTEDEND);
95
96
97
98
99
100
101
102
103
	if (tregion.length < 4)
		return (DNS_R_NOSPACE);

	memcpy(tregion.base, sregion.base, 4);
	isc_buffer_forward(source, 4);
	isc_buffer_add(target, 4);
	return (DNS_R_SUCCESS);
}

104
static inline isc_result_t
105
106
107
towire_in_a(dns_rdata_t *rdata, dns_compress_t *cctx, isc_buffer_t *target) {
	isc_region_t region;

Brian Wellington's avatar
Brian Wellington committed
108
109
	UNUSED(cctx);

Mark Andrews's avatar
Mark Andrews committed
110
	REQUIRE(rdata->type == 1);
111
	REQUIRE(rdata->rdclass == 1);
Mark Andrews's avatar
Mark Andrews committed
112

113
114
115
116
117
118
119
120
	isc_buffer_available(target, &region);
	if (region.length < rdata->length)
		return (DNS_R_NOSPACE);
	memcpy(region.base, rdata->data, rdata->length);
	isc_buffer_add(target, 4);
	return (DNS_R_SUCCESS);
}

121
static inline int
122
compare_in_a(dns_rdata_t *rdata1, dns_rdata_t *rdata2) {
Brian Wellington's avatar
Brian Wellington committed
123
124
	isc_region_t r1;
	isc_region_t r2;
Brian Wellington's avatar
Brian Wellington committed
125

Mark Andrews's avatar
Mark Andrews committed
126
	REQUIRE(rdata1->type == rdata2->type);
Brian Wellington's avatar
Brian Wellington committed
127
	REQUIRE(rdata1->rdclass == rdata2->rdclass);
Mark Andrews's avatar
Mark Andrews committed
128
	REQUIRE(rdata1->type == 1);
129
	REQUIRE(rdata1->rdclass == 1);
Mark Andrews's avatar
Mark Andrews committed
130

Brian Wellington's avatar
Brian Wellington committed
131
132
133
	dns_rdata_toregion(rdata1, &r1);
	dns_rdata_toregion(rdata2, &r2);
	return (compare_region(&r1, &r2));
134
135
}

136
static inline isc_result_t
137
fromstruct_in_a(dns_rdataclass_t rdclass, dns_rdatatype_t type, void *source,
Mark Andrews's avatar
Mark Andrews committed
138
139
	        isc_buffer_t *target)
{
Brian Wellington's avatar
Brian Wellington committed
140
141
	UNUSED(source);
	UNUSED(target);
Mark Andrews's avatar
Mark Andrews committed
142

Mark Andrews's avatar
Mark Andrews committed
143
	REQUIRE(type == 1);
144
	REQUIRE(rdclass == 1);
Mark Andrews's avatar
Mark Andrews committed
145

146
147
148
	return (DNS_R_NOTIMPLEMENTED);
}

149
static inline isc_result_t
150
tostruct_in_a(dns_rdata_t *rdata, void *target, isc_mem_t *mctx) {
151
152
153
	dns_rdata_in_a_t *a = target;
	isc_uint32_t n;
	isc_region_t region;
Mark Andrews's avatar
Mark Andrews committed
154

Brian Wellington's avatar
Brian Wellington committed
155
156
	UNUSED(mctx);

Mark Andrews's avatar
Mark Andrews committed
157
	REQUIRE(rdata->type == 1);
158
	REQUIRE(rdata->rdclass == 1);
Mark Andrews's avatar
Mark Andrews committed
159

160
161
162
163
164
165
166
167
168
	a->common.rdclass = rdata->rdclass;
	a->common.rdtype = rdata->type;
	ISC_LINK_INIT(&a->common, link);

	dns_rdata_toregion(rdata, &region);
	n = uint32_fromregion(&region);
	a->in_addr.s_addr = htonl(n);

	return (DNS_R_SUCCESS);
169
}
170

171
static inline void
172
173
174
175
freestruct_in_a(void *source) {
	REQUIRE(source != NULL);
	REQUIRE(ISC_FALSE);	/*XXX*/
}
176

177
static inline isc_result_t
178
179
180
additionaldata_in_a(dns_rdata_t *rdata, dns_additionaldatafunc_t add,
		    void *arg)
{
Brian Wellington's avatar
Brian Wellington committed
181
182
	UNUSED(add);
	UNUSED(arg);
183

Brian Wellington's avatar
Brian Wellington committed
184
185
186
	REQUIRE(rdata->type == 1);
	REQUIRE(rdata->rdclass == 1);

187
188
189
	return (DNS_R_SUCCESS);
}

190
static inline isc_result_t
191
192
193
194
195
196
197
198
199
200
201
digest_in_a(dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg) {
	isc_region_t r;

	REQUIRE(rdata->type == 1);
	REQUIRE(rdata->rdclass == 1);

	dns_rdata_toregion(rdata, &r);

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

202
#endif	/* RDATA_IN_1_A_1_C */