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 int rpc_which_events(struct rpc_context
*rpc
)
137 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
139 events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
141 if (rpc
->is_udp
!= 0) {
142 /* for udp sockets we only wait for pollin */
152 static int rpc_write_to_socket(struct rpc_context
*rpc
)
156 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
159 rpc_set_error(rpc
, "trying to write but not connected");
163 while (rpc
->outqueue
!= NULL
) {
166 total
= rpc
->outqueue
->outdata
.size
;
168 count
= send(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
, 0);
170 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
173 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
177 rpc
->outqueue
->written
+= count
;
178 if (rpc
->outqueue
->written
== total
) {
179 struct rpc_pdu
*pdu
= rpc
->outqueue
;
181 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
182 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
188 static int rpc_read_from_socket(struct rpc_context
*rpc
)
195 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
197 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
198 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
202 if (available
== 0) {
203 rpc_set_error(rpc
, "Socket has been closed");
209 socklen_t socklen
= sizeof(rpc
->udp_src
);
211 buf
= malloc(available
);
213 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
216 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
218 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
222 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
223 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
231 /* read record marker, 4 bytes at the beginning of every pdu */
232 if (rpc
->inbuf
== NULL
) {
234 rpc
->inbuf
= malloc(rpc
->insize
);
235 if (rpc
->inbuf
== NULL
) {
236 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
240 if (rpc
->inpos
< 4) {
241 size
= 4 - rpc
->inpos
;
243 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
245 if (errno
== EINTR
) {
248 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
255 if (available
== 0) {
259 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
260 if (rpc
->insize
< pdu_size
) {
263 buf
= malloc(pdu_size
);
265 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
268 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
271 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
275 if (size
> rpc
->insize
- rpc
->inpos
) {
276 size
= rpc
->insize
- rpc
->inpos
;
279 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
281 if (errno
== EINTR
) {
284 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
290 if (rpc
->inpos
== rpc
->insize
) {
291 char *buf
= rpc
->inbuf
;
297 if (rpc_process_pdu(rpc
, buf
, pdu_size
) != 0) {
298 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
309 int rpc_service(struct rpc_context
*rpc
, int revents
)
311 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
313 if (revents
& POLLERR
) {
319 socklen_t err_size
= sizeof(err
);
321 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
322 (char *)&err
, &err_size
) != 0 || err
!= 0) {
326 rpc_set_error(rpc
, "rpc_service: socket error "
330 rpc_set_error(rpc
, "rpc_service: POLLERR, "
331 "Unknown socket error.");
333 if (rpc
->connect_cb
!= NULL
) {
334 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
338 if (revents
& POLLHUP
) {
339 rpc_set_error(rpc
, "Socket failed with POLLHUP");
340 if (rpc
->connect_cb
!= NULL
) {
341 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
346 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
348 socklen_t err_size
= sizeof(err
);
350 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
351 (char *)&err
, &err_size
) != 0 || err
!= 0) {
355 rpc_set_error(rpc
, "rpc_service: socket error "
356 "%s(%d) while connecting.",
358 if (rpc
->connect_cb
!= NULL
) {
359 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
360 NULL
, rpc
->connect_data
);
365 rpc
->is_connected
= 1;
366 if (rpc
->connect_cb
!= NULL
) {
367 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
372 if (revents
& POLLIN
) {
373 if (rpc_read_from_socket(rpc
) != 0) {
374 rpc_reconnect_requeue(rpc
);
379 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
380 if (rpc_write_to_socket(rpc
) != 0) {
381 rpc_set_error(rpc
, "write to socket failed");
389 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
391 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
393 rpc
->auto_reconnect
= 1;
396 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
398 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
400 rpc
->auto_reconnect
= 0;
403 void rpc_set_tcp_syncnt(struct rpc_context
*rpc
, int v
)
405 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
414 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
418 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
420 switch (s
->ss_family
) {
422 socksize
= sizeof(struct sockaddr_in
);
423 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
424 #ifdef HAVE_NETINET_TCP_H
425 if (rpc
->tcp_syncnt
!= RPC_PARAM_UNDEFINED
) {
426 set_tcp_sockopt(rpc
->fd
, TCP_SYNCNT
, rpc
->tcp_syncnt
);
431 socksize
= sizeof(struct sockaddr_in6
);
432 rpc
->fd
= socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
433 #ifdef HAVE_NETINET_TCP_H
434 if (rpc
->tcp_syncnt
!= RPC_PARAM_UNDEFINED
) {
435 set_tcp_sockopt(rpc
->fd
, TCP_SYNCNT
, rpc
->tcp_syncnt
);
440 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
445 rpc_set_error(rpc
, "Failed to open socket");
449 /* Some systems allow you to set capabilities on an executable
450 * to allow the file to be executed with privilege to bind to
451 * privileged system ports, even if the user is not root.
453 * Opportunistically try to bind the socket to a low numbered
454 * system port in the hope that the user is either root or the
455 * executable has the CAP_NET_BIND_SERVICE.
457 * As soon as we fail the bind() with EACCES we know we will never
458 * be able to bind to a system port so we terminate the loop.
461 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
462 * to make the executable able to bind to a system port.
464 * On Windows, there is no concept of privileged ports. Thus
465 * binding will usually succeed.
468 struct sockaddr_storage ss
;
469 static int portOfs
= 0;
470 const int firstPort
= 512; /* >= 512 according to Sun docs */
471 const int portCount
= IPPORT_RESERVED
- firstPort
;
472 int startOfs
, port
, rc
;
475 portOfs
= time(NULL
) % 400;
480 port
= htons(firstPort
+ portOfs
);
481 portOfs
= (portOfs
+ 1) % portCount
;
483 /* skip well-known ports */
484 if (!getservbyport(port
, "tcp")) {
485 memset(&ss
, 0, sizeof(ss
));
487 switch (s
->ss_family
) {
489 ((struct sockaddr_in
*)&ss
)->sin_port
= port
;
490 ((struct sockaddr_in
*)&ss
)->sin_family
= AF_INET
;
491 #ifdef HAVE_SOCKADDR_LEN
492 ((struct sockaddr_in
*)&ss
)->sin_len
= sizeof(struct sockaddr_in
);
496 ((struct sockaddr_in6
*)&ss
)->sin6_port
= port
;
497 ((struct sockaddr_in6
*)&ss
)->sin6_family
= AF_INET6
;
498 #ifdef HAVE_SOCKADDR_LEN
499 ((struct sockaddr_in6
*)&ss
)->sin6_len
= sizeof(struct sockaddr_in
);
504 rc
= bind(rpc
->fd
, (struct sockaddr
*)&ss
, socksize
);
506 /* we got EACCES, so don't try again */
507 if (rc
!= 0 && errno
== EACCES
)
511 } while (rc
!= 0 && portOfs
!= startOfs
);
514 set_nonblocking(rpc
->fd
);
515 set_nolinger(rpc
->fd
);
517 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
518 rpc_set_error(rpc
, "connect() to server failed. %s(%d)", strerror(errno
), errno
);
525 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
527 struct addrinfo
*ai
= NULL
;
529 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
532 rpc_set_error(rpc
, "Trying to connect while already connected");
536 if (rpc
->is_udp
!= 0) {
537 rpc_set_error(rpc
, "Trying to connect on UDP socket");
541 rpc
->auto_reconnect
= 0;
543 if (getaddrinfo(server
, NULL
, NULL
, &ai
) != 0) {
544 rpc_set_error(rpc
, "Invalid address:%s. "
545 "Can not resolv into IPv4/v6 structure.", server
);
549 switch (ai
->ai_family
) {
551 ((struct sockaddr_in
*)&rpc
->s
)->sin_family
= ai
->ai_family
;
552 ((struct sockaddr_in
*)&rpc
->s
)->sin_port
= htons(port
);
553 #ifdef HAVE_SOCKADDR_LEN
554 ((struct sockaddr_in
*)&rpc
->s
)->sin_len
= sizeof(struct sockaddr_in
);
558 ((struct sockaddr_in6
*)&rpc
->s
)->sin6_family
= ai
->ai_family
;
559 ((struct sockaddr_in6
*)&rpc
->s
)->sin6_port
= htons(port
);
560 #ifdef HAVE_SOCKADDR_LEN
561 ((struct sockaddr_in6
*)&rpc
->s
)->sin6_len
= sizeof(struct sockaddr_in6
);
566 rpc
->connect_cb
= cb
;
567 rpc
->connect_data
= private_data
;
571 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
578 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
580 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
582 rpc_unset_autoreconnect(rpc
);
589 rpc
->is_connected
= 0;
591 rpc_error_all_pdus(rpc
, error
);
596 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
598 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
600 if (status
!= RPC_STATUS_SUCCESS
) {
601 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
605 rpc
->is_connected
= 1;
606 rpc
->connect_cb
= NULL
;
609 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
610 static int rpc_reconnect_requeue(struct rpc_context
*rpc
)
614 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
621 rpc
->is_connected
= 0;
623 /* socket is closed so we will not get any replies to any commands
624 * in flight. Move them all over from the waitpdu queue back to the out queue
626 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
627 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
628 SLIST_ADD(&rpc
->outqueue
, pdu
);
629 /* we have to re-send the whole pdu again */
633 if (rpc
->auto_reconnect
!= 0) {
634 rpc
->connect_cb
= reconnect_cb
;
636 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
637 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
646 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
648 struct addrinfo
*ai
= NULL
;
651 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
653 if (rpc
->is_udp
== 0) {
654 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
658 sprintf(service
, "%d", port
);
659 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
660 rpc_set_error(rpc
, "Invalid address:%s. "
661 "Can not resolv into IPv4/v6 structure.", addr
);
665 switch(ai
->ai_family
) {
667 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
669 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
674 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
675 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
681 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
691 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
693 struct addrinfo
*ai
= NULL
;
696 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
698 if (rpc
->is_udp
== 0) {
699 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
703 sprintf(service
, "%d", port
);
704 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
705 rpc_set_error(rpc
, "Invalid address:%s. "
706 "Can not resolv into IPv4/v6 structure.", addr
);
712 rpc
->udp_dest
= NULL
;
714 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
715 if (rpc
->udp_dest
== NULL
) {
716 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
720 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
723 rpc
->is_broadcast
= is_broadcast
;
724 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
729 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
731 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
733 return (struct sockaddr
*)&rpc
->udp_src
;
736 int rpc_queue_length(struct rpc_context
*rpc
)
741 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
743 for(pdu
= rpc
->outqueue
; pdu
; pdu
= pdu
->next
) {
746 for(pdu
= rpc
->waitpdu
; pdu
; pdu
= pdu
->next
) {
752 void rpc_set_fd(struct rpc_context
*rpc
, int fd
)
754 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);