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