chiark / gitweb /
ba9f9d6fe5f0cb095089c37f667aa53c4cc6631b
[secnet.git] / udp.c
1 /* UDP send/receive module for secnet */
2
3 /* This module enables sites to communicate by sending UDP
4  * packets. When an instance of the module is created we can
5  * optionally bind to a particular local IP address (not implemented
6  * yet).
7  *
8  * Sites register an interest in local port numbers for receiving
9  * packets, and can also send packets. We don't care about the source
10  * port number for sending packets. 
11  *
12  * Packets are offered to registered receivers in turn. Once one
13  * accepts it, it isn't offered to any more. */
14
15 #include "secnet.h"
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <sys/socket.h>
22 #include "util.h"
23
24 static beforepoll_fn udp_beforepoll;
25 static afterpoll_fn udp_afterpoll;
26 static comm_request_notify_fn request_notify;
27 static comm_release_notify_fn release_notify;
28 static comm_sendmsg_fn udp_sendmsg;
29
30 /* The UDP module exports a pure closure which can be used to construct a
31  * UDP send/receive module. Arguments:
32  */
33
34 struct notify_list {
35     comm_notify_fn *fn;
36     void *state;
37     struct notify_list *next;
38 };
39
40 struct udp {
41     closure_t cl;
42     struct comm_if ops;
43     struct cloc loc;
44     uint16_t port;
45     int fd;
46     struct buffer_if *rbuf;
47     struct notify_list *notify;
48 };
49
50 static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
51                           int *timeout_io, const struct timeval *tv,
52                           uint64_t *now)
53 {
54     struct udp *st=state;
55     if (*nfds_io<1) {
56         *nfds_io=1;
57         return ERANGE;
58     }
59     *nfds_io=1;
60     fds->fd=st->fd;
61     fds->events=POLLIN;
62     return 0;
63 }
64
65 static void udp_afterpoll(void *state, struct pollfd *fds, int nfds,
66                           const struct timeval *tv, uint64_t *now)
67 {
68     struct udp *st=state;
69     struct sockaddr_in from;
70     int fromlen;
71     struct notify_list *n;
72     bool_t done;
73     int rv;
74
75     if (nfds && (fds->revents & POLLIN)) {
76         do {
77             fromlen=sizeof(from);
78             BUF_ASSERT_FREE(st->rbuf);
79             BUF_ALLOC(st->rbuf,"udp_afterpoll");
80             rv=recvfrom(st->fd, st->rbuf->start, st->rbuf->len, 0,
81                         (struct sockaddr *)&from, &fromlen);
82             if (rv>0) {
83                 st->rbuf->size=rv;
84                 done=False;
85                 for (n=st->notify; n; n=n->next) {
86                     if (n->fn(n->state, st->rbuf, &from)) {
87                         done=True;
88                         break;
89                     }
90                 }
91                 if (!done) {
92                     /* XXX manufacture and send NAK packet */
93                     Message(M_WARNING,"Need to send NAK\n");
94                     BUF_FREE(st->rbuf);
95                 }
96                 BUF_ASSERT_FREE(st->rbuf);
97             } else {
98                 BUF_FREE(st->rbuf);
99             }
100         } while (rv>=0);
101     }
102 }
103
104 static void request_notify(void *commst, void *nst, comm_notify_fn *fn)
105 {
106     struct udp *st=commst;
107     struct notify_list *n;
108     
109     n=safe_malloc(sizeof(*n),"request_notify");
110     n->fn=fn;
111     n->state=nst;
112     n->next=st->notify;
113     st->notify=n;
114 }
115
116 static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
117 {
118     struct udp *st=commst;
119     struct notify_list *n, **p, *t;
120
121     /* XXX untested */
122     p=&st->notify;
123     for (n=st->notify; n; )
124     {
125         if (n->state==nst && n->fn==fn) {
126             t=n;
127             *p=n->next;
128             n=n->next;
129             free(t);
130         } else {
131             p=&n->next;
132             n=n->next;
133         }
134     }
135 }
136
137 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
138                           struct sockaddr_in *dest)
139 {
140     struct udp *st=commst;
141
142     /* XXX fix error reporting */
143     sendto(st->fd, buf->start, buf->size, 0,
144            (struct sockaddr *)dest, sizeof(*dest));
145
146     return True;
147 }
148
149 static void udp_phase_hook(void *sst, uint32_t new_phase)
150 {
151     struct udp *st=sst;
152     struct sockaddr_in addr;
153
154     st->fd=socket(AF_INET, SOCK_DGRAM, 0);
155     if (st->fd<0) {
156         fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line);
157     }
158     if (fcntl(st->fd, F_SETFL, fcntl(st->fd, F_GETFL)|O_NONBLOCK)==-1) {
159         fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
160                      st->loc.file,st->loc.line);
161     }
162     if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) {
163         fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)",
164                      st->loc.file,st->loc.line);
165     }
166
167     memset(&addr, 0, sizeof(addr));
168     addr.sin_family=AF_INET;
169     addr.sin_port=htons(st->port);
170     if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) {
171         fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line);
172     }
173
174     register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp");
175 }
176
177 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
178                          list_t *args)
179 {
180     struct udp *st;
181     item_t *i;
182     dict_t *d;
183
184     st=safe_malloc(sizeof(*st),"udp_apply(st)");
185     st->loc=loc;
186     st->cl.description="udp";
187     st->cl.type=CL_COMM;
188     st->cl.apply=NULL;
189     st->cl.interface=&st->ops;
190     st->ops.st=st;
191     st->ops.request_notify=request_notify;
192     st->ops.release_notify=release_notify;
193     st->ops.sendmsg=udp_sendmsg;
194     st->port=0;
195
196     i=list_elem(args,0);
197     if (!i || i->type!=t_dict) {
198         cfgfatal(st->loc,"udp","first argument must be a dictionary\n");
199     }
200     d=i->data.dict;
201
202     st->port=dict_read_number(d,"port",True,"udp",st->loc,0);
203     st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc);
204
205     add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
206
207     return new_closure(&st->cl);
208 }
209
210 init_module udp_module;
211 void udp_module(dict_t *dict)
212 {
213     add_closure(dict,"udp",udp_apply);
214 }