"Fossies" - the Fresh Open Source Software Archive

Member "udns-0.4/udns_rr_a.c" (5 Jul 2011, 3738 Bytes) of package /linux/misc/dns/udns-0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "udns_rr_a.c" see the Fossies "Dox" file reference documentation.

    1 /* udns_rr_a.c
    2    parse/query A/AAAA 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 #ifndef WINDOWS
   28 # include <sys/types.h>
   29 # include <netinet/in.h>
   30 #endif
   31 #include "udns.h"
   32 
   33 /* here, we use common routine to parse both IPv4 and IPv6 addresses.
   34  */
   35 
   36 /* this structure should match dns_rr_a[46] */
   37 struct dns_rr_a {
   38   dns_rr_common(dnsa);
   39   unsigned char *dnsa_addr;
   40 };
   41 
   42 static int
   43 dns_parse_a(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
   44             void **result, unsigned dsize) {
   45   struct dns_rr_a *ret;
   46   struct dns_parse p;
   47   struct dns_rr rr;
   48   int r;
   49 
   50   /* first, validate and count number of addresses */
   51   dns_initparse(&p, qdn, pkt, cur, end);
   52   while((r = dns_nextrr(&p, &rr)) > 0)
   53     if (rr.dnsrr_dsz != dsize)
   54       return DNS_E_PROTOCOL;
   55   if (r < 0)
   56     return DNS_E_PROTOCOL;
   57   else if (!p.dnsp_nrr)
   58     return DNS_E_NODATA;
   59 
   60   ret = malloc(sizeof(*ret) + dsize * p.dnsp_nrr + dns_stdrr_size(&p));
   61   if (!ret)
   62     return DNS_E_NOMEM;
   63 
   64   ret->dnsa_nrr = p.dnsp_nrr;
   65   ret->dnsa_addr = (unsigned char*)(ret+1);
   66 
   67   /* copy the RRs */
   68   for (dns_rewind(&p, qdn), r = 0; dns_nextrr(&p, &rr); ++r)
   69     memcpy(ret->dnsa_addr + dsize * r, rr.dnsrr_dptr, dsize);
   70 
   71   dns_stdrr_finish((struct dns_rr_null *)ret,
   72                    (char *)(ret->dnsa_addr + dsize * p.dnsp_nrr), &p);
   73   *result = ret;
   74   return 0;
   75 }
   76 
   77 int
   78 dns_parse_a4(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
   79              void **result) {
   80 #ifdef AF_INET
   81   assert(sizeof(struct in_addr) == 4);
   82 #endif
   83   assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_A);
   84   return dns_parse_a(qdn, pkt, cur, end, result, 4);
   85 }
   86 
   87 struct dns_query *
   88 dns_submit_a4(struct dns_ctx *ctx, const char *name, int flags,
   89               dns_query_a4_fn *cbck, void *data) {
   90   return
   91     dns_submit_p(ctx, name, DNS_C_IN, DNS_T_A, flags,
   92                  dns_parse_a4, (dns_query_fn*)cbck, data);
   93 }
   94 
   95 struct dns_rr_a4 *
   96 dns_resolve_a4(struct dns_ctx *ctx, const char *name, int flags) {
   97   return (struct dns_rr_a4 *)
   98     dns_resolve_p(ctx, name, DNS_C_IN, DNS_T_A, flags, dns_parse_a4);
   99 }
  100 
  101 int
  102 dns_parse_a6(dnscc_t *qdn, dnscc_t *pkt, dnscc_t *cur, dnscc_t *end,
  103              void **result) {
  104 #ifdef AF_INET6
  105   assert(sizeof(struct in6_addr) == 16);
  106 #endif
  107   assert(dns_get16(cur+2) == DNS_C_IN && dns_get16(cur+0) == DNS_T_AAAA);
  108   return dns_parse_a(qdn, pkt, cur, end, result, 16);
  109 }
  110 
  111 struct dns_query *
  112 dns_submit_a6(struct dns_ctx *ctx, const char *name, int flags,
  113               dns_query_a6_fn *cbck, void *data) {
  114   return
  115     dns_submit_p(ctx, name, DNS_C_IN, DNS_T_AAAA, flags,
  116                  dns_parse_a6, (dns_query_fn*)cbck, data);
  117 }
  118 
  119 struct dns_rr_a6 *
  120 dns_resolve_a6(struct dns_ctx *ctx, const char *name, int flags) {
  121   return (struct dns_rr_a6 *)
  122     dns_resolve_p(ctx, name, DNS_C_IN, DNS_T_AAAA, flags, dns_parse_a6);
  123 }