| 1 | /* UDP send/receive module for secnet */ |
| 2 | |
| 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 |
| 6 | * yet). |
| 7 | * |
| 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. |
| 11 | * |
| 12 | * Packets are offered to registered receivers in turn. Once one |
| 13 | * accepts it, it isn't offered to any more. */ |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <unistd.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <string.h> |
| 19 | #include <errno.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <netinet/in.h> |
| 23 | |
| 24 | #include "secnet.h" |
| 25 | #include "util.h" |
| 26 | |
| 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; |
| 32 | |
| 33 | /* The UDP module exports a pure closure which can be used to construct a |
| 34 | * UDP send/receive module. Arguments: |
| 35 | */ |
| 36 | |
| 37 | struct notify_list { |
| 38 | comm_notify_fn *fn; |
| 39 | void *state; |
| 40 | struct notify_list *next; |
| 41 | }; |
| 42 | |
| 43 | struct udp { |
| 44 | closure_t cl; |
| 45 | struct comm_if ops; |
| 46 | struct cloc loc; |
| 47 | int fd; |
| 48 | struct buffer_if *rbuf; |
| 49 | struct notify_list *notify; |
| 50 | }; |
| 51 | |
| 52 | static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io, |
| 53 | int *timeout_io, const struct timeval *tv, |
| 54 | uint64_t *now) |
| 55 | { |
| 56 | struct udp *st=state; |
| 57 | if (*nfds_io<1) { |
| 58 | *nfds_io=1; |
| 59 | return ERANGE; |
| 60 | } |
| 61 | *nfds_io=1; |
| 62 | fds->fd=st->fd; |
| 63 | fds->events=POLLIN; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static void udp_afterpoll(void *state, struct pollfd *fds, int nfds, |
| 68 | const struct timeval *tv, uint64_t *now) |
| 69 | { |
| 70 | struct udp *st=state; |
| 71 | struct sockaddr_in from; |
| 72 | int fromlen; |
| 73 | struct notify_list *n; |
| 74 | bool_t done; |
| 75 | int rv; |
| 76 | |
| 77 | if (nfds && (fds->revents & POLLIN)) { |
| 78 | do { |
| 79 | fromlen=sizeof(from); |
| 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); |
| 84 | if (rv>0) { |
| 85 | st->rbuf->size=rv; |
| 86 | done=False; |
| 87 | for (n=st->notify; n; n=n->next) { |
| 88 | if (n->fn(n->state, st->rbuf, &from)) { |
| 89 | done=True; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | if (!done) { |
| 94 | /* XXX manufacture and send NAK packet */ |
| 95 | Message(M_WARNING,"Need to send NAK\n"); |
| 96 | BUF_FREE(st->rbuf); |
| 97 | } |
| 98 | BUF_ASSERT_FREE(st->rbuf); |
| 99 | } else { |
| 100 | BUF_FREE(st->rbuf); |
| 101 | } |
| 102 | } while (rv>=0); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static void request_notify(void *commst, void *nst, comm_notify_fn *fn) |
| 107 | { |
| 108 | struct udp *st=commst; |
| 109 | struct notify_list *n; |
| 110 | |
| 111 | n=safe_malloc(sizeof(*n),"request_notify"); |
| 112 | n->fn=fn; |
| 113 | n->state=nst; |
| 114 | n->next=st->notify; |
| 115 | st->notify=n; |
| 116 | } |
| 117 | |
| 118 | static void release_notify(void *commst, void *nst, comm_notify_fn *fn) |
| 119 | { |
| 120 | struct udp *st=commst; |
| 121 | struct notify_list *n, **p, *t; |
| 122 | |
| 123 | /* XXX untested */ |
| 124 | p=&st->notify; |
| 125 | for (n=st->notify; n; ) |
| 126 | { |
| 127 | if (n->state==nst && n->fn==fn) { |
| 128 | t=n; |
| 129 | *p=n->next; |
| 130 | n=n->next; |
| 131 | free(t); |
| 132 | } else { |
| 133 | p=&n->next; |
| 134 | n=n->next; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | static bool_t udp_sendmsg(void *commst, struct buffer_if *buf, |
| 140 | struct sockaddr_in *dest) |
| 141 | { |
| 142 | struct udp *st=commst; |
| 143 | |
| 144 | /* XXX fix error reporting */ |
| 145 | sendto(st->fd, buf->start, buf->size, 0, |
| 146 | (struct sockaddr *)dest, sizeof(*dest)); |
| 147 | |
| 148 | return True; |
| 149 | } |
| 150 | |
| 151 | static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context, |
| 152 | list_t *args) |
| 153 | { |
| 154 | struct udp *st; |
| 155 | item_t *i; |
| 156 | dict_t *d; |
| 157 | uint16_t local_port=0; |
| 158 | struct sockaddr_in addr; |
| 159 | |
| 160 | st=safe_malloc(sizeof(*st),"udp_apply(st)"); |
| 161 | st->loc=loc; |
| 162 | st->cl.description="udp"; |
| 163 | st->cl.type=CL_COMM; |
| 164 | st->cl.apply=NULL; |
| 165 | st->cl.interface=&st->ops; |
| 166 | st->ops.st=st; |
| 167 | st->ops.request_notify=request_notify; |
| 168 | st->ops.release_notify=release_notify; |
| 169 | st->ops.sendmsg=udp_sendmsg; |
| 170 | |
| 171 | i=list_elem(args,0); |
| 172 | if (!i || i->type!=t_dict) { |
| 173 | cfgfatal(st->loc,"udp","first argument must be a dictionary\n"); |
| 174 | } |
| 175 | d=i->data.dict; |
| 176 | |
| 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); |
| 179 | |
| 180 | st->fd=socket(AF_INET, SOCK_DGRAM, 0); |
| 181 | if (st->fd<0) { |
| 182 | fatal_perror("udp_apply (%s:%d): socket",loc.file,loc.line); |
| 183 | } |
| 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)", |
| 186 | loc.file,loc.line); |
| 187 | } |
| 188 | if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) { |
| 189 | fatal_perror("udp_apply (%s:%d): fcntl(set FD_CLOEXEC)", |
| 190 | loc.file,loc.line); |
| 191 | } |
| 192 | |
| 193 | memset(&addr, 0, sizeof(addr)); |
| 194 | addr.sin_family=AF_INET; |
| 195 | if (local_port) { |
| 196 | addr.sin_port=htons(local_port); |
| 197 | } |
| 198 | if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) { |
| 199 | fatal_perror("udp_apply (%s:%d): bind",loc.file,loc.line); |
| 200 | } |
| 201 | |
| 202 | register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp"); |
| 203 | |
| 204 | return new_closure(&st->cl); |
| 205 | } |
| 206 | |
| 207 | init_module udp_module; |
| 208 | void udp_module(dict_t *dict) |
| 209 | { |
| 210 | add_closure(dict,"udp",udp_apply); |
| 211 | } |