chiark / gitweb /
Import release 0.1.10
[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 <sys/wait.h>
23 #include "util.h"
24
25 static beforepoll_fn udp_beforepoll;
26 static afterpoll_fn udp_afterpoll;
27 static comm_request_notify_fn request_notify;
28 static comm_release_notify_fn release_notify;
29 static comm_sendmsg_fn udp_sendmsg;
30
31 /* The UDP module exports a pure closure which can be used to construct a
32  * UDP send/receive module. Arguments:
33  */
34
35 struct notify_list {
36     comm_notify_fn *fn;
37     void *state;
38     struct notify_list *next;
39 };
40
41 struct udp {
42     closure_t cl;
43     struct comm_if ops;
44     struct cloc loc;
45     uint16_t port;
46     int fd;
47     string_t authbind;
48     struct buffer_if *rbuf;
49     struct notify_list *notify;
50 };
51
52 static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
53                           int *timeout_io, const struct timeval *tv,
54                           uint64_t *now)
55 {
56     struct udp *st=state;
57     if (*nfds_io<1) {
58         *nfds_io=1;
59         return ERANGE;
60     }
61     *nfds_io=1;
62     fds->fd=st->fd;
63     fds->events=POLLIN;
64     return 0;
65 }
66
67 static void udp_afterpoll(void *state, struct pollfd *fds, int nfds,
68                           const struct timeval *tv, uint64_t *now)
69 {
70     struct udp *st=state;
71     struct sockaddr_in from;
72     int fromlen;
73     struct notify_list *n;
74     bool_t done;
75     int rv;
76
77     if (nfds && (fds->revents & POLLIN)) {
78         do {
79             fromlen=sizeof(from);
80             BUF_ASSERT_FREE(st->rbuf);
81             BUF_ALLOC(st->rbuf,"udp_afterpoll");
82             rv=recvfrom(st->fd, st->rbuf->start, st->rbuf->len, 0,
83                         (struct sockaddr *)&from, &fromlen);
84             if (rv>0) {
85                 st->rbuf->size=rv;
86                 done=False;
87                 for (n=st->notify; n; n=n->next) {
88                     if (n->fn(n->state, st->rbuf, &from)) {
89                         done=True;
90                         break;
91                     }
92                 }
93                 if (!done) {
94                     /* XXX manufacture and send NAK packet */
95                     Message(M_WARNING,"Need to send NAK\n");
96                     BUF_FREE(st->rbuf);
97                 }
98                 BUF_ASSERT_FREE(st->rbuf);
99             } else {
100                 BUF_FREE(st->rbuf);
101             }
102         } while (rv>=0);
103     }
104 }
105
106 static void request_notify(void *commst, void *nst, comm_notify_fn *fn)
107 {
108     struct udp *st=commst;
109     struct notify_list *n;
110     
111     n=safe_malloc(sizeof(*n),"request_notify");
112     n->fn=fn;
113     n->state=nst;
114     n->next=st->notify;
115     st->notify=n;
116 }
117
118 static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
119 {
120     struct udp *st=commst;
121     struct notify_list *n, **p, *t;
122
123     /* XXX untested */
124     p=&st->notify;
125     for (n=st->notify; n; )
126     {
127         if (n->state==nst && n->fn==fn) {
128             t=n;
129             *p=n->next;
130             n=n->next;
131             free(t);
132         } else {
133             p=&n->next;
134             n=n->next;
135         }
136     }
137 }
138
139 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
140                           struct sockaddr_in *dest)
141 {
142     struct udp *st=commst;
143
144     /* XXX fix error reporting */
145     sendto(st->fd, buf->start, buf->size, 0,
146            (struct sockaddr *)dest, sizeof(*dest));
147
148     return True;
149 }
150
151 static void udp_phase_hook(void *sst, uint32_t new_phase)
152 {
153     struct udp *st=sst;
154     struct sockaddr_in addr;
155
156     st->fd=socket(AF_INET, SOCK_DGRAM, 0);
157     if (st->fd<0) {
158         fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line);
159     }
160     if (fcntl(st->fd, F_SETFL, fcntl(st->fd, F_GETFL)|O_NONBLOCK)==-1) {
161         fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
162                      st->loc.file,st->loc.line);
163     }
164     if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) {
165         fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)",
166                      st->loc.file,st->loc.line);
167     }
168
169     memset(&addr, 0, sizeof(addr));
170     addr.sin_family=AF_INET;
171     addr.sin_port=htons(st->port);
172     if (st->authbind) {
173         pid_t c;
174         int status;
175
176         /* XXX this fork() and waitpid() business needs to be hidden
177            in some system-specific library functions. */
178         c=fork();
179         if (c==-1) {
180             fatal_perror("udp_phase_hook: fork() for authbind");
181         }
182         if (c==0) {
183             char *argv[4];
184             argv[0]=st->authbind;
185             argv[1]="00000000";
186             argv[2]=alloca(8);
187             if (!argv[2]) exit(ENOMEM);
188             sprintf(argv[2],"%04X",htons(st->port));
189             argv[3]=NULL;
190             dup2(st->fd,0);
191             execvp(st->authbind,argv);
192             exit(ENOEXEC);
193         }
194         waitpid(c,&status,0);
195         if (WEXITSTATUS(status)!=0) {
196             errno=WEXITSTATUS(status);
197             fatal_perror("udp (%s:%d): authbind",st->loc.file,st->loc.line);
198         }
199     } else {
200         if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) {
201             fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line);
202         }
203     }
204
205     register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp");
206 }
207
208 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
209                          list_t *args)
210 {
211     struct udp *st;
212     item_t *i;
213     dict_t *d;
214
215     st=safe_malloc(sizeof(*st),"udp_apply(st)");
216     st->loc=loc;
217     st->cl.description="udp";
218     st->cl.type=CL_COMM;
219     st->cl.apply=NULL;
220     st->cl.interface=&st->ops;
221     st->ops.st=st;
222     st->ops.request_notify=request_notify;
223     st->ops.release_notify=release_notify;
224     st->ops.sendmsg=udp_sendmsg;
225     st->port=0;
226
227     i=list_elem(args,0);
228     if (!i || i->type!=t_dict) {
229         cfgfatal(st->loc,"udp","first argument must be a dictionary\n");
230     }
231     d=i->data.dict;
232
233     st->port=dict_read_number(d,"port",True,"udp",st->loc,0);
234     st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc);
235     st->authbind=dict_read_string(d,"authbind",False,"udp",st->loc);
236
237     add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
238
239     return new_closure(&st->cl);
240 }
241
242 init_module udp_module;
243 void udp_module(dict_t *dict)
244 {
245     add_closure(dict,"udp",udp_apply);
246 }