| 1 |
/* $Id: udns_rr_mx.c,v 1.13 2005/04/20 06:44:34 mjt Exp $ |
|---|
| 2 |
parse/query MX IN records |
|---|
| 3 |
|
|---|
| 4 |
Copyright (C) 2005 Michael Tokarev <mjt@corpit.ru> |
|---|
| 5 |
This file is part of UDNS library, an async DNS stub resolver. |
|---|
| 6 |
|
|---|
| 7 |
This library is free software; you can redistribute it and/or |
|---|
| 8 |
modify it under the terms of the GNU Lesser General Public |
|---|
| 9 |
License as published by the Free Software Foundation; either |
|---|
| 10 |
version 2.1 of the License, or (at your option) any later version. |
|---|
| 11 |
|
|---|
| 12 |
This library is distributed in the hope that it will be useful, |
|---|
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 15 |
Lesser General Public License for more details. |
|---|
| 16 |
|
|---|
| 17 |
You should have received a copy of the GNU Lesser General Public |
|---|
| 18 |
License along with this library, in file named COPYING.LGPL; if not, |
|---|
| 19 |
write to the Free Software Foundation, Inc., 59 Temple Place, |
|---|
| 20 |
Suite 330, Boston, MA 02111-1307 USA |
|---|
| 21 |
|
|---|
| 22 |
*/ |
|---|
| 23 |
|
|---|
| 24 |
#include <string.h> |
|---|
| 25 |
#include <stdlib.h> |
|---|
| 26 |
#include <assert.h> |
|---|
| 27 |
#include "udns.h" |
|---|
| 28 |
|
|---|
| 29 |
int |
|---|
| 30 |
dns_parse_mx(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end, |
|---|
| 31 |
void **result) { |
|---|
| 32 |
struct dns_rr_mx *ret; |
|---|
| 33 |
struct dns_parse p; |
|---|
| 34 |
struct dns_rr rr; |
|---|
| 35 |
int r, l; |
|---|
| 36 |
char *sp; |
|---|
| 37 |
dnsc_t mx[DNS_MAXDN]; |
|---|
| 38 |
|
|---|
| 39 |
assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_MX); |
|---|
| 40 |
|
|---|
| 41 |
/* first, validate the answer and count size of the result */ |
|---|
| 42 |
l = 0; |
|---|
| 43 |
dns_initparse(&p, qdn, pkt, cur, end); |
|---|
| 44 |
while((r = dns_nextrr(&p, &rr)) > 0) { |
|---|
| 45 |
cur = rr.dnsrr_dptr + 2; |
|---|
| 46 |
r = dns_getdn(pkt, &cur, end, mx, sizeof(mx)); |
|---|
| 47 |
if (r <= 0 || cur != rr.dnsrr_dend) |
|---|
| 48 |
return DNS_E_PROTOCOL; |
|---|
| 49 |
l += dns_dntop_size(mx); |
|---|
| 50 |
} |
|---|
| 51 |
if (r < 0) |
|---|
| 52 |
return DNS_E_PROTOCOL; |
|---|
| 53 |
if (!p.dnsp_nrr) |
|---|
| 54 |
return DNS_E_NODATA; |
|---|
| 55 |
|
|---|
| 56 |
/* next, allocate and set up result */ |
|---|
| 57 |
l += dns_stdrr_size(&p); |
|---|
| 58 |
ret = malloc(sizeof(*ret) + sizeof(struct dns_mx) * p.dnsp_nrr + l); |
|---|
| 59 |
if (!ret) |
|---|
| 60 |
return DNS_E_NOMEM; |
|---|
| 61 |
ret->dnsmx_nrr = p.dnsp_nrr; |
|---|
| 62 |
ret->dnsmx_mx = (struct dns_mx *)(ret+1); |
|---|
| 63 |
|
|---|
| 64 |
/* and 3rd, fill in result, finally */ |
|---|
| 65 |
sp = (char*)(ret->dnsmx_mx + p.dnsp_nrr); |
|---|
| 66 |
for (dns_rewind(&p, qdn), r = 0; dns_nextrr(&p, &rr); ++r) { |
|---|
| 67 |
ret->dnsmx_mx[r].name = sp; |
|---|
| 68 |
cur = rr.dnsrr_dptr; |
|---|
| 69 |
ret->dnsmx_mx[r].priority = dns_get16(cur); |
|---|
| 70 |
cur += 2; |
|---|
| 71 |
dns_getdn(pkt, &cur, end, mx, sizeof(mx)); |
|---|
| 72 |
sp += dns_dntop(mx, sp, DNS_MAXNAME); |
|---|
| 73 |
} |
|---|
| 74 |
dns_stdrr_finish((struct dns_rr_null *)ret, sp, &p); |
|---|
| 75 |
*result = ret; |
|---|
| 76 |
return 0; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
struct dns_query * |
|---|
| 80 |
dns_submit_mx(struct dns_ctx *ctx, const char *name, int flags, |
|---|
| 81 |
dns_query_mx_fn *cbck, void *data) { |
|---|
| 82 |
return |
|---|
| 83 |
dns_submit_p(ctx, name, DNS_C_IN, DNS_T_MX, flags, |
|---|
| 84 |
dns_parse_mx, (dns_query_fn *)cbck, data); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
struct dns_rr_mx * |
|---|
| 88 |
dns_resolve_mx(struct dns_ctx *ctx, const char *name, int flags) { |
|---|
| 89 |
return (struct dns_rr_mx *) |
|---|
| 90 |
dns_resolve_p(ctx, name, DNS_C_IN, DNS_T_MX, flags, dns_parse_mx); |
|---|
| 91 |
} |
|---|