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 * Packets are offered to registered receivers in turn. Once one
9 * accepts it, it isn't offered to any more. */
17 #include <sys/socket.h>
20 #include "unaligned.h"
23 static beforepoll_fn udp_beforepoll
;
24 static afterpoll_fn udp_afterpoll
;
25 static comm_request_notify_fn request_notify
;
26 static comm_release_notify_fn release_notify
;
27 static comm_sendmsg_fn udp_sendmsg
;
32 struct notify_list
*next
;
43 struct buffer_if
*rbuf
;
44 struct notify_list
*notify
;
46 struct sockaddr_in proxy
;
49 static int udp_beforepoll(void *state
, struct pollfd
*fds
, int *nfds_io
,
63 static void udp_afterpoll(void *state
, struct pollfd
*fds
, int nfds
)
66 struct sockaddr_in from
;
68 struct notify_list
*n
;
72 if (nfds
&& (fds
->revents
& POLLIN
)) {
75 BUF_ASSERT_FREE(st
->rbuf
);
76 BUF_ALLOC(st
->rbuf
,"udp_afterpoll");
77 rv
=recvfrom(st
->fd
, st
->rbuf
->start
, st
->rbuf
->len
, 0,
78 (struct sockaddr
*)&from
, &fromlen
);
82 /* Check that the packet came from our poxy server;
83 we shouldn't be contacted directly by anybody else
84 (since they can trivially forge source addresses) */
85 if (memcmp(&from
.sin_addr
,&st
->proxy
.sin_addr
,4)!=0 ||
86 memcmp(&from
.sin_port
,&st
->proxy
.sin_port
,2)!=0) {
87 Message(M_INFO
,"udp: received packet that's not "
92 memcpy(&from
.sin_addr
,buf_unprepend(st
->rbuf
,4),4);
93 buf_unprepend(st
->rbuf
,2);
94 memcpy(&from
.sin_port
,buf_unprepend(st
->rbuf
,2),2);
97 for (n
=st
->notify
; n
; n
=n
->next
) {
98 if (n
->fn(n
->state
, st
->rbuf
, &from
)) {
104 uint32_t source
,dest
;
105 /* Manufacture and send NAK packet */
106 source
=get_uint32(st
->rbuf
->start
); /* Us */
107 dest
=get_uint32(st
->rbuf
->start
+4); /* Them */
108 Message(M_INFO
,"udp (port %d): sending NAK\n",st
->port
);
109 buffer_init(st
->rbuf
,0);
110 buf_append_uint32(st
->rbuf
,dest
);
111 buf_append_uint32(st
->rbuf
,source
);
112 buf_append_uint32(st
->rbuf
,0); /* NAK is msg type 0 */
113 sendto(st
->fd
, st
->rbuf
->start
, st
->rbuf
->size
, 0,
114 (struct sockaddr
*)&from
, sizeof(from
));
117 BUF_ASSERT_FREE(st
->rbuf
);
125 static void request_notify(void *commst
, void *nst
, comm_notify_fn
*fn
)
127 struct udp
*st
=commst
;
128 struct notify_list
*n
;
130 n
=safe_malloc(sizeof(*n
),"request_notify");
137 static void release_notify(void *commst
, void *nst
, comm_notify_fn
*fn
)
139 struct udp
*st
=commst
;
140 struct notify_list
*n
, **p
, *t
;
144 for (n
=st
->notify
; n
; )
146 if (n
->state
==nst
&& n
->fn
==fn
) {
158 static bool_t
udp_sendmsg(void *commst
, struct buffer_if
*buf
,
159 struct sockaddr_in
*dest
)
161 struct udp
*st
=commst
;
166 memcpy(sa
,&dest
->sin_addr
,4);
168 memcpy(sa
+6,&dest
->sin_port
,2);
169 sendto(st
->fd
,sa
,buf
->size
+8,0,(struct sockaddr
*)&st
->proxy
,
172 sendto(st
->fd
, buf
->start
, buf
->size
, 0,
173 (struct sockaddr
*)dest
, sizeof(*dest
));
179 static void udp_phase_hook(void *sst
, uint32_t new_phase
)
182 struct sockaddr_in addr
;
184 st
->fd
=socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
186 fatal_perror("udp (%s:%d): socket",st
->loc
.file
,st
->loc
.line
);
188 if (fcntl(st
->fd
, F_SETFL
, fcntl(st
->fd
, F_GETFL
)|O_NONBLOCK
)==-1) {
189 fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
190 st
->loc
.file
,st
->loc
.line
);
192 if (fcntl(st
->fd
, F_SETFD
, FD_CLOEXEC
)==-1) {
193 fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)",
194 st
->loc
.file
,st
->loc
.line
);
197 memset(&addr
, 0, sizeof(addr
));
198 addr
.sin_family
=AF_INET
;
199 addr
.sin_addr
.s_addr
=htonl(st
->addr
);
200 addr
.sin_port
=htons(st
->port
);
205 /* XXX this fork() and waitpid() business needs to be hidden
206 in some system-specific library functions. */
209 fatal_perror("udp_phase_hook: fork() for authbind");
212 char *argv
[4], addrstr
[9], portstr
[5];
213 sprintf(addrstr
,"%08lX",(long)st
->addr
);
214 sprintf(portstr
,"%04X",st
->port
);
215 argv
[0]=st
->authbind
;
220 execvp(st
->authbind
,argv
);
223 while (waitpid(c
,&status
,0)==-1) {
224 if (errno
==EINTR
) continue;
225 fatal_perror("udp (%s:%d): authbind",st
->loc
.file
,st
->loc
.line
);
227 if (WIFSIGNALED(status
)) {
228 fatal("udp (%s:%d): authbind died on signal %d",st
->loc
.file
,
229 st
->loc
.line
, WTERMSIG(status
));
231 if (WIFEXITED(status
) && WEXITSTATUS(status
)!=0) {
232 fatal("udp (%s:%d): authbind died with status %d",st
->loc
.file
,
233 st
->loc
.line
, WEXITSTATUS(status
));
236 if (bind(st
->fd
, (struct sockaddr
*)&addr
, sizeof(addr
))!=0) {
237 fatal_perror("udp (%s:%d): bind",st
->loc
.file
,st
->loc
.line
);
241 register_for_poll(st
,udp_beforepoll
,udp_afterpoll
,1,"udp");
244 static list_t
*udp_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
253 st
=safe_malloc(sizeof(*st
),"udp_apply(st)");
255 st
->cl
.description
="udp";
258 st
->cl
.interface
=&st
->ops
;
260 st
->ops
.min_start_pad
=0;
261 st
->ops
.min_end_pad
=0;
262 st
->ops
.request_notify
=request_notify
;
263 st
->ops
.release_notify
=release_notify
;
264 st
->ops
.sendmsg
=udp_sendmsg
;
269 if (!i
|| i
->type
!=t_dict
) {
270 cfgfatal(st
->loc
,"udp","first argument must be a dictionary\n");
274 j
=dict_find_item(d
,"address",False
,"udp",st
->loc
);
275 st
->addr
=j?
string_item_to_ipaddr(j
, "udp"):INADDR_ANY
;
276 st
->port
=dict_read_number(d
,"port",True
,"udp",st
->loc
,0);
277 st
->rbuf
=find_cl_if(d
,"buffer",CL_BUFFER
,True
,"udp",st
->loc
);
278 st
->authbind
=dict_read_string(d
,"authbind",False
,"udp",st
->loc
);
279 l
=dict_lookup(d
,"proxy");
282 memset(&st
->proxy
,0,sizeof(st
->proxy
));
283 st
->proxy
.sin_family
=AF_INET
;
285 if (!i
|| i
->type
!=t_string
) {
286 cfgfatal(st
->loc
,"udp","proxy must supply ""addr"",port\n");
288 a
=string_item_to_ipaddr(i
,"proxy");
289 st
->proxy
.sin_addr
.s_addr
=htonl(a
);
291 if (!i
|| i
->type
!=t_number
) {
292 cfgfatal(st
->loc
,"udp","proxy must supply ""addr"",port\n");
294 st
->proxy
.sin_port
=htons(i
->data
.number
);
295 st
->ops
.min_start_pad
=8;
298 add_hook(PHASE_GETRESOURCES
,udp_phase_hook
,st
);
300 return new_closure(&st
->cl
);
303 void udp_module(dict_t
*dict
)
305 add_closure(dict
,"udp",udp_apply
);