-
Notifications
You must be signed in to change notification settings - Fork 22
/
ndesc.c
287 lines (251 loc) · 7.01 KB
/
ndesc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*-
* Copyright (c) 1997 Dave Richards <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* and exceptions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* 4. The code can not be used by Gary Random, Random Communications, Inc.,
* the employees of Random Communications, Inc. or its subsidiaries,
* including Defiance MUD, without prior written permission from the
* authors.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/time.h>
#include "config.h"
#include "lint.h"
#include "ndesc.h"
/*
* Network Descriptor Manager
*/
static ndesc_t *nd_head;
static ndesc_t *nd_tail;
static ndesc_t **nd_ndesc;
static struct pollfd *pollfds;
static int pollfd_max = 0;
static size_t ndesc_size = 256;
static size_t pollfd_size = 256;
/*
* Initialize the state associated with a given file descriptor. Clear the
* read, write and exception bits in the fd_sets and initialize the associated
* network descriptor object pointer to NULL.
*/
static void
nd_initfd(int fd)
{
nd_ndesc[fd] = NULL;
}
/*
* Initialize the network descriptor module.
*/
void
nd_init(void)
{
nd_head = NULL;
nd_tail = NULL;
pollfds = malloc(sizeof(struct pollfd) * pollfd_size);
nd_ndesc = malloc(sizeof(ndesc_t *) * ndesc_size);
}
/*
* Append a network descriptor object to the list of attached network
* descriptors.
*/
static void
nd_append(ndesc_t *nd)
{
nd->nd_next = NULL;
if ((nd->nd_prev = nd_tail) != NULL)
nd->nd_prev->nd_next = nd;
else
nd_head = nd;
nd_tail = nd;
}
/*
* Remove a network descriptor object from the list of attached network
* descriptors.
*/
static void
nd_remove(ndesc_t *nd)
{
if (nd->nd_next != NULL)
nd->nd_next->nd_prev = nd->nd_prev;
else
nd_tail = nd->nd_prev;
if (nd->nd_prev != NULL)
nd->nd_prev->nd_next = nd->nd_next;
else
nd_head = nd->nd_next;
}
/*
* Return the file descriptor currently associated with a network descriptor
* object. N.B. This can change over time!
*/
int
nd_fd(ndesc_t *nd)
{
return nd->nd_fd;
}
/*
* Return the user context pointer associated with a network descriptor
* object.
*/
void *
nd_vp(ndesc_t *nd)
{
return nd->nd_vp;
}
/*
* Attach a file descriptor to the network descriptor manager.
*/
ndesc_t *
nd_attach(int fd, void *rfunc, void *wfunc, void *xfunc, void *sfunc, void *vp)
{
ndesc_t *nd;
nd = xalloc(sizeof (ndesc_t));
nd->nd_fd = fd;
nd->nd_mask = 0;
nd->nd_rfunc = rfunc;
nd->nd_wfunc = wfunc;
nd->nd_xfunc = xfunc;
nd->nd_sfunc = sfunc;
nd->nd_vp = vp;
nd->nd_poll = -1;
nd_append(nd);
if (fd >= ndesc_size) {
ndesc_size *= 2;
nd_ndesc = realloc(nd_ndesc, ndesc_size * sizeof(ndesc_t *));
}
nd_ndesc[fd] = nd;
nd->nd_poll = pollfd_max;
if (nd->nd_poll >= pollfd_size) {
pollfd_size *= 2;
pollfds = realloc(pollfds, pollfd_size * sizeof(struct pollfd));
}
pollfd_max++;
pollfds[nd->nd_poll].fd = fd;
pollfds[nd->nd_poll].events = 0;
return nd;
}
/*
* Detach a file descriptor from the network descriptor manager.
*/
void
nd_detach(ndesc_t *nd)
{
nd_remove(nd);
nd_initfd(nd->nd_fd);
pollfd_max--;
if (nd->nd_poll < pollfd_max) {
ndesc_t *tail = nd_ndesc[pollfds[pollfd_max].fd];
pollfds[nd->nd_poll] = pollfds[pollfd_max];
tail->nd_poll = nd->nd_poll;
}
free(nd);
}
/*
* Enable read, write, exception and/or clean-up callbacks for a
* network descriptor.
*/
void
nd_enable(ndesc_t *nd, int mask)
{
nd->nd_mask |= mask & ND_MASK;
if (mask & ND_R)
pollfds[nd->nd_poll].events |= POLLIN;
if (mask & ND_W)
pollfds[nd->nd_poll].events |= POLLOUT;
if (mask & ND_X)
pollfds[nd->nd_poll].events |= POLLPRI;
}
/*
* Disable read, write, exception and/or clean-up callbacks for a
* network descriptor.
*/
void
nd_disable(ndesc_t *nd, int mask)
{
nd->nd_mask &= ~mask;
if (mask & ND_R)
pollfds[nd->nd_poll].events &= ~POLLIN;
if (mask & ND_W)
pollfds[nd->nd_poll].events &= ~POLLOUT;
if (mask & ND_X)
pollfds[nd->nd_poll].events &= ~POLLPRI;
}
/*
* Perform a select() on all network descriptors and call the read, write
* and exception callback functions for those descriptors which select()
* true. The callbacks are invoked in the following order: exception
* callback, write callback and read callback. If a clean-up callback
* has been enabled, call it last.
*/
void
nd_select(struct timeval *tvp)
{
int nfds;
ndesc_t *nd;
struct timeval tv = *tvp;
struct timespec ts;
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000;
/* Nothing else to process so wait on I/O */
nfds = ppoll(pollfds, pollfd_max, &ts, NULL);
if (nfds == -1) /* Error!!! */
return;
for (int pfd = 0; pfd < pollfd_max; pfd++) {
nd = nd_ndesc[pollfds[pfd].fd];
if (pollfds[pfd].revents & POLLPRI) {
(*nd->nd_xfunc)(nd, nd->nd_vp);
pollfds[pfd].revents = 0;
}
if (pollfds[pfd].revents & POLLOUT) {
(*nd->nd_wfunc)(nd, nd->nd_vp);
pollfds[pfd].revents = 0;
}
if (pollfds[pfd].revents & POLLIN) {
(*nd->nd_rfunc)(nd, nd->nd_vp);
pollfds[pfd].revents = 0;
}
}
}
/*
* Invoke the shutdown method for all network descriptors.
*/
void
nd_shutdown(void)
{
struct timeval tv;
ndesc_t *nd, *next;
tv.tv_sec = tv.tv_usec = 0;
nd_select(&tv);
for (nd = nd_head; nd != NULL; nd = next)
{
next = nd->nd_next;
if (nd->nd_sfunc != NULL)
(*nd->nd_sfunc)(nd, nd->nd_vp);
}
}