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 int rpc_which_events(struct rpc_context
*rpc
)
129 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
131 events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
133 if (rpc
->is_udp
!= 0) {
134 /* for udp sockets we only wait for pollin */
144 static int rpc_write_to_socket(struct rpc_context
*rpc
)
148 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
151 rpc_set_error(rpc
, "trying to write but not connected");
155 while (rpc
->outqueue
!= NULL
) {
158 total
= rpc
->outqueue
->outdata
.size
;
160 count
= send(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
, 0);
162 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
165 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
169 rpc
->outqueue
->written
+= count
;
170 if (rpc
->outqueue
->written
== total
) {
171 struct rpc_pdu
*pdu
= rpc
->outqueue
;
173 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
174 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
180 static int rpc_read_from_socket(struct rpc_context
*rpc
)
187 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
189 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
190 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
194 if (available
== 0) {
195 rpc_set_error(rpc
, "Socket has been closed");
201 socklen_t socklen
= sizeof(rpc
->udp_src
);
203 buf
= malloc(available
);
205 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
208 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
210 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
214 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
215 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
223 /* read record marker, 4 bytes at the beginning of every pdu */
224 if (rpc
->inbuf
== NULL
) {
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
);
232 if (rpc
->inpos
< 4) {
233 size
= 4 - rpc
->inpos
;
235 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
237 if (errno
== EINTR
) {
240 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
247 if (available
== 0) {
251 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
252 if (rpc
->insize
< pdu_size
) {
255 buf
= malloc(pdu_size
);
257 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
260 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
263 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
267 if (size
> rpc
->insize
- rpc
->inpos
) {
268 size
= rpc
->insize
- rpc
->inpos
;
271 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
273 if (errno
== EINTR
) {
276 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
282 if (rpc
->inpos
== rpc
->insize
) {
283 char *buf
= rpc
->inbuf
;
289 if (rpc_process_pdu(rpc
, buf
, pdu_size
) != 0) {
290 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
301 int rpc_service(struct rpc_context
*rpc
, int revents
)
303 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
305 if (revents
& POLLERR
) {
311 socklen_t err_size
= sizeof(err
);
313 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
314 (char *)&err
, &err_size
) != 0 || err
!= 0) {
318 rpc_set_error(rpc
, "rpc_service: socket error "
322 rpc_set_error(rpc
, "rpc_service: POLLERR, "
323 "Unknown socket error.");
325 if (rpc
->connect_cb
!= NULL
) {
326 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
330 if (revents
& POLLHUP
) {
331 rpc_set_error(rpc
, "Socket failed with POLLHUP");
332 if (rpc
->connect_cb
!= NULL
) {
333 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
338 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
340 socklen_t err_size
= sizeof(err
);
342 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
343 (char *)&err
, &err_size
) != 0 || err
!= 0) {
347 rpc_set_error(rpc
, "rpc_service: socket error "
348 "%s(%d) while connecting.",
350 if (rpc
->connect_cb
!= NULL
) {
351 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
352 NULL
, rpc
->connect_data
);
357 rpc
->is_connected
= 1;
358 if (rpc
->connect_cb
!= NULL
) {
359 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
364 if (revents
& POLLIN
) {
365 if (rpc_read_from_socket(rpc
) != 0) {
366 rpc_reconnect_requeue(rpc
);
371 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
372 if (rpc_write_to_socket(rpc
) != 0) {
373 rpc_set_error(rpc
, "write to socket failed");
381 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
383 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
385 rpc
->auto_reconnect
= 1;
388 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
390 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
392 rpc
->auto_reconnect
= 0;
395 void rpc_set_tcp_syncnt(struct rpc_context
*rpc
, int v
)
397 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
406 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
410 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
412 switch (s
->ss_family
) {
414 socksize
= sizeof(struct sockaddr_in
);
415 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
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
);
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
);
432 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
437 rpc_set_error(rpc
, "Failed to open socket");
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.
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.
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.
453 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
454 * to make the executable able to bind to a system port.
456 * On Windows, there is no concept of privileged ports. Thus
457 * binding will usually succeed.
460 struct sockaddr_storage ss
;
461 static int portOfs
= 0;
462 const int firstPort
= 512; /* >= 512 according to Sun docs */
463 const int portCount
= IPPORT_RESERVED
- firstPort
;
464 int startOfs
, port
, rc
;
467 portOfs
= time(NULL
) % 400;
472 port
= htons(firstPort
+ portOfs
);
473 portOfs
= (portOfs
+ 1) % portCount
;
475 /* skip well-known ports */
476 if (!getservbyport(port
, "tcp")) {
477 memset(&ss
, 0, sizeof(ss
));
479 switch (s
->ss_family
) {
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
);
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
);
496 rc
= bind(rpc
->fd
, (struct sockaddr
*)&ss
, socksize
);
498 /* we got EACCES, so don't try again */
499 if (rc
!= 0 && errno
== EACCES
)
503 } while (rc
!= 0 && portOfs
!= startOfs
);
506 set_nonblocking(rpc
->fd
);
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
);
516 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
518 struct addrinfo
*ai
= NULL
;
520 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
523 rpc_set_error(rpc
, "Trying to connect while already connected");
527 if (rpc
->is_udp
!= 0) {
528 rpc_set_error(rpc
, "Trying to connect on UDP socket");
532 rpc
->auto_reconnect
= 0;
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
);
540 switch (ai
->ai_family
) {
542 ((struct sockaddr_in
*)&rpc
->s
)->sin_family
= ai
->ai_family
;
543 ((struct sockaddr_in
*)&rpc
->s
)->sin_port
= htons(port
);
544 #ifdef HAVE_SOCKADDR_LEN
545 ((struct sockaddr_in
*)&rpc
->s
)->sin_len
= sizeof(struct sockaddr_in
);
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
);
557 rpc
->connect_cb
= cb
;
558 rpc
->connect_data
= private_data
;
562 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
569 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
571 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
573 rpc_unset_autoreconnect(rpc
);
580 rpc
->is_connected
= 0;
582 rpc_error_all_pdus(rpc
, error
);
587 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
589 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
591 if (status
!= RPC_STATUS_SUCCESS
) {
592 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
596 rpc
->is_connected
= 1;
597 rpc
->connect_cb
= NULL
;
600 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
601 static int rpc_reconnect_requeue(struct rpc_context
*rpc
)
605 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
612 rpc
->is_connected
= 0;
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
617 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
618 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
619 SLIST_ADD(&rpc
->outqueue
, pdu
);
620 /* we have to re-send the whole pdu again */
624 if (rpc
->auto_reconnect
!= 0) {
625 rpc
->connect_cb
= reconnect_cb
;
627 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
628 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
637 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
639 struct addrinfo
*ai
= NULL
;
642 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
644 if (rpc
->is_udp
== 0) {
645 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
649 sprintf(service
, "%d", port
);
650 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
651 rpc_set_error(rpc
, "Invalid address:%s. "
652 "Can not resolv into IPv4/v6 structure.", addr
);
656 switch(ai
->ai_family
) {
658 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
660 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
665 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
666 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
672 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
682 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
684 struct addrinfo
*ai
= NULL
;
687 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
689 if (rpc
->is_udp
== 0) {
690 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
694 sprintf(service
, "%d", port
);
695 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
696 rpc_set_error(rpc
, "Invalid address:%s. "
697 "Can not resolv into IPv4/v6 structure.", addr
);
703 rpc
->udp_dest
= NULL
;
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");
711 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
714 rpc
->is_broadcast
= is_broadcast
;
715 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
720 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
722 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
724 return (struct sockaddr
*)&rpc
->udp_src
;
727 int rpc_queue_length(struct rpc_context
*rpc
)
732 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
734 for(pdu
= rpc
->outqueue
; pdu
; pdu
= pdu
->next
) {
737 for(pdu
= rpc
->waitpdu
; pdu
; pdu
= pdu
->next
) {
743 void rpc_set_fd(struct rpc_context
*rpc
, int fd
)
745 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);