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>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
22 #include "unaligned.h"
26 static beforepoll_fn udp_beforepoll
;
27 static afterpoll_fn udp_afterpoll
;
28 static comm_request_notify_fn request_notify
;
29 static comm_release_notify_fn release_notify
;
30 static comm_sendmsg_fn udp_sendmsg
;
35 struct notify_list
*next
;
46 struct buffer_if
*rbuf
;
47 struct notify_list
*notify
;
49 struct sockaddr_in proxy
;
52 static const char *saddr_to_string(const struct sockaddr_in
*sin
) {
53 static char bufs
[2][100];
57 snprintf(bufs
[b
], sizeof(bufs
[b
]), "[%s]:%d",
58 inet_ntoa(sin
->sin_addr
),
59 ntohs(sin
->sin_port
));
63 static const char *addr_to_string(void *commst
, const struct comm_addr
*ca
) {
64 struct udp
*st
=commst
;
65 static char sbuf
[100];
67 struct sockaddr_in la
;
68 la
.sin_addr
.s_addr
=htonl(st
->addr
);
69 la
.sin_port
=htons(st
->port
);
71 snprintf(sbuf
, sizeof(sbuf
), "udp:%s-%s",
72 saddr_to_string(&la
), saddr_to_string(&ca
->sin
));
76 static int udp_beforepoll(void *state
, struct pollfd
*fds
, int *nfds_io
,
90 static void udp_afterpoll(void *state
, struct pollfd
*fds
, int nfds
)
93 struct sockaddr_in from
;
95 struct notify_list
*n
;
99 if (nfds
&& (fds
->revents
& POLLIN
)) {
102 fromlen
=sizeof(from
);
103 BUF_ASSERT_FREE(st
->rbuf
);
104 BUF_ALLOC(st
->rbuf
,"udp_afterpoll");
105 rv
=recvfrom(st
->fd
, st
->rbuf
->start
, st
->rbuf
->len
, 0,
106 (struct sockaddr
*)&from
, &fromlen
);
110 /* Check that the packet came from our poxy server;
111 we shouldn't be contacted directly by anybody else
112 (since they can trivially forge source addresses) */
113 if (memcmp(&from
.sin_addr
,&st
->proxy
.sin_addr
,4)!=0 ||
114 memcmp(&from
.sin_port
,&st
->proxy
.sin_port
,2)!=0) {
115 Message(M_INFO
,"udp: received packet that's not "
120 memcpy(&from
.sin_addr
,buf_unprepend(st
->rbuf
,4),4);
121 buf_unprepend(st
->rbuf
,2);
122 memcpy(&from
.sin_port
,buf_unprepend(st
->rbuf
,2),2);
125 for (n
=st
->notify
; n
; n
=n
->next
) {
130 if (n
->fn(n
->state
, st
->rbuf
, &ca
)) {
136 uint32_t source
,dest
;
137 /* Manufacture and send NAK packet */
138 source
=get_uint32(st
->rbuf
->start
); /* Us */
139 dest
=get_uint32(st
->rbuf
->start
+4); /* Them */
140 Message(M_INFO
,"udp (port %d): sending NAK\n",st
->port
);
141 buffer_init(st
->rbuf
,0);
142 buf_append_uint32(st
->rbuf
,dest
);
143 buf_append_uint32(st
->rbuf
,source
);
144 buf_append_uint32(st
->rbuf
,LABEL_NAK
);
145 sendto(st
->fd
, st
->rbuf
->start
, st
->rbuf
->size
, 0,
146 (struct sockaddr
*)&from
, sizeof(from
));
149 BUF_ASSERT_FREE(st
->rbuf
);
157 static void request_notify(void *commst
, void *nst
, comm_notify_fn
*fn
)
159 struct udp
*st
=commst
;
160 struct notify_list
*n
;
162 n
=safe_malloc(sizeof(*n
),"request_notify");
169 static void release_notify(void *commst
, void *nst
, comm_notify_fn
*fn
)
171 struct udp
*st
=commst
;
172 struct notify_list
*n
, **p
, *t
;
176 for (n
=st
->notify
; n
; )
178 if (n
->state
==nst
&& n
->fn
==fn
) {
190 static bool_t
udp_sendmsg(void *commst
, struct buffer_if
*buf
,
191 const struct comm_addr
*dest
)
193 struct udp
*st
=commst
;
198 memcpy(sa
,&dest
->sin
.sin_addr
,4);
200 memcpy(sa
+6,&dest
->sin
.sin_port
,2);
201 sendto(st
->fd
,sa
,buf
->size
+8,0,(struct sockaddr
*)&st
->proxy
,
204 sendto(st
->fd
, buf
->start
, buf
->size
, 0,
205 (struct sockaddr
*)&dest
->sin
, sizeof(dest
->sin
));
211 static void udp_phase_hook(void *sst
, uint32_t new_phase
)
214 struct sockaddr_in addr
;
216 st
->fd
=socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
218 fatal_perror("udp (%s:%d): socket",st
->loc
.file
,st
->loc
.line
);
220 if (fcntl(st
->fd
, F_SETFL
, fcntl(st
->fd
, F_GETFL
)|O_NONBLOCK
)==-1) {
221 fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
222 st
->loc
.file
,st
->loc
.line
);
224 if (fcntl(st
->fd
, F_SETFD
, FD_CLOEXEC
)==-1) {
225 fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)",
226 st
->loc
.file
,st
->loc
.line
);
230 addr
.sin_family
=AF_INET
;
231 addr
.sin_addr
.s_addr
=htonl(st
->addr
);
232 addr
.sin_port
=htons(st
->port
);
237 /* XXX this fork() and waitpid() business needs to be hidden
238 in some system-specific library functions. */
241 fatal_perror("udp_phase_hook: fork() for authbind");
244 char *argv
[4], addrstr
[9], portstr
[5];
245 sprintf(addrstr
,"%08lX",(long)addr
.sin_addr
.s_addr
);
246 sprintf(portstr
,"%04X",addr
.sin_port
);
247 argv
[0]=st
->authbind
;
252 execvp(st
->authbind
,argv
);
255 while (waitpid(c
,&status
,0)==-1) {
256 if (errno
==EINTR
) continue;
257 fatal_perror("udp (%s:%d): authbind",st
->loc
.file
,st
->loc
.line
);
259 if (WIFSIGNALED(status
)) {
260 fatal("udp (%s:%d): authbind died on signal %d",st
->loc
.file
,
261 st
->loc
.line
, WTERMSIG(status
));
263 if (WIFEXITED(status
) && WEXITSTATUS(status
)!=0) {
264 fatal("udp (%s:%d): authbind died with status %d",st
->loc
.file
,
265 st
->loc
.line
, WEXITSTATUS(status
));
268 if (bind(st
->fd
, (struct sockaddr
*)&addr
, sizeof(addr
))!=0) {
269 fatal_perror("udp (%s:%d): bind",st
->loc
.file
,st
->loc
.line
);
273 register_for_poll(st
,udp_beforepoll
,udp_afterpoll
,1,"udp");
276 static list_t
*udp_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
285 st
=safe_malloc(sizeof(*st
),"udp_apply(st)");
287 st
->cl
.description
="udp";
290 st
->cl
.interface
=&st
->ops
;
292 st
->ops
.min_start_pad
=0;
293 st
->ops
.min_end_pad
=0;
294 st
->ops
.request_notify
=request_notify
;
295 st
->ops
.release_notify
=release_notify
;
296 st
->ops
.sendmsg
=udp_sendmsg
;
297 st
->ops
.addr_to_string
=addr_to_string
;
302 if (!i
|| i
->type
!=t_dict
) {
303 cfgfatal(st
->loc
,"udp","first argument must be a dictionary\n");
307 j
=dict_find_item(d
,"address",False
,"udp",st
->loc
);
308 st
->addr
=j?
string_item_to_ipaddr(j
, "udp"):INADDR_ANY
;
309 st
->port
=dict_read_number(d
,"port",True
,"udp",st
->loc
,0);
310 st
->rbuf
=find_cl_if(d
,"buffer",CL_BUFFER
,True
,"udp",st
->loc
);
311 st
->authbind
=dict_read_string(d
,"authbind",False
,"udp",st
->loc
);
312 l
=dict_lookup(d
,"proxy");
316 st
->proxy
.sin_family
=AF_INET
;
318 if (!i
|| i
->type
!=t_string
) {
319 cfgfatal(st
->loc
,"udp","proxy must supply ""addr"",port\n");
321 a
=string_item_to_ipaddr(i
,"proxy");
322 st
->proxy
.sin_addr
.s_addr
=htonl(a
);
324 if (!i
|| i
->type
!=t_number
) {
325 cfgfatal(st
->loc
,"udp","proxy must supply ""addr"",port\n");
327 st
->proxy
.sin_port
=htons(i
->data
.number
);
328 st
->ops
.min_start_pad
=8;
331 add_hook(PHASE_GETRESOURCES
,udp_phase_hook
,st
);
333 return new_closure(&st
->cl
);
336 void udp_module(dict_t
*dict
)
338 add_closure(dict
,"udp",udp_apply
);