masqmail-0.2
view src/lookup.c @ 12:9fb7ddbaf129
removed noop-code; some beautifying
author | meillo@marmaro.de |
---|---|
date | Wed, 29 Oct 2008 16:34:13 +0100 |
parents | 08114f7dcc23 |
children | f671821d8222 |
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 unsigned short
49 getshort(unsigned char *c)
50 {
51 unsigned short u;
52 u = c[0];
53 return (u << 8) + c[1];
54 }
56 static int
57 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
110 dns_next()
111 {
112 int i;
114 if (num_answers <= 0)
115 return 2;
116 num_answers--;
118 if (resp_pos == resp_end)
119 return -1; /* soft */
121 i = dn_expand(response.buf, resp_end, resp_pos, name, 256);
122 if (i < 0)
123 return -1; /* soft */
124 resp_pos += i;
126 i = resp_end - resp_pos;
127 if (i < 4 + 3 * 2)
128 return -1; /* soft */
130 rr_type = getshort(resp_pos);
131 rr_dlen = getshort(resp_pos + 8);
132 resp_pos += 10;
134 return 0;
135 }
137 static int
138 dns_getip(guint32 * ip)
139 {
140 int ret;
142 if ((ret = dns_next()))
143 return ret;
145 if (rr_type == T_A) {
146 if (rr_dlen < 4)
147 return -1; /* soft */
148 *ip = *(guint32 *) (resp_pos);
149 DEBUG(5) debugf("DNS: dns_getip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip));
150 resp_pos += rr_dlen;
152 return 1;
153 }
154 resp_pos += rr_dlen;
155 return 0;
156 }
158 static int
159 dns_getmx(int *pref)
160 {
161 int ret;
163 if ((ret = dns_next()))
164 return ret;
166 if (rr_type == T_MX) {
167 if (rr_dlen < 3)
168 return -1; /* soft */
170 *pref = (resp_pos[0] << 8) + resp_pos[1];
171 if (dn_expand(response.buf, resp_end, resp_pos + 2, name, MAX_DNSNAME) < 0)
172 return -1;
174 resp_pos += rr_dlen;
176 return 1;
177 }
178 resp_pos += rr_dlen;
179 return 0;
180 }
182 /*
183 static int
184 dns_getname(int type)
185 {
186 int ret;
188 if((ret = dns_next())) return ret;
190 if (rr_type == type){
191 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0)
192 return -1;
194 resp_pos += rr_dlen;
196 return 1;
197 }
198 resp_pos += rr_dlen;
199 return 0;
200 }
201 */
203 static int
204 dns_look_ip(gchar * domain, guint32 * ip)
205 {
206 gchar *n = domain;
208 while (TRUE) {
209 if (dns_resolve(n, T_A, FALSE) == 0) {
210 dns_next();
211 if (rr_type == T_A) {
212 if (rr_dlen < 4)
213 return -1; /* soft */
214 *ip = *(guint32 *) (resp_pos);
216 DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip));
218 resp_pos += rr_dlen;
219 return 0;
220 } else if (rr_type == T_CNAME) {
221 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0)
222 return -1;
224 DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name);
226 resp_pos += rr_dlen;
227 n = name;
228 } else
229 return -1;
230 } else
231 return -1;
232 }
233 }
235 GList*
236 resolve_dns_a(GList * list, gchar * domain)
237 {
238 int ret;
240 DEBUG(5) debugf("DNS: resolve_dns_a entered\n");
242 if (dns_resolve(domain, T_A, TRUE) == 0) {
243 mxip_addr mxip;
244 while ((ret = dns_getip(&(mxip.ip))) != 2) {
245 if (ret == 1) {
246 mxip.name = g_strdup(name);
247 mxip.pref = 0;
248 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
249 }
250 }
251 }
252 return list;
253 }
255 static gint
256 _mx_sort_func(gconstpointer aa, gconstpointer bb)
257 {
258 const mxip_addr *a = (mxip_addr *) aa;
259 const mxip_addr *b = (mxip_addr *) bb;
261 if (a->pref == b->pref)
262 return a->ip - b->ip;
263 else
264 return a->pref - b->pref;
265 }
267 GList*
268 resolve_dns_mx(GList * list, gchar * domain)
269 {
270 GList *node;
271 int ret;
272 int cnt = 0;
274 DEBUG(5) debugf("DNS: resolve_dns_mx entered\n");
276 if (dns_resolve(domain, T_MX, TRUE) == 0) {
277 GList *node_next;
278 mxip_addr mxip;
279 while ((ret = dns_getmx(&(mxip.pref))) != 2) {
280 if (ret == 1) {
281 mxip.name = g_strdup(name);
282 mxip.ip = rand();
283 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
284 cnt++;
285 }
286 }
288 DEBUG(5) debugf("DNS: found %d mx records\n", cnt);
290 /* to randomize sequences with equal pref values,
291 we temporarily 'misused' the ip field and
292 put a random number in it as a secondary sort key.
293 */
294 list = g_list_sort(list, _mx_sort_func);
296 /* CNAME resolving has to be added as well. */
298 for (node = g_list_first(list); node != NULL; node = node_next) {
300 mxip_addr *p_mxip = (mxip_addr *) (node->data);
301 node_next = g_list_next(node);
303 if (dns_look_ip(p_mxip->name, &(p_mxip->ip)) != 0) {
304 DEBUG(1) debugf("DNS: could not resolve target of mx %s\n", p_mxip->name);
305 list = g_list_remove_link(list, node);
306 g_free(node->data);
307 g_list_free_1(node);
308 }
309 }
310 }
311 return list;
312 }
314 #endif
316 /* now something completely different... */
318 GList*
319 resolve_byname(GList * list, gchar * domain)
320 {
321 struct hostent *hent;
323 DEBUG(5) debugf("DNS: resolve_byname entered\n");
325 if ((hent = gethostbyname(domain))) {
326 char *haddr;
327 int i = 0;
328 while ((haddr = hent->h_addr_list[i++])) {
329 mxip_addr mxip;
330 mxip.ip = *(guint32 *) (haddr);
331 mxip.pref = 0;
332 mxip.name = g_strdup(hent->h_name);
333 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
334 }
335 }
336 return list;
337 }
339 #ifdef RESOLV_TEST
340 int
341 main(int argc, char *argv[])
342 {
343 GList *addr_list = NULL, *node;
345 g_print("starting res_init()\n");
347 g_print("retrans = %d, retry = %d\n", _res.retrans, _res.retry);
349 if (res_init() == 0) {
351 addr_list = resolve_dns_a(NULL, argv[1]);
352 g_print("A:\n");
354 foreach(addr_list, node) {
355 mxip_addr *p_mxip = (mxip_addr *) (node->data);
357 printf("name = %s\n IP = %s\n", p_mxip->name, inet_ntoa(*(struct in_addr *) &(p_mxip->ip)));
358 }
359 addr_list = resolve_dns_mx(NULL, argv[1]);
360 g_print("MX:\n");
362 foreach(addr_list, node) {
363 mxip_addr *p_mxip = (mxip_addr *) (node->data);
365 printf("name = %s\n IP = %s pref = %d\n", p_mxip->name, inet_ntoa(*(struct in_addr *) &(p_mxip->ip)), 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