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