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 | |
86e7b8d9 |
23 | #include <arpa/inet.h> |
24 | |
98a3f706 |
25 | #include "internal.h" |
26 | |
86e7b8d9 |
27 | static adns_status pa_inaddr(adns_query qu, int serv, |
e7a9ca47 |
28 | const byte *dgram, int dglen, int cbyte, int max, |
29 | void *store_r) { |
30 | struct in_addr *dr= store_r; |
31 | |
32 | if (max-cbyte != 4) return adns_s_invaliddata; |
33 | memcpy(dr,dgram+cbyte,4); |
34 | return adns_s_ok; |
35 | } |
36 | |
86e7b8d9 |
37 | static adns_status cs_inaddr(vbuf *vb, const void *data) { |
38 | const struct in_addr *dp= data; |
39 | const char *ia; |
40 | |
41 | ia= inet_ntoa(*dp); assert(ia); |
42 | return adns__vbuf_appendstr(vb,ia) ? adns_s_ok : adns_s_nolocalmem; |
43 | } |
e7a9ca47 |
44 | |
ffbda80c |
45 | static adns_status pa_domain_raw(adns_query qu, int serv, |
46 | const byte *dgram, int dglen, int cbyte, int max, |
47 | void *store_r) { |
48 | char **dpp= store_r; |
49 | adns_status st; |
50 | vbuf vb; |
51 | char *dp; |
86e7b8d9 |
52 | |
ffbda80c |
53 | adns__vbuf_init(&vb); |
54 | st= adns__parse_domain(qu->ads,serv,qu,&vb,qu->flags, |
55 | dgram,dglen, &cbyte,max); |
56 | if (st) goto x_error; |
57 | |
58 | dp= adns__alloc_interim(qu,vb.used+1); |
59 | if (!dp) { st= adns_s_nolocalmem; goto x_error; } |
60 | |
61 | dp[vb.used]= 0; |
62 | memcpy(dp,vb.buf,vb.used); |
63 | |
64 | if (cbyte != max) { st= adns_s_invaliddata; goto x_error; } |
65 | |
66 | st= adns_s_ok; |
67 | *dpp= dp; |
68 | |
69 | x_error: |
70 | adns__vbuf_free(&vb); |
71 | return st; |
72 | } |
73 | |
74 | static void mf_str(adns_query qu, void *data) { |
75 | char **ddp= data; |
76 | |
77 | adns__makefinal_str(qu,ddp); |
78 | } |
79 | |
80 | static adns_status cs_str(vbuf *vb, const void *data) { |
81 | const char *const *ddp= data; |
82 | const char *dp= *ddp; |
83 | |
84 | return (adns__vbuf_append(vb,"\"",1) && |
85 | adns__vbuf_appendstr(vb,dp) && |
86 | adns__vbuf_append(vb,"\"",1)) |
87 | ? adns_s_ok : adns_s_nolocalmem; |
88 | } |
89 | |
90 | static void mf_flat(adns_query qu, void *data) { } |
91 | |
92 | #define TYPE_SF(size,func,cp,free) size, pa_##func, mf_##free, cs_##cp |
93 | #define TYPE_SN(size,func,cp) size, pa_##func, mf_flat, cs_##cp |
e7a9ca47 |
94 | #define TYPESZ_M(member) (sizeof(((adns_answer*)0)->rrs.member)) |
86e7b8d9 |
95 | #define TYPE_MF(memb,parse) TYPE_SF(TYPESZ_M(memb),parse,memb,memb) |
96 | #define TYPE_MN(memb,parse) TYPE_SN(TYPESZ_M(memb),parse,memb) |
97 | |
ffbda80c |
98 | #define DEEP_MEMB(memb) TYPESZ_M(memb), mf_##memb, cs_##memb |
99 | #define FLAT_MEMB(memb) TYPESZ_M(memb), mf_flat, cs_##memb |
98a3f706 |
100 | |
e7a9ca47 |
101 | /* TYPE_<ms><nf> |
102 | * ms is M specify member name |
103 | * or S specify size explicitly |
104 | * nf is F full memory management, dependent on member name or specified func |
105 | * N no memory management required |
106 | */ |
98a3f706 |
107 | |
108 | static const typeinfo typeinfos[] = { |
109 | /* Must be in ascending order of rrtype ! */ |
86e7b8d9 |
110 | /* rr type code rrt fmt mem.mgmt member parser */ |
98a3f706 |
111 | |
86e7b8d9 |
112 | { adns_r_a, "A", 0, FLAT_MEMB(inaddr), pa_inaddr }, |
86e7b8d9 |
113 | { adns_r_ns_raw, "NS", "raw", DEEP_MEMB(str), pa_domain_raw }, |
114 | { adns_r_cname, "CNAME", 0, DEEP_MEMB(str), pa_domain_raw }, |
ffbda80c |
115 | #if 0 /*fixme*/ |
86e7b8d9 |
116 | { adns_r_soa_raw, "SOA", "raw", DEEP_MEMB(soa), pa_soa }, |
ffbda80c |
117 | #endif |
86e7b8d9 |
118 | { adns_r_ptr_raw, "PTR", "raw", DEEP_MEMB(str), pa_domain_raw }, |
ffbda80c |
119 | #if 0 /*fixme*/ |
86e7b8d9 |
120 | { adns_r_hinfo, "HINFO", 0, DEEP_MEMB(strpair), pa_hinfo }, |
121 | { adns_r_mx_raw, "MX", "raw", DEEP_MEMB(intstr), pa_mx_raw }, |
122 | { adns_r_txt, "TXT", 0, DEEP_MEMB(str), pa_txt }, |
123 | { adns_r_rp_raw, "RP", "raw", DEEP_MEMB(strpair), pa_rp }, |
124 | |
125 | { adns_r_ns, "NS", "+addr", DEEP_MEMB(dmaddr), pa_dmaddr }, |
126 | { adns_r_ptr, "PTR", "checked", DEEP_MEMB(str), pa_ptr }, |
127 | { adns_r_mx, "MX", "+addr", DEEP_MEMB(intdmaddr), pa_mx }, |
128 | |
129 | { adns_r_soa, "SOA", "822", DEEP_MEMB(soa), pa_soa }, |
130 | { adns_r_rp, "RP", "822", DEEP_MEMB(strpair), pa_rp }, |
e7a9ca47 |
131 | #endif |
98a3f706 |
132 | }; |
133 | |
f759e52e |
134 | const typeinfo *adns__findtype(adns_rrtype type) { |
135 | const typeinfo *begin, *end, *mid; |
98a3f706 |
136 | |
137 | begin= typeinfos; end= typeinfos+(sizeof(typeinfos)/sizeof(typeinfo)); |
138 | |
139 | while (begin < end) { |
140 | mid= begin + ((end-begin)>>1); |
141 | if (mid->type == type) return mid; |
142 | if (type > mid->type) begin= mid+1; |
143 | else end= mid; |
144 | } |
145 | return 0; |
146 | } |