ReactOS 0.4.15-dev-8145-ga541a46
general.c File Reference
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "internal.h"
Include dependency graph for general.c:

Go to the source code of this file.

Classes

struct  sinfo
 
struct  stinfo
 

Macros

#define SINFO(n, s)   { adns_s_##n, #n, s }
 
#define STINFO(max)   { adns_s_max_##max, #max }
 

Functions

void adns__vdiag (adns_state ads, const char *pfx, adns_initflags prevent, int serv, adns_query qu, const char *fmt, va_list al)
 
void adns__debug (adns_state ads, int serv, adns_query qu, const char *fmt,...)
 
void adns__warn (adns_state ads, int serv, adns_query qu, const char *fmt,...)
 
void adns__diag (adns_state ads, int serv, adns_query qu, const char *fmt,...)
 
void adns__vbuf_init (vbuf *vb)
 
int adns__vbuf_ensure (vbuf *vb, int want)
 
void adns__vbuf_appendq (vbuf *vb, const byte *data, int len)
 
int adns__vbuf_append (vbuf *vb, const byte *data, int len)
 
int adns__vbuf_appendstr (vbuf *vb, const char *data)
 
void adns__vbuf_free (vbuf *vb)
 
const charadns__diag_domain (adns_state ads, int serv, adns_query qu, vbuf *vb, const byte *dgram, int dglen, int cbyte)
 
adns_status adns_rr_info (adns_rrtype type, const char **rrtname_r, const char **fmtname_r, int *len_r, const void *datap, char **data_r)
 
static int si_compar (const void *key, const void *elem)
 
static const struct sinfofindsinfo (adns_status st)
 
const charadns_strerror (adns_status st)
 
const charadns_errabbrev (adns_status st)
 
static int sti_compar (const void *key, const void *elem)
 
const charadns_errtypeabbrev (adns_status st)
 
void adns__isort (void *array, int nobjs, int sz, void *tempbuf, int(*needswap)(void *context, const void *a, const void *b), void *context)
 
void adns__sigpipe_protect (adns_state ads)
 
void adns__sigpipe_unprotect (adns_state ads)
 

Variables

static const struct sinfo sinfos []
 
static const struct stinfo stinfos []
 

Macro Definition Documentation

◆ SINFO

