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>
24 static beforepoll_fn udp_beforepoll;
25 static afterpoll_fn udp_afterpoll;
26 static comm_request_notify_fn request_notify;
27 static comm_release_notify_fn release_notify;
28 static comm_sendmsg_fn udp_sendmsg;
30 /* The UDP module exports a pure closure which can be used to construct a
31 * UDP send/receive module. Arguments:
37 struct notify_list *next;
46 struct buffer_if *rbuf;
47 struct notify_list *notify;
50 static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
51 int *timeout_io, const struct timeval *tv,
65 static void udp_afterpoll(void *state, struct pollfd *fds, int nfds,
66 const struct timeval *tv, uint64_t *now)
69 struct sockaddr_in from;
71 struct notify_list *n;
75 if (nfds && (fds->revents & POLLIN)) {
78 BUF_ASSERT_FREE(st->rbuf);
79 BUF_ALLOC(st->rbuf,"udp_afterpoll");
80 rv=recvfrom(st->fd, st->rbuf->start, st->rbuf->len, 0,
81 (struct sockaddr *)&from, &fromlen);
85 for (n=st->notify; n; n=n->next) {
86 if (n->fn(n->state, st->rbuf, &from)) {
92 /* XXX manufacture and send NAK packet */
93 Message(M_WARNING,"Need to send NAK\n");
96 BUF_ASSERT_FREE(st->rbuf);
104 static void request_notify(void *commst, void *nst, comm_notify_fn *fn)
106 struct udp *st=commst;
107 struct notify_list *n;
109 n=safe_malloc(sizeof(*n),"request_notify");
116 static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
118 struct udp *st=commst;
119 struct notify_list *n, **p, *t;
123 for (n=st->notify; n; )
125 if (n->state==nst && n->fn==fn) {
137 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
138 struct sockaddr_in *dest)
140 struct udp *st=commst;
142 /* XXX fix error reporting */
143 sendto(st->fd, buf->start, buf->size, 0,
144 (struct sockaddr *)dest, sizeof(*dest));
149 static void udp_phase_hook(void *sst, uint32_t new_phase)
152 struct sockaddr_in addr;
154 st->fd=socket(AF_INET, SOCK_DGRAM, 0);
156 fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line);
158 if (fcntl(st->fd, F_SETFL, fcntl(st->fd, F_GETFL)|O_NONBLOCK)==-1) {
159 fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
160 st->loc.file,st->loc.line);
162 if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) {
163 fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)",
164 st->loc.file,st->loc.line);
167 memset(&addr, 0, sizeof(addr));
168 addr.sin_family=AF_INET;
169 addr.sin_port=htons(st->port);
170 if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) {
171 fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line);
174 register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp");
177 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
184 st=safe_malloc(sizeof(*st),"udp_apply(st)");
186 st->cl.description="udp";
189 st->cl.interface=&st->ops;
191 st->ops.request_notify=request_notify;
192 st->ops.release_notify=release_notify;
193 st->ops.sendmsg=udp_sendmsg;
197 if (!i || i->type!=t_dict) {
198 cfgfatal(st->loc,"udp","first argument must be a dictionary\n");
202 st->port=dict_read_number(d,"port",True,"udp",st->loc,0);
203 st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc);
205 add_hook(PHASE_GETRESOURCES,udp_phase_hook,st);
207 return new_closure(&st->cl);
210 init_module udp_module;
211 void udp_module(dict_t *dict)
213 add_closure(dict,"udp",udp_apply);