fds: Make many fds nonblocking
[secnet] / comm-common.c
CommitLineData
54d5ef00
IJ
1
2#include "secnet.h"
3#include "comm-common.h"
4
5void comm_request_notify(void *commst, void *nst, comm_notify_fn *fn)
6{
7 struct commcommon *st=commst;
8 struct comm_notify_entry *n;
9
10 n=safe_malloc(sizeof(*n),"comm_request_notify");
11 n->fn=fn;
12 n->state=nst;
13 LIST_INSERT_HEAD(&st->notify, n, entry);
14}
15
16void comm_release_notify(void *commst, void *nst, comm_notify_fn *fn)
17{
18 struct commcommon *st=commst;
19 struct comm_notify_entry *n, *t;
20
21 /* XXX untested */
22 LIST_FOREACH_SAFE(n, &st->notify, entry, t) {
23 if (n->state==nst && n->fn==fn) {
24 LIST_REMOVE(n, entry);
25 free(n);
26 }
27 }
28}
29
30bool_t comm_notify(struct comm_notify_list *notify,
31 struct buffer_if *buf, const struct comm_addr *ca)
32{
33 struct comm_notify_entry *n;
34
35 LIST_FOREACH(n, notify, entry) {
36 if (n->fn(n->state, buf, ca)) {
37 return True;
38 }
39 }
40 return False;
41}
42
43void comm_apply(struct commcommon *cc, void *st)
44{
45 assert(cc==st);
46 cc->cl.type=CL_COMM;
47 cc->cl.apply=NULL;
48 cc->cl.interface=&cc->ops;
49 cc->ops.st=cc;
50 cc->ops.request_notify=comm_request_notify;
51 cc->ops.release_notify=comm_release_notify;
52 LIST_INIT(&cc->notify);
53 cc->rbuf=NULL;
54}