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