chiark / gitweb /
Event loop stuff complete ?
[adns] / src / internal.h
CommitLineData
dfdbb32c 1/**/
2
3#ifndef ADNS_INTERNAL_H_INCLUDED
4#define ADNS_INTERNAL_H_INCLUDED
5
6#include <sys/time.h>
7
8#include "adns.h"
9
10/* Configuration and constants */
11
12#define MAXSERVERS 5
13#define MAXUDPRETRIES 15
14#define UDPRETRYMS 2000
15#define TCPMS 30000
16#define LOCALRESOURCEMS 20
17#define UDPMAXDGRAM 512
18#define NSPORT 53
19
20/* Shared data structures */
21
22union adns__align {
23 adns_status status;
24 char *cp;
25 adns_rrtype type;
26 int int;
27 struct in_addr ia;
28 unsigned long ul;
29};
30
31struct adns__query {
32 /* FIXME: make sure this is all init'd properly */
96e79df5 33 enum { query_udp, query_tcpwait, query_tcpsent, query_child, query_done } state;
dfdbb32c 34 adns_query back, next;
35 adns_query parent;
36 struct { adns_query head, tail; } children;
37 struct { adns_query back, next; } siblings;
38 adns_rrtype type;
39 adns_answer *answer;
40 size_t ansalloc; ansused;
96e79df5 41 int id, flags, udpretries;
42 int nextudpserver;
43 unsigned long sentudp, failedtcp; /* bitmap indexed by server */
dfdbb32c 44 struct timeval timeout;
45 void *context;
46 unsigned char *querymsg;
47 int querylen;
48 char owner[1];
96e79df5 49 /* After the owner name and nul comes the query message */
50
dfdbb32c 51 /* Possible states:
96e79df5 52 *
53 * state Queue child id answer nextudpserver sentudp failedtcp
54 *
55 * udp NONE null >=0 null 0 zero zero
56 * udp timew null >=0 null any nonzero zero
57 * udp NONE null >=0 null any nonzero zero
58 *
59 * tcpwait timew null >=0 null irrelevant zero any
60 * tcpsent timew null >=0 null irrelevant zero any
61 *
62 * child childw set >=0 partial irrelevant irrelevant irrelevant
63 * done output null -1 set/null irrelevant irrelevant irrelevant
64 *
65 * +------------------------+
66 * START -----> | udp/NONE |
67 * +------------------------+
68 * / |\ \
69 * too big for UDP / UDP timeout \ \ send via UDP
70 * do this ASAP! / more retries \ \ do this ASAP!
71 * |_ desired \ _|
72 * +---------------+ +-----------+
73 * | tcpwait/timew | ____ | udp/timew |
74 * +---------------+ \ +-----------+
75 * | ^ | | |
76 * TCP conn'd; | | TCP died | | |
77 * send via TCP | | more | UDP timeout | |
78 * do this ASAP! | | servers | no more | |
79 * v | to try | retries | |
80 * +---------------+ | desired | |
81 * | tcpsent/timew | ____ | | |
82 * +---------------+ \| | |
83 * \ \ TCP died | TCP | |
84 * \ \ no more | timeout / |
85 * \ \ servers | / |
86 * \ \ to try | / |
87 * got \ \ v |_ / got
88 * reply \ _| +------------------+ / reply
89 * \ | done/output FAIL | /
90 * \ +------------------+ /
91 * \ /
92 * _| |_
93 * (..... got reply ....)
94 * / \
95 * need child query/ies / \ no child query
96 * / \
97 * |_ _|
98 * +--------------+ +----------------+
99 * | child/childw | ----------------> | done/output OK |
100 * +--------------+ children done +----------------+
dfdbb32c 101 */
102};
103
96e79df5 104typedef struct {
dfdbb32c 105 size_t used, avail;
106 unsigned char *buf;
96e79df5 107} adns__vbuf;
dfdbb32c 108
109struct adns__state {
110 /* FIXME: make sure this is all init'd properly */
111 adns_initflags iflags;
112 FILE *diagfile;
96e79df5 113 struct { adns_query head, tail; } timew, childw, output;
dfdbb32c 114 int nextid, udpsocket;
115 adns_vbuf rqbuf, tcpsend, tcprecv;
116 int nservers, tcpserver;
71324651 117 enum adns__tcpstate { server_disconnected, server_connecting, server_ok } tcpstate;
dfdbb32c 118 int tcpsocket;
119 struct timeval tcptimeout;
120 struct server {
121 struct in_addr addr;
122 } servers[MAXSERVERS];
123};
124
125/* From setup.c: */
126
127void adns__vdiag(adns_state ads, adns_initflags prevent, const char *pfx,
128 int serv, const char *fmt, va_list al);
71324651 129void adns__debug(adns_state ads, int serv, const char *fmt, ...) PRINTFFORMAT(3,4);
130void adns__warn(adns_state ads, int serv, const char *fmt, ...) PRINTFFORMAT(3,4);
131void adns__diag(adns_state ads, int serv, const char *fmt, ...) PRINTFFORMAT(3,4);
dfdbb32c 132
96e79df5 133static inline int adns__vbuf_ensure(adns__vbuf *vb, size_t want);
134int adns__vbuf_append(adns__vbuf *vb, const byte *data, size_t len);
135int adns__vbuf_appendq(adns__vbuf *vb, const byte *data, size_t len);
136/* 1=>success, 0=>realloc failed */
137
dfdbb32c 138/* From submit.c: */
139
140void adns__query_fail(adns_state ads, adns_query qu, adns_status stat);
141
142/* From query.c: */
143
96e79df5 144void adns__query_udp(adns_state ads, adns_query qu, struct timeval now);
145void adns__query_tcp(adns_state ads, adns_query qu, struct timeval now);
48cb0b4b 146adns_status adns__mkquery(adns_state ads, const char *owner, int ol, int id,
147 adns_rrtype type, adns_queryflags flags, int *qml_r);
71324651 148
149/* From event.c: */
150void adns__tcp_broken(adns_state ads, const char *what, const char *why);
151void adns__tcp_tryconnect(adns_state ads);
dfdbb32c 152
153/* Useful static inline functions: */
154
155static inline void timevaladd(struct timeval *tv_io, long ms) {
156 struct timeval tmp;
157 assert(ms>=0);
158 tmp= *tv_io;
159 tmp.tv_usec += (ms%1000)*1000;
160 tmp.tv_sec += ms/1000;
161 if (tmp.tv_usec >= 1000) { tmp.tv_sec++; tmp.tv_usec -= 1000; }
162 *tv_io= tmp;
96e79df5 163}
dfdbb32c 164
165static inline int ctype_whitespace(int c) { return c==' ' || c=='\n' || c=='\t'; }
166static inline int ctype_digit(int c) { return c>='0' && c<='9'; }
167
168/* Useful macros */
169
170#define LIST_UNLINK_PART(list,node,part) \
171 do { \
172 if ((node)->back) (node)->back->part next= (node)->part next; \
173 else (list).head= (node)->part next; \
174 if ((node)->next) (node)->next->part back= (node)->part back; \
175 else (list).tail= (node)->part back; \
176 } while(0)
177
178#define LIST_LINK_TAIL_PART(list,node,part) \
179 do { \
180 (node)->part back= 0; \
181 (node)->part next= (list).tail; \
182 if ((list).tail) (list).tail->part back= (node); else (list).part head= (node); \
183 (list).tail= (node); \
184 } while(0)
185
186#define LIST_UNLINK(list,node) LIST_UNLINK_PART(list,node,)
187#define LIST_LINK_TAIL_PART(list,node) LIST_LINK_TAIL(list,node,)
188
189#endif