fix implicit decl warnings
[deb_libnfs.git] / lib / socket.c
1 /*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #ifdef AROS
22 #include "aros_compat.h"
23 #endif
24
25 #ifdef WIN32
26 #include "win32_compat.h"
27 #endif
28
29 #ifdef HAVE_ARPA_INET_H
30 #include <arpa/inet.h>
31 #endif
32
33 #ifdef HAVE_POLL_H
34 #include <poll.h>
35 #endif
36
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
43 #endif
44
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
47 #endif
48
49 #ifdef HAVE_NETINET_TCP_H
50 #include <netinet/tcp.h>
51 #endif
52
53 #ifdef HAVE_NETDB_H
54 #include <netdb.h>
55 #endif
56
57 #ifdef HAVE_SYS_FILIO_H
58 #include <sys/filio.h>
59 #endif
60
61 #ifdef HAVE_SYS_SOCKIO_H
62 #include <sys/sockio.h>
63 #endif
64
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <assert.h>
68 #include <fcntl.h>
69 #include <string.h>
70 #include <errno.h>
71 #include <time.h>
72 #include <sys/types.h>
73 #include "libnfs-zdr.h"
74 #include "libnfs.h"
75 #include "libnfs-raw.h"
76 #include "libnfs-private.h"
77 #include "slist.h"
78
79 #ifdef WIN32
80 //has to be included after stdlib!!
81 #include "win32_errnowrapper.h"
82 #endif
83
84 static int rpc_reconnect_requeue(struct rpc_context *rpc);
85 static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_storage *s);
86
87 static void set_nonblocking(int fd)
88 {
89 int v = 0;
90 #if defined(WIN32)
91 long nonblocking=1;
92 v = ioctl(fd, FIONBIO, &nonblocking);
93 #else
94 v = fcntl(fd, F_GETFL, 0);
95 fcntl(fd, F_SETFL, v | O_NONBLOCK);
96 #endif //FIXME
97 }
98
99 static void set_nolinger(int fd)
100 {
101 struct linger lng;
102 lng.l_onoff = 1;
103 lng.l_linger = 0;
104 setsockopt(fd, SOL_SOCKET, SO_LINGER, &lng, sizeof(lng));
105 }
106
107 #ifdef HAVE_NETINET_TCP_H
108 int set_tcp_sockopt(int sockfd, int optname, int value)
109 {
110 int level;
111
112 #if defined(__FreeBSD__) || defined(__sun) || (defined(__APPLE__) && defined(__MACH__))
113 struct protoent *buf;
114
115 if ((buf = getprotobyname("tcp")) != NULL)
116 level = buf->p_proto;
117 else
118 return -1;
119 #else
120 level = SOL_TCP;
121 #endif
122
123 return setsockopt(sockfd, level, optname, (char *)&value, sizeof(value));
124 }
125 #endif
126
127 int rpc_get_fd(struct rpc_context *rpc)
128 {
129 assert(rpc->magic == RPC_CONTEXT_MAGIC);
130
131 return rpc->fd;
132 }
133
134 static int rpc_has_queue(struct rpc_queue *q)
135 {
136 return q->head != NULL;
137 }
138
139 int rpc_which_events(struct rpc_context *rpc)
140 {
141 int events;
142
143 assert(rpc->magic == RPC_CONTEXT_MAGIC);
144
145 events = rpc->is_connected ? POLLIN : POLLOUT;
146
147 if (rpc->is_udp != 0) {
148 /* for udp sockets we only wait for pollin */
149 return POLLIN;
150 }
151
152 if (rpc_has_queue(&rpc->outqueue)) {
153 events |= POLLOUT;
154 }
155 return events;
156 }
157
158 static int rpc_write_to_socket(struct rpc_context *rpc)
159 {
160 int32_t count;
161 struct rpc_pdu *pdu;
162
163 assert(rpc->magic == RPC_CONTEXT_MAGIC);
164
165 if (rpc->fd == -1) {
166 rpc_set_error(rpc, "trying to write but not connected");
167 return -1;
168 }
169
170 while ((pdu = rpc->outqueue.head) != NULL) {
171 int64_t total;
172
173 total = pdu->outdata.size;
174
175 count = send(rpc->fd, pdu->outdata.data + pdu->written, total - pdu->written, 0);
176 if (count == -1) {
177 if (errno == EAGAIN || errno == EWOULDBLOCK) {
178 return 0;
179 }
180 rpc_set_error(rpc, "Error when writing to socket :%s(%d)", strerror(errno), errno);
181 return -1;
182 }
183
184 pdu->written += count;
185 if (pdu->written == total) {
186 unsigned int hash;
187
188 rpc->outqueue.head = pdu->next;
189 if (pdu->next == NULL)
190 rpc->outqueue.tail = NULL;
191
192 hash = rpc_hash_xid(pdu->xid);
193 rpc_enqueue(&rpc->waitpdu[hash], pdu);
194 }
195 }
196 return 0;
197 }
198
199 static int rpc_read_from_socket(struct rpc_context *rpc)
200 {
201 int available;
202 int size;
203 int pdu_size;
204 int32_t count;
205
206 assert(rpc->magic == RPC_CONTEXT_MAGIC);
207
208 if (ioctl(rpc->fd, FIONREAD, &available) != 0) {
209 rpc_set_error(rpc, "Ioctl FIONREAD returned error : %d. Closing socket.", errno);
210 return -1;
211 }
212
213 if (available == 0) {
214 rpc_set_error(rpc, "Socket has been closed");
215 return -1;
216 }
217
218 if (rpc->is_udp) {
219 char *buf;
220 socklen_t socklen = sizeof(rpc->udp_src);
221
222 buf = malloc(available);
223 if (buf == NULL) {
224 rpc_set_error(rpc, "Failed to malloc buffer for recvfrom");
225 return -1;
226 }
227 count = recvfrom(rpc->fd, buf, available, MSG_DONTWAIT, (struct sockaddr *)&rpc->udp_src, &socklen);
228 if (count < 0) {
229 rpc_set_error(rpc, "Failed recvfrom: %s", strerror(errno));
230 free(buf);
231 return -1;
232 }
233 if (rpc_process_pdu(rpc, buf, count) != 0) {
234 rpc_set_error(rpc, "Invalid/garbage pdu received from server. Ignoring PDU");
235 free(buf);
236 return -1;
237 }
238 free(buf);
239 return 0;
240 }
241
242 /* read record marker, 4 bytes at the beginning of every pdu */
243 if (rpc->inbuf == NULL) {
244 rpc->insize = 4;
245 rpc->inbuf = malloc(rpc->insize);
246 if (rpc->inbuf == NULL) {
247 rpc_set_error(rpc, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno);
248 return -1;
249 }
250 }
251 if (rpc->inpos < 4) {
252 size = 4 - rpc->inpos;
253
254 count = recv(rpc->fd, rpc->inbuf + rpc->inpos, size, 0);
255 if (count == -1) {
256 if (errno == EINTR) {
257 return 0;
258 }
259 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
260 return -1;
261 }
262 available -= count;
263 rpc->inpos += count;
264 }
265
266 if (available == 0) {
267 return 0;
268 }
269
270 pdu_size = rpc_get_pdu_size(rpc->inbuf);
271 if (rpc->insize < pdu_size) {
272 unsigned char *buf;
273
274 buf = malloc(pdu_size);
275 if (buf == NULL) {
276 rpc_set_error(rpc, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size, errno);
277 return -1;
278 }
279 memcpy(buf, rpc->inbuf, rpc->insize);
280 free(rpc->inbuf);
281 rpc->inbuf = buf;
282 rpc->insize = rpc_get_pdu_size(rpc->inbuf);
283 }
284
285 size = available;
286 if (size > rpc->insize - rpc->inpos) {
287 size = rpc->insize - rpc->inpos;
288 }
289
290 count = recv(rpc->fd, rpc->inbuf + rpc->inpos, size, 0);
291 if (count == -1) {
292 if (errno == EINTR) {
293 return 0;
294 }
295 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
296 return -1;
297 }
298 available -= count;
299 rpc->inpos += count;
300
301 if (rpc->inpos == rpc->insize) {
302 char *buf = rpc->inbuf;
303
304 rpc->inbuf = NULL;
305 rpc->insize = 0;
306 rpc->inpos = 0;
307
308 if (rpc_process_pdu(rpc, buf, pdu_size) != 0) {
309 rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket");
310 return -1;
311 }
312 free(buf);
313 }
314
315 return 0;
316 }
317
318
319
320 int rpc_service(struct rpc_context *rpc, int revents)
321 {
322 assert(rpc->magic == RPC_CONTEXT_MAGIC);
323
324 if (revents & POLLERR) {
325 #ifdef WIN32
326 char err = 0;
327 #else
328 int err = 0;
329 #endif
330 socklen_t err_size = sizeof(err);
331
332 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
333 (char *)&err, &err_size) != 0 || err != 0) {
334 if (err == 0) {
335 err = errno;
336 }
337 rpc_set_error(rpc, "rpc_service: socket error "
338 "%s(%d).",
339 strerror(err), err);
340 } else {
341 rpc_set_error(rpc, "rpc_service: POLLERR, "
342 "Unknown socket error.");
343 }
344 if (rpc->connect_cb != NULL) {
345 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
346 }
347 return -1;
348 }
349 if (revents & POLLHUP) {
350 rpc_set_error(rpc, "Socket failed with POLLHUP");
351 if (rpc->connect_cb != NULL) {
352 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
353 }
354 return -1;
355 }
356
357 if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) {
358 int err = 0;
359 socklen_t err_size = sizeof(err);
360
361 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
362 (char *)&err, &err_size) != 0 || err != 0) {
363 if (err == 0) {
364 err = errno;
365 }
366 rpc_set_error(rpc, "rpc_service: socket error "
367 "%s(%d) while connecting.",
368 strerror(err), err);
369 if (rpc->connect_cb != NULL) {
370 rpc->connect_cb(rpc, RPC_STATUS_ERROR,
371 NULL, rpc->connect_data);
372 }
373 return -1;
374 }
375
376 rpc->is_connected = 1;
377 if (rpc->connect_cb != NULL) {
378 rpc->connect_cb(rpc, RPC_STATUS_SUCCESS, NULL, rpc->connect_data);
379 }
380 return 0;
381 }
382
383 if (revents & POLLIN) {
384 if (rpc_read_from_socket(rpc) != 0) {
385 rpc_reconnect_requeue(rpc);
386 return 0;
387 }
388 }
389
390 if (revents & POLLOUT && rpc_has_queue(&rpc->outqueue)) {
391 if (rpc_write_to_socket(rpc) != 0) {
392 rpc_set_error(rpc, "write to socket failed");
393 return -1;
394 }
395 }
396
397 return 0;
398 }
399
400 void rpc_set_autoreconnect(struct rpc_context *rpc)
401 {
402 assert(rpc->magic == RPC_CONTEXT_MAGIC);
403
404 rpc->auto_reconnect = 1;
405 }
406
407 void rpc_unset_autoreconnect(struct rpc_context *rpc)
408 {
409 assert(rpc->magic == RPC_CONTEXT_MAGIC);
410
411 rpc->auto_reconnect = 0;
412 }
413
414 void rpc_set_tcp_syncnt(struct rpc_context *rpc, int v)
415 {
416 assert(rpc->magic == RPC_CONTEXT_MAGIC);
417
418 rpc->tcp_syncnt = v;
419 }
420
421 #ifndef TCP_SYNCNT
422 #define TCP_SYNCNT 7
423 #endif
424
425 static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_storage *s)
426 {
427 int socksize;
428
429 assert(rpc->magic == RPC_CONTEXT_MAGIC);
430
431 switch (s->ss_family) {
432 case AF_INET:
433 socksize = sizeof(struct sockaddr_in);
434 rpc->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
435 #ifdef HAVE_NETINET_TCP_H
436 if (rpc->tcp_syncnt != RPC_PARAM_UNDEFINED) {
437 set_tcp_sockopt(rpc->fd, TCP_SYNCNT, rpc->tcp_syncnt);
438 }
439 #endif
440 break;
441 case AF_INET6:
442 socksize = sizeof(struct sockaddr_in6);
443 rpc->fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
444 #ifdef HAVE_NETINET_TCP_H
445 if (rpc->tcp_syncnt != RPC_PARAM_UNDEFINED) {
446 set_tcp_sockopt(rpc->fd, TCP_SYNCNT, rpc->tcp_syncnt);
447 }
448 #endif
449 break;
450 default:
451 rpc_set_error(rpc, "Can not handle AF_FAMILY:%d", s->ss_family);
452 return -1;
453 }
454
455 if (rpc->fd == -1) {
456 rpc_set_error(rpc, "Failed to open socket");
457 return -1;
458 }
459
460 /* Some systems allow you to set capabilities on an executable
461 * to allow the file to be executed with privilege to bind to
462 * privileged system ports, even if the user is not root.
463 *
464 * Opportunistically try to bind the socket to a low numbered
465 * system port in the hope that the user is either root or the
466 * executable has the CAP_NET_BIND_SERVICE.
467 *
468 * As soon as we fail the bind() with EACCES we know we will never
469 * be able to bind to a system port so we terminate the loop.
470 *
471 * On linux, use
472 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
473 * to make the executable able to bind to a system port.
474 *
475 * On Windows, there is no concept of privileged ports. Thus
476 * binding will usually succeed.
477 */
478 {
479 struct sockaddr_storage ss;
480 static int portOfs = 0;
481 const int firstPort = 512; /* >= 512 according to Sun docs */
482 const int portCount = IPPORT_RESERVED - firstPort;
483 int startOfs, port, rc;
484
485 if (portOfs == 0) {
486 portOfs = time(NULL) % 400;
487 }
488 startOfs = portOfs;
489 do {
490 rc = -1;
491 port = htons(firstPort + portOfs);
492 portOfs = (portOfs + 1) % portCount;
493
494 /* skip well-known ports */
495 if (!getservbyport(port, "tcp")) {
496 memset(&ss, 0, sizeof(ss));
497
498 switch (s->ss_family) {
499 case AF_INET:
500 ((struct sockaddr_in *)&ss)->sin_port = port;
501 ((struct sockaddr_in *)&ss)->sin_family = AF_INET;
502 #ifdef HAVE_SOCKADDR_LEN
503 ((struct sockaddr_in *)&ss)->sin_len = sizeof(struct sockaddr_in);
504 #endif
505 break;
506 case AF_INET6:
507 ((struct sockaddr_in6 *)&ss)->sin6_port = port;
508 ((struct sockaddr_in6 *)&ss)->sin6_family = AF_INET6;
509 #ifdef HAVE_SOCKADDR_LEN
510 ((struct sockaddr_in6 *)&ss)->sin6_len = sizeof(struct sockaddr_in6);
511 #endif
512 break;
513 }
514
515 rc = bind(rpc->fd, (struct sockaddr *)&ss, socksize);
516 #if !defined(WIN32)
517 /* we got EACCES, so don't try again */
518 if (rc != 0 && errno == EACCES)
519 break;
520 #endif
521 }
522 } while (rc != 0 && portOfs != startOfs);
523 }
524
525 set_nonblocking(rpc->fd);
526 set_nolinger(rpc->fd);
527
528 if (connect(rpc->fd, (struct sockaddr *)s, socksize) != 0 && errno != EINPROGRESS) {
529 rpc_set_error(rpc, "connect() to server failed. %s(%d)", strerror(errno), errno);
530 return -1;
531 }
532
533 return 0;
534 }
535
536 int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data)
537 {
538 struct addrinfo *ai = NULL;
539
540 assert(rpc->magic == RPC_CONTEXT_MAGIC);
541
542 if (rpc->fd != -1) {
543 rpc_set_error(rpc, "Trying to connect while already connected");
544 return -1;
545 }
546
547 if (rpc->is_udp != 0) {
548 rpc_set_error(rpc, "Trying to connect on UDP socket");
549 return -1;
550 }
551
552 rpc->auto_reconnect = 0;
553
554 if (getaddrinfo(server, NULL, NULL, &ai) != 0) {
555 rpc_set_error(rpc, "Invalid address:%s. "
556 "Can not resolv into IPv4/v6 structure.", server);
557 return -1;
558 }
559
560 switch (ai->ai_family) {
561 case AF_INET:
562 ((struct sockaddr_in *)&rpc->s)->sin_family = ai->ai_family;
563 ((struct sockaddr_in *)&rpc->s)->sin_port = htons(port);
564 ((struct sockaddr_in *)&rpc->s)->sin_addr = ((struct sockaddr_in *)(ai->ai_addr))->sin_addr;
565 #ifdef HAVE_SOCKADDR_LEN
566 ((struct sockaddr_in *)&rpc->s)->sin_len = sizeof(struct sockaddr_in);
567 #endif
568 break;
569 case AF_INET6:
570 ((struct sockaddr_in6 *)&rpc->s)->sin6_family = ai->ai_family;
571 ((struct sockaddr_in6 *)&rpc->s)->sin6_port = htons(port);
572 ((struct sockaddr_in6 *)&rpc->s)->sin6_addr = ((struct sockaddr_in6 *)(ai->ai_addr))->sin6_addr;
573 #ifdef HAVE_SOCKADDR_LEN
574 ((struct sockaddr_in6 *)&rpc->s)->sin6_len = sizeof(struct sockaddr_in6);
575 #endif
576 break;
577 }
578
579 rpc->connect_cb = cb;
580 rpc->connect_data = private_data;
581
582 freeaddrinfo(ai);
583
584 if (rpc_connect_sockaddr_async(rpc, &rpc->s) != 0) {
585 return -1;
586 }
587
588 return 0;
589 }
590
591 int rpc_disconnect(struct rpc_context *rpc, char *error)
592 {
593 assert(rpc->magic == RPC_CONTEXT_MAGIC);
594
595 rpc_unset_autoreconnect(rpc);
596
597 if (rpc->fd != -1) {
598 close(rpc->fd);
599 }
600 rpc->fd = -1;
601
602 rpc->is_connected = 0;
603
604 rpc_error_all_pdus(rpc, error);
605
606 return 0;
607 }
608
609 static void reconnect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
610 {
611 assert(rpc->magic == RPC_CONTEXT_MAGIC);
612
613 if (status != RPC_STATUS_SUCCESS) {
614 rpc_error_all_pdus(rpc, "RPC ERROR: Failed to reconnect async");
615 return;
616 }
617
618 rpc->is_connected = 1;
619 rpc->connect_cb = NULL;
620 }
621
622 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
623 static int rpc_reconnect_requeue(struct rpc_context *rpc)
624 {
625 struct rpc_pdu *pdu;
626 unsigned int i;
627
628 assert(rpc->magic == RPC_CONTEXT_MAGIC);
629
630 if (rpc->fd != -1) {
631 close(rpc->fd);
632 }
633 rpc->fd = -1;
634
635 rpc->is_connected = 0;
636
637 /* socket is closed so we will not get any replies to any commands
638 * in flight. Move them all over from the waitpdu queue back to the out queue
639 */
640 for (i = 0; i < HASHES; i++) {
641 struct rpc_queue *q = &rpc->waitpdu[i];
642
643 for (pdu=q->head; pdu; pdu=pdu->next) {
644 rpc_return_to_queue(&rpc->outqueue, pdu);
645 /* we have to re-send the whole pdu again */
646 pdu->written = 0;
647 }
648 rpc_reset_queue(q);
649 }
650
651 if (rpc->auto_reconnect != 0) {
652 rpc->connect_cb = reconnect_cb;
653
654 if (rpc_connect_sockaddr_async(rpc, &rpc->s) != 0) {
655 rpc_error_all_pdus(rpc, "RPC ERROR: Failed to reconnect async");
656 return -1;
657 }
658 }
659
660 return 0;
661 }
662
663
664 int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port)
665 {
666 struct addrinfo *ai = NULL;
667 char service[6];
668
669 assert(rpc->magic == RPC_CONTEXT_MAGIC);
670
671 if (rpc->is_udp == 0) {
672 rpc_set_error(rpc, "Cant not bind UDP. Not UDP context");
673 return -1;
674 }
675
676 sprintf(service, "%d", port);
677 if (getaddrinfo(addr, service, NULL, &ai) != 0) {
678 rpc_set_error(rpc, "Invalid address:%s. "
679 "Can not resolv into IPv4/v6 structure.", addr);
680 return -1;
681 }
682
683 switch(ai->ai_family) {
684 case AF_INET:
685 rpc->fd = socket(ai->ai_family, SOCK_DGRAM, 0);
686 if (rpc->fd == -1) {
687 rpc_set_error(rpc, "Failed to create UDP socket: %s", strerror(errno));
688 freeaddrinfo(ai);
689 return -1;
690 }
691
692 if (bind(rpc->fd, (struct sockaddr *)ai->ai_addr, sizeof(struct sockaddr_in)) != 0) {
693 rpc_set_error(rpc, "Failed to bind to UDP socket: %s",strerror(errno));
694 freeaddrinfo(ai);
695 return -1;
696 }
697 break;
698 default:
699 rpc_set_error(rpc, "Can not handle UPD sockets of family %d yet", ai->ai_family);
700 freeaddrinfo(ai);
701 return -1;
702 }
703
704 freeaddrinfo(ai);
705
706 return 0;
707 }
708
709 int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int is_broadcast)
710 {
711 struct addrinfo *ai = NULL;
712 char service[6];
713
714 assert(rpc->magic == RPC_CONTEXT_MAGIC);
715
716 if (rpc->is_udp == 0) {
717 rpc_set_error(rpc, "Can not set destination sockaddr. Not UDP context");
718 return -1;
719 }
720
721 sprintf(service, "%d", port);
722 if (getaddrinfo(addr, service, NULL, &ai) != 0) {
723 rpc_set_error(rpc, "Invalid address:%s. "
724 "Can not resolv into IPv4/v6 structure.", addr);
725 return -1;
726 }
727
728 if (rpc->udp_dest) {
729 free(rpc->udp_dest);
730 rpc->udp_dest = NULL;
731 }
732 rpc->udp_dest = malloc(ai->ai_addrlen);
733 if (rpc->udp_dest == NULL) {
734 rpc_set_error(rpc, "Out of memory. Failed to allocate sockaddr structure");
735 freeaddrinfo(ai);
736 return -1;
737 }
738 memcpy(rpc->udp_dest, ai->ai_addr, ai->ai_addrlen);
739 freeaddrinfo(ai);
740
741 rpc->is_broadcast = is_broadcast;
742 setsockopt(rpc->fd, SOL_SOCKET, SO_BROADCAST, (char *)&is_broadcast, sizeof(is_broadcast));
743
744 return 0;
745 }
746
747 struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc)
748 {
749 assert(rpc->magic == RPC_CONTEXT_MAGIC);
750
751 return (struct sockaddr *)&rpc->udp_src;
752 }
753
754 int rpc_queue_length(struct rpc_context *rpc)
755 {
756 int i=0;
757 struct rpc_pdu *pdu;
758 unsigned int n;
759
760 assert(rpc->magic == RPC_CONTEXT_MAGIC);
761
762 for(pdu = rpc->outqueue.head; pdu; pdu = pdu->next) {
763 i++;
764 }
765
766 for (n = 0; n < HASHES; n++) {
767 struct rpc_queue *q = &rpc->waitpdu[n];
768
769 for(pdu = q->head; pdu; pdu = pdu->next)
770 i++;
771 }
772 return i;
773 }
774
775 void rpc_set_fd(struct rpc_context *rpc, int fd)
776 {
777 assert(rpc->magic == RPC_CONTEXT_MAGIC);
778
779 rpc->fd = fd;
780 }