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