Import release 0.1.16
[secnet] / 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 * Packets are offered to registered receivers in turn. Once one
9 * accepts it, it isn't offered to any more. */
10
11 #include "secnet.h"
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <sys/socket.h>
18 #include <sys/wait.h>
19 #include "util.h"
20 #include "unaligned.h"
21 #include "ipaddr.h"
22
23 static beforepoll_fn udp_beforepoll;
24 static afterpoll_fn udp_afterpoll;
25 static comm_request_notify_fn request_notify;
26 static comm_release_notify_fn release_notify;
27 static comm_sendmsg_fn udp_sendmsg;
28
29 struct notify_list {
30 comm_notify_fn *fn;
31 void *state;
32 struct notify_list *next;
33 };
34
35 struct udp {
36 closure_t cl;
37 struct comm_if ops;
38 struct cloc loc;
39 uint32_t addr;
40 uint16_t port;
41 int fd;
42 string_t authbind;
43 struct buffer_if *rbuf;
44 struct notify_list *notify;
45 bool_t use_proxy;
46 struct sockaddr_in proxy;
47 };
48
49 static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
50 int *timeout_io, const struct timeval *tv,
51 uint64_t *now)
52 {
53 struct udp *st=state;
54 if (*nfds_io<1) {
55 *nfds_io=1;
56 return ERANGE;
57 }
58 *nfds_io=1;
59 fds->fd=st->fd;
60 fds->events=POLLIN;
61 return 0;
62 }
63
64 static void udp_afterpoll(void *state, struct pollfd *fds, int nfds,
65 const struct timeval *tv, uint64_t *now)
66 {
67 struct udp *st=state;
68 struct sockaddr_in from;
69 int fromlen;
70 struct notify_list *n;
71 bool_t done;
72 int rv;
73
74 if (nfds && (fds->revents & POLLIN)) {
75 do {
76 fromlen=sizeof(from);
77 BUF_ASSERT_FREE(st->rbuf);
78 BUF_ALLOC(st->rbuf,"udp_afterpoll");
79 rv=recvfrom(st->fd, st->rbuf->start, st->rbuf->len, 0,
80 (struct sockaddr *)&from, &fromlen);
81 if (rv>0) {
82 st->rbuf->size=rv;
83 if (st->use_proxy) {
84 /* Check that the packet came from our poxy server;
85 we shouldn't be contacted directly by anybody else
86 (since they can trivially forge source addresses) */
87 if (memcmp(&from.sin_addr,&st->proxy.sin_addr,4)!=0 ||
88 memcmp(&from.sin_port,&st->proxy.sin_port,2)!=0) {
89 Message(M_INFO,"udp: received packet that's not "
90 "from the proxy\n");
91 BUF_FREE(st->rbuf);
92 continue;
93 }
94 memcpy(&from.sin_addr,buf_unprepend(st->rbuf,4),4);
95 buf_unprepend(st->rbuf,2);
96 memcpy(&from.sin_port,buf_unprepend(st->rbuf,2),2);
97 }
98 done=False;
99 for (n=st->notify; n; n=n->next) {
100 if (n->fn(n->state, st->rbuf, &from)) {
101 done=True;
102 break;
103 }
104 }
105 if (!done) {
106 uint32_t source,dest;
107 /* Manufacture and send NAK packet */
108 source=get_uint32(st->rbuf->start); /* Us */
109 dest=get_uint32(st->rbuf->start+4); /* Them */
110 Message(M_INFO,"udp (port %d): sending NAK\n",st->port);
111 buffer_init(st->rbuf,0);
112 buf_append_uint32(st->rbuf,dest);
113 buf_append_uint32(st->rbuf,source);
114 buf_append_uint32(st->rbuf,0); /* NAK is msg type 0 */
115 sendto(st->fd, st->rbuf->start, st->rbuf->size, 0,
116 (struct sockaddr *)&from, sizeof(from));
117 BUF_FREE(st->rbuf);
118 }
119 BUF_ASSERT_FREE(st->rbuf);
120 } else {
121 BUF_FREE(st->rbuf);
122 }
123 } while (rv>=0);
124 }
125 }
126
127 static void request_notify(void *commst, void *nst, comm_notify_fn *fn)
128 {
129 struct udp *st=commst;
130 struct notify_list *n;
131
132 n=safe_malloc(sizeof(*n),"request_notify");
133 n->fn=fn;
134 n->state=nst;
135 n->next=st->notify;
136 st->notify=n;
137 }
138
139 static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
140 {
141 struct udp *st=commst;
142 struct notify_list *n, **p, *t;
143
144 /* XXX untested */
145 p=&st->notify;
146 for (n=st->notify; n; )
147 {
148 if (n->state==nst && n->fn==fn) {
149 t=n;
150 *p=n->next;
151 n=n->next;
152 free(t);
153 } else {
154 p=&n->next;
155 n=n->next;
156 }
157 }
158 }
159
160 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
161 struct sockaddr_in *dest)
162 {
163 struct udp *st=commst;
164 uint8_t *sa;
165
166 if (st->use_proxy) {
167 sa=buf->start-8;
168 memcpy(sa,&dest->sin_addr,4);
169 memset(sa+4,0,4);
170 memcpy(sa+6,&dest->sin_port,2);
171 sendto(st->fd,sa,buf->size+8,0,(struct sockaddr *)&st->proxy,
172 sizeof(st->proxy));
173 } else {
174 sendto(st->fd, buf->start, buf->size, 0,
175 (struct sockaddr *)dest, sizeof(*dest));
176 }
177
178 return True;
179 }
180
181 static void udp_phase_hook(void *sst, uint32_t new_phase)
182 {
183 struct udp *st=sst;
184 struct sockaddr_in addr;
185
186 st->fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
187 if (st->fd<0) {
188 fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line);
189 }
190 if (fcntl(st->fd, F_SETFL, fcntl(st->fd, F_GETFL)|O_NONBLOCK)==-1) {
191 fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
192 st->loc.file,st->loc.line);
193 }
194 if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) {
195 fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)",
196 st->loc.file,st->loc.line);
197 }
198
199 memset(&addr, 0, sizeof(addr));
200 addr.sin_family=AF_INET;
201 addr.sin_addr.s_addr=htonl(st->addr);
202 addr.sin_port=htons(st->port);
203 if (st->authbind) {
204 pid_t c;
205 int status;
206
207 /* XXX this fork() and waitpid() business needs to be hidden
208 in some system-specific library functions. */
209 c=fork();
210 if (c==-1) {
211 fatal_perror("udp_phase_hook: fork() for authbind");
212 }
213 if (c==0) {
214 char *argv[4], addrstr[9], portstr[5];
215 sprintf(addrstr,"%08lX",(long)st->addr);
216 sprintf(portstr,"%04X",st->port);
217 argv[0]=st->authbind;
218 argv[1]=addrstr;
219 argv[2]=portstr;
220 argv[3]=NULL;
221 dup2(st->fd,0);
222 execvp(st->authbind,argv);
223 _exit(255);
224 }
225 while (waitpid(c,&status,0)==-1) {
226 if (errno==EINTR) continue;
227 fatal_perror("udp (%s:%d): authbind",st->loc.file,st->loc.line);
228 }
229 if (WIFSIGNALED(status)) {
230 fatal("udp (%s:%d): authbind died on signal %d",st->loc.file,
231 st->loc.line, WTERMSIG(status));
232 }
233 if (WIFEXITED(status) && WEXITSTATUS(status)!=0) {
234 fatal("udp (%s:%d): authbind died with status %d",st->loc.file,
235 st->loc.line, WEXITSTATUS(status));
236 }
237 } else {
238 if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) {
239 fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line);
240 }
241 }
242
243 register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp");
244 }
245
246 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
247 list_t *args)
248 {
249 struct udp *st;
250 item_t *i,*j;
251 dict_t *d;
252 list_t *l;
253 uint32_t a;
254
255 st=safe_malloc(sizeof(*st),"udp_apply(st)");
256 st->loc=loc;
257 st->cl.description="udp";
258 st->cl.type=CL_COMM;
259 st->cl.apply=NULL;
260 st->cl.interface=&st->ops;
261 st->ops.st=st;
262 st->ops.min_start_pad=0;
263 st->ops.min_end_pad=0;
264 st->ops.request_notify=request_notify;
265 st->ops.release_notify=release_notify;
266 st->ops.sendmsg=udp_sendmsg;
267 st->port=0;
268 st->use_proxy=False;
269
270 i=list_elem(args,0);
271 if (!i || i->type!=t_dict) {
272 cfgfatal(st->loc,"udp","first argument must be a dictionary\n");
273 }
274 d=i->data.dict;
275
276 j=dict_find_item(d,"address",False,"udp",st->loc);
277 st->addr=j?st->addr=string_item_to_ipaddr(j, "udp"):INADDR_ANY;
278 st->port=dict_read_number(d,"port",True,"udp",st->loc,0);
279 st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc);
280 st->authbind=dict_read_string(d,"authbind",False,"udp",st->loc);
281 l=dict_lookup(d,"proxy");
282 if (l) {
283 st->use_proxy=True;
284 memset(&st->proxy,0,sizeof(st->proxy));
285 st->proxy.sin_family=AF_INET;
286 i=list_elem(l,0);
287 if (!i || i->type!=t_string) {
288 cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
289 }
290 a=string_item_to_ipaddr(i,"proxy");
291 st->proxy.sin_addr.s_addr=htonl(a);
292 i=list_elem(l,1);
293 if (!i || i->type!=t_number) {
294 cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
295 }
296 st->proxy.sin_port=htons(i->data.number);
297 st->ops.min_start_pad=8;
298 }
299
300 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
301
302 return new_closure(&st->cl);
303 }
304
305 init_module udp_module;
306 void udp_module(dict_t *dict)
307 {
308 add_closure(dict,"udp",udp_apply);
309 }