Merge branches 'master' and 'suser_to_priv'
[dragonfly.git] / contrib / bind-9.3 / lib / bind / isc / eventlib_p.h
1 /*
2  * Copyright (c) 2005 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1995-1999 by Internet Software Consortium
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* eventlib_p.h - private interfaces for eventlib
19  * vix 09sep95 [initial]
20  *
21  * $Id: eventlib_p.h,v 1.3.2.1.4.4 2006/03/10 00:17:21 marka Exp $
22  */
23
24 #ifndef _EVENTLIB_P_H
25 #define _EVENTLIB_P_H
26
27 #include <sys/param.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <sys/un.h>
32
33 #define EVENTLIB_DEBUG 1
34
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #ifndef _LIBC
42 #include <isc/heap.h>
43 #include <isc/list.h>
44 #include <isc/memcluster.h>
45 #endif
46
47 #define EV_MASK_ALL     (EV_READ | EV_WRITE | EV_EXCEPT)
48 #define EV_ERR(e)               return (errno = (e), -1)
49 #define OK(x)           if ((x) < 0) EV_ERR(errno); else (void)NULL
50 #define OKFREE(x, y)    if ((x) < 0) { FREE((y)); EV_ERR(errno); } \
51                         else (void)NULL
52
53 #define NEW(p)          if (((p) = memget(sizeof *(p))) != NULL) \
54                                 FILL(p); \
55                         else \
56                                 (void)NULL;
57 #define OKNEW(p)        if (!((p) = memget(sizeof *(p)))) { \
58                                 errno = ENOMEM; \
59                                 return (-1); \
60                         } else \
61                                 FILL(p)
62 #define FREE(p)         memput((p), sizeof *(p))
63
64 #if EVENTLIB_DEBUG
65 #define FILL(p)         memset((p), 0xF5, sizeof *(p))
66 #else
67 #define FILL(p)
68 #endif
69
70 #ifdef USE_POLL
71 #ifdef HAVE_STROPTS_H
72 #include <stropts.h>
73 #endif
74 #include <poll.h>
75 #endif /* USE_POLL */
76
77 typedef struct evConn {
78         evConnFunc      func;
79         void *          uap;
80         int             fd;
81         int             flags;
82 #define EV_CONN_LISTEN          0x0001          /* Connection is a listener. */
83 #define EV_CONN_SELECTED        0x0002          /* evSelectFD(conn->file). */
84 #define EV_CONN_BLOCK           0x0004          /* Listener fd was blocking. */
85         evFileID        file;
86         struct evConn * prev;
87         struct evConn * next;
88 } evConn;
89
90 #ifndef _LIBC
91 typedef struct evAccept {
92         int             fd;
93         union {
94                 struct sockaddr         sa;
95                 struct sockaddr_in      in;
96 #ifndef NO_SOCKADDR_UN
97                 struct sockaddr_un      un;
98 #endif
99         }               la;
100         ISC_SOCKLEN_T   lalen;
101         union {
102                 struct sockaddr         sa;
103                 struct sockaddr_in      in;
104 #ifndef NO_SOCKADDR_UN
105                 struct sockaddr_un      un;
106 #endif
107         }               ra;
108         ISC_SOCKLEN_T   ralen;
109         int             ioErrno;
110         evConn *        conn;
111         LINK(struct evAccept) link;
112 } evAccept;
113
114 typedef struct evFile {
115         evFileFunc      func;
116         void *          uap;
117         int             fd;
118         int             eventmask;
119         int             preemptive;
120         struct evFile * prev;
121         struct evFile * next;
122         struct evFile * fdprev;
123         struct evFile * fdnext;
124 } evFile;
125
126 typedef struct evStream {
127         evStreamFunc    func;
128         void *          uap;
129         evFileID        file;
130         evTimerID       timer;
131         int             flags;
132 #define EV_STR_TIMEROK  0x0001  /* IFF timer valid. */
133         int             fd;
134         struct iovec *  iovOrig;
135         int             iovOrigCount;
136         struct iovec *  iovCur;
137         int             iovCurCount;
138         int             ioTotal;
139         int             ioDone;
140         int             ioErrno;
141         struct evStream *prevDone, *nextDone;
142         struct evStream *prev, *next;
143 } evStream;
144
145 typedef struct evTimer {
146         evTimerFunc     func;
147         void *          uap;
148         struct timespec due, inter;
149         int             index;
150         int             mode;
151 #define EV_TMR_RATE     1
152 } evTimer;
153
154 typedef struct evWait {
155         evWaitFunc      func;
156         void *          uap;
157         const void *    tag;
158         struct evWait * next;
159 } evWait;
160
161 typedef struct evWaitList {
162         evWait *                first;
163         evWait *                last;
164         struct evWaitList *     prev;
165         struct evWaitList *     next;
166 } evWaitList;
167
168 typedef struct evEvent_p {
169         enum {  Accept, File, Stream, Timer, Wait, Free, Null  } type;
170         union {
171                 struct {  evAccept *this;  }                    accept;
172                 struct {  evFile *this; int eventmask;  }       file;
173                 struct {  evStream *this;  }                    stream;
174                 struct {  evTimer *this;  }                     timer;
175                 struct {  evWait *this;  }                      wait;
176                 struct {  struct evEvent_p *next;  }            free;
177                 struct {  const void *placeholder;  }           null;
178         } u;
179 } evEvent_p;
180 #endif /* !_LIBC */
181
182 #ifdef USE_POLL
183 typedef struct { 
184         void            *ctx;   /* pointer to the evContext_p   */ 
185         uint32_t        type;   /* READ, WRITE, EXCEPT, nonblk  */ 
186         uint32_t        result; /* 1 => revents, 0 => events    */ 
187 } __evEmulMask; 
188
189 #define emulMaskInit(ctx, field, ev, lastnext) \
190         ctx->field.ctx = ctx; \
191         ctx->field.type = ev; \
192         ctx->field.result = lastnext; 
193   
194 extern short    *__fd_eventfield(int fd, __evEmulMask *maskp); 
195 extern short    __poll_event(__evEmulMask *maskp); 
196 extern void             __fd_clr(int fd, __evEmulMask *maskp); 
197 extern void             __fd_set(int fd, __evEmulMask *maskp); 
198
199 #undef  FD_ZERO 
200 #define FD_ZERO(maskp) 
201   
202 #undef  FD_SET 
203 #define FD_SET(fd, maskp) \
204         __fd_set(fd, maskp) 
205
206 #undef  FD_CLR 
207 #define FD_CLR(fd, maskp) \
208         __fd_clr(fd, maskp) 
209
210 #undef  FD_ISSET 
211 #define FD_ISSET(fd, maskp) \
212         ((*__fd_eventfield(fd, maskp) & __poll_event(maskp)) != 0) 
213
214 #endif /* USE_POLL */
215
216 #ifndef _LIBC
217 typedef struct {
218         /* Global. */
219         const evEvent_p *cur;
220         /* Debugging. */
221         int             debug;
222         FILE            *output;
223         /* Connections. */
224         evConn          *conns;
225         LIST(evAccept)  accepts;
226         /* Files. */
227         evFile          *files, *fdNext;
228 #ifndef USE_POLL
229         fd_set          rdLast, rdNext;
230         fd_set          wrLast, wrNext;
231         fd_set          exLast, exNext;
232         fd_set          nonblockBefore;
233         int             fdMax, fdCount, highestFD;
234         evFile          *fdTable[FD_SETSIZE];
235 #else
236         struct pollfd   *pollfds;       /* Allocated as needed  */ 
237         evFile          **fdTable;      /* Ditto                */ 
238         int             maxnfds;        /* # elements in above  */ 
239         int             firstfd;        /* First active fd      */ 
240         int             fdMax;          /* Last active fd       */ 
241         int             fdCount;        /* # fd:s with I/O      */ 
242         int             highestFD;      /* max fd allowed by OS */ 
243         __evEmulMask    rdLast, rdNext; 
244         __evEmulMask    wrLast, wrNext; 
245         __evEmulMask    exLast, exNext; 
246         __evEmulMask    nonblockBefore; 
247 #endif /* USE_POLL */
248 #ifdef EVENTLIB_TIME_CHECKS
249         struct timespec lastSelectTime;
250         int             lastFdCount;
251 #endif
252         /* Streams. */
253         evStream        *streams;
254         evStream        *strDone, *strLast;
255         /* Timers. */
256         struct timespec lastEventTime;
257         heap_context    timers;
258         /* Waits. */
259         evWaitList      *waitLists;
260         evWaitList      waitDone;
261 } evContext_p;
262
263 /* eventlib.c */
264 #define evPrintf __evPrintf
265 void evPrintf(const evContext_p *ctx, int level, const char *fmt, ...)
266      ISC_FORMAT_PRINTF(3, 4);
267
268 #ifdef USE_POLL
269 extern int evPollfdRealloc(evContext_p *ctx, int pollfd_chunk_size, int fd);
270 #endif /* USE_POLL */
271
272 /* ev_timers.c */
273 #define evCreateTimers __evCreateTimers
274 heap_context evCreateTimers(const evContext_p *);
275 #define evDestroyTimers __evDestroyTimers
276 void evDestroyTimers(const evContext_p *);
277
278 /* ev_waits.c */
279 #define evFreeWait __evFreeWait
280 evWait *evFreeWait(evContext_p *ctx, evWait *old);
281 #endif /* !_LIBC */
282
283 /* Global options */
284 #ifndef _LIBC
285 extern int      __evOptMonoTime;
286 #endif /* !_LIBC */
287
288 #endif /*_EVENTLIB_P_H*/