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;
45 struct buffer_if *rbuf;
46 struct notify_list *notify;
49 static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
50 int *timeout_io, const struct timeval *tv,
64 static void udp_afterpoll(void *state, struct pollfd *fds, int nfds,
65 const struct timeval *tv, uint64_t *now)
68 struct sockaddr_in from;
70 struct notify_list *n;
74 if (nfds && (fds->revents & POLLIN)) {
77 BUF_ASSERT_FREE(st->rbuf);
78 BUF_ALLOC(st->rbuf,"udp_afterpoll");
79 rv=recvfrom(st->fd, st->rbuf->start, st->rbuf->len, 0,
80 (struct sockaddr *)&from, &fromlen);
84 for (n=st->notify; n; n=n->next) {
85 if (n->fn(n->state, st->rbuf, &from)) {
91 /* XXX manufacture and send NAK packet */
92 Message(M_WARNING,"Need to send NAK\n");
95 BUF_ASSERT_FREE(st->rbuf);
103 static void request_notify(void *commst, void *nst, comm_notify_fn *fn)
105 struct udp *st=commst;
106 struct notify_list *n;
108 n=safe_malloc(sizeof(*n),"request_notify");
115 static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
117 struct udp *st=commst;
118 struct notify_list *n, **p, *t;
122 for (n=st->notify; n; )
124 if (n->state==nst && n->fn==fn) {
136 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
137 struct sockaddr_in *dest)
139 struct udp *st=commst;
141 /* XXX fix error reporting */
142 sendto(st->fd, buf->start, buf->size, 0,
143 (struct sockaddr *)dest, sizeof(*dest));
148 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
154 uint16_t local_port=0;
155 struct sockaddr_in addr;
157 st=safe_malloc(sizeof(*st),"udp_apply(st)");
159 st->cl.description="udp";
162 st->cl.interface=&st->ops;
164 st->ops.request_notify=request_notify;
165 st->ops.release_notify=release_notify;
166 st->ops.sendmsg=udp_sendmsg;
169 if (!i || i->type!=t_dict) {
170 cfgfatal(st->loc,"udp","first argument must be a dictionary\n");
174 local_port=dict_read_number(d,"port",False,"udp",st->loc,0);
175 st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc);
177 st->fd=socket(AF_INET, SOCK_DGRAM, 0);
179 fatal_perror("udp_apply (%s:%d): socket",loc.file,loc.line);
181 if (fcntl(st->fd, F_SETFL, fcntl(st->fd, F_GETFL)|O_NONBLOCK)==-1) {
182 fatal_perror("udp_apply (%s:%d): fcntl(set O_NONBLOCK)",
185 if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) {
186 fatal_perror("udp_apply (%s:%d): fcntl(set FD_CLOEXEC)",
190 memset(&addr, 0, sizeof(addr));
191 addr.sin_family=AF_INET;
193 addr.sin_port=htons(local_port);
195 if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) {
196 fatal_perror("udp_apply (%s:%d): bind",loc.file,loc.line);
199 register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp");
201 return new_closure(&st->cl);
204 init_module udp_module;
205 void udp_module(dict_t *dict)
207 add_closure(dict,"udp",udp_apply);