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 #ifdef HAVE_NETINET_TCP_H
99 int set_tcp_sockopt(int sockfd
, int optname
, int value
)
103 #if defined(__FreeBSD__) || defined(__sun) || (defined(__APPLE__) && defined(__MACH__))
104 struct protoent
*buf
;
106 if ((buf
= getprotobyname("tcp")) != NULL
)
107 level
= buf
->p_proto
;
114 return setsockopt(sockfd
, level
, optname
, (char *)&value
, sizeof(value
));
118 int rpc_get_fd(struct rpc_context
*rpc
)
120 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
125 static int rpc_has_queue(struct rpc_queue
*q
)
127 return q
->head
!= NULL
;
130 int rpc_which_events(struct rpc_context
*rpc
)
134 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
136 events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
138 if (rpc
->is_udp
!= 0) {
139 /* for udp sockets we only wait for pollin */
143 if (rpc_has_queue(&rpc
->outqueue
)) {
149 static int rpc_write_to_socket(struct rpc_context
*rpc
)
154 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
157 rpc_set_error(rpc
, "trying to write but not connected");
161 while ((pdu
= rpc
->outqueue
.head
) != NULL
) {
164 total
= pdu
->outdata
.size
;
166 count
= send(rpc
->fd
, pdu
->outdata
.data
+ pdu
->written
, total
- pdu
->written
, 0);
168 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
171 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
175 pdu
->written
+= count
;
176 if (pdu
->written
== total
) {
179 rpc
->outqueue
.head
= pdu
->next
;
180 if (pdu
->next
== NULL
)
181 rpc
->outqueue
.tail
= NULL
;
183 hash
= rpc_hash_xid(pdu
->xid
);
184 rpc_enqueue(&rpc
->waitpdu
[hash
], pdu
);
190 static int rpc_read_from_socket(struct rpc_context
*rpc
)
197 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
199 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
200 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
204 if (available
== 0) {
205 rpc_set_error(rpc
, "Socket has been closed");
211 socklen_t socklen
= sizeof(rpc
->udp_src
);
213 buf
= malloc(available
);
215 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
218 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
220 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
224 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
225 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
233 /* read record marker, 4 bytes at the beginning of every pdu */
234 if (rpc
->inbuf
== NULL
) {
236 rpc
->inbuf
= malloc(rpc
->insize
);
237 if (rpc
->inbuf
== NULL
) {
238 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
242 if (rpc
->inpos
< 4) {
243 size
= 4 - rpc
->inpos
;
245 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
247 if (errno
== EINTR
) {
250 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
257 if (available
== 0) {
261 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
262 if (rpc
->insize
< pdu_size
) {
265 buf
= malloc(pdu_size
);
267 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
270 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
273 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
277 if (size
> rpc
->insize
- rpc
->inpos
) {
278 size
= rpc
->insize
- rpc
->inpos
;
281 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
283 if (errno
== EINTR
) {
286 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
292 if (rpc
->inpos
== rpc
->insize
) {
293 char *buf
= rpc
->inbuf
;
299 if (rpc_process_pdu(rpc
, buf
, pdu_size
) != 0) {
300 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
311 int rpc_service(struct rpc_context
*rpc
, int revents
)
313 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
315 if (revents
& POLLERR
) {
321 socklen_t err_size
= sizeof(err
);
323 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
324 (char *)&err
, &err_size
) != 0 || err
!= 0) {
328 rpc_set_error(rpc
, "rpc_service: socket error "
332 rpc_set_error(rpc
, "rpc_service: POLLERR, "
333 "Unknown socket error.");
335 if (rpc
->connect_cb
!= NULL
) {
336 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
340 if (revents
& POLLHUP
) {
341 rpc_set_error(rpc
, "Socket failed with POLLHUP");
342 if (rpc
->connect_cb
!= NULL
) {
343 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
348 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
350 socklen_t err_size
= sizeof(err
);
352 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
353 (char *)&err
, &err_size
) != 0 || err
!= 0) {
357 rpc_set_error(rpc
, "rpc_service: socket error "
358 "%s(%d) while connecting.",
360 if (rpc
->connect_cb
!= NULL
) {
361 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
362 NULL
, rpc
->connect_data
);
367 rpc
->is_connected
= 1;
368 if (rpc
->connect_cb
!= NULL
) {
369 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
374 if (revents
& POLLIN
) {
375 if (rpc_read_from_socket(rpc
) != 0) {
376 rpc_reconnect_requeue(rpc
);
381 if (revents
& POLLOUT
&& rpc_has_queue(&rpc
->outqueue
)) {
382 if (rpc_write_to_socket(rpc
) != 0) {
383 rpc_set_error(rpc
, "write to socket failed");
391 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
393 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
395 rpc
->auto_reconnect
= 1;
398 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
400 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
402 rpc
->auto_reconnect
= 0;
405 void rpc_set_tcp_syncnt(struct rpc_context
*rpc
, int v
)
407 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
416 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
420 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
422 switch (s
->ss_family
) {
424 socksize
= sizeof(struct sockaddr_in
);
425 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
426 #ifdef HAVE_NETINET_TCP_H
427 if (rpc
->tcp_syncnt
!= RPC_PARAM_UNDEFINED
) {
428 set_tcp_sockopt(rpc
->fd
, TCP_SYNCNT
, rpc
->tcp_syncnt
);
433 socksize
= sizeof(struct sockaddr_in6
);
434 rpc
->fd
= socket(AF_INET6
, 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 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
447 rpc_set_error(rpc
, "Failed to open socket");
451 /* Some systems allow you to set capabilities on an executable
452 * to allow the file to be executed with privilege to bind to
453 * privileged system ports, even if the user is not root.
455 * Opportunistically try to bind the socket to a low numbered
456 * system port in the hope that the user is either root or the
457 * executable has the CAP_NET_BIND_SERVICE.
459 * As soon as we fail the bind() with EACCES we know we will never
460 * be able to bind to a system port so we terminate the loop.
463 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
464 * to make the executable able to bind to a system port.
466 * On Windows, there is no concept of privileged ports. Thus
467 * binding will usually succeed.
470 struct sockaddr_storage ss
;
471 static int portOfs
= 0;
472 const int firstPort
= 512; /* >= 512 according to Sun docs */
473 const int portCount
= IPPORT_RESERVED
- firstPort
;
474 int startOfs
, port
, rc
;
477 portOfs
= time(NULL
) % 400;
482 port
= htons(firstPort
+ portOfs
);
483 portOfs
= (portOfs
+ 1) % portCount
;
485 /* skip well-known ports */
486 if (!getservbyport(port
, "tcp")) {
487 memset(&ss
, 0, sizeof(ss
));
489 switch (s
->ss_family
) {
491 ((struct sockaddr_in
*)&ss
)->sin_port
= port
;
492 ((struct sockaddr_in
*)&ss
)->sin_family
= AF_INET
;
493 #ifdef HAVE_SOCKADDR_LEN
494 ((struct sockaddr_in
*)&ss
)->sin_len
= sizeof(struct sockaddr_in
);
498 ((struct sockaddr_in6
*)&ss
)->sin6_port
= port
;
499 ((struct sockaddr_in6
*)&ss
)->sin6_family
= AF_INET6
;
500 #ifdef HAVE_SOCKADDR_LEN
501 ((struct sockaddr_in6
*)&ss
)->sin6_len
= sizeof(struct sockaddr_in
);
506 rc
= bind(rpc
->fd
, (struct sockaddr
*)&ss
, socksize
);
508 /* we got EACCES, so don't try again */
509 if (rc
!= 0 && errno
== EACCES
)
513 } while (rc
!= 0 && portOfs
!= startOfs
);
516 set_nonblocking(rpc
->fd
);
518 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
519 rpc_set_error(rpc
, "connect() to server failed. %s(%d)", strerror(errno
), errno
);
526 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
528 struct addrinfo
*ai
= NULL
;
530 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
533 rpc_set_error(rpc
, "Trying to connect while already connected");
537 if (rpc
->is_udp
!= 0) {
538 rpc_set_error(rpc
, "Trying to connect on UDP socket");
542 rpc
->auto_reconnect
= 0;
544 if (getaddrinfo(server
, NULL
, NULL
, &ai
) != 0) {
545 rpc_set_error(rpc
, "Invalid address:%s. "
546 "Can not resolv into IPv4/v6 structure.", server
);
550 switch (ai
->ai_family
) {
552 ((struct sockaddr_in
*)&rpc
->s
)->sin_family
= ai
->ai_family
;
553 ((struct sockaddr_in
*)&rpc
->s
)->sin_port
= htons(port
);
554 #ifdef HAVE_SOCKADDR_LEN
555 ((struct sockaddr_in
*)&rpc
->s
)->sin_len
= sizeof(struct sockaddr_in
);
559 ((struct sockaddr_in6
*)&rpc
->s
)->sin6_family
= ai
->ai_family
;
560 ((struct sockaddr_in6
*)&rpc
->s
)->sin6_port
= htons(port
);
561 #ifdef HAVE_SOCKADDR_LEN
562 ((struct sockaddr_in6
*)&rpc
->s
)->sin6_len
= sizeof(struct sockaddr_in6
);
567 rpc
->connect_cb
= cb
;
568 rpc
->connect_data
= private_data
;
572 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
579 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
581 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
583 rpc_unset_autoreconnect(rpc
);
590 rpc
->is_connected
= 0;
592 rpc_error_all_pdus(rpc
, error
);
597 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
599 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
601 if (status
!= RPC_STATUS_SUCCESS
) {
602 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
606 rpc
->is_connected
= 1;
607 rpc
->connect_cb
= NULL
;
610 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
611 static int rpc_reconnect_requeue(struct rpc_context
*rpc
)
616 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
623 rpc
->is_connected
= 0;
625 /* socket is closed so we will not get any replies to any commands
626 * in flight. Move them all over from the waitpdu queue back to the out queue
628 for (i
= 0; i
< HASHES
; i
++) {
629 struct rpc_queue
*q
= &rpc
->waitpdu
[i
];
631 for (pdu
=q
->head
; pdu
; pdu
=pdu
->next
) {
632 rpc_return_to_queue(&rpc
->outqueue
, pdu
);
633 /* we have to re-send the whole pdu again */
639 if (rpc
->auto_reconnect
!= 0) {
640 rpc
->connect_cb
= reconnect_cb
;
642 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
643 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
652 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
654 struct addrinfo
*ai
= NULL
;
657 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
659 if (rpc
->is_udp
== 0) {
660 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
664 sprintf(service
, "%d", port
);
665 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
666 rpc_set_error(rpc
, "Invalid address:%s. "
667 "Can not resolv into IPv4/v6 structure.", addr
);
671 switch(ai
->ai_family
) {
673 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
675 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
680 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
681 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
687 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
697 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
699 struct addrinfo
*ai
= NULL
;
702 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
704 if (rpc
->is_udp
== 0) {
705 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
709 sprintf(service
, "%d", port
);
710 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
711 rpc_set_error(rpc
, "Invalid address:%s. "
712 "Can not resolv into IPv4/v6 structure.", addr
);
718 rpc
->udp_dest
= NULL
;
720 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
721 if (rpc
->udp_dest
== NULL
) {
722 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
726 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
729 rpc
->is_broadcast
= is_broadcast
;
730 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
735 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
737 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
739 return (struct sockaddr
*)&rpc
->udp_src
;
742 int rpc_queue_length(struct rpc_context
*rpc
)
748 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
750 for(pdu
= rpc
->outqueue
.head
; pdu
; pdu
= pdu
->next
) {
754 for (n
= 0; n
< HASHES
; n
++) {
755 struct rpc_queue
*q
= &rpc
->waitpdu
[n
];
757 for(pdu
= q
->head
; pdu
; pdu
= pdu
->next
)
763 void rpc_set_fd(struct rpc_context
*rpc
, int fd
)
765 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);