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>
71 #include <sys/types.h>
72 #include "libnfs-zdr.h"
74 #include "libnfs-raw.h"
75 #include "libnfs-private.h"
79 //has to be included after stdlib!!
80 #include "win32_errnowrapper.h"
83 static int rpc_reconnect_requeue(struct rpc_context
*rpc
);
84 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
);
86 static void set_nonblocking(int fd
)
91 v
= ioctl(fd
, FIONBIO
, &nonblocking
);
93 v
= fcntl(fd
, F_GETFL
, 0);
94 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
98 static void set_nolinger(int fd
)
103 setsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lng
, sizeof(lng
));
106 #ifdef HAVE_NETINET_TCP_H
107 int set_tcp_sockopt(int sockfd
, int optname
, int value
)
111 #if defined(__FreeBSD__) || defined(__sun) || (defined(__APPLE__) && defined(__MACH__))
112 struct protoent
*buf
;
114 if ((buf
= getprotobyname("tcp")) != NULL
)
115 level
= buf
->p_proto
;
122 return setsockopt(sockfd
, level
, optname
, (char *)&value
, sizeof(value
));
126 int rpc_get_fd(struct rpc_context
*rpc
)
128 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
133 static int rpc_has_queue(struct rpc_queue
*q
)
135 return q
->head
!= NULL
;
138 int rpc_which_events(struct rpc_context
*rpc
)
142 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
144 events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
146 if (rpc
->is_udp
!= 0) {
147 /* for udp sockets we only wait for pollin */
151 if (rpc_has_queue(&rpc
->outqueue
)) {
157 static int rpc_write_to_socket(struct rpc_context
*rpc
)
162 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
165 rpc_set_error(rpc
, "trying to write but not connected");
169 while ((pdu
= rpc
->outqueue
.head
) != NULL
) {
172 total
= pdu
->outdata
.size
;
174 count
= send(rpc
->fd
, pdu
->outdata
.data
+ pdu
->written
, total
- pdu
->written
, 0);
176 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
179 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
183 pdu
->written
+= count
;
184 if (pdu
->written
== total
) {
187 rpc
->outqueue
.head
= pdu
->next
;
188 if (pdu
->next
== NULL
)
189 rpc
->outqueue
.tail
= NULL
;
191 hash
= rpc_hash_xid(pdu
->xid
);
192 rpc_enqueue(&rpc
->waitpdu
[hash
], pdu
);
198 static int rpc_read_from_socket(struct rpc_context
*rpc
)
205 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
207 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
208 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
212 if (available
== 0) {
213 rpc_set_error(rpc
, "Socket has been closed");
219 socklen_t socklen
= sizeof(rpc
->udp_src
);
221 buf
= malloc(available
);
223 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
226 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
228 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
232 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
233 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
241 /* read record marker, 4 bytes at the beginning of every pdu */
242 if (rpc
->inbuf
== NULL
) {
244 rpc
->inbuf
= malloc(rpc
->insize
);
245 if (rpc
->inbuf
== NULL
) {
246 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
250 if (rpc
->inpos
< 4) {
251 size
= 4 - rpc
->inpos
;
253 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
255 if (errno
== EINTR
) {
258 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
265 if (available
== 0) {
269 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
270 if (rpc
->insize
< pdu_size
) {
273 buf
= malloc(pdu_size
);
275 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
278 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
281 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
285 if (size
> rpc
->insize
- rpc
->inpos
) {
286 size
= rpc
->insize
- rpc
->inpos
;
289 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
291 if (errno
== EINTR
) {
294 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
300 if (rpc
->inpos
== rpc
->insize
) {
301 char *buf
= rpc
->inbuf
;
307 if (rpc_process_pdu(rpc
, buf
, pdu_size
) != 0) {
308 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
319 int rpc_service(struct rpc_context
*rpc
, int revents
)
321 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
323 if (revents
& POLLERR
) {
329 socklen_t err_size
= sizeof(err
);
331 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
332 (char *)&err
, &err_size
) != 0 || err
!= 0) {
336 rpc_set_error(rpc
, "rpc_service: socket error "
340 rpc_set_error(rpc
, "rpc_service: POLLERR, "
341 "Unknown socket error.");
343 if (rpc
->connect_cb
!= NULL
) {
344 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
348 if (revents
& POLLHUP
) {
349 rpc_set_error(rpc
, "Socket failed with POLLHUP");
350 if (rpc
->connect_cb
!= NULL
) {
351 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
356 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
358 socklen_t err_size
= sizeof(err
);
360 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
361 (char *)&err
, &err_size
) != 0 || err
!= 0) {
365 rpc_set_error(rpc
, "rpc_service: socket error "
366 "%s(%d) while connecting.",
368 if (rpc
->connect_cb
!= NULL
) {
369 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
370 NULL
, rpc
->connect_data
);
375 rpc
->is_connected
= 1;
376 if (rpc
->connect_cb
!= NULL
) {
377 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
382 if (revents
& POLLIN
) {
383 if (rpc_read_from_socket(rpc
) != 0) {
384 rpc_reconnect_requeue(rpc
);
389 if (revents
& POLLOUT
&& rpc_has_queue(&rpc
->outqueue
)) {
390 if (rpc_write_to_socket(rpc
) != 0) {
391 rpc_set_error(rpc
, "write to socket failed");
399 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
401 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
403 rpc
->auto_reconnect
= 1;
406 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
408 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
410 rpc
->auto_reconnect
= 0;
413 void rpc_set_tcp_syncnt(struct rpc_context
*rpc
, int v
)
415 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
424 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
428 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
430 switch (s
->ss_family
) {
432 socksize
= sizeof(struct sockaddr_in
);
433 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
434 #ifdef HAVE_NETINET_TCP_H
435 if (rpc
->tcp_syncnt
!= RPC_PARAM_UNDEFINED
) {
436 set_tcp_sockopt(rpc
->fd
, TCP_SYNCNT
, rpc
->tcp_syncnt
);
441 socksize
= sizeof(struct sockaddr_in6
);
442 rpc
->fd
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
443 #ifdef HAVE_NETINET_TCP_H
444 if (rpc
->tcp_syncnt
!= RPC_PARAM_UNDEFINED
) {
445 set_tcp_sockopt(rpc
->fd
, TCP_SYNCNT
, rpc
->tcp_syncnt
);
450 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
455 rpc_set_error(rpc
, "Failed to open socket");
459 /* Some systems allow you to set capabilities on an executable
460 * to allow the file to be executed with privilege to bind to
461 * privileged system ports, even if the user is not root.
463 * Opportunistically try to bind the socket to a low numbered
464 * system port in the hope that the user is either root or the
465 * executable has the CAP_NET_BIND_SERVICE.
467 * As soon as we fail the bind() with EACCES we know we will never
468 * be able to bind to a system port so we terminate the loop.
471 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
472 * to make the executable able to bind to a system port.
474 * On Windows, there is no concept of privileged ports. Thus
475 * binding will usually succeed.
478 struct sockaddr_storage ss
;
479 static int portOfs
= 0;
480 const int firstPort
= 512; /* >= 512 according to Sun docs */
481 const int portCount
= IPPORT_RESERVED
- firstPort
;
482 int startOfs
, port
, rc
;
485 portOfs
= time(NULL
) % 400;
490 port
= htons(firstPort
+ portOfs
);
491 portOfs
= (portOfs
+ 1) % portCount
;
493 /* skip well-known ports */
494 if (!getservbyport(port
, "tcp")) {
495 memset(&ss
, 0, sizeof(ss
));
497 switch (s
->ss_family
) {
499 ((struct sockaddr_in
*)&ss
)->sin_port
= port
;
500 ((struct sockaddr_in
*)&ss
)->sin_family
= AF_INET
;
501 #ifdef HAVE_SOCKADDR_LEN
502 ((struct sockaddr_in
*)&ss
)->sin_len
= sizeof(struct sockaddr_in
);
506 ((struct sockaddr_in6
*)&ss
)->sin6_port
= port
;
507 ((struct sockaddr_in6
*)&ss
)->sin6_family
= AF_INET6
;
508 #ifdef HAVE_SOCKADDR_LEN
509 ((struct sockaddr_in6
*)&ss
)->sin6_len
= sizeof(struct sockaddr6_in
);
514 rc
= bind(rpc
->fd
, (struct sockaddr
*)&ss
, socksize
);
516 /* we got EACCES, so don't try again */
517 if (rc
!= 0 && errno
== EACCES
)
521 } while (rc
!= 0 && portOfs
!= startOfs
);
524 set_nonblocking(rpc
->fd
);
525 set_nolinger(rpc
->fd
);
527 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
528 rpc_set_error(rpc
, "connect() to server failed. %s(%d)", strerror(errno
), errno
);
535 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
537 struct addrinfo
*ai
= NULL
;
539 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
542 rpc_set_error(rpc
, "Trying to connect while already connected");
546 if (rpc
->is_udp
!= 0) {
547 rpc_set_error(rpc
, "Trying to connect on UDP socket");
551 rpc
->auto_reconnect
= 0;
553 if (getaddrinfo(server
, NULL
, NULL
, &ai
) != 0) {
554 rpc_set_error(rpc
, "Invalid address:%s. "
555 "Can not resolv into IPv4/v6 structure.", server
);
559 switch (ai
->ai_family
) {
561 ((struct sockaddr_in
*)&rpc
->s
)->sin_family
= ai
->ai_family
;
562 ((struct sockaddr_in
*)&rpc
->s
)->sin_port
= htons(port
);
563 ((struct sockaddr_in
*)&rpc
->s
)->sin_addr
= ((struct sockaddr_in
*)(ai
->ai_addr
))->sin_addr
;
564 #ifdef HAVE_SOCKADDR_LEN
565 ((struct sockaddr_in
*)&rpc
->s
)->sin_len
= sizeof(struct sockaddr_in
);
569 ((struct sockaddr_in6
*)&rpc
->s
)->sin6_family
= ai
->ai_family
;
570 ((struct sockaddr_in6
*)&rpc
->s
)->sin6_port
= htons(port
);
571 ((struct sockaddr_in6
*)&rpc
->s
)->sin6_addr
= ((struct sockaddr_in6
*)(ai
->ai_addr
))->sin6_addr
;
572 #ifdef HAVE_SOCKADDR_LEN
573 ((struct sockaddr_in6
*)&rpc
->s
)->sin6_len
= sizeof(struct sockaddr_in6
);
578 rpc
->connect_cb
= cb
;
579 rpc
->connect_data
= private_data
;
583 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
590 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
592 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
594 rpc_unset_autoreconnect(rpc
);
601 rpc
->is_connected
= 0;
603 rpc_error_all_pdus(rpc
, error
);
608 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
610 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
612 if (status
!= RPC_STATUS_SUCCESS
) {
613 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
617 rpc
->is_connected
= 1;
618 rpc
->connect_cb
= NULL
;
621 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
622 static int rpc_reconnect_requeue(struct rpc_context
*rpc
)
627 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
634 rpc
->is_connected
= 0;
636 /* socket is closed so we will not get any replies to any commands
637 * in flight. Move them all over from the waitpdu queue back to the out queue
639 for (i
= 0; i
< HASHES
; i
++) {
640 struct rpc_queue
*q
= &rpc
->waitpdu
[i
];
642 for (pdu
=q
->head
; pdu
; pdu
=pdu
->next
) {
643 rpc_return_to_queue(&rpc
->outqueue
, pdu
);
644 /* we have to re-send the whole pdu again */
650 if (rpc
->auto_reconnect
!= 0) {
651 rpc
->connect_cb
= reconnect_cb
;
653 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
654 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
663 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
665 struct addrinfo
*ai
= NULL
;
668 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
670 if (rpc
->is_udp
== 0) {
671 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
675 sprintf(service
, "%d", port
);
676 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
677 rpc_set_error(rpc
, "Invalid address:%s. "
678 "Can not resolv into IPv4/v6 structure.", addr
);
682 switch(ai
->ai_family
) {
684 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
686 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
691 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
692 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
698 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
708 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
710 struct addrinfo
*ai
= NULL
;
713 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
715 if (rpc
->is_udp
== 0) {
716 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
720 sprintf(service
, "%d", port
);
721 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
722 rpc_set_error(rpc
, "Invalid address:%s. "
723 "Can not resolv into IPv4/v6 structure.", addr
);
729 rpc
->udp_dest
= NULL
;
731 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
732 if (rpc
->udp_dest
== NULL
) {
733 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
737 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
740 rpc
->is_broadcast
= is_broadcast
;
741 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
746 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
748 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
750 return (struct sockaddr
*)&rpc
->udp_src
;
753 int rpc_queue_length(struct rpc_context
*rpc
)
759 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
761 for(pdu
= rpc
->outqueue
.head
; pdu
; pdu
= pdu
->next
) {
765 for (n
= 0; n
< HASHES
; n
++) {
766 struct rpc_queue
*q
= &rpc
->waitpdu
[n
];
768 for(pdu
= q
->head
; pdu
; pdu
= pdu
->next
)
774 void rpc_set_fd(struct rpc_context
*rpc
, int fd
)
776 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);