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 * 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.
12 * Packets are offered to registered receivers in turn. Once one
13 * accepts it, it isn't offered to any more. */
21 #include <sys/socket.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;
31 /* The UDP module exports a pure closure which can be used to construct a
32 * UDP send/receive module. Arguments:
38 struct notify_list *next;
48 struct buffer_if *rbuf;
49 struct notify_list *notify;
52 static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
53 int *timeout_io, const struct timeval *tv,
67 static void udp_afterpoll(void *state, struct pollfd *fds, int nfds,
68 const struct timeval *tv, uint64_t *now)
71 struct sockaddr_in from;
73 struct notify_list *n;
77 if (nfds && (fds->revents & POLLIN)) {
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);
87 for (n=st->notify; n; n=n->next) {
88 if (n->fn(n->state, st->rbuf, &from)) {
94 /* XXX manufacture and send NAK packet */
95 Message(M_WARNING,"Need to send NAK\n");
98 BUF_ASSERT_FREE(st->rbuf);
106 static void request_notify(void *commst, void *nst, comm_notify_fn *fn)
108 struct udp *st=commst;
109 struct notify_list *n;
111 n=safe_malloc(sizeof(*n),"request_notify");
118 static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
120 struct udp *st=commst;
121 struct notify_list *n, **p, *t;
125 for (n=st->notify; n; )
127 if (n->state==nst && n->fn==fn) {
139 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
140 struct sockaddr_in *dest)
142 struct udp *st=commst;
144 /* XXX fix error reporting */
145 sendto(st->fd, buf->start, buf->size, 0,
146 (struct sockaddr *)dest, sizeof(*dest));
151 static void udp_phase_hook(void *sst, uint32_t new_phase)
154 struct sockaddr_in addr;
156 st->fd=socket(AF_INET, SOCK_DGRAM, 0);
158 fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line);
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);
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);
169 memset(&addr, 0, sizeof(addr));
170 addr.sin_family=AF_INET;
171 addr.sin_port=htons(st->port);
176 /* XXX this fork() and waitpid() business needs to be hidden
177 in some system-specific library functions. */
180 fatal_perror("udp_phase_hook: fork() for authbind");
184 argv[0]=st->authbind;
187 if (!argv[2]) exit(ENOMEM);
188 sprintf(argv[2],"%04X",htons(st->port));
191 execvp(st->authbind,argv);
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);
200 if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) {
201 fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line);
205 register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp");
208 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
215 st=safe_malloc(sizeof(*st),"udp_apply(st)");
217 st->cl.description="udp";
220 st->cl.interface=&st->ops;
222 st->ops.request_notify=request_notify;
223 st->ops.release_notify=release_notify;
224 st->ops.sendmsg=udp_sendmsg;
228 if (!i || i->type!=t_dict) {
229 cfgfatal(st->loc,"udp","first argument must be a dictionary\n");
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);
237 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
239 return new_closure(&st->cl);
242 init_module udp_module;
243 void udp_module(dict_t *dict)
245 add_closure(dict,"udp",udp_apply);