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