2ced4c06b6c35a390bdc49924613730b58b46664
3 * - diagnostic functions
7 * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <arpa/inet.h>
30 /* Core diagnostic functions */
32 void adns__vdiag(adns_state ads
, const char *pfx
, adns_initflags prevent
,
33 int serv
, adns_query qu
, const char *fmt
, va_list al
) {
34 const char *bef
, *aft
;
36 if (!(ads
->iflags
& adns_if_debug
) && (!prevent
|| (ads
->iflags
& prevent
))) return;
38 fprintf(stderr
,"adns%s: ",pfx
);
40 vfprintf(stderr
,fmt
,al
);
45 if (qu
&& qu
->query_dgram
) {
47 fprintf(stderr
,"%sQNAME=%s, QTYPE=%s",
49 adns__diag_domain(qu
->ads
,-1,0, &vb
,qu
->flags
,
50 qu
->query_dgram
,qu
->query_dglen
,DNS_HDRSIZE
),
51 qu
->typei ? qu
->typei
->name
: "<unknown>");
56 fprintf(stderr
,"%sNS=%s",bef
,inet_ntoa(ads
->servers
[serv
].addr
));
63 void adns__debug(adns_state ads
, int serv
, adns_query qu
, const char *fmt
, ...) {
67 adns__vdiag(ads
," debug",0,serv
,qu
,fmt
,al
);
71 void adns__warn(adns_state ads
, int serv
, adns_query qu
, const char *fmt
, ...) {
75 adns__vdiag(ads
," warning",adns_if_noerrprint
|adns_if_noserverwarn
,serv
,qu
,fmt
,al
);
79 void adns__diag(adns_state ads
, int serv
, adns_query qu
, const char *fmt
, ...) {
83 adns__vdiag(ads
,"",adns_if_noerrprint
,serv
,qu
,fmt
,al
);
89 void adns__vbuf_init(vbuf
*vb
) {
90 vb
->used
= vb
->avail
= 0; vb
->buf
= 0;
93 int adns__vbuf_ensure(vbuf
*vb
, int want
) {
96 if (vb
->avail
>= want
) return 1;
97 nb
= realloc(vb
->buf
,want
); if (!nb
) return 0;
103 void adns__vbuf_appendq(vbuf
*vb
, const byte
*data
, int len
) {
104 memcpy(vb
->buf
+vb
->used
,data
,len
);
108 int adns__vbuf_append(vbuf
*vb
, const byte
*data
, int len
) {
112 newlen
= vb
->used
+len
;
113 if (vb
->avail
< newlen
) {
115 nb
= realloc(vb
->buf
,newlen
);
116 if (!nb
) { newlen
>>= 1; nb
= realloc(vb
->buf
,newlen
); }
121 adns__vbuf_appendq(vb
,data
,len
);
125 /* Additional diagnostic functions */
127 const char *adns__diag_domain(adns_state ads
, int serv
, adns_query qu
, vbuf
*vb
,
128 int flags
, const byte
*dgram
, int dglen
, int cbyte
) {
131 st
= adns__parse_domain(ads
,serv
,qu
,vb
, flags
,dgram
,dglen
,&cbyte
,dglen
);
132 if (st
== adns_s_nolocalmem
) {
133 return "<cannot report domain... out of memory>";
137 if (!(adns__vbuf_appendstr(vb
,"<bad format... ") &&
138 adns__vbuf_appendstr(vb
,adns_strerror(st
)) &&
139 adns__vbuf_appendstr(vb
,">") &&
140 adns__vbuf_append(vb
,"",1))) {
141 return "<cannot report bad format... out of memory>";
145 adns__vbuf_appendstr(vb
,"<truncated ...>");
146 adns__vbuf_append(vb
,"",1);
151 const char *adns_strerror(adns_status st
) {
152 static char buf
[100];
153 snprintf(buf
,sizeof(buf
),"code %d",st
);