Mercurial > masqmail
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 wrap: on
line diff
--- a/src/lookup.c Mon Oct 27 16:21:27 2008 +0100 +++ b/src/lookup.c Mon Oct 27 16:23:10 2008 +0100 @@ -4,7 +4,7 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -33,8 +33,8 @@ #ifdef ENABLE_RESOLVER static union { - HEADER hdr; - unsigned char buf[PACKETSZ]; + HEADER hdr; + unsigned char buf[PACKETSZ]; } response; static unsigned char *resp_end; static unsigned char *resp_pos; @@ -45,139 +45,143 @@ unsigned short rr_type; unsigned short rr_dlen; -static -unsigned short getshort(unsigned char *c) +static unsigned short +getshort(unsigned char *c) { - unsigned short u; - u = c[0]; - return (u << 8) + c[1]; + unsigned short u; + u = c[0]; + return (u << 8) + c[1]; } -static -int dns_resolve(char *domain, int type, gboolean do_search) +static int +dns_resolve(char *domain, int type, gboolean do_search) { - int n; - int i; + int n; + int i; - int resp_len; - /*errno = 0;*/ + int resp_len; + /*errno = 0; */ - /* - if (!stralloc_copy(&glue,domain)) return DNS_MEM; - if (!stralloc_0(&glue)) return DNS_MEM; - */ + /* + if (!stralloc_copy(&glue,domain)) return DNS_MEM; + if (!stralloc_0(&glue)) return DNS_MEM; + */ - // resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response)); - DEBUG(5) debugf("DNS: before res_search()\n"); - if(do_search) - resp_len = res_search(domain, C_IN, type, response.buf, sizeof(response)); - else - resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response)); - DEBUG(5) debugf("DBG: after res_search()\n"); + // resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response)); + DEBUG(5) debugf("DNS: before res_search()\n"); + if (do_search) + resp_len = res_search(domain, C_IN, type, response.buf, sizeof(response)); + else + resp_len = res_query(domain, C_IN, type, response.buf, sizeof(response)); + DEBUG(5) debugf("DBG: after res_search()\n"); - if (resp_len <= 0){ - /* - if (errno == ECONNREFUSED) return DNS_SOFT; - if (h_errno == TRY_AGAIN) return DNS_SOFT; - return DNS_HARD; - */ - return -1; - } - if (resp_len >= sizeof(response)) - resp_len = sizeof(response); + if (resp_len <= 0) { + /* + if (errno == ECONNREFUSED) return DNS_SOFT; + if (h_errno == TRY_AGAIN) return DNS_SOFT; + return DNS_HARD; + */ + return -1; + } + if (resp_len >= sizeof(response)) + resp_len = sizeof(response); - resp_end = response.buf + resp_len; - resp_pos = response.buf + sizeof(HEADER); - n = ntohs(response.hdr.qdcount); + resp_end = response.buf + resp_len; + resp_pos = response.buf + sizeof(HEADER); + n = ntohs(response.hdr.qdcount); - while (n-- > 0){ - i = dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME); - if (i < 0) - return -1; - DEBUG(5) debugf("DBG: resolve name = %s\n", name); - resp_pos += i; - i = resp_end - resp_pos; - if (i < QFIXEDSZ) - return -1; - resp_pos += QFIXEDSZ; - } - num_answers = ntohs(response.hdr.ancount); + while (n-- > 0) { + i = dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME); + if (i < 0) + return -1; + DEBUG(5) debugf("DBG: resolve name = %s\n", name); + resp_pos += i; + i = resp_end - resp_pos; + if (i < QFIXEDSZ) + return -1; + resp_pos += QFIXEDSZ; + } + num_answers = ntohs(response.hdr.ancount); - return 0; + return 0; } -static int dns_next() +static int +dns_next() { - int i; + int i; - if (num_answers <= 0) return 2; - num_answers--; + if (num_answers <= 0) + return 2; + num_answers--; - if (resp_pos == resp_end) - return -1 /* soft */; + if (resp_pos == resp_end) + return -1; /* soft */ - i = dn_expand(response.buf, resp_end, resp_pos, name, 256); - if (i < 0) - return -1; /* soft */ - resp_pos += i; + i = dn_expand(response.buf, resp_end, resp_pos, name, 256); + if (i < 0) + return -1; /* soft */ + resp_pos += i; - i = resp_end - resp_pos; - if (i < 4 + 3 * 2) - return -1; /* soft */ - - rr_type = getshort(resp_pos); - rr_dlen = getshort(resp_pos + 8); - resp_pos += 10; + i = resp_end - resp_pos; + if (i < 4 + 3 * 2) + return -1; /* soft */ - return 0; + rr_type = getshort(resp_pos); + rr_dlen = getshort(resp_pos + 8); + resp_pos += 10; + + return 0; } -static -int dns_getip(guint32 *ip) +static int +dns_getip(guint32 * ip) { - int ret; + int ret; - if((ret = dns_next())) return ret; + if ((ret = dns_next())) + return ret; - if (rr_type == T_A){ - if (rr_dlen < 4) - return -1; /* soft */ - *ip = *(guint32 *)(resp_pos); - DEBUG(5) debugf("DNS: dns_getip(): ip = %s\n", inet_ntoa(*(struct in_addr*)ip)); - resp_pos += rr_dlen; + if (rr_type == T_A) { + if (rr_dlen < 4) + return -1; /* soft */ + *ip = *(guint32 *) (resp_pos); + DEBUG(5) debugf("DNS: dns_getip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip)); + resp_pos += rr_dlen; - return 1; - } - resp_pos += rr_dlen; - return 0; + return 1; + } + resp_pos += rr_dlen; + return 0; } -static -int dns_getmx(int *pref) +static int +dns_getmx(int *pref) { - int ret; + int ret; - if((ret = dns_next())) return ret; - - if (rr_type == T_MX){ - if (rr_dlen < 3) - return -1; /* soft */ + if ((ret = dns_next())) + return ret; - *pref = (resp_pos[0] << 8) + resp_pos[1]; - if (dn_expand(response.buf, resp_end, resp_pos + 2, name, MAX_DNSNAME) < 0) - return -1; - - resp_pos += rr_dlen; + if (rr_type == T_MX) { + if (rr_dlen < 3) + return -1; /* soft */ - return 1; - } - resp_pos += rr_dlen; - return 0; + *pref = (resp_pos[0] << 8) + resp_pos[1]; + if (dn_expand(response.buf, resp_end, resp_pos + 2, name, MAX_DNSNAME) < 0) + return -1; + + resp_pos += rr_dlen; + + return 1; + } + resp_pos += rr_dlen; + return 0; } /* -static -int dns_getname(int type) +static int +dns_getname(int type) { int ret; @@ -186,7 +190,7 @@ if (rr_type == type){ if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) return -1; - + resp_pos += rr_dlen; return 1; @@ -196,181 +200,177 @@ } */ -static -int dns_look_ip(gchar *domain, guint32 *ip) +static int +dns_look_ip(gchar * domain, guint32 * ip) { - gchar *n = domain; + gchar *n = domain; + + while (TRUE) { + if (dns_resolve(n, T_A, FALSE) == 0) { + dns_next(); + if (rr_type == T_A) { + if (rr_dlen < 4) + return -1; /* soft */ + *ip = *(guint32 *) (resp_pos); + + DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", inet_ntoa(*(struct in_addr *) ip)); - while(TRUE){ - if(dns_resolve(n, T_A, FALSE) == 0){ - dns_next(); - if(rr_type == T_A){ - if (rr_dlen < 4) - return -1; /* soft */ - *ip = *(guint32 *)(resp_pos); - - DEBUG(5) debugf("DNS: dns_look_ip(): ip = %s\n", - inet_ntoa(*(struct in_addr*)ip)); - - resp_pos += rr_dlen; - return 0; - }else if(rr_type == T_CNAME){ - if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) - return -1; + resp_pos += rr_dlen; + return 0; + } else if (rr_type == T_CNAME) { + if (dn_expand(response.buf, resp_end, resp_pos, name, MAX_DNSNAME) < 0) + return -1; + + DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name); - DEBUG(5) debugf("DNS: (CNAME) dns_look_ip(): name = %s\n", name); - - resp_pos += rr_dlen; - n = name; - }else - return -1; - }else - return -1; - } + resp_pos += rr_dlen; + n = name; + } else + return -1; + } else + return -1; + } } -GList *resolve_dns_a(GList *list, gchar *domain) +GList* +resolve_dns_a(GList * list, gchar * domain) { - int ret; + int ret; - DEBUG(5) debugf("DNS: resolve_dns_a entered\n"); + DEBUG(5) debugf("DNS: resolve_dns_a entered\n"); - if(dns_resolve(domain, T_A, TRUE) == 0){ - mxip_addr mxip; - while((ret = dns_getip(&(mxip.ip))) != 2){ - if(ret == 1){ - mxip.name = g_strdup(name); - mxip.pref = 0; - list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); - } - } - } - return list; + if (dns_resolve(domain, T_A, TRUE) == 0) { + mxip_addr mxip; + while ((ret = dns_getip(&(mxip.ip))) != 2) { + if (ret == 1) { + mxip.name = g_strdup(name); + mxip.pref = 0; + list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); + } + } + } + return list; } -static -gint _mx_sort_func(gconstpointer aa, gconstpointer bb) +static gint +_mx_sort_func(gconstpointer aa, gconstpointer bb) { - const mxip_addr *a = (mxip_addr *)aa; - const mxip_addr *b = (mxip_addr *)bb; + const mxip_addr *a = (mxip_addr *) aa; + const mxip_addr *b = (mxip_addr *) bb; - if(a->pref == b->pref) - return a->ip - b->ip; - else - return a->pref - b->pref; + if (a->pref == b->pref) + return a->ip - b->ip; + else + return a->pref - b->pref; } -GList *resolve_dns_mx(GList *list, gchar *domain) +GList* +resolve_dns_mx(GList * list, gchar * domain) { - GList *node; - int ret; - int cnt = 0; + GList *node; + int ret; + int cnt = 0; - DEBUG(5) debugf("DNS: resolve_dns_mx entered\n"); + DEBUG(5) debugf("DNS: resolve_dns_mx entered\n"); - if(dns_resolve(domain, T_MX, TRUE) == 0){ - GList *node_next; - mxip_addr mxip; - while((ret = dns_getmx(&(mxip.pref))) != 2){ - if(ret == 1){ - mxip.name = g_strdup(name); - mxip.ip = rand(); - list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); - cnt++; - } - } + if (dns_resolve(domain, T_MX, TRUE) == 0) { + GList *node_next; + mxip_addr mxip; + while ((ret = dns_getmx(&(mxip.pref))) != 2) { + if (ret == 1) { + mxip.name = g_strdup(name); + mxip.ip = rand(); + list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); + cnt++; + } + } - DEBUG(5) debugf("DNS: found %d mx records\n", cnt); + DEBUG(5) debugf("DNS: found %d mx records\n", cnt); - /* to randomize sequences with equal pref values, - we temporarily 'misused' the ip field and - put a random number in it as a secondary sort key. - */ - list = g_list_sort(list, _mx_sort_func); + /* to randomize sequences with equal pref values, + we temporarily 'misused' the ip field and + put a random number in it as a secondary sort key. + */ + list = g_list_sort(list, _mx_sort_func); - /* CNAME resolving has to be added as well. */ + /* CNAME resolving has to be added as well. */ - for(node = g_list_first(list); - node != NULL; - node = node_next){ + for (node = g_list_first(list); node != NULL; node = node_next) { - mxip_addr *p_mxip = (mxip_addr *)(node->data); - node_next = g_list_next(node); + mxip_addr *p_mxip = (mxip_addr *) (node->data); + node_next = g_list_next(node); - if(dns_look_ip(p_mxip->name, &(p_mxip->ip)) != 0){ - DEBUG(1) debugf("DNS: could not resolve target of mx %s\n", p_mxip->name); - list = g_list_remove_link(list, node); - g_free(node->data); - g_list_free_1(node); - } - } - } - return list; + if (dns_look_ip(p_mxip->name, &(p_mxip->ip)) != 0) { + DEBUG(1) debugf("DNS: could not resolve target of mx %s\n", p_mxip->name); + list = g_list_remove_link(list, node); + g_free(node->data); + g_list_free_1(node); + } + } + } + return list; } #endif /* now something completely different... */ -GList *resolve_byname(GList *list, gchar *domain) +GList* +resolve_byname(GList * list, gchar * domain) { - struct hostent *hent; + struct hostent *hent; - DEBUG(5) debugf("DNS: resolve_byname entered\n"); + DEBUG(5) debugf("DNS: resolve_byname entered\n"); - if((hent = gethostbyname(domain))){ - char *haddr; - int i = 0; - while((haddr = hent->h_addr_list[i++])){ - mxip_addr mxip; - mxip.ip = *(guint32 *)(haddr); - mxip.pref = 0; - mxip.name = g_strdup(hent->h_name); - list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); - } - } - return list; + if ((hent = gethostbyname(domain))) { + char *haddr; + int i = 0; + while ((haddr = hent->h_addr_list[i++])) { + mxip_addr mxip; + mxip.ip = *(guint32 *) (haddr); + mxip.pref = 0; + mxip.name = g_strdup(hent->h_name); + list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); + } + } + return list; } #ifdef RESOLV_TEST -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { - GList *addr_list = NULL, *node; + GList *addr_list = NULL, *node; - g_print("starting res_init()\n"); + g_print("starting res_init()\n"); - g_print("retrans = %d, retry = %d\n", _res.retrans, _res.retry); + g_print("retrans = %d, retry = %d\n", _res.retrans, _res.retry); + + if (res_init() == 0) { - if(res_init() == 0){ - - addr_list = resolve_dns_a(NULL, argv[1]); - g_print("A:\n"); + addr_list = resolve_dns_a(NULL, argv[1]); + g_print("A:\n"); + + foreach(addr_list, node) { + mxip_addr *p_mxip = (mxip_addr *) (node->data); - foreach(addr_list, node){ - mxip_addr *p_mxip = (mxip_addr *)(node->data); - - printf("name = %s\n IP = %s\n", - p_mxip->name, - inet_ntoa(*(struct in_addr *)&(p_mxip->ip))); - } - addr_list = resolve_dns_mx(NULL, argv[1]); - g_print("MX:\n"); + printf("name = %s\n IP = %s\n", p_mxip->name, inet_ntoa(*(struct in_addr *) &(p_mxip->ip))); + } + addr_list = resolve_dns_mx(NULL, argv[1]); + g_print("MX:\n"); + + foreach(addr_list, node) { + mxip_addr *p_mxip = (mxip_addr *) (node->data); - foreach(addr_list, node){ - mxip_addr *p_mxip = (mxip_addr *)(node->data); - - printf("name = %s\n IP = %s pref = %d\n", - p_mxip->name, - inet_ntoa(*(struct in_addr *)&(p_mxip->ip)), - p_mxip->pref); - } - { - guint32 ip; - dns_look_ip(argv[1], &ip); - printf("dns_look_ip: %s\n", inet_ntoa(*((struct in_addr *)(&ip)))); - } - }else - printf("res_init() failed.\n"); - + printf("name = %s\n IP = %s pref = %d\n", p_mxip->name, inet_ntoa(*(struct in_addr *) &(p_mxip->ip)), p_mxip->pref); + } + { + guint32 ip; + dns_look_ip(argv[1], &ip); + printf("dns_look_ip: %s\n", inet_ntoa(*((struct in_addr *) (&ip)))); + } + } else + printf("res_init() failed.\n"); + } #endif