2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1999 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Author: James da Silva, Systems Design and Analysis Group
24 * Computer Science Department
25 * University of Maryland at College Park
28 * $Id: dgram.c,v 1.32 2006/07/05 19:54:20 martinea Exp $
30 * library routines to marshall/send, recv/unmarshall UDP packets
37 #include "sockaddr-util.h"
44 if(socket < 0 || socket >= (int)FD_SETSIZE) {
45 error(_("dgram_socket: socket %d out of range (0 .. %d)\n"),
46 socket, (int)FD_SETSIZE-1);
49 dgram->socket = socket;
65 portrange = getconf_intrange(CNF_RESERVED_UDP_PORT);
66 *portp = (in_port_t)0;
67 if((s = socket(family, SOCK_DGRAM, 0)) == -1) {
69 dbprintf(_("dgram_bind: socket() failed: %s\n"),
70 strerror(save_errno));
74 if(s < 0 || s >= (int)FD_SETSIZE) {
75 dbprintf(_("dgram_bind: socket out of range: %d\n"),
78 errno = EMFILE; /* out of range */
82 SU_INIT(&name, family);
83 SU_SET_INADDR_ANY(&name);
86 * If a port range was specified, we try to get a port in that
87 * range first. Next, we try to get a reserved port. If that
88 * fails, we just go for any port.
90 * In all cases, not to use port that's assigned to other services.
92 * It is up to the caller to make sure we have the proper permissions
93 * to get the desired port, and to make sure we return a port that
94 * is within the range it requires.
96 for (retries = 0; ; retries++) {
97 if (bind_portrange(s, &name, portrange[0], portrange[1], "udp") == 0)
99 dbprintf(_("dgram_bind: Could not bind to port in range: %d - %d.\n"),
100 portrange[0], portrange[1]);
101 if (retries >= BIND_CYCLE_RETRIES) {
102 dbprintf(_("dgram_bind: Giving up...\n"));
106 dbprintf(_("dgram_bind: Retrying entire range after 10 second delay.\n"));
111 dbprintf(_("dgram_bind: bind(in6addr_any) failed: %s\n"),
112 strerror(save_errno));
118 /* find out what name was actually used */
120 len = (socklen_t_equiv)sizeof(name);
121 if(getsockname(s, (struct sockaddr *)&name, &len) == -1) {
123 dbprintf(_("dgram_bind: getsockname() failed: %s\n"), strerror(save_errno));
128 *portp = SU_GET_PORT(&name);
131 dbprintf(_("dgram_bind: socket %d bound to %s\n"),
132 dgram->socket, str_sockaddr(&name));
139 sockaddr_union *addr,
147 #if defined(USE_REUSEADDR)
152 dbprintf(_("dgram_send_addr(addr=%p, dgram=%p)\n"),
155 dbprintf(_("dgram_send_addr: %p->socket = %d\n"),
156 dgram, dgram->socket);
157 if(dgram->socket != -1) {
161 if((s = socket(SU_GET_FAMILY(addr), SOCK_DGRAM, 0)) == -1) {
163 dbprintf(_("dgram_send_addr: socket() failed: %s\n"),
164 strerror(save_errno));
170 r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
171 (void *)&on, (socklen_t_equiv)sizeof(on));
173 dbprintf(_("dgram_send_addr: setsockopt(SO_REUSEADDR) failed: %s\n"),
179 if(s < 0 || s >= (int)FD_SETSIZE) {
180 dbprintf(_("dgram_send_addr: socket out of range: %d\n"), s);
181 errno = EMFILE; /* out of range */
184 max_wait = 300 / 5; /* five minutes */
191 (struct sockaddr *)addr,
192 SS_LEN(addr)) == -1) {
194 if(errno == ECONNREFUSED && wait_count++ < max_wait) {
196 dbprintf(_("dgram_send_addr: sendto(%s): retry %d after ECONNREFUSED\n"),
203 if(errno == EAGAIN && wait_count++ < max_wait) {
205 dbprintf(_("dgram_send_addr: sendto(%s): retry %d after EAGAIN\n"),
212 dbprintf(_("dgram_send_addr: sendto(%s) failed: %s \n"),
214 strerror(save_errno));
224 dbprintf(_("dgram_send_addr: close(%s): failed: %s\n"),
228 * Calling function should not care that the close failed.
229 * It does care about the send results though.
244 sockaddr_union *fromaddr)
246 SELECT_ARG_TYPE ready;
250 socklen_t_equiv addrlen;
254 sock = dgram->socket;
257 FD_SET(sock, &ready);
261 dbprintf(_("dgram_recv(dgram=%p, timeout=%u, fromaddr=%p)\n"),
262 dgram, timeout, fromaddr);
264 nfound = (ssize_t)select(sock+1, &ready, NULL, NULL, &to);
265 if(nfound <= 0 || !FD_ISSET(sock, &ready)) {
268 dbprintf(_("dgram_recv: select() failed: %s\n"), strerror(save_errno));
269 } else if(nfound == 0) {
270 dbprintf(plural(_("dgram_recv: timeout after %d second\n"),
271 _("dgram_recv: timeout after %d seconds\n"),
275 } else if (!FD_ISSET(sock, &ready)) {
278 for(i = 0; i < sock + 1; i++) {
279 if(FD_ISSET(i, &ready)) {
280 dbprintf(_("dgram_recv: got fd %d instead of %d\n"), i, sock);
290 addrlen = (socklen_t_equiv)sizeof(sockaddr_union);
291 size = recvfrom(sock, dgram->data, (size_t)MAX_DGRAM, 0,
292 (struct sockaddr *)fromaddr, &addrlen);
295 dbprintf(_("dgram_recv: recvfrom() failed: %s\n"), strerror(save_errno));
299 dump_sockaddr(fromaddr);
300 dgram->len = (size_t)size;
301 dgram->data[size] = '\0';
302 dgram->cur = dgram->data;
311 dgram->cur = dgram->data;
313 *(dgram->cur) = '\0';
316 printf_arglist_function1(int dgram_cat, dgram_t *, dgram, const char *, fmt)
322 assert(dgram != NULL);
325 assert(dgram->len == (size_t)(dgram->cur - dgram->data));
326 assert(dgram->len < SIZEOF(dgram->data));
328 bufsize = (ssize_t)(sizeof(dgram->data) - dgram->len);
332 arglist_start(argp, fmt);
333 len = g_vsnprintf(dgram->cur, (size_t)bufsize, fmt, argp);
337 } else if((ssize_t)len > bufsize) {
338 dgram->len = sizeof(dgram->data);
339 dgram->cur = dgram->data + dgram->len;
344 dgram->cur = dgram->data + dgram->len;
353 char *p = dgram->cur;
354 char *end = dgram->data + dgram->len;
356 while(p < end && *p && *p != '\n')