Home | History | Annotate | Line # | Download | only in isc
eventlib_p.h revision 1.1
      1  1.1  christos /*	$NetBSD: eventlib_p.h,v 1.1 2004/05/20 19:34:32 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2004 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.1  christos /* eventlib_p.h - private interfaces for eventlib
     21  1.1  christos  * vix 09sep95 [initial]
     22  1.1  christos  *
     23  1.1  christos  * Id: eventlib_p.h,v 1.3.2.1.4.1 2004/03/09 08:33:43 marka Exp
     24  1.1  christos  */
     25  1.1  christos 
     26  1.1  christos #ifndef _EVENTLIB_P_H
     27  1.1  christos #define _EVENTLIB_P_H
     28  1.1  christos 
     29  1.1  christos #include <sys/param.h>
     30  1.1  christos #include <sys/types.h>
     31  1.1  christos #include <sys/socket.h>
     32  1.1  christos #include <netinet/in.h>
     33  1.1  christos #include <sys/un.h>
     34  1.1  christos 
     35  1.1  christos #define EVENTLIB_DEBUG 1
     36  1.1  christos 
     37  1.1  christos #include <errno.h>
     38  1.1  christos #include <fcntl.h>
     39  1.1  christos #include <stdio.h>
     40  1.1  christos #include <stdlib.h>
     41  1.1  christos #include <string.h>
     42  1.1  christos 
     43  1.1  christos #include <isc/heap.h>
     44  1.1  christos #include <isc/list.h>
     45  1.1  christos #include <isc/memcluster.h>
     46  1.1  christos 
     47  1.1  christos #define	EV_MASK_ALL	(EV_READ | EV_WRITE | EV_EXCEPT)
     48  1.1  christos #define EV_ERR(e)		return (errno = (e), -1)
     49  1.1  christos #define OK(x)		if ((x) < 0) EV_ERR(errno); else (void)NULL
     50  1.1  christos 
     51  1.1  christos #define	NEW(p)		if (((p) = memget(sizeof *(p))) != NULL) \
     52  1.1  christos 				FILL(p); \
     53  1.1  christos 			else \
     54  1.1  christos 				(void)NULL;
     55  1.1  christos #define OKNEW(p)	if (!((p) = memget(sizeof *(p)))) { \
     56  1.1  christos 				errno = ENOMEM; \
     57  1.1  christos 				return (-1); \
     58  1.1  christos 			} else \
     59  1.1  christos 				FILL(p)
     60  1.1  christos #define FREE(p)		memput((p), sizeof *(p))
     61  1.1  christos 
     62  1.1  christos #if EVENTLIB_DEBUG
     63  1.1  christos #define FILL(p)		memset((p), 0xF5, sizeof *(p))
     64  1.1  christos #else
     65  1.1  christos #define FILL(p)
     66  1.1  christos #endif
     67  1.1  christos 
     68  1.1  christos typedef struct evConn {
     69  1.1  christos 	evConnFunc	func;
     70  1.1  christos 	void *		uap;
     71  1.1  christos 	int		fd;
     72  1.1  christos 	int		flags;
     73  1.1  christos #define EV_CONN_LISTEN		0x0001		/* Connection is a listener. */
     74  1.1  christos #define EV_CONN_SELECTED	0x0002		/* evSelectFD(conn->file). */
     75  1.1  christos #define EV_CONN_BLOCK		0x0004		/* Listener fd was blocking. */
     76  1.1  christos 	evFileID	file;
     77  1.1  christos 	struct evConn *	prev;
     78  1.1  christos 	struct evConn *	next;
     79  1.1  christos } evConn;
     80  1.1  christos 
     81  1.1  christos typedef struct evAccept {
     82  1.1  christos 	int		fd;
     83  1.1  christos 	union {
     84  1.1  christos 		struct sockaddr		sa;
     85  1.1  christos 		struct sockaddr_in	in;
     86  1.1  christos #ifndef NO_SOCKADDR_UN
     87  1.1  christos 		struct sockaddr_un	un;
     88  1.1  christos #endif
     89  1.1  christos 	}		la;
     90  1.1  christos 	ISC_SOCKLEN_T	lalen;
     91  1.1  christos 	union {
     92  1.1  christos 		struct sockaddr		sa;
     93  1.1  christos 		struct sockaddr_in	in;
     94  1.1  christos #ifndef NO_SOCKADDR_UN
     95  1.1  christos 		struct sockaddr_un	un;
     96  1.1  christos #endif
     97  1.1  christos 	}		ra;
     98  1.1  christos 	ISC_SOCKLEN_T	ralen;
     99  1.1  christos 	int		ioErrno;
    100  1.1  christos 	evConn *	conn;
    101  1.1  christos 	LINK(struct evAccept) link;
    102  1.1  christos } evAccept;
    103  1.1  christos 
    104  1.1  christos typedef struct evFile {
    105  1.1  christos 	evFileFunc	func;
    106  1.1  christos 	void *		uap;
    107  1.1  christos 	int		fd;
    108  1.1  christos 	int		eventmask;
    109  1.1  christos 	int		preemptive;
    110  1.1  christos 	struct evFile *	prev;
    111  1.1  christos 	struct evFile *	next;
    112  1.1  christos 	struct evFile *	fdprev;
    113  1.1  christos 	struct evFile *	fdnext;
    114  1.1  christos } evFile;
    115  1.1  christos 
    116  1.1  christos typedef struct evStream {
    117  1.1  christos 	evStreamFunc	func;
    118  1.1  christos 	void *		uap;
    119  1.1  christos 	evFileID	file;
    120  1.1  christos 	evTimerID	timer;
    121  1.1  christos 	int		flags;
    122  1.1  christos #define EV_STR_TIMEROK	0x0001	/* IFF timer valid. */
    123  1.1  christos 	int		fd;
    124  1.1  christos 	struct iovec *	iovOrig;
    125  1.1  christos 	int		iovOrigCount;
    126  1.1  christos 	struct iovec *	iovCur;
    127  1.1  christos 	int		iovCurCount;
    128  1.1  christos 	int		ioTotal;
    129  1.1  christos 	int		ioDone;
    130  1.1  christos 	int		ioErrno;
    131  1.1  christos 	struct evStream	*prevDone, *nextDone;
    132  1.1  christos 	struct evStream	*prev, *next;
    133  1.1  christos } evStream;
    134  1.1  christos 
    135  1.1  christos typedef struct evTimer {
    136  1.1  christos 	evTimerFunc	func;
    137  1.1  christos 	void *		uap;
    138  1.1  christos 	struct timespec	due, inter;
    139  1.1  christos 	int		index;
    140  1.1  christos 	int		mode;
    141  1.1  christos #define EV_TMR_RATE	1
    142  1.1  christos } evTimer;
    143  1.1  christos 
    144  1.1  christos typedef struct evWait {
    145  1.1  christos 	evWaitFunc	func;
    146  1.1  christos 	void *		uap;
    147  1.1  christos 	const void *	tag;
    148  1.1  christos 	struct evWait *	next;
    149  1.1  christos } evWait;
    150  1.1  christos 
    151  1.1  christos typedef struct evWaitList {
    152  1.1  christos 	evWait *		first;
    153  1.1  christos 	evWait *		last;
    154  1.1  christos 	struct evWaitList *	prev;
    155  1.1  christos 	struct evWaitList *	next;
    156  1.1  christos } evWaitList;
    157  1.1  christos 
    158  1.1  christos typedef struct evEvent_p {
    159  1.1  christos 	enum {  Accept, File, Stream, Timer, Wait, Free, Null  } type;
    160  1.1  christos 	union {
    161  1.1  christos 		struct {  evAccept *this;  }			accept;
    162  1.1  christos 		struct {  evFile *this; int eventmask;  }	file;
    163  1.1  christos 		struct {  evStream *this;  }			stream;
    164  1.1  christos 		struct {  evTimer *this;  }			timer;
    165  1.1  christos 		struct {  evWait *this;  }			wait;
    166  1.1  christos 		struct {  struct evEvent_p *next;  }		free;
    167  1.1  christos 		struct {  const void *placeholder;  }		null;
    168  1.1  christos 	} u;
    169  1.1  christos } evEvent_p;
    170  1.1  christos 
    171  1.1  christos typedef struct {
    172  1.1  christos 	/* Global. */
    173  1.1  christos 	const evEvent_p	*cur;
    174  1.1  christos 	/* Debugging. */
    175  1.1  christos 	int		debug;
    176  1.1  christos 	FILE		*output;
    177  1.1  christos 	/* Connections. */
    178  1.1  christos 	evConn		*conns;
    179  1.1  christos 	LIST(evAccept)	accepts;
    180  1.1  christos 	/* Files. */
    181  1.1  christos 	evFile		*files, *fdNext;
    182  1.1  christos 	fd_set		rdLast, rdNext;
    183  1.1  christos 	fd_set		wrLast, wrNext;
    184  1.1  christos 	fd_set		exLast, exNext;
    185  1.1  christos 	fd_set		nonblockBefore;
    186  1.1  christos 	int		fdMax, fdCount, highestFD;
    187  1.1  christos 	evFile		*fdTable[FD_SETSIZE];
    188  1.1  christos #ifdef EVENTLIB_TIME_CHECKS
    189  1.1  christos 	struct timespec	lastSelectTime;
    190  1.1  christos 	int		lastFdCount;
    191  1.1  christos #endif
    192  1.1  christos 	/* Streams. */
    193  1.1  christos 	evStream	*streams;
    194  1.1  christos 	evStream	*strDone, *strLast;
    195  1.1  christos 	/* Timers. */
    196  1.1  christos 	struct timespec	lastEventTime;
    197  1.1  christos 	heap_context	timers;
    198  1.1  christos 	/* Waits. */
    199  1.1  christos 	evWaitList	*waitLists;
    200  1.1  christos 	evWaitList	waitDone;
    201  1.1  christos } evContext_p;
    202  1.1  christos 
    203  1.1  christos /* eventlib.c */
    204  1.1  christos #define evPrintf __evPrintf
    205  1.1  christos void evPrintf(const evContext_p *ctx, int level, const char *fmt, ...)
    206  1.1  christos      ISC_FORMAT_PRINTF(3, 4);
    207  1.1  christos 
    208  1.1  christos /* ev_timers.c */
    209  1.1  christos #define evCreateTimers __evCreateTimers
    210  1.1  christos heap_context evCreateTimers(const evContext_p *);
    211  1.1  christos #define evDestroyTimers __evDestroyTimers
    212  1.1  christos void evDestroyTimers(const evContext_p *);
    213  1.1  christos 
    214  1.1  christos /* ev_waits.c */
    215  1.1  christos #define evFreeWait __evFreeWait
    216  1.1  christos evWait *evFreeWait(evContext_p *ctx, evWait *old);
    217  1.1  christos 
    218  1.1  christos /* Global options */
    219  1.1  christos int		__evOptMonoTime;
    220  1.1  christos 
    221  1.1  christos #endif /*_EVENTLIB_P_H*/
    222