/*
* types.c
* - RR-type-specific code, and the machinery to call it
*/
/*
* This file is
* Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
*
* It is part of adns, which is
* Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
* Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "internal.h"
#define R_NOMEM return adns_s_nomemory
#define CSP_ADDSTR(s) do { if (!adns__vbuf_appendstr(vb,(s))) R_NOMEM; } while (0)
/*
* order of sections:
*
* _string (pap)
* _textdata, _qstring (csp)
* _str (mf,cs)
* _intstr (mf,csp,cs)
* _manyistr (mf,cs)
* _txt (pa)
* _inaddr (pa,dip,di)
* _addr (pa,di,csp,cs)
* _domain (pap)
* _host_raw (pa)
* _hostaddr (pap,pa,dip,di,mfp,mf,csp,cs +pap_findaddrs)
* _mx_raw (pa,di)
* _mx (pa,di)
* _inthostaddr (mf,cs)
* _ptr (pa)
* _strpair (mf,cs)
* _intstrpair (mf,cs)
* _hinfo (pa)
* _mailbox (pap +pap_mailbox822)
* _rp (pa)
* _soa (pa,mf,cs)
* _flat (mf)
*
* within each section:
* pap_*
* pa_*
* dip_*
* di_*
* mfp_*
* mf_*
* csp_*
* cs_*
*/
/*
* _qstring (pap,csp)
*/
static adns_status pap_qstring(const parseinfo *pai, int *cbyte_io, int max,
int *len_r, char **str_r) {
/* Neither len_r nor str_r may be null.
* End of datagram (overrun) is indicated by returning adns_s_invaliddata;
*/
const byte *dgram= pai->dgram;
int l, cbyte;
char *str;
cbyte= *cbyte_io;
if (cbyte >= max) return adns_s_invaliddata;
GET_B(cbyte,l);
if (cbyte+l > max) return adns_s_invaliddata;
str= adns__alloc_interim(pai->qu, l+1);
if (!str) R_NOMEM;
str[l]= 0;
memcpy(str,dgram+cbyte,l);
*len_r= l;
*str_r= str;
*cbyte_io= cbyte+l;
return adns_s_ok;
}
static adns_status csp_qstring(vbuf *vb, const char *dp, int len) {
unsigned char ch;
char buf[10];
int cn;
CSP_ADDSTR("\"");
for (cn=0; cn<len; cn++) {
ch= *dp++;
if (ch == '\\') {
CSP_ADDSTR("\\\\");
} else if (ch == '"') {
CSP_ADDSTR("\\\"");
} else if (ch >= 32 && ch <= 126) {
if (!adns__vbuf_append(vb,&ch,1)) R_NOMEM;
} else {
sprintf(buf,"\\x%02x",ch);
CSP_ADDSTR(buf);
}
}
CSP_ADDSTR("\"");
return adns_s_ok;
}
/*
* _str (mf)
*/
static void mf_str(adns_query qu, void *datap) {
char **rrp= datap;
adns__makefinal_str(qu,rrp);
}
/*
* _intstr (mf)
*/
static void mf_intstr(adns_query qu, void *datap) {
adns_rr_intstr *rrp= datap;
adns__makefinal_str(qu,&rrp->str);
}
/*
* _manyistr (mf)
*/
static void mf_manyistr(adns_query qu, void *datap) {
adns_rr_intstr **rrp= datap;
adns_rr_intstr *te, *table;
void *tablev;
int tc;
for (tc=0, te= *rrp; te->i >= 0; te++, tc++);
tablev= *rrp;
adns__makefinal_block(qu,&tablev,sizeof(*te)*(tc+1));
*rrp= table= tablev;
for (te= *rrp; te->i >= 0; te++)
adns__makefinal_str(qu,&te->str);
}
/*
* _txt (pa,cs)
*/
static adns_status pa_txt(const parseinfo *pai, int cbyte, int max, void *datap) {
adns_rr_intstr **rrp= datap, *table, *te;
const byte *dgram= pai->dgram;
int ti, tc, l, startbyte;
adns_status st;
startbyte= cbyte;
if (cbyte >= max) return adns_s_invaliddata;
tc= 0;
while (cbyte < max) {
GET_B(cbyte,l);
cbyte+= l;
tc++;
}
if (cbyte != max || !tc) return adns_s_invaliddata;
table= adns__alloc_interim(pai->qu,sizeof(*table)*(tc+1));
if (!table) R_NOMEM;
for (cbyte=startbyte, ti=0, te=table; ti<tc; ti++, te++) {
st= pap_qstring(pai, &cbyte, max, &te->i, &te->str);
if (st) return st;
}
assert(cbyte == max);
te->i= -1;
te->str= 0;
*rrp= table;
return adns_s_ok;
}
static adns_status cs_txt(vbuf *vb, const void *datap) {
const adns_rr_intstr *const *rrp= datap;
const adns_rr_intstr *current;
adns_status st;
int spc;
for (current= *rrp, spc=0; current->i >= 0; current++, spc=1) {
if (spc) CSP_ADDSTR(" ");
st= csp_qstring(vb,current->str,current->i); if (st) return st;
}
return adns_s_ok;
}
/*
* _hinfo (cs)
*/
static adns_status cs_hinfo(vbuf *vb, const void *datap) {
const adns_rr_intstrpair *rrp= datap;
adns_status st;
st= csp_qstring(vb,rrp->array[0].str,rrp->array[0].i); if (st) return st;
CSP_ADDSTR(" ");
st= csp_qstring(vb,rrp->array[1].str,rrp->array[1].i); if (st) return st;
return adns_s_ok;
}
/*
* _inaddr (pa,dip,di)
*/
static adns_status pa_inaddr(const parseinfo *pai, int cbyte, int max, void *datap) {
struct in_addr *storeto= datap;
if (max-cbyte != 4) return adns_s_invaliddata;
memcpy(storeto, pai->dgram + cbyte, 4);
return adns_s_ok;
}
static int search_sortlist(adns_state ads, struct in_addr ad) {
const struct sortlist *slp;
int i;
for (i=0, slp=ads->sortlist;
i<ads->nsortlist && !((ad.s_addr & slp->mask.s_addr) == slp->base.s_addr);
i++, slp++);
return i;
}
static int dip_inaddr(adns_state ads, struct in_addr a, struct in_addr b) {
int ai, bi;
if (!ads->nsortlist) return 0;
ai= search_sortlist(ads,a);
bi= search_sortlist(ads,b);
return bi<ai;
}
static int di_inaddr(adns_state ads, const void *datap_a, const void *datap_b) {
const struct in_addr *ap= datap_a, *bp= datap_b;
return dip_inaddr(ads,*ap,*bp);
}
static adns_status cs_inaddr(vbuf *vb, const void *datap) {
const struct in_addr *rrp= datap, rr= *rrp;
const char *ia;
ia= inet_ntoa(rr); assert(ia);
CSP_ADDSTR(ia);
return adns_s_ok;
}
/*
* _addr (pa,di,csp,cs)
*/
static adns_status pa_addr(const parseinfo *pai, int cbyte, int max, void *datap) {
adns_rr_addr *storeto= datap;
const byte *dgram= pai->dgram;
if (max-cbyte != 4) return adns_s_invaliddata;
storeto->len= sizeof(storeto->addr.inet);
memset(&storeto->addr,0,sizeof(storeto->addr.inet));
storeto->addr.inet.sin_family= AF_INET;
memcpy(&storeto->addr.inet.sin_addr,dgram+cbyte,4);
return adns_s_ok;
}
static int di_addr(adns_state ads, const void *datap_a, const void *datap_b) {
const adns_rr_addr *ap= datap_a, *bp= datap_b;
assert(ap->addr.sa.sa_family == AF_INET);
return dip_inaddr(ads, ap->addr.inet.sin_addr, bp->addr.inet.sin_addr);
}
static int div_addr(void *context, const void *datap_a, const void *datap_b) {
const adns_state ads= context;
return di_addr(ads, datap_a, datap_b);
}
static adns_status csp_addr(vbuf *vb, const adns_rr_addr *rrp) {
const char *ia;
static char buf[30];
switch (rrp->addr.inet.sin_family) {
case AF_INET:
CSP_ADDSTR("INET ");
ia= inet_ntoa(rrp->addr.inet.sin_addr); assert(ia);
CSP_ADDSTR(ia);
break;
default:
sprintf(buf,"AF=%u",rrp->addr.sa.sa_family);
CSP_ADDSTR(buf);
break;
}
return adns_s_ok