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