masqmail-0.2
diff src/lookup.c @ 10:26e34ae9a3e3
changed indention and line wrapping to a more consistent style
author | meillo@marmaro.de |
---|---|
date | Mon, 27 Oct 2008 16:23:10 +0100 |
parents | 08114f7dcc23 |
children | f671821d8222 |
line diff
1.1 --- a/src/lookup.c Mon Oct 27 16:21:27 2008 +0100 1.2 +++ b/src/lookup.c Mon Oct 27 16:23:10 2008 +0100 1.3 @@ -4,7 +4,7 @@ 1.4 * it under the terms of the GNU General Public License as published by 1.5 * the Free Software Foundation; either version 2 of the License, or 1.6 * (at your option) any later version. 1.7 - * 1.8 + * 1.9 * This program is distributed in the hope that it will be useful, 1.10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 1.11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.12 @@ -33,8 +33,8 @@ 1.13 #ifdef ENABLE_RESOLVER 1.14 1.15 static union { 1.16 - HEADER hdr; 1.17 - unsigned char buf[PACKETSZ]; 1.18 + HEADER hdr; 1.19 + unsigned char buf[PACKETSZ]; 1.20 } response; 1.21 static unsigned char *resp_end; 1.22 static unsigned char *resp_pos; 1.23 @@ -45,139 +45,143 @@ 1.24 unsigned short rr_type; 1.25 unsigned short rr_dlen; 1.26 1.27 -static 1.28 -unsigned short getshort(unsigned char *c) 1.29 +static unsigned short 1.30 +getshort(unsigned char *c) 1.31 { 1.32 - unsigned short u; 1.33 - u = c[0]; 1.34 - return (u << 8) + c[1]; 1.35 + unsigned short u; 1.36 + u = c[0]; 1.37 + return (u << 8) + c[1]; 1.38 } 1.39 1.40 -static 1.41 -int dns_resolve(char *domain, int type, gboolean do_search) 1.42 +static int 1.43 +dns_resolve(char *domain, int type, gboolean do_search) 1.44 { 1.45 - int n; 1.46 - int i; 1.47 + int n; 1.48 + int i; 1.49 1.50 - int resp_len; 1.51 - /*errno = 0;*/ 1.52 + int resp_len; 1.53 + /*errno = 0; */ 1.54 1.55 - /* 1.56 - if (!stralloc_copy(&glue,domain)) return DNS_MEM; 1.57 - if (!stralloc_0(&glue)) return DNS_MEM; 1.58 - */ 1.59 + /* 1.60 + if (!stralloc_copy(&glue,domain)) return DNS_MEM; 1.61 + if (!stralloc_0(&glue)) return DNS_MEM; 1.62 + */ 1.63 1.64 - // resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response)); 1.65 - DEBUG(5) debugf("DNS: before res_search()\n"); 1.66 - if(do_search) 1.67 - resp_len = res_search(domain, C_IN, type, response.buf, sizeof(response)); 1.68 - else 1.69 - resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response)); 1.70 - DEBUG(5) debugf("DBG: after res_search()\n"); 1.71 + // resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response)); 1.72 + DEBUG(5) debugf("DNS: before res_search()\n"); 1.73 + if (do_search) 1.74 + resp_len = res_search(domain, C_IN, type, response.buf, sizeof(response)); 1.75 + else 1.76 + resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response)); 1.77 + DEBUG(5) debugf("DBG: after res_search()\n"); 1.78 1.79 - if (resp_len <= 0){ 1.80 - /* 1.81 - if (errno == ECONNREFUSED) return DNS_SOFT; 1.82 - if (h_errno == TRY_AGAIN) return DNS_SOFT; 1.83 - return DNS_HARD; 1.84 - */ 1.85 - return -1; 1.86 - } 1.87 - if (resp_len >= sizeof(response)) 1.88 - resp_len = sizeof(response); 1.89 + if (resp_len <= 0) { 1.90 + /* 1.91 + if (errno == ECONNREFUSED) return DNS_SOFT; 1.92 + if (h_errno == TRY_AGAIN) return DNS_SOFT; 1.93 + return DNS_HARD; 1.94 + */ 1.95 + return -1; 1.96 + } 1.97 + if (resp_len >= sizeof(response)) 1.98 + resp_len = sizeof(response); 1.99 1.100 - resp_end = response.buf + resp_len; 1.101 - resp_pos = response.buf + sizeof(HEADER); 1.102 - n = ntohs(response.hdr.qdcount); 1.103 + resp_end = response.buf + resp_len; 1.104 + resp_pos = response.buf + sizeof(HEADER); 1.105 + n = ntohs(response.hdr.qdcount); 1.106 1.107 - while (n-- > 0){ 1.108 - i = dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME); 1.109 - if (i < 0) 1.110 - return -1; 1.111 - DEBUG(5) debugf("DBG: resolve name = %s\n", name); 1.112 - resp_pos += i; 1.113 - i = resp_end - resp_pos; 1.114 - if (i < QFIXEDSZ) 1.115 - return -1; 1.116 - resp_pos += QFIXEDSZ; 1.117 - } 1.118 - num_answers = ntohs(response.hdr.ancount); 1.119 + while (n-- > 0) { 1.120 + i = dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME); 1.121 + if (i < 0) 1.122 + return -1; 1.123 + DEBUG(5) debugf("DBG: resolve name = %s\n", name); 1.124 + resp_pos += i; 1.125 + i = resp_end - resp_pos; 1.126 + if (i < QFIXEDSZ) 1.127 + return -1; 1.128 + resp_pos += QFIXEDSZ; 1.129 + } 1.130 + num_answers = ntohs(response.hdr.ancount); 1.131 1.132 - return 0; 1.133 + return 0; 1.134 } 1.135 1.136 -static int dns_next() 1.137 +static int 1.138 +dns_next() 1.139 { 1.140 - int i; 1.141 + int i; 1.142 1.143 - if (num_answers <= 0) return 2; 1.144 - num_answers--; 1.145 + if (num_answers <= 0) 1.146 + return 2; 1.147 + num_answers--; 1.148 1.149 - if (resp_pos == resp_end) 1.150 - return -1 /* soft */; 1.151 + if (resp_pos == resp_end) 1.152 + return -1; /* soft */ 1.153 1.154 - i = dn_expand(response.buf, resp_end, resp_pos, name, 256); 1.155 - if (i < 0) 1.156 - return -1; /* soft */ 1.157 - resp_pos += i; 1.158 + i = dn_expand(response.buf, resp_end, resp_pos, name, 256); 1.159 + if (i < 0) 1.160 + return -1; /* soft */ 1.161 + resp_pos += i; 1.162 1.163 - i = resp_end - resp_pos; 1.164 - if (i < 4 + 3 * 2) 1.165 - return -1; /* soft */ 1.166 - 1.167 - rr_type = getshort(resp_pos); 1.168 - rr_dlen = getshort(resp_pos + 8); 1.169 - resp_pos += 10; 1.170 + i = resp_end - resp_pos; 1.171 + if (i < 4 + 3 * 2) 1.172 + return -1; /* soft */ 1.173 1.174 - return 0; 1.175 + rr_type = getshort(resp_pos); 1.176 + rr_dlen = getshort(resp_pos + 8); 1.177 + resp_pos += 10; 1.178 + 1.179 + return 0; 1.180 } 1.181 1.182 -static 1.183 -int dns_getip(guint32 *ip) 1.184 +static int 1.185 +dns_getip(guint32 * ip) 1.186 { 1.187 - int ret; 1.188 + int ret; 1.189 1.190 - if((ret = dns_next())) return ret; 1.191 + if ((ret = dns_next())) 1.192 + return ret; 1.193 1.194 - if (rr_type == T_A){ 1.195 - if (rr_dlen < 4) 1.196 - return -1; /* soft */ 1.197 - *ip = *(guint32 *)(resp_pos); 1.198 - DEBUG(5) debugf("DNS: dns_getip(): ip = %s\n", inet_ntoa(*(struct in_addr*)ip)); 1.199 - resp_pos += rr_dlen; 1.200 + if (rr_type == T_A) { 1.201 + if (rr_dlen < 4) 1.202 + return -1; /* soft */ 1.203 + *ip = *(guint32 *) (resp_pos); 1.204 + DEBUG(5) debugf("DNS: dns_getip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip)); 1.205 + resp_pos += rr_dlen; 1.206 1.207 - return 1; 1.208 - } 1.209 - resp_pos += rr_dlen; 1.210 - return 0; 1.211 + return 1; 1.212 + } 1.213 + resp_pos += rr_dlen; 1.214 + return 0; 1.215 } 1.216 1.217 -static 1.218 -int dns_getmx(int *pref) 1.219 +static int 1.220 +dns_getmx(int *pref) 1.221 { 1.222 - int ret; 1.223 + int ret; 1.224 1.225 - if((ret = dns_next())) return ret; 1.226 + if ((ret = dns_next())) 1.227 + return ret; 1.228 1.229 - if (rr_type == T_MX){ 1.230 - if (rr_dlen < 3) 1.231 - return -1; /* soft */ 1.232 + if (rr_type == T_MX) { 1.233 + if (rr_dlen < 3) 1.234 + return -1; /* soft */ 1.235 1.236 - *pref = (resp_pos[0] << 8) + resp_pos[1]; 1.237 - if (dn_expand(response.buf, resp_end, resp_pos + 2, name, MAX_DNSNAME) < 0) 1.238 - return -1; 1.239 - 1.240 - resp_pos += rr_dlen; 1.241 + *pref = (resp_pos[0] << 8) + resp_pos[1]; 1.242 + if (dn_expand(response.buf, resp_end, resp_pos + 2, name, MAX_DNSNAME) < 0) 1.243 + return -1; 1.244 1.245 - return 1; 1.246 - } 1.247 - resp_pos += rr_dlen; 1.248 - return 0; 1.249 + resp_pos += rr_dlen; 1.250 + 1.251 + return 1; 1.252 + } 1.253 + resp_pos += rr_dlen; 1.254 + return 0; 1.255 } 1.256 1.257 /* 1.258 -static 1.259 -int dns_getname(int type) 1.260 +static int 1.261 +dns_getname(int type) 1.262 { 1.263 int ret; 1.264 1.265 @@ -186,7 +190,7 @@ 1.266 if (rr_type == type){ 1.267 if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) 1.268 return -1; 1.269 - 1.270 + 1.271 resp_pos += rr_dlen; 1.272 1.273 return 1; 1.274 @@ -196,181 +200,177 @@ 1.275 } 1.276 */ 1.277 1.278 -static 1.279 -int dns_look_ip(gchar *domain, guint32 *ip) 1.280 +static int 1.281 +dns_look_ip(gchar * domain, guint32 * ip) 1.282 { 1.283 - gchar *n = domain; 1.284 + gchar *n = domain; 1.285 1.286 - while(TRUE){ 1.287 - if(dns_resolve(n, T_A, FALSE) == 0){ 1.288 - dns_next(); 1.289 - if(rr_type == T_A){ 1.290 - if (rr_dlen < 4) 1.291 - return -1; /* soft */ 1.292 - *ip = *(guint32 *)(resp_pos); 1.293 - 1.294 - DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", 1.295 - inet_ntoa(*(struct in_addr*)ip)); 1.296 - 1.297 - resp_pos += rr_dlen; 1.298 - return 0; 1.299 - }else if(rr_type == T_CNAME){ 1.300 - if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) 1.301 - return -1; 1.302 + while (TRUE) { 1.303 + if (dns_resolve(n, T_A, FALSE) == 0) { 1.304 + dns_next(); 1.305 + if (rr_type == T_A) { 1.306 + if (rr_dlen < 4) 1.307 + return -1; /* soft */ 1.308 + *ip = *(guint32 *) (resp_pos); 1.309 1.310 - DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name); 1.311 - 1.312 - resp_pos += rr_dlen; 1.313 - n = name; 1.314 - }else 1.315 - return -1; 1.316 - }else 1.317 - return -1; 1.318 - } 1.319 + DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip)); 1.320 + 1.321 + resp_pos += rr_dlen; 1.322 + return 0; 1.323 + } else if (rr_type == T_CNAME) { 1.324 + if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) 1.325 + return -1; 1.326 + 1.327 + DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name); 1.328 + 1.329 + resp_pos += rr_dlen; 1.330 + n = name; 1.331 + } else 1.332 + return -1; 1.333 + } else 1.334 + return -1; 1.335 + } 1.336 } 1.337 1.338 -GList *resolve_dns_a(GList *list, gchar *domain) 1.339 +GList* 1.340 +resolve_dns_a(GList * list, gchar * domain) 1.341 { 1.342 - int ret; 1.343 + int ret; 1.344 1.345 - DEBUG(5) debugf("DNS: resolve_dns_a entered\n"); 1.346 + DEBUG(5) debugf("DNS: resolve_dns_a entered\n"); 1.347 1.348 - if(dns_resolve(domain, T_A, TRUE) == 0){ 1.349 - mxip_addr mxip; 1.350 - while((ret = dns_getip(&(mxip.ip))) != 2){ 1.351 - if(ret == 1){ 1.352 - mxip.name = g_strdup(name); 1.353 - mxip.pref = 0; 1.354 - list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); 1.355 - } 1.356 - } 1.357 - } 1.358 - return list; 1.359 + if (dns_resolve(domain, T_A, TRUE) == 0) { 1.360 + mxip_addr mxip; 1.361 + while ((ret = dns_getip(&(mxip.ip))) != 2) { 1.362 + if (ret == 1) { 1.363 + mxip.name = g_strdup(name); 1.364 + mxip.pref = 0; 1.365 + list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); 1.366 + } 1.367 + } 1.368 + } 1.369 + return list; 1.370 } 1.371 1.372 -static 1.373 -gint _mx_sort_func(gconstpointer aa, gconstpointer bb) 1.374 +static gint 1.375 +_mx_sort_func(gconstpointer aa, gconstpointer bb) 1.376 { 1.377 - const mxip_addr *a = (mxip_addr *)aa; 1.378 - const mxip_addr *b = (mxip_addr *)bb; 1.379 + const mxip_addr *a = (mxip_addr *) aa; 1.380 + const mxip_addr *b = (mxip_addr *) bb; 1.381 1.382 - if(a->pref == b->pref) 1.383 - return a->ip - b->ip; 1.384 - else 1.385 - return a->pref - b->pref; 1.386 + if (a->pref == b->pref) 1.387 + return a->ip - b->ip; 1.388 + else 1.389 + return a->pref - b->pref; 1.390 } 1.391 1.392 -GList *resolve_dns_mx(GList *list, gchar *domain) 1.393 +GList* 1.394 +resolve_dns_mx(GList * list, gchar * domain) 1.395 { 1.396 - GList *node; 1.397 - int ret; 1.398 - int cnt = 0; 1.399 + GList *node; 1.400 + int ret; 1.401 + int cnt = 0; 1.402 1.403 - DEBUG(5) debugf("DNS: resolve_dns_mx entered\n"); 1.404 + DEBUG(5) debugf("DNS: resolve_dns_mx entered\n"); 1.405 1.406 - if(dns_resolve(domain, T_MX, TRUE) == 0){ 1.407 - GList *node_next; 1.408 - mxip_addr mxip; 1.409 - while((ret = dns_getmx(&(mxip.pref))) != 2){ 1.410 - if(ret == 1){ 1.411 - mxip.name = g_strdup(name); 1.412 - mxip.ip = rand(); 1.413 - list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); 1.414 - cnt++; 1.415 - } 1.416 - } 1.417 + if (dns_resolve(domain, T_MX, TRUE) == 0) { 1.418 + GList *node_next; 1.419 + mxip_addr mxip; 1.420 + while ((ret = dns_getmx(&(mxip.pref))) != 2) { 1.421 + if (ret == 1) { 1.422 + mxip.name = g_strdup(name); 1.423 + mxip.ip = rand(); 1.424 + list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); 1.425 + cnt++; 1.426 + } 1.427 + } 1.428 1.429 - DEBUG(5) debugf("DNS: found %d mx records\n", cnt); 1.430 + DEBUG(5) debugf("DNS: found %d mx records\n", cnt); 1.431 1.432 - /* to randomize sequences with equal pref values, 1.433 - we temporarily 'misused' the ip field and 1.434 - put a random number in it as a secondary sort key. 1.435 - */ 1.436 - list = g_list_sort(list, _mx_sort_func); 1.437 + /* to randomize sequences with equal pref values, 1.438 + we temporarily 'misused' the ip field and 1.439 + put a random number in it as a secondary sort key. 1.440 + */ 1.441 + list = g_list_sort(list, _mx_sort_func); 1.442 1.443 - /* CNAME resolving has to be added as well. */ 1.444 + /* CNAME resolving has to be added as well. */ 1.445 1.446 - for(node = g_list_first(list); 1.447 - node != NULL; 1.448 - node = node_next){ 1.449 + for (node = g_list_first(list); node != NULL; node = node_next) { 1.450 1.451 - mxip_addr *p_mxip = (mxip_addr *)(node->data); 1.452 - node_next = g_list_next(node); 1.453 + mxip_addr *p_mxip = (mxip_addr *) (node->data); 1.454 + node_next = g_list_next(node); 1.455 1.456 - if(dns_look_ip(p_mxip->name, &(p_mxip->ip)) != 0){ 1.457 - DEBUG(1) debugf("DNS: could not resolve target of mx %s\n", p_mxip->name); 1.458 - list = g_list_remove_link(list, node); 1.459 - g_free(node->data); 1.460 - g_list_free_1(node); 1.461 - } 1.462 - } 1.463 - } 1.464 - return list; 1.465 + if (dns_look_ip(p_mxip->name, &(p_mxip->ip)) != 0) { 1.466 + DEBUG(1) debugf("DNS: could not resolve target of mx %s\n", p_mxip->name); 1.467 + list = g_list_remove_link(list, node); 1.468 + g_free(node->data); 1.469 + g_list_free_1(node); 1.470 + } 1.471 + } 1.472 + } 1.473 + return list; 1.474 } 1.475 1.476 #endif 1.477 1.478 /* now something completely different... */ 1.479 1.480 -GList *resolve_byname(GList *list, gchar *domain) 1.481 +GList* 1.482 +resolve_byname(GList * list, gchar * domain) 1.483 { 1.484 - struct hostent *hent; 1.485 + struct hostent *hent; 1.486 1.487 - DEBUG(5) debugf("DNS: resolve_byname entered\n"); 1.488 + DEBUG(5) debugf("DNS: resolve_byname entered\n"); 1.489 1.490 - if((hent = gethostbyname(domain))){ 1.491 - char *haddr; 1.492 - int i = 0; 1.493 - while((haddr = hent->h_addr_list[i++])){ 1.494 - mxip_addr mxip; 1.495 - mxip.ip = *(guint32 *)(haddr); 1.496 - mxip.pref = 0; 1.497 - mxip.name = g_strdup(hent->h_name); 1.498 - list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); 1.499 - } 1.500 - } 1.501 - return list; 1.502 + if ((hent = gethostbyname(domain))) { 1.503 + char *haddr; 1.504 + int i = 0; 1.505 + while ((haddr = hent->h_addr_list[i++])) { 1.506 + mxip_addr mxip; 1.507 + mxip.ip = *(guint32 *) (haddr); 1.508 + mxip.pref = 0; 1.509 + mxip.name = g_strdup(hent->h_name); 1.510 + list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); 1.511 + } 1.512 + } 1.513 + return list; 1.514 } 1.515 1.516 #ifdef RESOLV_TEST 1.517 -int main(int argc, char *argv[]) 1.518 +int 1.519 +main(int argc, char *argv[]) 1.520 { 1.521 - GList *addr_list = NULL, *node; 1.522 + GList *addr_list = NULL, *node; 1.523 1.524 - g_print("starting res_init()\n"); 1.525 + g_print("starting res_init()\n"); 1.526 1.527 - g_print("retrans = %d, retry = %d\n", _res.retrans, _res.retry); 1.528 + g_print("retrans = %d, retry = %d\n", _res.retrans, _res.retry); 1.529 1.530 - if(res_init() == 0){ 1.531 - 1.532 - addr_list = resolve_dns_a(NULL, argv[1]); 1.533 - g_print("A:\n"); 1.534 + if (res_init() == 0) { 1.535 1.536 - foreach(addr_list, node){ 1.537 - mxip_addr *p_mxip = (mxip_addr *)(node->data); 1.538 - 1.539 - printf("name = %s\n IP = %s\n", 1.540 - p_mxip->name, 1.541 - inet_ntoa(*(struct in_addr *)&(p_mxip->ip))); 1.542 - } 1.543 - addr_list = resolve_dns_mx(NULL, argv[1]); 1.544 - g_print("MX:\n"); 1.545 + addr_list = resolve_dns_a(NULL, argv[1]); 1.546 + g_print("A:\n"); 1.547 1.548 - foreach(addr_list, node){ 1.549 - mxip_addr *p_mxip = (mxip_addr *)(node->data); 1.550 - 1.551 - printf("name = %s\n IP = %s pref = %d\n", 1.552 - p_mxip->name, 1.553 - inet_ntoa(*(struct in_addr *)&(p_mxip->ip)), 1.554 - p_mxip->pref); 1.555 - } 1.556 - { 1.557 - guint32 ip; 1.558 - dns_look_ip(argv[1], &ip); 1.559 - printf("dns_look_ip: %s\n", inet_ntoa(*((struct in_addr *)(&ip)))); 1.560 - } 1.561 - }else 1.562 - printf("res_init() failed.\n"); 1.563 - 1.564 + foreach(addr_list, node) { 1.565 + mxip_addr *p_mxip = (mxip_addr *) (node->data); 1.566 + 1.567 + printf("name = %s\n IP = %s\n", p_mxip->name, inet_ntoa(*(struct in_addr *) &(p_mxip->ip))); 1.568 + } 1.569 + addr_list = resolve_dns_mx(NULL, argv[1]); 1.570 + g_print("MX:\n"); 1.571 + 1.572 + foreach(addr_list, node) { 1.573 + mxip_addr *p_mxip = (mxip_addr *) (node->data); 1.574 + 1.575 + printf("name = %s\n IP = %s pref = %d\n", p_mxip->name, inet_ntoa(*(struct in_addr *) &(p_mxip->ip)), p_mxip->pref); 1.576 + } 1.577 + { 1.578 + guint32 ip; 1.579 + dns_look_ip(argv[1], &ip); 1.580 + printf("dns_look_ip: %s\n", inet_ntoa(*((struct in_addr *) (&ip)))); 1.581 + } 1.582 + } else 1.583 + printf("res_init() failed.\n"); 1.584 + 1.585 } 1.586 #endif