masqmail
view src/lookup.c @ 8:7e01b9096bb6
removed tests from Makefiles
author | meillo@marmaro.de |
---|---|
date | Mon, 27 Oct 2008 16:20:41 +0100 |
parents | |
children | 26e34ae9a3e3 |
line source
1 /* MasqMail Copyright (C) Oliver Kurth,
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 */
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/nameser.h>
21 #include <resolv.h>
23 #include "masqmail.h"
25 #ifdef RESOLV_TEST
27 #undef DEBUG
28 #define DEBUG(x) if(x > 0)
30 #define debugf g_print
31 #endif
33 #ifdef ENABLE_RESOLVER
35 static union {
36 HEADER hdr;
37 unsigned char buf[PACKETSZ];
38 } response;
39 static unsigned char *resp_end;
40 static unsigned char *resp_pos;
42 static int num_answers;
43 static char name[MAX_DNSNAME];
45 unsigned short rr_type;
46 unsigned short rr_dlen;
48 static
49 unsigned short getshort(unsigned char *c)
50 {
51 unsigned short u;
52 u = c[0];
53 return (u << 8) + c[1];
54 }
56 static
57 int dns_resolve(char *domain, int type, gboolean do_search)
58 {
59 int n;
60 int i;
62 int resp_len;
63 /*errno = 0;*/
65 /*
66 if (!stralloc_copy(&glue,domain)) return DNS_MEM;
67 if (!stralloc_0(&glue)) return DNS_MEM;
68 */
70 // resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response));
71 DEBUG(5) debugf("DNS: before res_search()\n");
72 if(do_search)
73 resp_len = res_search(domain, C_IN, type, response.buf, sizeof(response));
74 else
75 resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response));
76 DEBUG(5) debugf("DBG: after res_search()\n");
78 if (resp_len <= 0){
79 /*
80 if (errno == ECONNREFUSED) return DNS_SOFT;
81 if (h_errno == TRY_AGAIN) return DNS_SOFT;
82 return DNS_HARD;
83 */
84 return -1;
85 }
86 if (resp_len >= sizeof(response))
87 resp_len = sizeof(response);
89 resp_end = response.buf + resp_len;
90 resp_pos = response.buf + sizeof(HEADER);
91 n = ntohs(response.hdr.qdcount);
93 while (n-- > 0){
94 i = dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME);
95 if (i < 0)
96 return -1;
97 DEBUG(5) debugf("DBG: resolve name = %s\n", name);
98 resp_pos += i;
99 i = resp_end - resp_pos;
100 if (i < QFIXEDSZ)
101 return -1;
102 resp_pos += QFIXEDSZ;
103 }
104 num_answers = ntohs(response.hdr.ancount);
106 return 0;
107 }
109 static int dns_next()
110 {
111 int i;
113 if (num_answers <= 0) return 2;
114 num_answers--;
116 if (resp_pos == resp_end)
117 return -1 /* soft */;
119 i = dn_expand(response.buf, resp_end, resp_pos, name, 256);
120 if (i < 0)
121 return -1; /* soft */
122 resp_pos += i;
124 i = resp_end - resp_pos;
125 if (i < 4 + 3 * 2)
126 return -1; /* soft */
128 rr_type = getshort(resp_pos);
129 rr_dlen = getshort(resp_pos + 8);
130 resp_pos += 10;
132 return 0;
133 }
135 static
136 int dns_getip(guint32 *ip)
137 {
138 int ret;
140 if((ret = dns_next())) return ret;
142 if (rr_type == T_A){
143 if (rr_dlen < 4)
144 return -1; /* soft */
145 *ip = *(guint32 *)(resp_pos);
146 DEBUG(5) debugf("DNS: dns_getip(): ip = %s\n", inet_ntoa(*(struct in_addr*)ip));
147 resp_pos += rr_dlen;
149 return 1;
150 }
151 resp_pos += rr_dlen;
152 return 0;
153 }
155 static
156 int dns_getmx(int *pref)
157 {
158 int ret;
160 if((ret = dns_next())) return ret;
162 if (rr_type == T_MX){
163 if (rr_dlen < 3)
164 return -1; /* soft */
166 *pref = (resp_pos[0] << 8) + resp_pos[1];
167 if (dn_expand(response.buf, resp_end, resp_pos + 2, name, MAX_DNSNAME) < 0)
168 return -1;
170 resp_pos += rr_dlen;
172 return 1;
173 }
174 resp_pos += rr_dlen;
175 return 0;
176 }
178 /*
179 static
180 int dns_getname(int type)
181 {
182 int ret;
184 if((ret = dns_next())) return ret;
186 if (rr_type == type){
187 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0)
188 return -1;
190 resp_pos += rr_dlen;
192 return 1;
193 }
194 resp_pos += rr_dlen;
195 return 0;
196 }
197 */
199 static
200 int dns_look_ip(gchar *domain, guint32 *ip)
201 {
202 gchar *n = domain;
204 while(TRUE){
205 if(dns_resolve(n, T_A, FALSE) == 0){
206 dns_next();
207 if(rr_type == T_A){
208 if (rr_dlen < 4)
209 return -1; /* soft */
210 *ip = *(guint32 *)(resp_pos);
212 DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n",
213 inet_ntoa(*(struct in_addr*)ip));
215 resp_pos += rr_dlen;
216 return 0;
217 }else if(rr_type == T_CNAME){
218 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0)
219 return -1;
221 DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name);
223 resp_pos += rr_dlen;
224 n = name;
225 }else
226 return -1;
227 }else
228 return -1;
229 }
230 }
232 GList *resolve_dns_a(GList *list, gchar *domain)
233 {
234 int ret;
236 DEBUG(5) debugf("DNS: resolve_dns_a entered\n");
238 if(dns_resolve(domain, T_A, TRUE) == 0){
239 mxip_addr mxip;
240 while((ret = dns_getip(&(mxip.ip))) != 2){
241 if(ret == 1){
242 mxip.name = g_strdup(name);
243 mxip.pref = 0;
244 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
245 }
246 }
247 }
248 return list;
249 }
251 static
252 gint _mx_sort_func(gconstpointer aa, gconstpointer bb)
253 {
254 const mxip_addr *a = (mxip_addr *)aa;
255 const mxip_addr *b = (mxip_addr *)bb;
257 if(a->pref == b->pref)
258 return a->ip - b->ip;
259 else
260 return a->pref - b->pref;
261 }
263 GList *resolve_dns_mx(GList *list, gchar *domain)
264 {
265 GList *node;
266 int ret;
267 int cnt = 0;
269 DEBUG(5) debugf("DNS: resolve_dns_mx entered\n");
271 if(dns_resolve(domain, T_MX, TRUE) == 0){
272 GList *node_next;
273 mxip_addr mxip;
274 while((ret = dns_getmx(&(mxip.pref))) != 2){
275 if(ret == 1){
276 mxip.name = g_strdup(name);
277 mxip.ip = rand();
278 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
279 cnt++;
280 }
281 }
283 DEBUG(5) debugf("DNS: found %d mx records\n", cnt);
285 /* to randomize sequences with equal pref values,
286 we temporarily 'misused' the ip field and
287 put a random number in it as a secondary sort key.
288 */
289 list = g_list_sort(list, _mx_sort_func);
291 /* CNAME resolving has to be added as well. */
293 for(node = g_list_first(list);
294 node != NULL;
295 node = node_next){
297 mxip_addr *p_mxip = (mxip_addr *)(node->data);
298 node_next = g_list_next(node);
300 if(dns_look_ip(p_mxip->name, &(p_mxip->ip)) != 0){
301 DEBUG(1) debugf("DNS: could not resolve target of mx %s\n", p_mxip->name);
302 list = g_list_remove_link(list, node);
303 g_free(node->data);
304 g_list_free_1(node);
305 }
306 }
307 }
308 return list;
309 }
311 #endif
313 /* now something completely different... */
315 GList *resolve_byname(GList *list, gchar *domain)
316 {
317 struct hostent *hent;
319 DEBUG(5) debugf("DNS: resolve_byname entered\n");
321 if((hent = gethostbyname(domain))){
322 char *haddr;
323 int i = 0;
324 while((haddr = hent->h_addr_list[i++])){
325 mxip_addr mxip;
326 mxip.ip = *(guint32 *)(haddr);
327 mxip.pref = 0;
328 mxip.name = g_strdup(hent->h_name);
329 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
330 }
331 }
332 return list;
333 }
335 #ifdef RESOLV_TEST
336 int main(int argc, char *argv[])
337 {
338 GList *addr_list = NULL, *node;
340 g_print("starting res_init()\n");
342 g_print("retrans = %d, retry = %d\n", _res.retrans, _res.retry);
344 if(res_init() == 0){
346 addr_list = resolve_dns_a(NULL, argv[1]);
347 g_print("A:\n");
349 foreach(addr_list, node){
350 mxip_addr *p_mxip = (mxip_addr *)(node->data);
352 printf("name = %s\n IP = %s\n",
353 p_mxip->name,
354 inet_ntoa(*(struct in_addr *)&(p_mxip->ip)));
355 }
356 addr_list = resolve_dns_mx(NULL, argv[1]);
357 g_print("MX:\n");
359 foreach(addr_list, node){
360 mxip_addr *p_mxip = (mxip_addr *)(node->data);
362 printf("name = %s\n IP = %s pref = %d\n",
363 p_mxip->name,
364 inet_ntoa(*(struct in_addr *)&(p_mxip->ip)),
365 p_mxip->pref);
366 }
367 {
368 guint32 ip;
369 dns_look_ip(argv[1], &ip);
370 printf("dns_look_ip: %s\n", inet_ntoa(*((struct in_addr *)(&ip))));
371 }
372 }else
373 printf("res_init() failed.\n");
375 }
376 #endif