Debian Codegraph @

src/libevsocket.c

raw symbols SCIP
1 /* librist. Copyright © 2019 SipRadius LLC. All right reserved.
2 * Author: Daniele Lacamera <[email protected]>
3 * Author: Sergio Ammirata, Ph.D. <[email protected]>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #include "common/attributes.h"
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #ifndef _WIN32
14 #include "log-private.h"
15 #endif
16
17 #ifdef _WIN32
18
19 /* librist uses WSAPoll and needs struct pollfd / POLLIN / POLLOUT from
20 * <winsock2.h>, all gated on _WIN32_WINNT >= 0x0600 by mingw-w64. */
21 #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600
22 # undef _WIN32_WINNT
23 # define _WIN32_WINNT 0x0600
24 #endif
25
26 #if !defined(UNDER_CE)
27 # define _NO_OLDNAMES 1
28 # include <io.h>
29 #endif
30
31 #include <winsock2.h>
32 #include "log-private.h"
33
34 /* Type used for the number of file descriptors. */
35 typedef unsigned long int nfds_t;
36
37 #include "contrib/poll_win.c"
38
39 #else
40 # include <poll.h>
41 #endif
42
43 #include "stdio-shim.h"
44 #include "libevsocket.h"
45 #include "socket-shim.h"
46 #include "pthread-shim.h"
47 #include "librist/udpsocket.h"
48
49 struct evsocket_event {
50 int fd;
51 short events;
52 void (*callback)(struct evsocket_ctx *ctx, int fd, short revents, void *arg);
53 void (*err_callback)(struct evsocket_ctx *ctx, int fd, short revents, void *arg);
54 void *arg;
55 struct evsocket_event *next;
56 };
57
58 struct evsocket_ctx {
59 int changed;
60 int n_events;
61 int last_served;
62 struct pollfd *pfd;
63 struct evsocket_event *events;
64 struct evsocket_event *_array;
65 int giveup;
66 struct evsocket_ctx *next;
67 evsocket_poll_func poll_override;
68 void *poll_opaque;
69 };
70 #if !defined(_WIN32) || HAVE_PTHREADS
71 static pthread_mutex_t ctx_list_mutex = PTHREAD_MUTEX_INITIALIZER;
72 #else
73 static pthread_mutex_t ctx_list_mutex;
74 static INIT_ONCE once_var;
75 #endif
76
77 static struct evsocket_ctx *CTX_LIST = NULL;
78
79 static void ctx_add(struct evsocket_ctx *c)
80 {
81 #if defined(_WIN32) && !HAVE_PTHREADS
82 init_mutex_once(&ctx_list_mutex, &once_var);
83 #endif
84 pthread_mutex_lock(&ctx_list_mutex);
85 c->next = CTX_LIST;
86 CTX_LIST = c;
87 pthread_mutex_unlock(&ctx_list_mutex);
88 }
89
90 static void ctx_del(struct evsocket_ctx *delme)
91 {
92 pthread_mutex_lock(&ctx_list_mutex);
93 struct evsocket_ctx *p = NULL, *c = CTX_LIST;
94 while(c) {
95 if (c == delme) {
96 if (p) {
97 p->next = c->next;
98 } else {
99 // removing the head: promote c->next, do not clear CTX_LIST
100 CTX_LIST = c->next;
101 }
102 goto out;
103 }
104
105 p = c;
106 c = c->next;
107 }
108 out:
109 pthread_mutex_unlock(&ctx_list_mutex);
110 }
111
112 struct evsocket_event *evsocket_addevent(struct evsocket_ctx *ctx, int fd, short events,
113 void (*callback)(struct evsocket_ctx *ctx, int fd, short revents, void *arg),
114 void (*err_callback)(struct evsocket_ctx *ctx, int fd, short revents, void *arg),
115 void *arg)
116 {
117 struct evsocket_event *e;
118
119 if (!ctx) {
120 return NULL;
121 }
122
123 e = malloc(sizeof(struct evsocket_event));
124 if (!e) {
125 return e;
126 }
127
128 e->fd = fd;
129 e->events = events;
130 e->callback = callback;
131 e->err_callback = err_callback;
132 e->arg = arg;
133
134 ctx->changed = 1;
135
136 e->next = ctx->events;
137 ctx->events = e;
138 ctx->n_events++;
139 return e;
140 }
141
142 void evsocket_delevent(struct evsocket_ctx *ctx, struct evsocket_event *e)
143 {
144 struct evsocket_event *cur, *prev;
145
146 if (!ctx) {
147 return;
148 }
149
150 ctx->changed = 1;
151 cur = ctx->events;
152 prev = NULL;
153
154 while(cur) {
155 if (cur == e) {
156 if (!prev) {
157 ctx->events = e->next;
158 } else {
159 prev->next = e->next;
160 }
161
162 free(e);
163 break;
164 }
165
166 prev = cur;
167 cur = cur->next;
168 }
169 ctx->n_events--;
170 }
171
172
173 static void rebuild_poll(struct evsocket_ctx *ctx)
174 {
175 struct evsocket_event *e;
176 void *ptr = NULL;
177
178 if (!ctx) {
179 return;
180 }
181
182 if (ctx->pfd) {
183 ptr = ctx->pfd;
184 ctx->pfd = NULL;
185 free(ptr);
186 }
187 if (ctx->_array) {
188 ptr = ctx->_array;
189 ctx->_array = NULL;
190 free(ptr);
191 }
192
193 if (ctx->n_events > 0) {
194 ctx->pfd = malloc(sizeof(struct pollfd) * ctx->n_events);
195 ctx->_array = calloc(ctx->n_events, sizeof(struct evsocket_event));
196 }
197
198 if ((!ctx->pfd) || (!ctx->_array)) {
199 /* TODO: notify error, events are disabled.
200 * perhaps provide a context-wide callback for errors.
201 */
202 if (ctx->n_events > 0) {
203 rist_log_priv3( RIST_LOG_ERROR, "libevsocket, rebuild_poll: events are disabled (%d)\n",
204 ctx->n_events);
205 }
206
207 ctx->n_events = 0;
208 ctx->changed = 0;
209 return;
210 }
211
212 int i = 0;
213 e = ctx->events;
214 while(e) {
215 memcpy(ctx->_array + i, e, sizeof(struct evsocket_event));
216 ctx->pfd[i].fd = e->fd;
217 ctx->pfd[i++].events = (e->events & (POLLIN | POLLOUT)) | (POLLHUP | POLLERR);
218 e = e->next;
219 }
220
221 ctx->last_served = 1;
222 ctx->changed = 0;
223 }
224
225
226 static void serve_event(struct evsocket_ctx *ctx, int n)
227 {
228 struct evsocket_event *e = ctx->_array + n;
229
230 if (!ctx) {
231 return;
232 }
233
234 if (n >= ctx->n_events) {
235 rist_log_priv3( RIST_LOG_ERROR, "libevsocket, serve_event: Invalid event %d >= %d\n",
236 n, ctx->n_events);
237 return;
238 }
239
240 if (e) {
241 ctx->last_served = n;
242 if ((ctx->pfd[n].revents & (POLLHUP | POLLERR)) && e->err_callback)
243 e->err_callback(ctx, e->fd, ctx->pfd[n].revents, e->arg);
244 else if (e->callback) {
245 e->callback(ctx, e->fd, ctx->pfd[n].revents, e->arg);
246 }
247 }
248 }
249
250
251 /*** PUBLIC API ***/
252
253 struct evsocket_ctx *evsocket_create(void)
254 {
255 struct evsocket_ctx *ctx;
256
257 pthread_mutex_init(&ctx_list_mutex, NULL);
258
259 ctx = calloc(1, sizeof(struct evsocket_ctx));
260 if (!ctx) {
261 return NULL;
262 }
263
264 ctx->giveup = 0;
265 ctx->n_events = 0;
266 ctx->changed = 0;
267 ctx_add(ctx);
268 return ctx;
269 }
270
271 void evsocket_loop(struct evsocket_ctx *ctx, int timeout)
272 {
273 /* main loop */
274 for(;;) {
275 if (!ctx || ctx->giveup)
276 break;
277 evsocket_loop_single(ctx, timeout, 10);
278 }
279 }
280
281 int evsocket_loop_single(struct evsocket_ctx *ctx, int timeout, int max_events)
282 {
283 int pollret, i;
284 int event_count = 0;
285 int retval = 0;
286
287 if (!ctx || ctx->giveup) {
288 retval = -1;
289 goto loop_error;
290 }
291
292 if (ctx->changed) {
293 //rist_log_priv3( RIST_LOG_DEBUG, "libevsocket, evsocket_loop_single: rebuild poll\n");
294 rebuild_poll(ctx);
295 }
296
297 if (ctx->pfd == NULL) {
298 //rist_log_priv3( RIST_LOG_DEBUG, "libevsocket, evsocket_loop_single: ctx->pfd is null, no events?\n");
299 ctx->changed = 1;
300 retval = -2;
301 goto loop_error;
302 }
303
304 if (ctx->n_events < 1) {
305 rist_log_priv3( RIST_LOG_ERROR, "libevsocket, evsocket_loop_single: no events (%d)\n",
306 ctx->n_events);
307 retval = -3;
308 goto loop_error;
309 }
310
311 if (ctx->poll_override)
312 pollret = ctx->poll_override(ctx->poll_opaque, ctx->pfd, ctx->n_events, timeout);
313 else
314 pollret = poll(ctx->pfd, ctx->n_events, timeout);
315 if (pollret <= 0) {
316 if (pollret < 0) {
317 rist_log_priv3( RIST_LOG_ERROR, "libevsocket, evsocket_loop: poll returned %d, n_events = %d, error = %d\n",
318 pollret, ctx->n_events, errno);
319 retval = -4;
320 goto loop_error;
321 }
322 // No events, regular timeout
323 return 0;
324 }
325
326 for (i = ctx->last_served +1; i < ctx->n_events; i++) {
327 if (ctx->pfd[i].revents != 0) {
328 serve_event(ctx, i);
329 if (max_events > 0 && ++event_count >= max_events)
330 return 0;
331 }
332 }
333
334 for (i = 0; i <= ctx->last_served; i++) {
335 if (ctx->pfd[i].revents != 0) {
336 serve_event(ctx, i);
337 if (max_events > 0 && ++event_count >= max_events)
338 return 0;
339 }
340 }
341
342 return 0;
343
344 loop_error:
345 if (timeout > 0)
346 usleep(timeout * 1000);
347 return retval;
348 }
349
350 void evsocket_destroy(struct evsocket_ctx *ctx)
351 {
352 ctx_del(ctx);
353 if (ctx->pfd)
354 free(ctx->pfd);
355 if (ctx->_array)
356 free(ctx->_array);
357 free(ctx);
358 ctx = NULL;
359 }
360
361 void evsocket_loop_stop(struct evsocket_ctx *ctx)
362 {
363 if (ctx)
364 ctx->giveup = 1;
365 }
366
367 void evsocket_set_poll_override(struct evsocket_ctx *ctx,
368 evsocket_poll_func func,
369 void *opaque)
370 {
371 if (ctx) {
372 ctx->poll_override = func;
373 ctx->poll_opaque = opaque;
374 }
375 }
376
377 int evsocket_geteventcount(struct evsocket_ctx *ctx)
378 {
379 if (ctx)
380 return ctx->n_events;
381 else
382 return 0;
383 }