Merge from vendor branch LIBARCHIVE:
[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 #include <isc/heap.h>
42 #include <isc/list.h>
43 #include <isc/memcluster.h>
44
45 #define EV_MASK_ALL     (EV_READ | EV_WRITE | EV_EXCEPT)
46 #define EV_ERR(e)               return (errno = (e), -1)
47 #define OK(x)           if ((x) < 0) EV_ERR(errno); else (void)NULL
48 #define OKFREE(x, y)    if ((x) < 0) { FREE((y)); EV_ERR(errno); } \
49                         else (void)NULL
50
51 #define NEW(p)          if (((p) = memget(sizeof *(p))) != NULL) \
52                                 FILL(p); \
53                         else \
54                                 (void)NULL;
55 #define OKNEW(p)        if (!((p) = memget(sizeof *(p)))) { \
56                                 errno = ENOMEM; \
57                                 return (-1); \
58                         } else \
59                                 FILL(p)
60 #define FREE(p)         memput((p), sizeof *(p))
61
62 #if EVENTLIB_DEBUG
63 #define FILL(p)         memset((p), 0xF5, sizeof *(p))
64 #else
65 #define FILL(p)
66 #endif
67
68 #ifdef USE_POLL
69 #ifdef HAVE_STROPTS_H
70 #include <stropts.h>
71 #endif
72 #include <poll.h>
73 #endif /* USE_POLL */
74
75 typedef struct evConn {
76         evConnFunc      func;
77         void *          uap;
78         int             fd;
79         int             flags;
80 #define EV_CONN_LISTEN          0x0001          /* Connection is a listener. */
81 #define EV_CONN_SELECTED        0x0002          /* evSelectFD(conn->file). */
82 #define EV_CONN_BLOCK           0x0004          /* Listener fd was blocking. */
83         evFileID        file;
84         struct evConn * prev;
85         struct evConn * next;
86 } evConn;
87
88 typedef struct evAccept {
89         int             fd;
90         union {
91                 struct sockaddr         sa;
92                 struct sockaddr_in      in;
93 #ifndef NO_SOCKADDR_UN
94                 struct sockaddr_un      un;
95 #endif
96         }               la;
97         ISC_SOCKLEN_T   lalen;
98         union {
99                 struct sockaddr         sa;
100                 struct sockaddr_in      in;
101 #ifndef NO_SOCKADDR_UN
102                 struct sockaddr_un      un;
103 #endif
104         }               ra;
105         ISC_SOCKLEN_T   ralen;
106         int             ioErrno;
107         evConn *        conn;
108         LINK(struct evAccept) link;
109 } evAccept;
110
111 typedef struct evFile {
112         evFileFunc      func;
113         void *          uap;
114         int             fd;
115         int             eventmask;
116         int             preemptive;
117         struct evFile * prev;
118         struct evFile * next;
119         struct evFile * fdprev;
120         struct evFile * fdnext;
121 } evFile;
122
123 typedef struct evStream {
124         evStreamFunc    func;
125         void *          uap;
126         evFileID        file;
127         evTimerID       timer;
128         int             flags;
129 #define EV_STR_TIMEROK  0x0001  /* IFF timer valid. */
130         int             fd;
131         struct iovec *  iovOrig;
132         int             iovOrigCount;
133         struct iovec *  iovCur;
134         int             iovCurCount;
135         int             ioTotal;
136         int             ioDone;
137         int             ioErrno;
138         struct evStream *prevDone, *nextDone;
139         struct evStream *prev, *next;
140 } evStream;
141
142 typedef struct evTimer {
143         evTimerFunc     func;
144         void *          uap;
145         struct timespec due, inter;
146         int             index;
147         int             mode;
148 #define EV_TMR_RATE     1
149 } evTimer;
150
151 typedef struct evWait {
152         evWaitFunc      func;
153         void *          uap;
154         const void *    tag;
155         struct evWait * next;
156 } evWait;
157
158 typedef struct evWaitList {
159         evWait *                first;
160         evWait *                last;
161         struct evWaitList *     prev;
162         struct evWaitList *     next;
163 } evWaitList;
164
165 typedef struct evEvent_p {
166         enum {  Accept, File, Stream, Timer, Wait, Free, Null  } type;
167         union {
168                 struct {  evAccept *this;  }                    accept;
169                 struct {  evFile *this; int eventmask;  }       file;
170                 struct {  evStream *this;  }                    stream;
171                 struct {  evTimer *this;  }                     timer;
172                 struct {  evWait *this;  }                      wait;
173                 struct {  struct evEvent_p *next;  }            free;
174                 struct {  const void *placeholder;  }           null;
175         } u;
176 } evEvent_p;
177
178 #ifdef USE_POLL
179 typedef struct { 
180         void            *ctx;   /* pointer to the evContext_p   */ 
181         uint32_t        type;   /* READ, WRITE, EXCEPT, nonblk  */ 
182         uint32_t        result; /* 1 => revents, 0 => events    */ 
183 } __evEmulMask; 
184
185 #define emulMaskInit(ctx, field, ev, lastnext) \
186         ctx->field.ctx = ctx; \
187         ctx->field.type = ev; \
188         ctx->field.result = lastnext; 
189   
190 extern short    *__fd_eventfield(int fd, __evEmulMask *maskp); 
191 extern short    __poll_event(__evEmulMask *maskp); 
192 extern void             __fd_clr(int fd, __evEmulMask *maskp); 
193 extern void             __fd_set(int fd, __evEmulMask *maskp); 
194
195 #undef  FD_ZERO 
196 #define FD_ZERO(maskp) 
197   
198 #undef  FD_SET 
199 #define FD_SET(fd, maskp) \
200         __fd_set(fd, maskp) 
201
202 #undef  FD_CLR 
203 #define FD_CLR(fd, maskp) \
204         __fd_clr(fd, maskp) 
205
206 #undef  FD_ISSET 
207 #define FD_ISSET(fd, maskp) \
208         ((*__fd_eventfield(fd, maskp) & __poll_event(maskp)) != 0) 
209
210 #endif /* USE_POLL */
211
212 typedef struct {
213         /* Global. */
214         const evEvent_p *cur;
215         /* Debugging. */
216         int             debug;
217         FILE            *output;
218         /* Connections. */
219         evConn          *conns;
220         LIST(evAccept)  accepts;
221         /* Files. */
222         evFile          *files, *fdNext;
223 #ifndef USE_POLL
224         fd_set          rdLast, rdNext;
225         fd_set          wrLast, wrNext;
226         fd_set          exLast, exNext;
227         fd_set          nonblockBefore;
228         int             fdMax, fdCount, highestFD;
229         evFile          *fdTable[FD_SETSIZE];
230 #else
231         struct pollfd   *pollfds;       /* Allocated as needed  */ 
232         evFile          **fdTable;      /* Ditto                */ 
233         int             maxnfds;        /* # elements in above  */ 
234         int             firstfd;        /* First active fd      */ 
235         int             fdMax;          /* Last active fd       */ 
236         int             fdCount;        /* # fd:s with I/O      */ 
237         int             highestFD;      /* max fd allowed by OS */ 
238         __evEmulMask    rdLast, rdNext; 
239         __evEmulMask    wrLast, wrNext; 
240         __evEmulMask    exLast, exNext; 
241         __evEmulMask    nonblockBefore; 
242 #endif /* USE_POLL */
243 #ifdef EVENTLIB_TIME_CHECKS
244         struct timespec lastSelectTime;
245         int             lastFdCount;
246 #endif
247         /* Streams. */
248         evStream        *streams;
249         evStream        *strDone, *strLast;
250         /* Timers. */
251         struct timespec lastEventTime;
252         heap_context    timers;
253         /* Waits. */
254         evWaitList      *waitLists;
255         evWaitList      waitDone;
256 } evContext_p;
257
258 /* eventlib.c */
259 #define evPrintf __evPrintf
260 void evPrintf(const evContext_p *ctx, int level, const char *fmt, ...)
261      ISC_FORMAT_PRINTF(3, 4);
262
263 #ifdef USE_POLL
264 extern int evPollfdRealloc(evContext_p *ctx, int pollfd_chunk_size, int fd);
265 #endif /* USE_POLL */
266
267 /* ev_timers.c */
268 #define evCreateTimers __evCreateTimers
269 heap_context evCreateTimers(const evContext_p *);
270 #define evDestroyTimers __evDestroyTimers
271 void evDestroyTimers(const evContext_p *);
272
273 /* ev_waits.c */
274 #define evFreeWait __evFreeWait
275 evWait *evFreeWait(evContext_p *ctx, evWait *old);
276
277 /* Global options */
278 extern int      __evOptMonoTime;
279
280 #endif /*_EVENTLIB_P_H*/