2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 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.11.2.3.4.3.2.4 2002/11/12 19:19:03 martinea Exp $
30 * library routines to marshall/send, recv/unmarshall UDP packets
36 void dgram_socket(dgram, socket)
40 if(socket < 0 || socket >= FD_SETSIZE) {
41 error("dgram_socket: socket %d out of range (0 .. %d)\n",
42 socket, FD_SETSIZE-1);
44 dgram->socket = socket;
48 int dgram_bind(dgram, portp)
54 struct sockaddr_in name;
57 if((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
59 dbprintf(("%s: dgram_bind: socket() failed: %s\n",
61 strerror(save_errno)));
65 if(s < 0 || s >= FD_SETSIZE) {
66 dbprintf(("%s: dgram_bind: socket out of range: %d\n",
70 errno = EMFILE; /* out of range */
74 memset(&name, 0, sizeof(name));
75 name.sin_family = AF_INET;
76 name.sin_addr.s_addr = INADDR_ANY;
79 * If a port range was specified, we try to get a port in that
80 * range first. Next, we try to get a reserved port. If that
81 * fails, we just go for any port.
83 * It is up to the caller to make sure we have the proper permissions
84 * to get the desired port, and to make sure we return a port that
85 * is within the range it requires.
88 if (bind_portrange(s, &name, UDPPORTRANGE) == 0)
92 if (bind_portrange(s, &name, 512, IPPORT_RESERVED - 1) == 0)
95 name.sin_port = INADDR_ANY;
96 if (bind(s, (struct sockaddr *)&name, (socklen_t)sizeof name) == -1) {
98 dbprintf(("%s: dgram_bind: bind(INADDR_ANY) failed: %s\n",
100 strerror(save_errno)));
107 /* find out what name was actually used */
109 len = (socklen_t) sizeof name;
110 if(getsockname(s, (struct sockaddr *)&name, &len) == -1) {
112 dbprintf(("%s: dgram_bind: getsockname() failed: %s\n",
114 strerror(save_errno)));
119 *portp = ntohs(name.sin_port);
121 dbprintf(("%s: dgram_bind: socket bound to %s.%d\n",
122 debug_prefix_time(NULL),
123 inet_ntoa(name.sin_addr),
129 int dgram_send_addr(addr, dgram)
130 struct sockaddr_in addr;
135 struct sockaddr_in addr_save;
140 if(dgram->socket != -1) {
144 if((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
146 dbprintf(("%s: dgram_send_addr: socket() failed: %s\n",
148 strerror(save_errno)));
155 if(s < 0 || s >= FD_SETSIZE) {
156 dbprintf(("%s: dgram_send_addr: socket out of range: %d\n",
162 errno = EMFILE; /* out of range */
166 memcpy(&addr_save, &addr, sizeof(addr));
167 max_wait = 300 / 5; /* five minutes */
173 (struct sockaddr *)&addr,
174 (socklen_t) sizeof(struct sockaddr_in)) == -1) {
176 if(errno == ECONNREFUSED && wait_count++ < max_wait) {
178 dbprintf(("%s: dgram_send_addr: sendto(%s.%d): retry %d after ECONNREFUSED\n",
179 debug_prefix_time(NULL),
180 inet_ntoa(addr_save.sin_addr),
181 (int) ntohs(addr.sin_port),
187 dbprintf(("%s: dgram_send_addr: sendto(%s.%d) failed: %s \n",
188 debug_prefix_time(NULL),
189 inet_ntoa(addr_save.sin_addr),
190 (int) ntohs(addr.sin_port),
191 strerror(save_errno)));
199 dbprintf(("%s: dgram_send_addr: close(%s.%d): failed: %s\n",
201 inet_ntoa(addr_save.sin_addr),
202 (int) ntohs(addr.sin_port),
203 strerror(save_errno)));
214 int dgram_send(hostname, port, dgram)
219 struct sockaddr_in name;
223 if((hp = gethostbyname(hostname)) == 0) {
225 dbprintf(("%s: dgram_send: gethostbyname(%s) failed\n",
226 debug_prefix_time(NULL),
231 memcpy(&name.sin_addr, hp->h_addr, hp->h_length);
232 name.sin_family = AF_INET;
233 name.sin_port = htons(port);
235 return dgram_send_addr(name, dgram);
239 int dgram_recv(dgram, timeout, fromaddr)
242 struct sockaddr_in *fromaddr;
252 sock = dgram->socket;
255 FD_SET(sock, &ready);
259 nfound = select(sock+1, (SELECT_ARG_TYPE *)&ready, NULL, NULL, &to);
260 if(nfound <= 0 || !FD_ISSET(sock, &ready)) {
263 dbprintf(("%s: dgram_recv: select() failed: %s\n",
264 debug_prefix_time(NULL),
265 strerror(save_errno)));
266 } else if(nfound == 0) {
267 dbprintf(("%s: dgram_recv: timeout after %d second%s\n",
268 debug_prefix_time(NULL),
270 (timeout == 1) ? "" : "s"));
272 } else if (!FD_ISSET(sock, &ready)) {
275 for(i = 0; i < sock + 1; i++) {
276 if(FD_ISSET(i, &ready)) {
277 dbprintf(("%s: dgram_recv: got fd %d instead of %d\n",
278 debug_prefix_time(NULL),
290 addrlen = (socklen_t) sizeof(struct sockaddr_in);
291 size = recvfrom(sock, dgram->data, MAX_DGRAM, 0,
292 (struct sockaddr *)fromaddr, &addrlen);
295 dbprintf(("%s: dgram_recv: recvfrom() failed: %s\n",
297 strerror(save_errno)));
302 dgram->data[size] = '\0';
303 dgram->cur = dgram->data;
308 void dgram_zero(dgram)
311 dgram->cur = dgram->data;
313 *(dgram->cur) = '\0';
316 dgram_t *debug_dgram_alloc(s, l)
322 malloc_enter(dbmalloc_caller_loc(s, l));
323 p = (dgram_t *) alloc(sizeof(dgram_t));
326 malloc_leave(dbmalloc_caller_loc(s, l));
332 void dgram_cat(dgram, str)
336 int len = strlen(str);
338 if(dgram->len + len > MAX_DGRAM) len = MAX_DGRAM - dgram->len;
339 strncpy(dgram->cur, str, len);
342 *(dgram->cur) = '\0';
345 void dgram_eatline(dgram)
348 char *p = dgram->cur;
349 char *end = dgram->data + dgram->len;
351 while(p < end && *p && *p != '\n') p++;