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 g_debug("dgram_bind: setting up a socket with family %d", family);
68 if((s = socket(family, SOCK_DGRAM, 0)) == -1) {
70 dbprintf(_("dgram_bind: socket() failed: %s\n"),
71 strerror(save_errno));
75 if(s < 0 || s >= (int)FD_SETSIZE) {
76 dbprintf(_("dgram_bind: socket out of range: %d\n"),
79 errno = EMFILE; /* out of range */
83 SU_INIT(&name, family);
84 SU_SET_INADDR_ANY(&name);
87 * If a port range was specified, we try to get a port in that
88 * range first. Next, we try to get a reserved port. If that
89 * fails, we just go for any port.
91 * In all cases, not to use port that's assigned to other services.
93 * It is up to the caller to make sure we have the proper permissions
94 * to get the desired port, and to make sure we return a port that
95 * is within the range it requires.
97 for (retries = 0; ; retries++) {
98 if (bind_portrange(s, &name, portrange[0], portrange[1], "udp") == 0)
100 dbprintf(_("dgram_bind: Could not bind to port in range: %d - %d.\n"),
101 portrange[0], portrange[1]);
102 if (retries >= BIND_CYCLE_RETRIES) {
103 dbprintf(_("dgram_bind: Giving up...\n"));
107 dbprintf(_("dgram_bind: Retrying entire range after 10 second delay.\n"));
112 dbprintf(_("dgram_bind: bind(in6addr_any) failed: %s\n"),
113 strerror(save_errno));
119 /* find out what name was actually used */
121 len = (socklen_t_equiv)sizeof(name);
122 if(getsockname(s, (struct sockaddr *)&name, &len) == -1) {
124 dbprintf(_("dgram_bind: getsockname() failed: %s\n"), strerror(save_errno));
129 *portp = SU_GET_PORT(&name);
132 dbprintf(_("dgram_bind: socket %d bound to %s\n"),
133 dgram->socket, str_sockaddr(&name));
140 sockaddr_union *addr,
148 #if defined(USE_REUSEADDR)
153 dbprintf(_("dgram_send_addr(addr=%p, dgram=%p)\n"),
156 dbprintf(_("dgram_send_addr: %p->socket = %d\n"),
157 dgram, dgram->socket);
158 if(dgram->socket != -1) {
162 g_debug("dgram_send_addr: setting up a socket with family %d", SU_GET_FAMILY(addr));
163 if((s = socket(SU_GET_FAMILY(addr), SOCK_DGRAM, 0)) == -1) {
165 dbprintf(_("dgram_send_addr: socket() failed: %s\n"),
166 strerror(save_errno));
172 r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
173 (void *)&on, (socklen_t_equiv)sizeof(on));
175 dbprintf(_("dgram_send_addr: setsockopt(SO_REUSEADDR) failed: %s\n"),
181 if(s < 0 || s >= (int)FD_SETSIZE) {
182 dbprintf(_("dgram_send_addr: socket out of range: %d\n"), s);
183 errno = EMFILE; /* out of range */
186 max_wait = 300 / 5; /* five minutes */
193 (struct sockaddr *)addr,
194 SS_LEN(addr)) == -1) {
196 if(errno == ECONNREFUSED && wait_count++ < max_wait) {
198 dbprintf(_("dgram_send_addr: sendto(%s): retry %d after ECONNREFUSED\n"),
205 if(errno == EAGAIN && wait_count++ < max_wait) {
207 dbprintf(_("dgram_send_addr: sendto(%s): retry %d after EAGAIN\n"),
214 dbprintf(_("dgram_send_addr: sendto(%s) failed: %s \n"),
216 strerror(save_errno));
226 dbprintf(_("dgram_send_addr: close(%s): failed: %s\n"),
230 * Calling function should not care that the close failed.
231 * It does care about the send results though.
246 sockaddr_union *fromaddr)
248 SELECT_ARG_TYPE ready;
252 socklen_t_equiv addrlen;
256 sock = dgram->socket;
259 FD_SET(sock, &ready);
263 dbprintf(_("dgram_recv(dgram=%p, timeout=%u, fromaddr=%p)\n"),
264 dgram, timeout, fromaddr);
266 nfound = (ssize_t)select(sock+1, &ready, NULL, NULL, &to);
267 if(nfound <= 0 || !FD_ISSET(sock, &ready)) {
270 dbprintf(_("dgram_recv: select() failed: %s\n"), strerror(save_errno));
271 } else if(nfound == 0) {
272 dbprintf(plural(_("dgram_recv: timeout after %d second\n"),
273 _("dgram_recv: timeout after %d seconds\n"),
277 } else if (!FD_ISSET(sock, &ready)) {
280 for(i = 0; i < sock + 1; i++) {
281 if(FD_ISSET(i, &ready)) {
282 dbprintf(_("dgram_recv: got fd %d instead of %d\n"), i, sock);
292 addrlen = (socklen_t_equiv)sizeof(sockaddr_union);
293 size = recvfrom(sock, dgram->data, (size_t)MAX_DGRAM, 0,
294 (struct sockaddr *)fromaddr, &addrlen);
297 dbprintf(_("dgram_recv: recvfrom() failed: %s\n"), strerror(save_errno));
301 dump_sockaddr(fromaddr);
302 dgram->len = (size_t)size;
303 dgram->data[size] = '\0';
304 dgram->cur = dgram->data;
313 dgram->cur = dgram->data;
315 *(dgram->cur) = '\0';
318 printf_arglist_function1(int dgram_cat, dgram_t *, dgram, const char *, fmt)
324 assert(dgram != NULL);
327 assert(dgram->len == (size_t)(dgram->cur - dgram->data));
328 assert(dgram->len < SIZEOF(dgram->data));
330 bufsize = (ssize_t)(sizeof(dgram->data) - dgram->len);
334 arglist_start(argp, fmt);
335 len = g_vsnprintf(dgram->cur, (size_t)bufsize, fmt, argp);
339 } else if((ssize_t)len > bufsize) {
340 dgram->len = sizeof(dgram->data);
341 dgram->cur = dgram->data + dgram->len;
346 dgram->cur = dgram->data + dgram->len;
355 char *p = dgram->cur;
356 char *end = dgram->data + dgram->len;
358 while(p < end && *p && *p != '\n')