e576be50 |
1 | /* |
2 | * types.c |
3 | * - RR-type-specific code, and the machinery to call it |
4 | */ |
5 | /* |
6 | * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2, or (at your option) |
11 | * any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with this program; if not, write to the Free Software Foundation, |
20 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 | */ |
98a3f706 |
22 | |
1b644113 |
23 | #include <stdlib.h> |
24 | |
86e7b8d9 |
25 | #include <arpa/inet.h> |
26 | |
98a3f706 |
27 | #include "internal.h" |
28 | |
86e7b8d9 |
29 | static adns_status pa_inaddr(adns_query qu, int serv, |
e7a9ca47 |
30 | const byte *dgram, int dglen, int cbyte, int max, |
e062dcae |
31 | void *datap) { |
32 | struct in_addr *storeto= datap; |
e7a9ca47 |
33 | |
34 | if (max-cbyte != 4) return adns_s_invaliddata; |
e062dcae |
35 | memcpy(storeto,dgram+cbyte,4); |
e7a9ca47 |
36 | return adns_s_ok; |
37 | } |
38 | |
e062dcae |
39 | static int dip_inaddr(struct in_addr a, struct in_addr b) { |
40 | /* fixme implement sortlist */ |
41 | return 0; |
42 | } |
43 | |
44 | static int di_inaddr(const void *datap_a, const void *datap_b) { |
45 | const struct in_addr *ap= datap_a, *bp= datap_b; |
46 | |
47 | return dip_inaddr(*ap,*bp); |
48 | } |
49 | |
50 | static adns_status cs_inaddr(vbuf *vb, const void *datap) { |
51 | const struct in_addr *rrp= datap, rr= *rrp; |
86e7b8d9 |
52 | const char *ia; |
53 | |
e062dcae |
54 | ia= inet_ntoa(rr); assert(ia); |
86e7b8d9 |
55 | return adns__vbuf_appendstr(vb,ia) ? adns_s_ok : adns_s_nolocalmem; |
56 | } |
e7a9ca47 |
57 | |
e062dcae |
58 | static adns_status pap_domain(adns_query qu, int serv, |
59 | const byte *dgram, int dglen, int *cbyte_io, int max, |
60 | char **domain_r) { |
61 | adns_status st; |
62 | char *dm; |
63 | |
64 | st= adns__parse_domain(qu->ads,serv,qu,&qu->vb,qu->flags, |
65 | dgram,dglen, cbyte_io,max); |
66 | if (st) return st; |
67 | if (!qu->vb.used) return adns_s_invaliddata; |
68 | |
69 | dm= adns__alloc_interim(qu,qu->vb.used+1); |
70 | if (!dm) return adns_s_nolocalmem; |
71 | |
72 | dm[qu->vb.used]= 0; |
73 | memcpy(dm,qu->vb.buf,qu->vb.used); |
74 | |
75 | *domain_r= dm; |
76 | return adns_s_ok; |
77 | } |
78 | |
ffbda80c |
79 | static adns_status pa_domain_raw(adns_query qu, int serv, |
80 | const byte *dgram, int dglen, int cbyte, int max, |
e062dcae |
81 | void *datap) { |
82 | char **rrp= datap; |
ffbda80c |
83 | adns_status st; |
86e7b8d9 |
84 | |
e062dcae |
85 | st= pap_domain(qu,serv,dgram,dglen,&cbyte,max,rrp); |
86 | if (st) return st; |
87 | |
88 | if (cbyte != max) return adns_s_invaliddata; |
89 | return adns_s_ok; |
90 | } |
ffbda80c |
91 | |
e062dcae |
92 | static adns_status pa_mx_raw(adns_query qu, int serv, |
93 | const byte *dgram, int dglen, int cbyte, int max, |
94 | void *datap) { |
95 | adns_rr_intstr *rrp= datap; |
96 | adns_status st; |
97 | int pref; |
ffbda80c |
98 | |
e062dcae |
99 | if (cbyte+2 > max) return adns_s_invaliddata; |
100 | GET_W(cbyte,pref); |
101 | rrp->i= pref; |
102 | st= pap_domain(qu,serv,dgram,dglen,&cbyte,max,&rrp->str); |
103 | if (st) return st; |
104 | |
105 | if (cbyte != max) return adns_s_invaliddata; |
106 | return adns_s_ok; |
107 | } |
ffbda80c |
108 | |
e062dcae |
109 | static int di_mx_raw(const void *datap_a, const void *datap_b) { |
110 | const adns_rr_intstr *ap= datap_a, *bp= datap_b; |
ffbda80c |
111 | |
e062dcae |
112 | if (ap->i < bp->i) return 0; |
113 | if (ap->i > bp->i) return 1; |
114 | return 0; |
ffbda80c |
115 | } |
116 | |
e062dcae |
117 | static adns_status pa_txt(adns_query qu, int serv, |
118 | const byte *dgram, int dglen, int startbyte, int max, |
119 | void *datap) { |
120 | adns_rr_intstr **rrp= datap, *table, *te; |
121 | int ti, tc, cbyte, l; |
ffbda80c |
122 | |
e062dcae |
123 | cbyte= startbyte; |
124 | if (cbyte >= max) return adns_s_invaliddata; |
125 | tc= 0; |
126 | while (cbyte < max) { |
127 | GET_B(cbyte,l); |
128 | cbyte+= l; |
129 | } |
130 | if (cbyte != max) return adns_s_invaliddata; |
131 | |
132 | table= adns__alloc_interim(qu,sizeof(*table)*(tc+1)); |
133 | if (!table) return adns_s_nolocalmem; |
134 | |
135 | for (cbyte=startbyte, ti=0, te=table; ti<tc; ti++, te++) { |
136 | GET_B(cbyte,l); |
137 | te->str= adns__alloc_interim(qu,l+1); |
138 | if (!te->str) return adns_s_nolocalmem; |
139 | te->str[l]= 0; |
140 | memcpy(te->str,dgram+cbyte,l); |
141 | te->i= l; |
142 | } |
143 | assert(cbyte == max); |
144 | |
145 | te->i= -1; |
146 | te->str= 0; |
147 | |
148 | *rrp= table; |
149 | return adns_s_ok; |
ffbda80c |
150 | } |
151 | |
e062dcae |
152 | static int csp_textdata(vbuf *vb, const char *dp, int len) { |
1b644113 |
153 | unsigned char ch; |
154 | char buf[10]; |
e062dcae |
155 | int cn; |
1b644113 |
156 | |
157 | if (!adns__vbuf_append(vb,"\"",1)) return 0; |
158 | |
e062dcae |
159 | for (cn=0; cn<len; cn++) { |
160 | ch= *dp++; |
1b644113 |
161 | if (ch >= 32 && ch <= 126 && ch != '"' && ch != '\\') { |
162 | if (!adns__vbuf_append(vb,&ch,1)) return 0; |
163 | } else { |
164 | sprintf(buf,"\\%02x",ch); |
165 | if (!adns__vbuf_appendstr(vb,buf)) return 0; |
166 | } |
167 | } |
ffbda80c |
168 | |
1b644113 |
169 | if (!adns__vbuf_append(vb,"\"",1)) return 0; |
170 | return 1; |
171 | } |
172 | |
e062dcae |
173 | static int csp_qstring(vbuf *vb, const char *dp) { |
174 | return csp_textdata(vb, dp, strlen(dp)); |
175 | } |
176 | |
177 | static adns_status cs_str(vbuf *vb, const void *datap) { |
178 | const char *const *rrp= datap; |
179 | |
180 | return csp_qstring(vb,*rrp) ? adns_s_ok : adns_s_nolocalmem; |
181 | } |
182 | |
183 | static adns_status cs_intstr(vbuf *vb, const void *datap) { |
184 | const adns_rr_intstr *rrp= datap; |
185 | char buf[10]; |
186 | |
187 | sprintf(buf,"%u ",rrp->i); |
188 | return (adns__vbuf_appendstr(vb,buf) && |
189 | csp_qstring(vb,rrp->str)) ? adns_s_ok : adns_s_nolocalmem; |
190 | } |
191 | |
192 | static adns_status cs_manyistr(vbuf *vb, const void *datap) { |
193 | const adns_rr_intstr *const *rrp= datap; |
194 | const adns_rr_intstr *current; |
195 | int spc; |
196 | |
197 | for (spc=0, current= *rrp; current->i >= 0; current++) { |
198 | if (spc) |
199 | if (!adns__vbuf_append(vb," ",1)) goto x_nomem; |
200 | if (!csp_textdata(vb,current->str,current->i)) goto x_nomem; |
201 | } |
202 | return adns_s_ok; |
1b644113 |
203 | |
e062dcae |
204 | x_nomem: |
205 | return adns_s_nolocalmem; |
206 | } |
207 | |
208 | static void mf_str(adns_query qu, void *datap) { |
209 | char **rrp= datap; |
210 | |
211 | adns__makefinal_str(qu,rrp); |
212 | } |
213 | |
214 | static void mf_intstr(adns_query qu, void *datap) { |
215 | adns_rr_intstr *rrp= datap; |
216 | |
217 | adns__makefinal_str(qu,&rrp->str); |
218 | } |
219 | |
220 | static void mf_manyistr(adns_query qu, void *datap) { |
221 | adns_rr_intstr **rrp= datap; |
222 | adns_rr_intstr *te, *table; |
223 | void *tablev; |
224 | int tc; |
225 | |
226 | for (tc=0, te= *rrp; te->i >= 0; te++, tc++); |
227 | tablev= *rrp; |
228 | adns__makefinal_block(qu,&tablev,sizeof(*te)*(tc+1)); |
229 | *rrp= table= tablev; |
230 | for (te= *rrp; te->i >= 0; te++) |
231 | adns__makefinal_str(qu,&te->str); |
ffbda80c |
232 | } |
233 | |
234 | static void mf_flat(adns_query qu, void *data) { } |
235 | |
236 | #define TYPE_SF(size,func,cp,free) size, pa_##func, mf_##free, cs_##cp |
237 | #define TYPE_SN(size,func,cp) size, pa_##func, mf_flat, cs_##cp |
e062dcae |
238 | #define TYPESZ_M(member) (sizeof(*((adns_answer*)0)->rrs.member)) |
86e7b8d9 |
239 | #define TYPE_MF(memb,parse) TYPE_SF(TYPESZ_M(memb),parse,memb,memb) |
240 | #define TYPE_MN(memb,parse) TYPE_SN(TYPESZ_M(memb),parse,memb) |
241 | |
ffbda80c |
242 | #define DEEP_MEMB(memb) TYPESZ_M(memb), mf_##memb, cs_##memb |
243 | #define FLAT_MEMB(memb) TYPESZ_M(memb), mf_flat, cs_##memb |
98a3f706 |
244 | |
e7a9ca47 |
245 | /* TYPE_<ms><nf> |
246 | * ms is M specify member name |
247 | * or S specify size explicitly |
248 | * nf is F full memory management, dependent on member name or specified func |
249 | * N no memory management required |
250 | */ |
98a3f706 |
251 | |
252 | static const typeinfo typeinfos[] = { |
253 | /* Must be in ascending order of rrtype ! */ |
e062dcae |
254 | /* rr type code rrt fmt mem.mgmt member parser comparer */ |
98a3f706 |
255 | |
e062dcae |
256 | { adns_r_a, "A", 0, FLAT_MEMB(inaddr), pa_inaddr, di_inaddr }, |
257 | { adns_r_ns_raw, "NS", "raw", DEEP_MEMB(str), pa_domain_raw, 0 }, |
258 | { adns_r_cname, "CNAME", 0, DEEP_MEMB(str), pa_domain_raw, 0 }, |
ffbda80c |
259 | #if 0 /*fixme*/ |
e062dcae |
260 | { adns_r_soa_raw, "SOA", "raw", DEEP_MEMB(soa), pa_soa, 0 }, |
ffbda80c |
261 | #endif |
e062dcae |
262 | { adns_r_ptr_raw, "PTR", "raw", DEEP_MEMB(str), pa_domain_raw, 0 }, |
263 | #if 0 /*fixme*/ |
264 | { adns_r_hinfo, "HINFO", 0, DEEP_MEMB(strpair), pa_hinfo, 0 }, |
265 | #endif |
266 | { adns_r_mx_raw, "MX", "raw", DEEP_MEMB(intstr), pa_mx_raw, di_mx_raw }, |
267 | { adns_r_txt, "TXT", 0, DEEP_MEMB(manyistr), pa_txt, 0 }, |
268 | #if 0 /*fixme*/ |
269 | { adns_r_rp_raw, "RP", "raw", DEEP_MEMB(strpair), pa_rp, 0 }, |
270 | #endif |
271 | #if 0 /*fixme*/ |
86e7b8d9 |
272 | |
e062dcae |
273 | { adns_r_ns, "NS", "+addr", DEEP_MEMB(dmaddr), pa_dmaddr, di_dmaddr }, |
274 | { adns_r_ptr, "PTR","checked", DEEP_MEMB(str), pa_ptr, 0 }, |
275 | { adns_r_mx, "MX", "+addr", DEEP_MEMB(intdmaddr), pa_mx, di_mx }, |
86e7b8d9 |
276 | |
e062dcae |
277 | #endif |
278 | #if 0 /*fixme*/ |
279 | { adns_r_soa, "SOA", "822", DEEP_MEMB(soa), pa_soa, 0 }, |
280 | { adns_r_rp, "RP", "822", DEEP_MEMB(strpair), pa_rp, 0 }, |
e7a9ca47 |
281 | #endif |
98a3f706 |
282 | }; |
283 | |
f759e52e |
284 | const typeinfo *adns__findtype(adns_rrtype type) { |
285 | const typeinfo *begin, *end, *mid; |
98a3f706 |
286 | |
287 | begin= typeinfos; end= typeinfos+(sizeof(typeinfos)/sizeof(typeinfo)); |
288 | |
289 | while (begin < end) { |
290 | mid= begin + ((end-begin)>>1); |
291 | if (mid->type == type) return mid; |
292 | if (type > mid->type) begin= mid+1; |
293 | else end= mid; |
294 | } |
295 | return 0; |
296 | } |