Merge pull request #32 from hulu/master
[deb_libnfs.git] / lib / socket.c
CommitLineData
84004dbf
RS
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*/
a8a1b858
M
17#ifdef WIN32
18#include "win32_compat.h"
19#else
20#include <unistd.h>
21#include <poll.h>
22#include <arpa/inet.h>
23#include <sys/ioctl.h>
24#include <sys/socket.h>
25#include <netdb.h>
26#endif/*WIN32*/
84004dbf 27
fc01d2a9
TN
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
84004dbf 31#include <stdio.h>
98f5fee8 32#include <stdlib.h>
4a2b0876 33#include <assert.h>
84004dbf 34#include <fcntl.h>
84004dbf
RS
35#include <string.h>
36#include <errno.h>
fc01d2a9
TN
37#ifdef HAVE_SYS_FILIO_H
38#include <sys/filio.h>
39#endif
647d2ea1
RS
40#ifdef HAVE_SYS_SOCKIO_H
41#include <sys/sockio.h>
42#endif
485bc9b9 43#include <sys/types.h>
763cd6e3 44#include "libnfs-zdr.h"
84004dbf
RS
45#include "libnfs.h"
46#include "libnfs-raw.h"
47#include "libnfs-private.h"
48#include "slist.h"
49
fcc42bfe
M
50#ifdef WIN32
51//has to be included after stdlib!!
52#include "win32_errnowrapper.h"
53#endif
54
55
1744ef90
RS
56static int rpc_reconnect_requeue(struct rpc_context *rpc);
57static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_storage *s);
b077fdeb 58
84004dbf
RS
59static void set_nonblocking(int fd)
60{
99c14c9b 61 int v = 0;
6874f61e 62#if defined(WIN32)
99c14c9b 63 long nonblocking=1;
64 v = ioctlsocket(fd, FIONBIO,&nonblocking);
6874f61e 65#else
84004dbf
RS
66 v = fcntl(fd, F_GETFL, 0);
67 fcntl(fd, F_SETFL, v | O_NONBLOCK);
a8a1b858 68#endif //FIXME
84004dbf
RS
69}
70
71int rpc_get_fd(struct rpc_context *rpc)
72{
4a2b0876
RS
73 assert(rpc->magic == RPC_CONTEXT_MAGIC);
74
84004dbf
RS
75 return rpc->fd;
76}
77
78int rpc_which_events(struct rpc_context *rpc)
79{
4a2b0876
RS
80 int events;
81
82 assert(rpc->magic == RPC_CONTEXT_MAGIC);
83
84 events = rpc->is_connected ? POLLIN : POLLOUT;
84004dbf 85
5911f3e8
RS
86 if (rpc->is_udp != 0) {
87 /* for udp sockets we only wait for pollin */
88 return POLLIN;
89 }
90
84004dbf
RS
91 if (rpc->outqueue) {
92 events |= POLLOUT;
93 }
94 return events;
95}
96
97static int rpc_write_to_socket(struct rpc_context *rpc)
98{
d14e2838 99 int32_t count;
84004dbf 100
4a2b0876
RS
101 assert(rpc->magic == RPC_CONTEXT_MAGIC);
102
84004dbf 103 if (rpc->fd == -1) {
1896d37b
RS
104 rpc_set_error(rpc, "trying to write but not connected");
105 return -1;
84004dbf
RS
106 }
107
108 while (rpc->outqueue != NULL) {
183451cf 109 int64_t total;
84004dbf
RS
110
111 total = rpc->outqueue->outdata.size;
112
6874f61e
RS
113#if defined(WIN32)
114 count = send(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written, 0);
115#else
84004dbf 116 count = write(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written);
6874f61e 117#endif
84004dbf
RS
118 if (count == -1) {
119 if (errno == EAGAIN || errno == EWOULDBLOCK) {
84004dbf
RS
120 return 0;
121 }
1896d37b
RS
122 rpc_set_error(rpc, "Error when writing to socket :%s(%d)", strerror(errno), errno);
123 return -1;
84004dbf
RS
124 }
125
126 rpc->outqueue->written += count;
127 if (rpc->outqueue->written == total) {
128 struct rpc_pdu *pdu = rpc->outqueue;
129
130 SLIST_REMOVE(&rpc->outqueue, pdu);
131 SLIST_ADD_END(&rpc->waitpdu, pdu);
132 }
133 }
134 return 0;
135}
136
137static int rpc_read_from_socket(struct rpc_context *rpc)
138{
139 int available;
140 int size;
cdb19ec1 141 int pdu_size;
d14e2838 142 int32_t count;
84004dbf 143
f3a75078 144 assert(rpc->magic == RPC_CONTEXT_MAGIC);
84004dbf 145
4a2b0876
RS
146 assert(rpc->magic == RPC_CONTEXT_MAGIC);
147
6874f61e
RS
148#if defined(WIN32)
149 if (ioctlsocket(rpc->fd, FIONREAD, &available) != 0) {
150#else
84004dbf 151 if (ioctl(rpc->fd, FIONREAD, &available) != 0) {
6874f61e 152#endif
84004dbf
RS
153 rpc_set_error(rpc, "Ioctl FIONREAD returned error : %d. Closing socket.", errno);
154 return -1;
155 }
a8a1b858 156
84004dbf
RS
157 if (available == 0) {
158 rpc_set_error(rpc, "Socket has been closed");
1896d37b 159 return -1;
84004dbf 160 }
cdb19ec1 161
0268794f
RS
162 if (rpc->is_udp) {
163 char *buf;
164 socklen_t socklen = sizeof(rpc->udp_src);
165
166 buf = malloc(available);
167 if (buf == NULL) {
168 rpc_set_error(rpc, "Failed to malloc buffer for recvfrom");
169 return -1;
170 }
171 count = recvfrom(rpc->fd, buf, available, MSG_DONTWAIT, (struct sockaddr *)&rpc->udp_src, &socklen);
172 if (count < 0) {
173 rpc_set_error(rpc, "Failed recvfrom: %s", strerror(errno));
174 free(buf);
175 }
176 if (rpc_process_pdu(rpc, buf, count) != 0) {
177 rpc_set_error(rpc, "Invalid/garbage pdu received from server. Ignoring PDU");
178 free(buf);
179 return -1;
180 }
181 free(buf);
182 return 0;
183 }
184
cdb19ec1
RS
185 /* read record marker, 4 bytes at the beginning of every pdu */
186 if (rpc->inbuf == NULL) {
187 rpc->insize = 4;
188 rpc->inbuf = malloc(rpc->insize);
189 if (rpc->inbuf == NULL) {
190 rpc_set_error(rpc, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno);
191 return -1;
192 }
193 }
194 if (rpc->inpos < 4) {
195 size = 4 - rpc->inpos;
196
6874f61e
RS
197#if defined(WIN32)
198 count = recv(rpc->fd, rpc->inbuf + rpc->inpos, size, 0);
199#else
cdb19ec1 200 count = read(rpc->fd, rpc->inbuf + rpc->inpos, size);
6874f61e 201#endif
cdb19ec1
RS
202 if (count == -1) {
203 if (errno == EINTR) {
204 return 0;
205 }
206 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
207 return -1;
208 }
209 available -= count;
210 rpc->inpos += count;
211 }
212
213 if (available == 0) {
214 return 0;
215 }
216
217 pdu_size = rpc_get_pdu_size(rpc->inbuf);
218 if (rpc->insize < pdu_size) {
219 unsigned char *buf;
220
221 buf = malloc(pdu_size);
222 if (buf == NULL) {
223 rpc_set_error(rpc, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size, errno);
224 return -1;
225 }
226 memcpy(buf, rpc->inbuf, rpc->insize);
227 free(rpc->inbuf);
228 rpc->inbuf = buf;
229 rpc->insize = rpc_get_pdu_size(rpc->inbuf);
84004dbf 230 }
cdb19ec1
RS
231
232 size = available;
233 if (size > rpc->insize - rpc->inpos) {
234 size = rpc->insize - rpc->inpos;
84004dbf
RS
235 }
236
6874f61e
RS
237#if defined(WIN32)
238 count = recv(rpc->fd, rpc->inbuf + rpc->inpos, size, 0);
239#else
cdb19ec1 240 count = read(rpc->fd, rpc->inbuf + rpc->inpos, size);
6874f61e 241#endif
84004dbf
RS
242 if (count == -1) {
243 if (errno == EINTR) {
84004dbf
RS
244 return 0;
245 }
246 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
1896d37b 247 return -1;
84004dbf 248 }
cdb19ec1
RS
249 available -= count;
250 rpc->inpos += count;
84004dbf 251
cdb19ec1
RS
252 if (rpc->inpos == rpc->insize) {
253 if (rpc_process_pdu(rpc, rpc->inbuf, pdu_size) != 0) {
84004dbf 254 rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket");
1896d37b 255 return -1;
84004dbf 256 }
cdb19ec1
RS
257 free(rpc->inbuf);
258 rpc->inbuf = NULL;
259 rpc->insize = 0;
260 rpc->inpos = 0;
84004dbf 261 }
cdb19ec1 262
84004dbf
RS
263 return 0;
264}
265
266
267
268int rpc_service(struct rpc_context *rpc, int revents)
269{
4a2b0876
RS
270 assert(rpc->magic == RPC_CONTEXT_MAGIC);
271
84004dbf 272 if (revents & POLLERR) {
fcc42bfe 273#ifdef WIN32
a8a1b858 274 char err = 0;
fcc42bfe
M
275#else
276 int err = 0;
277#endif
912f7ad5
RS
278 socklen_t err_size = sizeof(err);
279
280 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
bb4e9ed6 281 (char *)&err, &err_size) != 0 || err != 0) {
912f7ad5
RS
282 if (err == 0) {
283 err = errno;
284 }
285 rpc_set_error(rpc, "rpc_service: socket error "
286 "%s(%d).",
287 strerror(err), err);
84004dbf 288 } else {
912f7ad5
RS
289 rpc_set_error(rpc, "rpc_service: POLLERR, "
290 "Unknown socket error.");
84004dbf 291 }
b990de23
RS
292 if (rpc->connect_cb != NULL) {
293 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
294 }
84004dbf
RS
295 return -1;
296 }
297 if (revents & POLLHUP) {
84004dbf 298 rpc_set_error(rpc, "Socket failed with POLLHUP");
b990de23
RS
299 if (rpc->connect_cb != NULL) {
300 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
301 }
1896d37b 302 return -1;
84004dbf
RS
303 }
304
305 if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) {
912f7ad5
RS
306 int err = 0;
307 socklen_t err_size = sizeof(err);
308
309 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
bb4e9ed6 310 (char *)&err, &err_size) != 0 || err != 0) {
912f7ad5
RS
311 if (err == 0) {
312 err = errno;
313 }
314 rpc_set_error(rpc, "rpc_service: socket error "
315 "%s(%d) while connecting.",
316 strerror(err), err);
b990de23
RS
317 if (rpc->connect_cb != NULL) {
318 rpc->connect_cb(rpc, RPC_STATUS_ERROR,
912f7ad5 319 NULL, rpc->connect_data);
b990de23 320 }
912f7ad5
RS
321 return -1;
322 }
323
84004dbf 324 rpc->is_connected = 1;
b990de23
RS
325 if (rpc->connect_cb != NULL) {
326 rpc->connect_cb(rpc, RPC_STATUS_SUCCESS, NULL, rpc->connect_data);
327 }
84004dbf
RS
328 return 0;
329 }
330
b077fdeb
RS
331 if (revents & POLLIN) {
332 if (rpc_read_from_socket(rpc) != 0) {
1744ef90 333 rpc_reconnect_requeue(rpc);
b077fdeb 334 return 0;
84004dbf
RS
335 }
336 }
337
b077fdeb
RS
338 if (revents & POLLOUT && rpc->outqueue != NULL) {
339 if (rpc_write_to_socket(rpc) != 0) {
340 rpc_set_error(rpc, "write to socket failed");
1896d37b 341 return -1;
84004dbf
RS
342 }
343 }
344
345 return 0;
346}
347
1744ef90 348void rpc_set_autoreconnect(struct rpc_context *rpc)
84004dbf 349{
4a2b0876
RS
350 assert(rpc->magic == RPC_CONTEXT_MAGIC);
351
1744ef90
RS
352 rpc->auto_reconnect = 1;
353}
84004dbf 354
1744ef90
RS
355void rpc_unset_autoreconnect(struct rpc_context *rpc)
356{
4a2b0876
RS
357 assert(rpc->magic == RPC_CONTEXT_MAGIC);
358
1744ef90
RS
359 rpc->auto_reconnect = 0;
360}
070287e5 361
1744ef90
RS
362static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_storage *s)
363{
364 int socksize;
84004dbf 365
4a2b0876
RS
366 assert(rpc->magic == RPC_CONTEXT_MAGIC);
367
1744ef90 368 switch (s->ss_family) {
84004dbf 369 case AF_INET:
84004dbf 370 socksize = sizeof(struct sockaddr_in);
6874f61e 371 rpc->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
84004dbf 372 break;
1744ef90
RS
373 default:
374 rpc_set_error(rpc, "Can not handle AF_FAMILY:%d", s->ss_family);
375 return -1;
84004dbf
RS
376 }
377
378 if (rpc->fd == -1) {
379 rpc_set_error(rpc, "Failed to open socket");
1896d37b 380 return -1;
84004dbf
RS
381 }
382
07fd0cbc
RS
383 /* Some systems allow you to set capabilities on an executable
384 * to allow the file to be executed with privilege to bind to
385 * privileged system ports, even if the user is not root.
386 *
387 * Opportunistically try to bind the socket to a low numbered
388 * system port in the hope that the user is either root or the
389 * executable has the CAP_NET_BIND_SERVICE.
390 *
391 * As soon as we fail the bind() with EACCES we know we will never
392 * be able to bind to a system port so we terminate the loop.
393 *
394 * On linux, use
395 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
396 * to make the executable able to bind to a system port.
ac559609
RI
397 *
398 * On Windows, there is no concept of privileged ports. Thus
399 * binding will usually succeed.
07fd0cbc 400 */
ac559609
RI
401 {
402 struct sockaddr_in sin;
403 static int portOfs = 0;
404 const int firstPort = 512; /* >= 512 according to Sun docs */
405 const int portCount = IPPORT_RESERVED - firstPort;
406 int startOfs = portOfs, port, rc;
407
408 do {
409 rc = -1;
410 port = htons(firstPort + portOfs);
411 portOfs = (portOfs + 1) % portCount;
412
413 /* skip well-known ports */
414 if (!getservbyport(port, "tcp")) {
415 memset(&sin, 0, sizeof(sin));
416 sin.sin_port = port;
417 sin.sin_family = AF_INET;
418 sin.sin_addr.s_addr = 0;
419
420 rc = bind(rpc->fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in));
421#if !defined(WIN32)
422 /* we got EACCES, so don't try again */
423 if (rc != 0 && errno == EACCES)
424 break;
425#endif
07fd0cbc 426 }
ac559609 427 } while (rc != 0 && portOfs != startOfs);
07fd0cbc 428 }
07fd0cbc 429
84004dbf 430 set_nonblocking(rpc->fd);
07fd0cbc 431
f96b24fa
RI
432 if (connect(rpc->fd, (struct sockaddr *)s, socksize) != 0 && errno != EINPROGRESS) {
433 rpc_set_error(rpc, "connect() to server failed. %s(%d)", strerror(errno), errno);
1896d37b 434 return -1;
84004dbf
RS
435 }
436
437 return 0;
438}
439
1744ef90
RS
440int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data)
441{
442 struct sockaddr_in *sin = (struct sockaddr_in *)&rpc->s;
443
4a2b0876
RS
444 assert(rpc->magic == RPC_CONTEXT_MAGIC);
445
1744ef90
RS
446 if (rpc->fd != -1) {
447 rpc_set_error(rpc, "Trying to connect while already connected");
448 return -1;
449 }
450
451 if (rpc->is_udp != 0) {
452 rpc_set_error(rpc, "Trying to connect on UDP socket");
453 return -1;
454 }
455
456 rpc->auto_reconnect = 0;
457
458 sin->sin_family = AF_INET;
459 sin->sin_port = htons(port);
460 if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) {
461 rpc_set_error(rpc, "Not a valid server ip address");
462 return -1;
463 }
464
465
466 switch (rpc->s.ss_family) {
467 case AF_INET:
468#ifdef HAVE_SOCKADDR_LEN
469 sin->sin_len = sizeof(struct sockaddr_in);
470#endif
471 break;
472 }
473
474 rpc->connect_cb = cb;
475 rpc->connect_data = private_data;
476
477 if (rpc_connect_sockaddr_async(rpc, &rpc->s) != 0) {
478 return -1;
479 }
480
481 return 0;
482}
483
84004dbf
RS
484int rpc_disconnect(struct rpc_context *rpc, char *error)
485{
4a2b0876
RS
486 assert(rpc->magic == RPC_CONTEXT_MAGIC);
487
1744ef90
RS
488 rpc_unset_autoreconnect(rpc);
489
84004dbf 490 if (rpc->fd != -1) {
6874f61e
RS
491#if defined(WIN32)
492 closesocket(rpc->fd);
493#else
84004dbf 494 close(rpc->fd);
6874f61e 495#endif
84004dbf
RS
496 }
497 rpc->fd = -1;
498
499 rpc->is_connected = 0;
500
501 rpc_error_all_pdus(rpc, error);
502
503 return 0;
504}
485bc9b9 505
1744ef90
RS
506static void reconnect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
507{
4a2b0876
RS
508 assert(rpc->magic == RPC_CONTEXT_MAGIC);
509
1744ef90
RS
510 if (status != RPC_STATUS_SUCCESS) {
511 rpc_error_all_pdus(rpc, "RPC ERROR: Failed to reconnect async");
512 return;
513 }
514
515 rpc->is_connected = 1;
b990de23 516 rpc->connect_cb = NULL;
1744ef90
RS
517}
518
519/* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
520static int rpc_reconnect_requeue(struct rpc_context *rpc)
b077fdeb
RS
521{
522 struct rpc_pdu *pdu;
523
4a2b0876
RS
524 assert(rpc->magic == RPC_CONTEXT_MAGIC);
525
b077fdeb 526 if (rpc->fd != -1) {
6874f61e
RS
527#if defined(WIN32)
528 closesocket(rpc->fd);
529#else
b077fdeb 530 close(rpc->fd);
6874f61e 531#endif
b077fdeb
RS
532 }
533 rpc->fd = -1;
534
535 rpc->is_connected = 0;
536
537 /* socket is closed so we will not get any replies to any commands
538 * in flight. Move them all over from the waitpdu queue back to the out queue
539 */
540 for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
541 SLIST_REMOVE(&rpc->waitpdu, pdu);
542 SLIST_ADD(&rpc->outqueue, pdu);
1744ef90
RS
543 /* we have to re-send the whole pdu again */
544 pdu->written = 0;
545 }
546
547 if (rpc->auto_reconnect != 0) {
548 rpc->connect_cb = reconnect_cb;
549
550 if (rpc_connect_sockaddr_async(rpc, &rpc->s) != 0) {
551 rpc_error_all_pdus(rpc, "RPC ERROR: Failed to reconnect async");
552 return -1;
553 }
b077fdeb
RS
554 }
555
556 return 0;
557}
558
485bc9b9
RS
559
560int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port)
561{
562 struct addrinfo *ai = NULL;
563 char service[6];
564
4a2b0876
RS
565 assert(rpc->magic == RPC_CONTEXT_MAGIC);
566
485bc9b9
RS
567 if (rpc->is_udp == 0) {
568 rpc_set_error(rpc, "Cant not bind UDP. Not UDP context");
569 return -1;
570 }
571
6874f61e 572 sprintf(service, "%d", port);
485bc9b9
RS
573 if (getaddrinfo(addr, service, NULL, &ai) != 0) {
574 rpc_set_error(rpc, "Invalid address:%s. "
575 "Can not resolv into IPv4/v6 structure.");
576 return -1;
577 }
578
579 switch(ai->ai_family) {
580 case AF_INET:
581 rpc->fd = socket(ai->ai_family, SOCK_DGRAM, 0);
582 if (rpc->fd == -1) {
583 rpc_set_error(rpc, "Failed to create UDP socket: %s", strerror(errno));
584 freeaddrinfo(ai);
585 return -1;
586 }
587
588 if (bind(rpc->fd, (struct sockaddr *)ai->ai_addr, sizeof(struct sockaddr_in)) != 0) {
589 rpc_set_error(rpc, "Failed to bind to UDP socket: %s",strerror(errno));
590 freeaddrinfo(ai);
591 return -1;
592 }
593 break;
594 default:
595 rpc_set_error(rpc, "Can not handle UPD sockets of family %d yet", ai->ai_family);
596 freeaddrinfo(ai);
597 return -1;
598 }
599
600 freeaddrinfo(ai);
601
602 return 0;
603}
604
5bf60dc6
RS
605int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int is_broadcast)
606{
607 struct addrinfo *ai = NULL;
608 char service[6];
609
4a2b0876
RS
610 assert(rpc->magic == RPC_CONTEXT_MAGIC);
611
5bf60dc6
RS
612 if (rpc->is_udp == 0) {
613 rpc_set_error(rpc, "Can not set destination sockaddr. Not UDP context");
614 return -1;
615 }
616
6874f61e 617 sprintf(service, "%d", port);
5bf60dc6
RS
618 if (getaddrinfo(addr, service, NULL, &ai) != 0) {
619 rpc_set_error(rpc, "Invalid address:%s. "
620 "Can not resolv into IPv4/v6 structure.");
621 return -1;
622 }
623
624 if (rpc->udp_dest) {
625 free(rpc->udp_dest);
626 rpc->udp_dest = NULL;
627 }
628 rpc->udp_dest = malloc(ai->ai_addrlen);
629 if (rpc->udp_dest == NULL) {
630 rpc_set_error(rpc, "Out of memory. Failed to allocate sockaddr structure");
be7f5933 631 freeaddrinfo(ai);
5bf60dc6
RS
632 return -1;
633 }
634 memcpy(rpc->udp_dest, ai->ai_addr, ai->ai_addrlen);
635 freeaddrinfo(ai);
636
637 rpc->is_broadcast = is_broadcast;
bb4e9ed6 638 setsockopt(rpc->fd, SOL_SOCKET, SO_BROADCAST, (char *)&is_broadcast, sizeof(is_broadcast));
5bf60dc6
RS
639
640 return 0;
641}
c481da67
RS
642
643struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc)
644{
4a2b0876
RS
645 assert(rpc->magic == RPC_CONTEXT_MAGIC);
646
c481da67
RS
647 return (struct sockaddr *)&rpc->udp_src;
648}
83aa785d
RS
649
650int rpc_queue_length(struct rpc_context *rpc)
651{
652 int i=0;
653 struct rpc_pdu *pdu;
654
4a2b0876
RS
655 assert(rpc->magic == RPC_CONTEXT_MAGIC);
656
83aa785d
RS
657 for(pdu = rpc->outqueue; pdu; pdu = pdu->next) {
658 i++;
659 }
660 for(pdu = rpc->waitpdu; pdu; pdu = pdu->next) {
661 i++;
662 }
663 return i;
664}