1 /* UDP send/receive module for secnet */
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
8 * Packets are offered to registered receivers in turn. Once one
9 * accepts it, it isn't offered to any more. */
17 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
22 #include "unaligned.h"
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;
34 struct notify_list *next;
38 string_t address; /* DNS name for bootstrapping, optional */
39 int remoteport; /* Port for bootstrapping, optional */
50 struct buffer_if *rbuf;
51 struct notify_list *notify;
52 struct resolver_if *resolver;
54 struct sockaddr_in proxy;
57 static const char *saddr_to_string(const struct sockaddr_in *sin) {
58 static char bufs[2][100];
62 snprintf(bufs[b], sizeof(bufs[b]), "[%s]:%d",
63 inet_ntoa(sin->sin_addr),
64 ntohs(sin->sin_port));
68 static const char *addr_to_string(void *commst, const struct comm_addr *ca) {
69 struct udp *st=commst;
70 static char sbuf[100];
72 struct sockaddr_in la;
73 la.sin_addr.s_addr=htonl(st->addr);
74 la.sin_port=htons(st->port);
76 snprintf(sbuf, sizeof(sbuf), "udp:%s-%s",
77 saddr_to_string(&la), saddr_to_string(&ca->priv.sin));
81 struct peer_addr_cst {
83 struct peer_config *cfg;
84 comm_peer_addr_answer_fn *cb;
88 static void *peer_addr_config(void *commst, dict_t *dict, struct cloc loc,
91 struct peer_config *cfg;
92 cfg=safe_malloc(sizeof(*cfg),"peer addr config");
93 cfg->address=dict_read_string(dict, "address",False,desc,loc);
95 cfg->remoteport=dict_read_number(dict,"port",True,desc,loc,0);
96 else cfg->remoteport=0;
103 static void peer_resolve_callback(void *sst, struct in_addr *address)
105 struct peer_addr_cst *pacst=sst;
106 struct udp *st=pacst->udp;
107 struct peer_config *cfg=pacst->cfg;
112 ca.priv.sin.sin_family=AF_INET;
113 ca.priv.sin.sin_port=htons(cfg->remoteport);
114 ca.priv.sin.sin_addr=*address;
115 pacst->cb(pacst->cst,&ca);
117 pacst->cb(pacst->cst,0);
122 static bool_t peer_addr_request(void *commst, void *from_peer_config,
123 comm_peer_addr_answer_fn *cb, void *cst)
125 struct udp *st=commst;
126 struct peer_config *cfg=from_peer_config;
127 struct peer_addr_cst *pacst;
128 pacst=safe_malloc(sizeof(*pacst),"udp peer addr request cst");
133 bool_t ok=st->resolver->request(st->resolver->st,cfg->address,
134 peer_resolve_callback,pacst);
140 static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
143 struct udp *st=state;
154 static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
156 struct udp *st=state;
157 struct sockaddr_in from;
159 struct notify_list *n;
163 if (nfds && (fds->revents & POLLIN)) {
166 fromlen=sizeof(from);
167 BUF_ASSERT_FREE(st->rbuf);
168 BUF_ALLOC(st->rbuf,"udp_afterpoll");
169 rv=recvfrom(st->fd, st->rbuf->start, st->rbuf->len, 0,
170 (struct sockaddr *)&from, &fromlen);
174 /* Check that the packet came from our poxy server;
175 we shouldn't be contacted directly by anybody else
176 (since they can trivially forge source addresses) */
177 if (memcmp(&from.sin_addr,&st->proxy.sin_addr,4)!=0 ||
178 memcmp(&from.sin_port,&st->proxy.sin_port,2)!=0) {
179 Message(M_INFO,"udp: received packet that's not "
184 memcpy(&from.sin_addr,buf_unprepend(st->rbuf,4),4);
185 buf_unprepend(st->rbuf,2);
186 memcpy(&from.sin_port,buf_unprepend(st->rbuf,2),2);
189 for (n=st->notify; n; n=n->next) {
194 if (n->fn(n->state, st->rbuf, &ca)) {
200 uint32_t source,dest;
201 /* Manufacture and send NAK packet */
202 source=get_uint32(st->rbuf->start); /* Us */
203 dest=get_uint32(st->rbuf->start+4); /* Them */
204 Message(M_INFO,"udp (port %d): sending NAK\n",st->port);
205 buffer_init(st->rbuf,0);
206 buf_append_uint32(st->rbuf,dest);
207 buf_append_uint32(st->rbuf,source);
208 buf_append_uint32(st->rbuf,0); /* NAK is msg type 0 */
209 sendto(st->fd, st->rbuf->start, st->rbuf->size, 0,
210 (struct sockaddr *)&from, sizeof(from));
213 BUF_ASSERT_FREE(st->rbuf);
221 static void request_notify(void *commst, void *nst, comm_notify_fn *fn)
223 struct udp *st=commst;
224 struct notify_list *n;
226 n=safe_malloc(sizeof(*n),"request_notify");
233 static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
235 struct udp *st=commst;
236 struct notify_list *n, **p, *t;
240 for (n=st->notify; n; )
242 if (n->state==nst && n->fn==fn) {
254 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
255 const struct comm_addr *dest)
257 struct udp *st=commst;
262 memcpy(sa,&dest->priv.sin.sin_addr,4);
264 memcpy(sa+6,&dest->priv.sin.sin_port,2);
265 sendto(st->fd,sa,buf->size+8,0,(struct sockaddr *)&st->proxy,
268 sendto(st->fd, buf->start, buf->size, 0,
269 (struct sockaddr *)&dest->priv.sin, sizeof(dest->priv.sin));
275 static void udp_phase_hook(void *sst, uint32_t new_phase)
278 struct sockaddr_in addr;
280 st->fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
282 fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line);
284 if (fcntl(st->fd, F_SETFL, fcntl(st->fd, F_GETFL)|O_NONBLOCK)==-1) {
285 fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
286 st->loc.file,st->loc.line);
288 if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) {
289 fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)",
290 st->loc.file,st->loc.line);
294 addr.sin_family=AF_INET;
295 addr.sin_addr.s_addr=htonl(st->addr);
296 addr.sin_port=htons(st->port);
301 /* XXX this fork() and waitpid() business needs to be hidden
302 in some system-specific library functions. */
305 fatal_perror("udp_phase_hook: fork() for authbind");
308 char *argv[4], addrstr[9], portstr[5];
309 sprintf(addrstr,"%08lX",(long)st->addr);
310 sprintf(portstr,"%04X",st->port);
311 argv[0]=st->authbind;
316 execvp(st->authbind,argv);
319 while (waitpid(c,&status,0)==-1) {
320 if (errno==EINTR) continue;
321 fatal_perror("udp (%s:%d): authbind",st->loc.file,st->loc.line);
323 if (WIFSIGNALED(status)) {
324 fatal("udp (%s:%d): authbind died on signal %d",st->loc.file,
325 st->loc.line, WTERMSIG(status));
327 if (WIFEXITED(status) && WEXITSTATUS(status)!=0) {
328 fatal("udp (%s:%d): authbind died with status %d",st->loc.file,
329 st->loc.line, WEXITSTATUS(status));
332 if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) {
333 fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line);
337 register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp");
340 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
349 st=safe_malloc(sizeof(*st),"udp_apply(st)");
351 st->cl.description="udp";
354 st->cl.interface=&st->ops;
356 st->ops.min_start_pad=0;
357 st->ops.min_end_pad=0;
358 st->ops.request_notify=request_notify;
359 st->ops.release_notify=release_notify;
360 st->ops.sendmsg=udp_sendmsg;
361 st->ops.peer_addr_config=peer_addr_config;
362 st->ops.peer_addr_request=peer_addr_request;
363 st->ops.addr_to_string=addr_to_string;
368 if (!i || i->type!=t_dict) {
369 cfgfatal(st->loc,"udp","first argument must be a dictionary\n");
373 j=dict_find_item(d,"address",False,"udp",st->loc);
374 st->addr=j?string_item_to_ipaddr(j, "udp"):INADDR_ANY;
375 st->port=dict_read_number(d,"port",True,"udp",st->loc,0);
376 st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc);
377 st->authbind=dict_read_string(d,"authbind",False,"udp",st->loc);
378 l=dict_lookup(d,"proxy");
382 st->proxy.sin_family=AF_INET;
384 if (!i || i->type!=t_string) {
385 cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
387 a=string_item_to_ipaddr(i,"proxy");
388 st->proxy.sin_addr.s_addr=htonl(a);
390 if (!i || i->type!=t_number) {
391 cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
393 st->proxy.sin_port=htons(i->data.number);
394 st->ops.min_start_pad=8;
396 st->resolver=find_cl_if(d,"resolver",CL_RESOLVER,True,"comm",loc);
398 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
400 return new_closure(&st->cl);
403 void udp_module(dict_t *dict)
405 add_closure(dict,"udp",udp_apply);