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. */
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
27 static beforepoll_fn udp_beforepoll;
28 static afterpoll_fn udp_afterpoll;
29 static comm_request_notify_fn request_notify;
30 static comm_release_notify_fn release_notify;
31 static comm_sendmsg_fn udp_sendmsg;
33 /* The UDP module exports a pure closure which can be used to construct a
34 * UDP send/receive module. Arguments:
40 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 list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
157 uint16_t local_port=0;
158 struct sockaddr_in addr;
160 st=safe_malloc(sizeof(*st),"udp_apply(st)");
162 st->cl.description="udp";
165 st->cl.interface=&st->ops;
167 st->ops.request_notify=request_notify;
168 st->ops.release_notify=release_notify;
169 st->ops.sendmsg=udp_sendmsg;
172 if (!i || i->type!=t_dict) {
173 cfgfatal(st->loc,"udp","first argument must be a dictionary\n");
177 local_port=dict_read_number(d,"port",False,"udp",st->loc,0);
178 st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc);
180 st->fd=socket(AF_INET, SOCK_DGRAM, 0);
182 fatal_perror("udp_apply (%s:%d): socket",loc.file,loc.line);
184 if (fcntl(st->fd, F_SETFL, fcntl(st->fd, F_GETFL)|O_NONBLOCK)==-1) {
185 fatal_perror("udp_apply (%s:%d): fcntl(set O_NONBLOCK)",
188 if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) {
189 fatal_perror("udp_apply (%s:%d): fcntl(set FD_CLOEXEC)",
193 memset(&addr, 0, sizeof(addr));
194 addr.sin_family=AF_INET;
196 addr.sin_port=htons(local_port);
198 if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) {
199 fatal_perror("udp_apply (%s:%d): bind",loc.file,loc.line);
202 register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp");
204 return new_closure(&st->cl);
207 init_module udp_module;
208 void udp_module(dict_t *dict)
210 add_closure(dict,"udp",udp_apply);