chiark / gitweb /
Finalise 0.4.0
[secnet.git] / comm-common.c
1 /*
2  * This file is part of secnet.
3  * See README for full list of copyright holders.
4  *
5  * secnet is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version d of the License, or
8  * (at your option) any later version.
9  * 
10  * secnet is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * version 3 along with secnet; if not, see
17  * https://www.gnu.org/licenses/gpl.html.
18  */
19
20 #include "secnet.h"
21 #include "comm-common.h"
22
23 void comm_request_notify(void *commst, void *nst, comm_notify_fn *fn)
24 {
25     struct commcommon *st=commst;
26     struct comm_notify_entry *n;
27     
28     NEW(n);
29     n->fn=fn;
30     n->state=nst;
31     LIST_INSERT_HEAD(&st->notify, n, entry);
32 }
33
34 void comm_release_notify(void *commst, void *nst, comm_notify_fn *fn)
35 {
36     struct commcommon *st=commst;
37     struct comm_notify_entry *n, *t;
38
39     /* XXX untested */
40     LIST_FOREACH_SAFE(n, &st->notify, entry, t) {
41        if (n->state==nst && n->fn==fn) {
42            LIST_REMOVE(n, entry);
43            free(n);
44        }
45     }
46 }
47
48 bool_t comm_notify(struct comm_notify_list *notify,
49                    struct buffer_if *buf, const struct comm_addr *ca)
50 {
51     struct comm_notify_entry *n;
52
53     LIST_FOREACH(n, notify, entry) {
54         if (n->fn(n->state, buf, ca)) {
55             return True;
56         }
57     }
58     return False;
59 }
60
61 void comm_apply(struct commcommon *cc, void *st)
62 {
63     assert(cc==st);
64     cc->cl.type=CL_COMM;
65     cc->cl.apply=NULL;
66     cc->cl.interface=&cc->ops;
67     cc->ops.st=cc;
68     cc->ops.request_notify=comm_request_notify;
69     cc->ops.release_notify=comm_release_notify;
70     LIST_INIT(&cc->notify);
71     cc->rbuf=NULL;
72 }