X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fgeneral.c;h=ae1f05f96056826a944c7070f6fe74fc6b4c02e8;hb=73eb26037c46bebeaa30ba38eba887216f0e9a6e;hp=fbab698eb39aa26f1104c9dffea2005f530865da;hpb=ea1e31e326a99219a0a6edf28a75845b79b74893;p=adns.git diff --git a/src/general.c b/src/general.c index fbab698..ae1f05f 100644 --- a/src/general.c +++ b/src/general.c @@ -4,7 +4,12 @@ * - vbuf handling */ /* - * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson + * This file is + * Copyright (C) 1997-2000 Ian Jackson + * + * It is part of adns, which is + * Copyright (C) 1997-2000 Ian Jackson + * Copyright (C) 1999 Tony Finch * * 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 @@ -22,8 +27,11 @@ */ #include -#include +#include +#include +#include +#include #include #include "internal.h" @@ -34,33 +42,41 @@ void adns__vdiag(adns_state ads, const char *pfx, adns_initflags prevent, int serv, adns_query qu, const char *fmt, va_list al) { const char *bef, *aft; vbuf vb; - if (!(ads->iflags & adns_if_debug) && (!prevent || (ads->iflags & prevent))) return; - - fprintf(stderr,"adns%s: ",pfx); + + if (!ads->diagfile || + (!(ads->iflags & adns_if_debug) && (!prevent || (ads->iflags & prevent)))) + return; + + if (ads->iflags & adns_if_logpid) { + fprintf(ads->diagfile,"adns%s [%ld]: ",pfx,(long)getpid()); + } else { + fprintf(ads->diagfile,"adns%s: ",pfx); + } - vfprintf(stderr,fmt,al); + vfprintf(ads->diagfile,fmt,al); bef= " ("; aft= "\n"; if (qu && qu->query_dgram) { adns__vbuf_init(&vb); - fprintf(stderr,"%sQNAME=%s, QTYPE=%s", + fprintf(ads->diagfile,"%sQNAME=%s, QTYPE=%s", bef, adns__diag_domain(qu->ads,-1,0, &vb, qu->query_dgram,qu->query_dglen,DNS_HDRSIZE), qu->typei ? qu->typei->rrtname : ""); if (qu->typei && qu->typei->fmtname) - fprintf(stderr,"(%s)",qu->typei->fmtname); + fprintf(ads->diagfile,"(%s)",qu->typei->fmtname); bef=", "; aft=")\n"; + adns__vbuf_free(&vb); } if (serv>=0) { - fprintf(stderr,"%sNS=%s",bef,inet_ntoa(ads->servers[serv].addr)); + fprintf(ads->diagfile,"%sNS=%s",bef,inet_ntoa(ads->servers[serv].addr)); bef=", "; aft=")\n"; } - fputs(aft,stderr); + fputs(aft,ads->diagfile); } void adns__debug(adns_state ads, int serv, adns_query qu, const char *fmt, ...) { @@ -194,16 +210,19 @@ adns_status adns_rr_info(adns_rrtype type, return st; } -#define SINFO(n,s) { adns_s_##n, s } + +#define SINFO(n,s) { adns_s_##n, #n, s } static const struct sinfo { adns_status st; + const char *abbrev; const char *string; } sinfos[]= { SINFO( ok, "OK" ), SINFO( nomemory, "Out of memory" ), SINFO( unknownrrtype, "Query not implemented in DNS library" ), + SINFO( systemfail, "General resolver or system failure" ), SINFO( timeout, "DNS query timed out" ), SINFO( allservfail, "All nameservers failed" ), @@ -218,17 +237,17 @@ static const struct sinfo { SINFO( rcodeunknown, "Nameserver sent unknown response code" ), SINFO( inconsistent, "Inconsistent resource records in DNS" ), - SINFO( prohibitedcname, "DNS data refers to an alias" ), + SINFO( prohibitedcname, "DNS alias found where canonical name wanted" ), SINFO( answerdomaininvalid, "Found syntactically invalid domain name" ), SINFO( answerdomaintoolong, "Found overly-long domain name" ), SINFO( invaliddata, "Found invalid DNS data" ), SINFO( querydomainwrong, "Domain invalid for particular DNS query type" ), SINFO( querydomaininvalid, "Domain name is syntactically invalid" ), - SINFO( querydomaintoolong, "Domain name is too long" ), + SINFO( querydomaintoolong, "Domain name or component is too long" ), SINFO( nxdomain, "No such domain" ), - SINFO( nodata, "No such data" ), + SINFO( nodata, "No such data" ) }; static int si_compar(const void *key, const void *elem) { @@ -238,26 +257,71 @@ static int si_compar(const void *key, const void *elem) { return *st < si->st ? -1 : *st > si->st ? 1 : 0; } +static const struct sinfo *findsinfo(adns_status st) { + return bsearch(&st,sinfos,sizeof(sinfos)/sizeof(*sinfos),sizeof(*sinfos),si_compar); +} + const char *adns_strerror(adns_status st) { - static char buf[100]; + const struct sinfo *si; + + si= findsinfo(st); + return si->string; +} +const char *adns_errabbrev(adns_status st) { const struct sinfo *si; - si= bsearch(&st,sinfos,sizeof(sinfos)/sizeof(*si),sizeof(*si),si_compar); - if (si) return si->string; + si= findsinfo(st); + return si->abbrev; +} + + +#define STINFO(max) { adns_s_max_##max, #max } + +static const struct stinfo { + adns_status stmax; + const char *abbrev; +} stinfos[]= { + { adns_s_ok, "ok" }, + STINFO( localfail ), + STINFO( remotefail ), + STINFO( tempfail ), + STINFO( misconfig ), + STINFO( misquery ), + STINFO( permfail ) +}; + +static int sti_compar(const void *key, const void *elem) { + const adns_status *st= key; + const struct stinfo *sti= elem; + + adns_status here, min, max; + + here= *st; + min= (sti==stinfos) ? 0 : sti[-1].stmax+1; + max= sti->stmax; - snprintf(buf,sizeof(buf),"code %d",st); - return buf; + return here < min ? -1 : here > max ? 1 : 0; } +const char *adns_errtypeabbrev(adns_status st) { + const struct stinfo *sti; + + sti= bsearch(&st,stinfos,sizeof(stinfos)/sizeof(*stinfos),sizeof(*stinfos),sti_compar); + return sti->abbrev; +} + + void adns__isort(void *array, int nobjs, int sz, void *tempbuf, - int (*needswap)(const void *a, const void *b)) { + int (*needswap)(void *context, const void *a, const void *b), + void *context) { byte *data= array; int i, place; for (i=0; i0 && needswap(data + (place-1)*sz, data + i*sz); place--); - + for (place= i; + place>0 && needswap(context, data + (place-1)*sz, data + i*sz); + place--); if (place != i) { memcpy(tempbuf, data + i*sz, sz); memmove(data + (place+1)*sz, data + place*sz, (i-place)*sz); @@ -265,3 +329,32 @@ void adns__isort(void *array, int nobjs, int sz, void *tempbuf, } } } + +/* SIGPIPE protection. */ + +void adns__sigpipe_protect(adns_state ads) { + sigset_t toblock; + struct sigaction sa; + int r; + + if (ads->iflags & adns_if_nosigpipe) return; + + sigfillset(&toblock); + sigdelset(&toblock,SIGPIPE); + + sa.sa_handler= SIG_IGN; + sigfillset(&sa.sa_mask); + sa.sa_flags= 0; + + r= sigprocmask(SIG_SETMASK,&toblock,&ads->stdsigmask); assert(!r); + r= sigaction(SIGPIPE,&sa,&ads->stdsigpipe); assert(!r); +} + +void adns__sigpipe_unprotect(adns_state ads) { + int r; + + if (ads->iflags & adns_if_nosigpipe) return; + + r= sigaction(SIGPIPE,&ads->stdsigpipe,0); assert(!r); + r= sigprocmask(SIG_SETMASK,&ads->stdsigmask,0); assert(!r); +}