comparison src/lookup.c @ 200:116b0269c934

reworked resolvtest; let it build; refactored in lookup.c the resolvtest helper and code test tool was available in the code but did not get built I moved it to resolvtest.c and included it into the build process the define RESOLV_TEST was removed
author meillo@marmaro.de
date Fri, 16 Jul 2010 12:53:07 +0200
parents f671821d8222
children 138e66e1a61f
comparison
equal deleted inserted replaced
199:a8855df82650 200:116b0269c934
20 #include <arpa/nameser.h> 20 #include <arpa/nameser.h>
21 #include <resolv.h> 21 #include <resolv.h>
22 22
23 #include "masqmail.h" 23 #include "masqmail.h"
24 24
25 #ifdef RESOLV_TEST
26 #undef DEBUG
27 #define DEBUG(x) if(x > 0)
28 #define debugf g_print
29 #endif
30 25
31 #ifdef ENABLE_RESOLVER 26 #ifdef ENABLE_RESOLVER
32 27
33 static union { 28 static union {
34 HEADER hdr; 29 HEADER hdr;
196 resp_pos += rr_dlen; 191 resp_pos += rr_dlen;
197 return 0; 192 return 0;
198 } 193 }
199 */ 194 */
200 195
201 static int 196 int
202 dns_look_ip(gchar * domain, guint32 * ip) 197 dns_look_ip(gchar * domain, guint32 * ip)
203 { 198 {
204 gchar *n = domain; 199 gchar *n = domain;
205 200
206 while (TRUE) { 201 while (TRUE) {
207 if (dns_resolve(n, T_A, FALSE) == 0) { 202 if (dns_resolve(n, T_A, FALSE) != 0) {
208 dns_next(); 203 return -1;
209 if (rr_type == T_A) { 204 }
210 if (rr_dlen < 4) 205
211 return -1; /* soft */ 206 dns_next();
212 *ip = *(guint32 *) (resp_pos); 207 if (rr_type == T_A) {
213 208 if (rr_dlen < 4) {
214 DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip)); 209 return -1; /* soft */
215 210 }
216 resp_pos += rr_dlen; 211 *ip = *(guint32 *) (resp_pos);
217 return 0; 212
218 } else if (rr_type == T_CNAME) { 213 DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip));
219 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) 214
220 return -1; 215 resp_pos += rr_dlen;
221 216 return 0;
222 DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name); 217 } else if (rr_type == T_CNAME) {
223 218 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) {
224 resp_pos += rr_dlen;
225 n = name;
226 } else
227 return -1; 219 return -1;
228 } else 220 }
229 return -1; 221
222 DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name);
223
224 resp_pos += rr_dlen;
225 n = name;
226 } else {
227 return -1;
228 }
230 } 229 }
231 } 230 }
232 231
233 GList* 232 GList*
234 resolve_dns_a(GList * list, gchar * domain) 233 resolve_dns_a(GList * list, gchar * domain)
331 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); 330 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
332 } 331 }
333 } 332 }
334 return list; 333 return list;
335 } 334 }
336
337 #ifdef RESOLV_TEST
338 int
339 main(int argc, char *argv[])
340 {
341 GList *addr_list = NULL, *node;
342
343 g_print("starting res_init()\n");
344 g_print("retrans = %d, retry = %d\n", _res.retrans, _res.retry);
345 if (res_init() == 0) {
346 addr_list = resolve_dns_a(NULL, argv[1]);
347 g_print("A:\n");
348 foreach(addr_list, node) {
349 mxip_addr *p_mxip = (mxip_addr *) (node->data);
350 printf("name = %s\n IP = %s\n", p_mxip->name, inet_ntoa(*(struct in_addr *) &(p_mxip->ip)));
351 }
352 addr_list = resolve_dns_mx(NULL, argv[1]);
353 g_print("MX:\n");
354 foreach(addr_list, node) {
355 mxip_addr *p_mxip = (mxip_addr *) (node->data);
356 printf("name = %s\n IP = %s pref = %d\n", p_mxip->name,
357 inet_ntoa(*(struct in_addr *) &(p_mxip->ip)), p_mxip->pref);
358 }
359 {
360 guint32 ip;
361 dns_look_ip(argv[1], &ip);
362 printf("dns_look_ip: %s\n", inet_ntoa(*((struct in_addr *) (&ip))));
363 }
364 } else
365 printf("res_init() failed.\n");
366 }
367 #endif