f8db957e0bb600510fcba27afc06dd04b2145c1b
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
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.
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.
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/>.
22 #include "aros_compat.h"
26 #include "win32_compat.h"
29 #ifdef HAVE_ARPA_INET_H
30 #include <arpa/inet.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
49 #ifdef HAVE_NETINET_TCP_H
50 #include <netinet/tcp.h>
57 #ifdef HAVE_SYS_FILIO_H
58 #include <sys/filio.h>
61 #ifdef HAVE_SYS_SOCKIO_H
62 #include <sys/sockio.h>
72 #include <sys/types.h>
73 #include "libnfs-zdr.h"
75 #include "libnfs-raw.h"
76 #include "libnfs-private.h"
80 //has to be included after stdlib!!
81 #include "win32_errnowrapper.h"
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
);
87 static void set_nonblocking(int fd
)
92 v
= ioctl(fd
, FIONBIO
, &nonblocking
);
94 v
= fcntl(fd
, F_GETFL
, 0);
95 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
99 static void set_nolinger(int fd
)
104 setsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lng
, sizeof(lng
));
107 #ifdef HAVE_NETINET_TCP_H
108 int set_tcp_sockopt(int sockfd
, int optname
, int value
)
112 #if defined(__FreeBSD__) || defined(__sun) || (defined(__APPLE__) && defined(__MACH__))
113 struct protoent
*buf
;
115 if ((buf
= getprotobyname("tcp")) != NULL
)
116 level
= buf
->p_proto
;
123 return setsockopt(sockfd
, level
, optname
, (char *)&value
, sizeof(value
));
127 int rpc_get_fd(struct rpc_context
*rpc
)
129 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
134 static int rpc_has_queue(struct rpc_queue
*q
)
136 return q
->head
!= NULL
;
139 int rpc_which_events(struct rpc_context
*rpc
)
143 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
145 events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
147 if (rpc
->is_udp
!= 0) {
148 /* for udp sockets we only wait for pollin */
152 if (rpc_has_queue(&rpc
->outqueue
)) {
158 static int rpc_write_to_socket(struct rpc_context
*rpc
)
163 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
166 rpc_set_error(rpc
, "trying to write but not connected");
170 while ((pdu
= rpc
->outqueue
.head
) != NULL
) {
173 total
= pdu
->outdata
.size
;
175 count
= send(rpc
->fd
, pdu
->outdata
.data
+ pdu
->written
, total
- pdu
->written
, 0);
177 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
180 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
184 pdu
->written
+= count
;
185 if (pdu
->written
== total
) {
188 rpc
->outqueue
.head
= pdu
->next
;
189 if (pdu
->next
== NULL
)
190 rpc
->outqueue
.tail
= NULL
;
192 hash
= rpc_hash_xid(pdu
->xid
);
193 rpc_enqueue(&rpc
->waitpdu
[hash
], pdu
);
199 static int rpc_read_from_socket(struct rpc_context
*rpc
)
206 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
208 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
209 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
213 if (available
== 0) {
214 rpc_set_error(rpc
, "Socket has been closed");
220 socklen_t socklen
= sizeof(rpc
->udp_src
);
222 buf
= malloc(available
);
224 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
227 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
229 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
233 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
234 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
242 /* read record marker, 4 bytes at the beginning of every pdu */
243 if (rpc
->inbuf
== NULL
) {
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
);
251 if (rpc
->inpos
< 4) {
252 size
= 4 - rpc
->inpos
;
254 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
256 if (errno
== EINTR
) {
259 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
266 if (available
== 0) {
270 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
271 if (rpc
->insize
< pdu_size
) {
274 buf
= malloc(pdu_size
);
276 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
279 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
282 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
286 if (size
> rpc
->insize
- rpc
->inpos
) {
287 size
= rpc
->insize
- rpc
->inpos
;
290 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
292 if (errno
== EINTR
) {
295 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
301 if (rpc
->inpos
== rpc
->insize
) {
302 char *buf
= rpc
->inbuf
;
308 if (rpc_process_pdu(rpc
, buf
, pdu_size
) != 0) {
309 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
320 int rpc_service(struct rpc_context
*rpc
, int revents
)
322 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
324 if (revents
& POLLERR
) {
330 socklen_t err_size
= sizeof(err
);
332 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
333 (char *)&err
, &err_size
) != 0 || err
!= 0) {
337 rpc_set_error(rpc
, "rpc_service: socket error "
341 rpc_set_error(rpc
, "rpc_service: POLLERR, "
342 "Unknown socket error.");
344 if (rpc
->connect_cb
!= NULL
) {
345 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
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
);
357 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
359 socklen_t err_size
= sizeof(err
);
361 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
362 (char *)&err
, &err_size
) != 0 || err
!= 0) {
366 rpc_set_error(rpc
, "rpc_service: socket error "
367 "%s(%d) while connecting.",
369 if (rpc
->connect_cb
!= NULL
) {
370 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
371 NULL
, rpc
->connect_data
);
376 rpc
->is_connected
= 1;
377 if (rpc
->connect_cb
!= NULL
) {
378 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
383 if (revents
& POLLIN
) {
384 if (rpc_read_from_socket(rpc
) != 0) {
385 rpc_reconnect_requeue(rpc
);
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");
400 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
402 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
404 rpc
->auto_reconnect
= 1;
407 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
409 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
411 rpc
->auto_reconnect
= 0;
414 void rpc_set_tcp_syncnt(struct rpc_context
*rpc
, int v
)
416 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
425 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
429 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
431 switch (s
->ss_family
) {
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
);
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
);
451 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
456 rpc_set_error(rpc
, "Failed to open socket");
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.
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.
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.
472 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
473 * to make the executable able to bind to a system port.
475 * On Windows, there is no concept of privileged ports. Thus
476 * binding will usually succeed.
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
;
486 portOfs
= time(NULL
) % 400;
491 port
= htons(firstPort
+ portOfs
);
492 portOfs
= (portOfs
+ 1) % portCount
;
494 /* skip well-known ports */
495 if (!getservbyport(port
, "tcp")) {
496 memset(&ss
, 0, sizeof(ss
));
498 switch (s
->ss_family
) {
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
);
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
);
515 rc
= bind(rpc
->fd
, (struct sockaddr
*)&ss
, socksize
);
517 /* we got EACCES, so don't try again */
518 if (rc
!= 0 && errno
== EACCES
)
522 } while (rc
!= 0 && portOfs
!= startOfs
);
525 set_nonblocking(rpc
->fd
);
526 set_nolinger(rpc
->fd
);
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
);
536 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
538 struct addrinfo
*ai
= NULL
;
540 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
543 rpc_set_error(rpc
, "Trying to connect while already connected");
547 if (rpc
->is_udp
!= 0) {
548 rpc_set_error(rpc
, "Trying to connect on UDP socket");
552 rpc
->auto_reconnect
= 0;
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
);
560 switch (ai
->ai_family
) {
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
);
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
);
579 rpc
->connect_cb
= cb
;
580 rpc
->connect_data
= private_data
;
584 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
591 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
593 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
595 rpc_unset_autoreconnect(rpc
);
602 rpc
->is_connected
= 0;
604 rpc_error_all_pdus(rpc
, error
);
609 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
611 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
613 if (status
!= RPC_STATUS_SUCCESS
) {
614 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
618 rpc
->is_connected
= 1;
619 rpc
->connect_cb
= NULL
;
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
)
628 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
635 rpc
->is_connected
= 0;
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
640 for (i
= 0; i
< HASHES
; i
++) {
641 struct rpc_queue
*q
= &rpc
->waitpdu
[i
];
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 */
651 if (rpc
->auto_reconnect
!= 0) {
652 rpc
->connect_cb
= reconnect_cb
;
654 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
655 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
664 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
666 struct addrinfo
*ai
= NULL
;
669 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
671 if (rpc
->is_udp
== 0) {
672 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
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
);
683 switch(ai
->ai_family
) {
685 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
687 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
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
));
699 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
709 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
711 struct addrinfo
*ai
= NULL
;
714 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
716 if (rpc
->is_udp
== 0) {
717 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
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
);
730 rpc
->udp_dest
= NULL
;
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");
738 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
741 rpc
->is_broadcast
= is_broadcast
;
742 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
747 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
749 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
751 return (struct sockaddr
*)&rpc
->udp_src
;
754 int rpc_queue_length(struct rpc_context
*rpc
)
760 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
762 for(pdu
= rpc
->outqueue
.head
; pdu
; pdu
= pdu
->next
) {
766 for (n
= 0; n
< HASHES
; n
++) {
767 struct rpc_queue
*q
= &rpc
->waitpdu
[n
];
769 for(pdu
= q
->head
; pdu
; pdu
= pdu
->next
)
775 void rpc_set_fd(struct rpc_context
*rpc
, int fd
)
777 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);