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