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;
64 int sndbufsize = MAX_DGRAM;
66 portrange = getconf_intrange(CNF_RESERVED_UDP_PORT);
67 *portp = (in_port_t)0;
68 g_debug("dgram_bind: setting up a socket with family %d", family);
69 if((s = socket(family, SOCK_DGRAM, 0)) == -1) {
71 dbprintf(_("dgram_bind: socket() failed: %s\n"),
72 strerror(save_errno));
76 if(s < 0 || s >= (int)FD_SETSIZE) {
77 dbprintf(_("dgram_bind: socket out of range: %d\n"),
80 errno = EMFILE; /* out of range */
84 /* try setting the buffer size (= maximum allowable UDP packet size) */
85 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
86 (void *) &sndbufsize, sizeof(sndbufsize)) < 0) {
87 dbprintf("dgram_bind: could not set udp send buffer to %d: %s (ignored)\n",
88 sndbufsize, strerror(errno));
91 SU_INIT(&name, family);
92 SU_SET_INADDR_ANY(&name);
95 * If a port range was specified, we try to get a port in that
96 * range first. Next, we try to get a reserved port. If that
97 * fails, we just go for any port.
99 * In all cases, not to use port that's assigned to other services.
101 * It is up to the caller to make sure we have the proper permissions
102 * to get the desired port, and to make sure we return a port that
103 * is within the range it requires.
105 for (retries = 0; ; retries++) {
106 if (bind_portrange(s, &name, portrange[0], portrange[1], "udp") == 0)
108 dbprintf(_("dgram_bind: Could not bind to port in range: %d - %d.\n"),
109 portrange[0], portrange[1]);
110 if (retries >= BIND_CYCLE_RETRIES) {
111 dbprintf(_("dgram_bind: Giving up...\n"));
115 dbprintf(_("dgram_bind: Retrying entire range after 10 second delay.\n"));
120 dbprintf(_("dgram_bind: bind(in6addr_any) failed: %s\n"),
121 strerror(save_errno));
127 /* find out what name was actually used */
129 len = (socklen_t_equiv)sizeof(name);
130 if(getsockname(s, (struct sockaddr *)&name, &len) == -1) {
132 dbprintf(_("dgram_bind: getsockname() failed: %s\n"), strerror(save_errno));
137 *portp = SU_GET_PORT(&name);
140 dbprintf(_("dgram_bind: socket %d bound to %s\n"),
141 dgram->socket, str_sockaddr(&name));
148 sockaddr_union *addr,
156 #if defined(USE_REUSEADDR)
161 dbprintf(_("dgram_send_addr(addr=%p, dgram=%p)\n"),
164 dbprintf(_("dgram_send_addr: %p->socket = %d\n"),
165 dgram, dgram->socket);
166 if(dgram->socket != -1) {
170 int sndbufsize = MAX_DGRAM;
172 g_debug("dgram_send_addr: setting up a socket with family %d", SU_GET_FAMILY(addr));
173 if((s = socket(SU_GET_FAMILY(addr), SOCK_DGRAM, 0)) == -1) {
175 dbprintf(_("dgram_send_addr: socket() failed: %s\n"),
176 strerror(save_errno));
182 r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
183 (void *)&on, (socklen_t_equiv)sizeof(on));
185 dbprintf(_("dgram_send_addr: setsockopt(SO_REUSEADDR) failed: %s\n"),
190 /* try setting the buffer size (= maximum allowable UDP packet size) */
191 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
192 (void *) &sndbufsize, sizeof(sndbufsize)) < 0) {
193 dbprintf("dgram_send_addr: could not set udp send buffer to %d: %s (ignored)\n",
194 sndbufsize, strerror(errno));
198 if(s < 0 || s >= (int)FD_SETSIZE) {
199 dbprintf(_("dgram_send_addr: socket out of range: %d\n"), s);
200 errno = EMFILE; /* out of range */
203 max_wait = 300 / 5; /* five minutes */
210 (struct sockaddr *)addr,
211 SS_LEN(addr)) == -1) {
213 if(errno == ECONNREFUSED && wait_count++ < max_wait) {
215 dbprintf(_("dgram_send_addr: sendto(%s): retry %d after ECONNREFUSED\n"),
222 if(errno == EAGAIN && wait_count++ < max_wait) {
224 dbprintf(_("dgram_send_addr: sendto(%s): retry %d after EAGAIN\n"),
231 dbprintf(_("dgram_send_addr: sendto(%s) failed: %s \n"),
233 strerror(save_errno));
243 dbprintf(_("dgram_send_addr: close(%s): failed: %s\n"),
247 * Calling function should not care that the close failed.
248 * It does care about the send results though.
263 sockaddr_union *fromaddr)
265 SELECT_ARG_TYPE ready;
269 socklen_t_equiv addrlen;
273 sock = dgram->socket;
276 FD_SET(sock, &ready);
280 dbprintf(_("dgram_recv(dgram=%p, timeout=%u, fromaddr=%p)\n"),
281 dgram, timeout, fromaddr);
283 nfound = (ssize_t)select(sock+1, &ready, NULL, NULL, &to);
284 if(nfound <= 0 || !FD_ISSET(sock, &ready)) {
287 dbprintf(_("dgram_recv: select() failed: %s\n"), strerror(save_errno));
288 } else if(nfound == 0) {
289 dbprintf(plural(_("dgram_recv: timeout after %d second\n"),
290 _("dgram_recv: timeout after %d seconds\n"),
294 } else if (!FD_ISSET(sock, &ready)) {
297 for(i = 0; i < sock + 1; i++) {
298 if(FD_ISSET(i, &ready)) {
299 dbprintf(_("dgram_recv: got fd %d instead of %d\n"), i, sock);
309 addrlen = (socklen_t_equiv)sizeof(sockaddr_union);
310 size = recvfrom(sock, dgram->data, (size_t)MAX_DGRAM, 0,
311 (struct sockaddr *)fromaddr, &addrlen);
314 dbprintf(_("dgram_recv: recvfrom() failed: %s\n"), strerror(save_errno));
318 dump_sockaddr(fromaddr);
319 dgram->len = (size_t)size;
320 dgram->data[size] = '\0';
321 dgram->cur = dgram->data;
330 dgram->cur = dgram->data;
332 *(dgram->cur) = '\0';
335 printf_arglist_function1(int dgram_cat, dgram_t *, dgram, const char *, fmt)
341 assert(dgram != NULL);
344 assert(dgram->len == (size_t)(dgram->cur - dgram->data));
345 assert(dgram->len < SIZEOF(dgram->data));
347 bufsize = (ssize_t)(sizeof(dgram->data) - dgram->len);
351 arglist_start(argp, fmt);
352 len = g_vsnprintf(dgram->cur, (size_t)bufsize, fmt, argp);
356 } else if((ssize_t)len > bufsize) {
357 dgram->len = sizeof(dgram->data);
358 dgram->cur = dgram->data + dgram->len;
363 dgram->cur = dgram->data + dgram->len;
372 char *p = dgram->cur;
373 char *end = dgram->data + dgram->len;
375 while(p < end && *p && *p != '\n')