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