#define SINFO (   n,
  s 
)    { adns_s_##n, #n, s }

Definition at line 218 of file general.c.

◆ STINFO

#define STINFO (   max)    { adns_s_max_##max, #max }

Definition at line 283 of file general.c.

Function Documentation

◆ adns__debug()

void adns__debug ( adns_state  ads,
int  serv,
adns_query  qu,
const char fmt,
  ... 
)

Definition at line 86 of file general.c.

86 {
87 va_list al;
88
89 va_start(al,fmt);
90 adns__vdiag(ads," debug",0,serv,qu,fmt,al);
91 va_end(al);
92}
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
adns_state ads
Definition: adh-query.c:35
void adns__vdiag(adns_state ads, const char *pfx, adns_initflags prevent, int serv, adns_query qu, const char *fmt, va_list al)
Definition: general.c:45
Definition: dsound.c:943

Referenced by addserver(), adns__procdgram(), ccf_nameserver(), instrum_getenv(), readconfig(), readconfigenv(), readconfigenvtext(), and tcp_connected().

◆ adns__diag()

void adns__diag ( adns_state  ads,
int  serv,
adns_query  qu,
const char fmt,
  ... 
)

◆ adns__diag_domain()

const char * adns__diag_domain ( adns_state  ads,
int  serv,
adns_query  qu,
vbuf vb,
const byte dgram,
int  dglen,
int  cbyte 
)

Definition at line 162 of file general.c.

163 {
164 adns_status st;
165
166 st= adns__parse_domain(ads,serv,qu,vb, pdf_quoteok, dgram,dglen,&cbyte,dglen);
167 if (st == adns_s_nomemory) {
168 return "<cannot report domain... out of memory>";
169 }
170 if (st) {
171 vb->used= 0;
172 if (!(adns__vbuf_appendstr(vb,"<bad format... ") &&
174 adns__vbuf_appendstr(vb,">") &&
175 adns__vbuf_append(vb,(byte*)"",1))) {
176 return "<cannot report bad format... out of memory>";
177 }
178 }
179 if (!vb->used) {
180 adns__vbuf_appendstr(vb,"<truncated ...>");
181 adns__vbuf_append(vb,(byte*)"",1);
182 }
183 return (char*)vb->buf;
184}
adns_status
Definition: adns.h:215
@ adns_s_nomemory
Definition: adns.h:219
static int cbyte
Definition: adnsresfilter.c:73
const char * adns_strerror(adns_status st)
Definition: general.c:268
int adns__vbuf_append(vbuf *vb, const byte *data, int len)
Definition: general.c:131
int adns__vbuf_appendstr(vbuf *vb, const char *data)
Definition: general.c:149
adns_status adns__parse_domain(adns_state ads, int serv, adns_query qu, vbuf *vb, adns_queryflags flags, const byte *dgram, int dglen, int *cbyte_io, int max)
Definition: parse.c:117
@ pdf_quoteok
Definition: internal.h:578
int used
Definition: internal.h:111
byte * buf
Definition: internal.h:112

Referenced by adns__procdgram(), and adns__vdiag().

◆ adns__isort()

void adns__isort ( void array,
int  nobjs,
int  sz,
void tempbuf,
int(*)(void *context, const void *a, const void *b needswap,
void context 
)

Definition at line 319 of file general.c.

321 {
322 byte *data= array;
323 int i, place;
324
325 for (i=0; i<nobjs; i++) {
326 for (place= i;
327 place>0 && needswap(context, data + (place-1)*sz, data + i*sz);
328 place--);
329 if (place != i) {
330 memcpy(tempbuf, data + i*sz, (size_t) sz);
331 memmove(data + (place+1)*sz, data + place*sz, (size_t) (i-place)*sz);
332 memcpy(data + place*sz, tempbuf, (size_t) sz);
333 }
334 }
335}
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
static char * place
Definition: getopt.c:55
Definition: http.c:7252

Referenced by adns__query_done(), and pap_findaddrs().

◆ adns__sigpipe_protect()

void adns__sigpipe_protect ( adns_state  ads)

Definition at line 340 of file general.c.

340 {
341#ifndef ADNS_JGAA_WIN32
342 sigset_t toblock;
343 struct sigaction sa;
344 int r;
345
346 if (ads->iflags & adns_if_nosigpipe) return;
347
348 sigfillset(&toblock);
349 sigdelset(&toblock,SIGPIPE);
350
351 sa.sa_handler= SIG_IGN;
352 sigfillset(&sa.sa_mask);
353 sa.sa_flags= 0;
354
355 r= sigprocmask(SIG_SETMASK,&toblock,&ads->stdsigmask); assert(!r);
356 r= sigaction(SIGPIPE,&sa,&ads->stdsigpipe); assert(!r);
357#endif
358}
@ adns_if_nosigpipe
Definition: adns.h:95
static struct sockaddr_in sa
Definition: adnsresfilter.c:69
#define SIGPIPE
Definition: signal.h:35
#define SIG_IGN
Definition: signal.h:48
#define assert(x)
Definition: debug.h:53
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
sigset_t stdsigmask
Definition: internal.h:313
adns_initflags iflags
Definition: internal.h:292
struct sigaction stdsigpipe
Definition: internal.h:312

Referenced by adns__querysend_tcp(), and adns_processwriteable().

◆ adns__sigpipe_unprotect()

void adns__sigpipe_unprotect ( adns_state  ads)

Definition at line 360 of file general.c.

360 {
361#ifndef ADNS_JGAA_WIN32
362 int r;
363
364 if (ads->iflags & adns_if_nosigpipe) return;
365
366 r= sigaction(SIGPIPE,&ads->stdsigpipe,0); assert(!r);
367 r= sigprocmask(SIG_SETMASK,&ads->stdsigmask,0); assert(!r);
368#endif
369}

Referenced by adns__querysend_tcp(), and adns_processwriteable().

◆ adns__vbuf_append()

int adns__vbuf_append ( vbuf vb,
const byte data,
int  len 
)

Definition at line 131 of file general.c.

131 {
132 int newlen;
133 void *nb;
134
135 newlen= vb->used+len;
136 if (vb->avail < newlen) {
137 if (newlen<20) newlen= 20;
138 newlen <<= 1;
139 nb= realloc(vb->buf,(size_t) newlen);
140 if (!nb) { newlen= vb->used+len; nb= realloc(vb->buf, (size_t) newlen); }
141 if (!nb) return 0;
142 vb->buf= nb;
143 vb->avail= newlen;
144 }
146 return 1;
147}
#define realloc
Definition: debug_ros.c:6
GLenum GLsizei len
Definition: glext.h:6722
void adns__vbuf_appendq(vbuf *vb, const byte *data, int len)
Definition: general.c:126
int avail
Definition: internal.h:111

Referenced by adns__diag_domain(), adns__parse_domain_more(), adns__querysend_tcp(), adns__search_next(), adns__vbuf_appendstr(), adns_rr_info(), adns_submit(), csp_qstring(), pap_mailbox822(), and vbuf__append_quoted1035().

◆ adns__vbuf_appendq()

void adns__vbuf_appendq ( vbuf vb,
const byte data,
int  len 
)

Definition at line 126 of file general.c.

126 {
127 memcpy(vb->buf+vb->used,data, (size_t) len);
128 vb->used+= len;
129}

Referenced by adns__vbuf_append(), and pap_mailbox822().

◆ adns__vbuf_appendstr()

int adns__vbuf_appendstr ( vbuf vb,
const char data 
)

Definition at line 149 of file general.c.

149 {
150 int l;
151 l= strlen(data);
152 return adns__vbuf_append(vb,(byte*)data,l);
153}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
r l[0]
Definition: byte_order.h:168

Referenced by adns__diag_domain(), adns__search_next(), and pap_mailbox822().

◆ adns__vbuf_ensure()

int adns__vbuf_ensure ( vbuf vb,
int  want 
)

Definition at line 116 of file general.c.

116 {
117 void *nb;
118
119 if (vb->avail >= want) return 1;
120 nb= realloc(vb->buf, (size_t) want); if (!nb) return 0;
121 vb->buf= nb;
122 vb->avail= want;
123 return 1;
124}

Referenced by adns__query_done(), adns__querysend_tcp(), adns_processreadable(), adns_processwriteable(), mkquery_header(), pap_findaddrs(), and pap_mailbox822().

◆ adns__vbuf_free()

void adns__vbuf_free ( vbuf vb)

Definition at line 155 of file general.c.

155 {
156 free(vb->buf);
157 adns__vbuf_init(vb);
158}
#define free
Definition: debug_ros.c:5
void adns__vbuf_init(vbuf *vb)
Definition: general.c:112

Referenced by adns__internal_submit(), adns__procdgram(), adns__vdiag(), adns_finish(), adns_rr_info(), and free_query_allocs().

◆ adns__vbuf_init()

void adns__vbuf_init ( vbuf vb)

Definition at line 112 of file general.c.

112 {
113 vb->used= vb->avail= 0; vb->buf= 0;
114}

Referenced by adns__procdgram(), adns__vbuf_free(), adns__vdiag(), adns_rr_info(), init_begin(), query_alloc(), query_simple(), and query_submit().

◆ adns__vdiag()

void adns__vdiag ( adns_state  ads,
const char pfx,
adns_initflags  prevent,
int  serv,
adns_query  qu,
const char fmt,
va_list  al 
)

Definition at line 45 of file general.c.

46 {
47 const char *bef, *aft;
48 vbuf vb;
49
50 if (!ads->diagfile ||
51 (!(ads->iflags & adns_if_debug) && (!prevent || (ads->iflags & prevent))))
52 return;
53
54 if (ads->iflags & adns_if_logpid) {
55 fprintf(ads->diagfile,"adns%s [%ld]: ",pfx,(long)getpid());
56 } else {
57 fprintf(ads->diagfile,"adns%s: ",pfx);
58 }
59
61
62 bef= " (";
63 aft= "\n";
64
65 if (qu && qu->query_dgram) {
66 adns__vbuf_init(&vb);
67 fprintf(ads->diagfile,"%sQNAME=%s, QTYPE=%s",
68 bef,
69 adns__diag_domain(qu->ads,-1,0, &vb,
71 qu->typei ? qu->typei->rrtname : "<unknown>");
72 if (qu->typei && qu->typei->fmtname)
73 fprintf(ads->diagfile,"(%s)",qu->typei->fmtname);
74 bef=", "; aft=")\n";
75 adns__vbuf_free(&vb);
76 }
77
78 if (serv>=0) {
79 fprintf(ads->diagfile,"%sNS=%s",bef,inet_ntoa(ads->servers[serv].addr));
80 bef=", "; aft=")\n";
81 }
82
83 fputs(aft,ads->diagfile);
84}
@ adns_if_logpid
Definition: adns.h:92
@ adns_if_debug
Definition: adns.h:91
_Check_return_opt_ _CRTIMP int __cdecl fputs(_In_z_ const char *_Str, _Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_opt_ _CRTIMP int __cdecl vfprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format, va_list _ArgList)
#define inet_ntoa(addr)
Definition: inet.h:100
void adns__vbuf_free(vbuf *vb)
Definition: general.c:155
const char * adns__diag_domain(adns_state ads, int serv, adns_query qu, vbuf *vb, const byte *dgram, int dglen, int cbyte)
Definition: general.c:162
#define DNS_HDRSIZE
Definition: internal.h:76
byte * query_dgram
Definition: internal.h:192
const typeinfo * typei
Definition: internal.h:191
int query_dglen
Definition: internal.h:193
adns_state ads
Definition: internal.h:182
struct adns__state::server servers[MAXSERVERS]
FILE * diagfile
Definition: internal.h:293
const char * rrtname
Definition: internal.h:126
const char * fmtname
Definition: internal.h:127
Definition: internal.h:110
#define getpid
Definition: wintirpc.h:52

Referenced by adns__debug(), adns__diag(), and adns__warn().

◆ adns__warn()

void adns__warn ( adns_state  ads,
int  serv,
adns_query  qu,
const char fmt,
  ... 
)

Definition at line 94 of file general.c.

94 {
95 va_list al;
96
97 va_start(al,fmt);
99 va_end(al);
100}
@ adns_if_noserverwarn
Definition: adns.h:90

Referenced by adns__procdgram(), adns__query_send(), adns__tcp_broken(), and adns_processreadable().

◆ adns_errabbrev()

const char * adns_errabbrev ( adns_status  st)

Definition at line 275 of file general.c.

275 {
276 const struct sinfo *si;
277
278 si= findsinfo(st);
279 return si->abbrev;
280}
static const struct sinfo * findsinfo(adns_status st)
Definition: general.c:264
Definition: general.c:220
const char * abbrev
Definition: general.c:222
adns_status st
Definition: general.c:221

Referenced by csp_hostaddr(), main(), and print_status().

◆ adns_errtypeabbrev()

const char * adns_errtypeabbrev ( adns_status  st)

Definition at line 311 of file general.c.

311 {
312 const struct stinfo *sti;
313
314 sti= bsearch(&st,stinfos,sizeof(stinfos)/sizeof(*stinfos),sizeof(*stinfos),sti_compar);
315 return sti->abbrev;
316}
static int sti_compar(const void *key, const void *elem)
Definition: general.c:298
static const struct stinfo stinfos[]
const char * abbrev
Definition: general.c:287
#define bsearch

Referenced by csp_hostaddr(), and print_status().

◆ adns_rr_info()

adns_status adns_rr_info ( adns_rrtype  type,
const char **  rrtname_r,
const char **  fmtname_r,
int len_r,
const void datap,
char **  data_r 
)

Definition at line 186 of file general.c.

189 {
190 const typeinfo *typei;
191 vbuf vb;
192 adns_status st;
193
194 typei= adns__findtype(type);
195 if (!typei) return adns_s_unknownrrtype;
196
197 if (rrtname_r) *rrtname_r= typei->rrtname;
198 if (fmtname_r) *fmtname_r= typei->fmtname;
199 if (len_r) *len_r= typei->rrsz;
200
201 if (!datap) return adns_s_ok;
202
203 adns__vbuf_init(&vb);
204 st= typei->convstring(&vb,datap);
205 if (st) goto x_freevb;
206 if (!adns__vbuf_append(&vb,(byte*)"",1)) { st= adns_s_nomemory; goto x_freevb; }
207 assert((int)strlen((char*)vb.buf) == vb.used-1);
208 *data_r= realloc(vb.buf, (size_t) vb.used);
209 if (!*data_r) *data_r= (char*)vb.buf;
210 return adns_s_ok;
211
212 x_freevb:
213 adns__vbuf_free(&vb);
214 return st;
215}
@ adns_s_unknownrrtype
Definition: adns.h:220
@ adns_s_ok
Definition: adns.h:216
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
const typeinfo * adns__findtype(adns_rrtype type)
Definition: types.c:1025
adns_status(* convstring)(vbuf *vb, const void *data)
Definition: internal.h:135
int rrsz
Definition: internal.h:128

Referenced by main(), print_dnsfail(), and query_done().

◆ adns_strerror()

const char * adns_strerror ( adns_status  st)

Definition at line 268 of file general.c.

268 {
269 const struct sinfo *si;
270
271 si= findsinfo(st);
272 return si->string;
273}
const char * string
Definition: general.c:223

Referenced by adns__diag_domain(), csp_hostaddr(), dumptype(), failure_status(), main(), print_dnsfail(), and print_status().

◆ findsinfo()

static const struct sinfo * findsinfo ( adns_status  st)
static

Definition at line 264 of file general.c.

264 {
265 return bsearch(&st,sinfos,sizeof(sinfos)/sizeof(*sinfos),sizeof(*sinfos),si_compar);
266}
static int si_compar(const void *key, const void *elem)
Definition: general.c:257
static const struct sinfo sinfos[]

Referenced by adns_errabbrev(), and adns_strerror().

◆ si_compar()

static int si_compar ( const void key,
const void elem 
)
static

Definition at line 257 of file general.c.

257 {
258 const adns_status *st= key;
259 const struct sinfo *si= elem;
260
261 return *st < si->st ? -1 : *st > si->st ? 1 : 0;
262}
static size_t elem
Definition: string.c:68
Definition: copy.c:22

Referenced by findsinfo().

◆ sti_compar()

static int sti_compar ( const void key,
const void elem 
)
static

Definition at line 298 of file general.c.

298 {
299 const adns_status *st= key;
300 const struct stinfo *sti= elem;
301
302 adns_status here, min, max;
303
304 here= *st;
305 min= (sti==stinfos) ? 0 : sti[-1].stmax+1;
306 max= sti->stmax;
307
308 return here < min ? -1 : here > max ? 1 : 0;
309}
#define min(a, b)
Definition: monoChain.cc:55
adns_status stmax
Definition: general.c:286
#define max(a, b)
Definition: svc.c:63

Referenced by adns_errtypeabbrev().

Variable Documentation

◆ sinfos

const struct sinfo sinfos[]
static

Referenced by findsinfo().

◆ stinfos

const struct stinfo stinfos[]
static
Initial value:
= {
{ adns_s_ok, "ok" },
STINFO( localfail ),
STINFO( remotefail ),
STINFO( tempfail ),
STINFO( misconfig ),
STINFO( misquery ),
}
#define STINFO(max)
Definition: general.c:283

Referenced by adns_errtypeabbrev(), and sti_compar().