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"
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 #ifdef HAVE_NETINET_TCP_H
100 int set_tcp_sockopt(int sockfd
, int optname
, int value
)
104 #if defined(__FreeBSD__) || defined(__sun) || (defined(__APPLE__) && defined(__MACH__))
105 struct protoent
*buf
;
107 if ((buf
= getprotobyname("tcp")) != NULL
)
108 level
= buf
->p_proto
;
115 return setsockopt(sockfd
, level
, optname
, (char *)&value
, sizeof(value
));
119 int rpc_get_fd(struct rpc_context
*rpc
)
121 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
126 int rpc_which_events(struct rpc_context
*rpc
)
130 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
132 events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
134 if (rpc
->is_udp
!= 0) {
135 /* for udp sockets we only wait for pollin */
145 static int rpc_write_to_socket(struct rpc_context
*rpc
)
149 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
152 rpc_set_error(rpc
, "trying to write but not connected");
156 while (rpc
->outqueue
!= NULL
) {
159 total
= rpc
->outqueue
->outdata
.size
;
161 count
= send(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
, 0);
163 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
166 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
170 rpc
->outqueue
->written
+= count
;
171 if (rpc
->outqueue
->written
== total
) {
172 struct rpc_pdu
*pdu
= rpc
->outqueue
;
174 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
175 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
181 static int rpc_read_from_socket(struct rpc_context
*rpc
)
188 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
190 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
191 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
195 if (available
== 0) {
196 rpc_set_error(rpc
, "Socket has been closed");
202 socklen_t socklen
= sizeof(rpc
->udp_src
);
204 buf
= malloc(available
);
206 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
209 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
211 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
215 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
216 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
224 /* read record marker, 4 bytes at the beginning of every pdu */
225 if (rpc
->inbuf
== NULL
) {
227 rpc
->inbuf
= malloc(rpc
->insize
);
228 if (rpc
->inbuf
== NULL
) {
229 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
233 if (rpc
->inpos
< 4) {
234 size
= 4 - rpc
->inpos
;
236 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
238 if (errno
== EINTR
) {
241 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
248 if (available
== 0) {
252 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
253 if (rpc
->insize
< pdu_size
) {
256 buf
= malloc(pdu_size
);
258 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
261 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
264 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
268 if (size
> rpc
->insize
- rpc
->inpos
) {
269 size
= rpc
->insize
- rpc
->inpos
;
272 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
274 if (errno
== EINTR
) {
277 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
283 if (rpc
->inpos
== rpc
->insize
) {
284 char *buf
= rpc
->inbuf
;
290 if (rpc_process_pdu(rpc
, buf
, pdu_size
) != 0) {
291 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
302 int rpc_service(struct rpc_context
*rpc
, int revents
)
304 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
306 if (revents
& POLLERR
) {
312 socklen_t err_size
= sizeof(err
);
314 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
315 (char *)&err
, &err_size
) != 0 || err
!= 0) {
319 rpc_set_error(rpc
, "rpc_service: socket error "
323 rpc_set_error(rpc
, "rpc_service: POLLERR, "
324 "Unknown socket error.");
326 if (rpc
->connect_cb
!= NULL
) {
327 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
331 if (revents
& POLLHUP
) {
332 rpc_set_error(rpc
, "Socket failed with POLLHUP");
333 if (rpc
->connect_cb
!= NULL
) {
334 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
339 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
341 socklen_t err_size
= sizeof(err
);
343 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
344 (char *)&err
, &err_size
) != 0 || err
!= 0) {
348 rpc_set_error(rpc
, "rpc_service: socket error "
349 "%s(%d) while connecting.",
351 if (rpc
->connect_cb
!= NULL
) {
352 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
353 NULL
, rpc
->connect_data
);
358 rpc
->is_connected
= 1;
359 if (rpc
->connect_cb
!= NULL
) {
360 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
365 if (revents
& POLLIN
) {
366 if (rpc_read_from_socket(rpc
) != 0) {
367 rpc_reconnect_requeue(rpc
);
372 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
373 if (rpc_write_to_socket(rpc
) != 0) {
374 rpc_set_error(rpc
, "write to socket failed");
382 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
384 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
386 rpc
->auto_reconnect
= 1;
389 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
391 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
393 rpc
->auto_reconnect
= 0;
396 void rpc_set_tcp_syncnt(struct rpc_context
*rpc
, int v
)
398 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
407 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
411 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
413 switch (s
->ss_family
) {
415 socksize
= sizeof(struct sockaddr_in
);
416 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
417 #ifdef HAVE_NETINET_TCP_H
418 if (rpc
->tcp_syncnt
!= RPC_PARAM_UNDEFINED
) {
419 set_tcp_sockopt(rpc
->fd
, TCP_SYNCNT
, rpc
->tcp_syncnt
);
424 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
429 rpc_set_error(rpc
, "Failed to open socket");
433 /* Some systems allow you to set capabilities on an executable
434 * to allow the file to be executed with privilege to bind to
435 * privileged system ports, even if the user is not root.
437 * Opportunistically try to bind the socket to a low numbered
438 * system port in the hope that the user is either root or the
439 * executable has the CAP_NET_BIND_SERVICE.
441 * As soon as we fail the bind() with EACCES we know we will never
442 * be able to bind to a system port so we terminate the loop.
445 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
446 * to make the executable able to bind to a system port.
448 * On Windows, there is no concept of privileged ports. Thus
449 * binding will usually succeed.
452 struct sockaddr_in sin
;
453 static int portOfs
= 0;
454 const int firstPort
= 512; /* >= 512 according to Sun docs */
455 const int portCount
= IPPORT_RESERVED
- firstPort
;
456 int startOfs
, port
, rc
;
459 portOfs
= time(NULL
) % 400;
464 port
= htons(firstPort
+ portOfs
);
465 portOfs
= (portOfs
+ 1) % portCount
;
467 /* skip well-known ports */
468 if (!getservbyport(port
, "tcp")) {
469 memset(&sin
, 0, sizeof(sin
));
471 sin
.sin_family
= AF_INET
;
472 sin
.sin_addr
.s_addr
= 0;
474 rc
= bind(rpc
->fd
, (struct sockaddr
*)&sin
, sizeof(struct sockaddr_in
));
476 /* we got EACCES, so don't try again */
477 if (rc
!= 0 && errno
== EACCES
)
481 } while (rc
!= 0 && portOfs
!= startOfs
);
484 set_nonblocking(rpc
->fd
);
486 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
487 rpc_set_error(rpc
, "connect() to server failed. %s(%d)", strerror(errno
), errno
);
494 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
496 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&rpc
->s
;
498 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
501 rpc_set_error(rpc
, "Trying to connect while already connected");
505 if (rpc
->is_udp
!= 0) {
506 rpc_set_error(rpc
, "Trying to connect on UDP socket");
510 rpc
->auto_reconnect
= 0;
512 sin
->sin_family
= AF_INET
;
513 sin
->sin_port
= htons(port
);
514 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
515 rpc_set_error(rpc
, "Not a valid server ip address");
520 switch (rpc
->s
.ss_family
) {
522 #ifdef HAVE_SOCKADDR_LEN
523 sin
->sin_len
= sizeof(struct sockaddr_in
);
528 rpc
->connect_cb
= cb
;
529 rpc
->connect_data
= private_data
;
531 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
538 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
540 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
542 rpc_unset_autoreconnect(rpc
);
549 rpc
->is_connected
= 0;
551 rpc_error_all_pdus(rpc
, error
);
556 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
558 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
560 if (status
!= RPC_STATUS_SUCCESS
) {
561 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
565 rpc
->is_connected
= 1;
566 rpc
->connect_cb
= NULL
;
569 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
570 static int rpc_reconnect_requeue(struct rpc_context
*rpc
)
574 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
581 rpc
->is_connected
= 0;
583 /* socket is closed so we will not get any replies to any commands
584 * in flight. Move them all over from the waitpdu queue back to the out queue
586 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
587 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
588 SLIST_ADD(&rpc
->outqueue
, pdu
);
589 /* we have to re-send the whole pdu again */
593 if (rpc
->auto_reconnect
!= 0) {
594 rpc
->connect_cb
= reconnect_cb
;
596 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
597 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
606 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
608 struct addrinfo
*ai
= NULL
;
611 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
613 if (rpc
->is_udp
== 0) {
614 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
618 sprintf(service
, "%d", port
);
619 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
620 rpc_set_error(rpc
, "Invalid address:%s. "
621 "Can not resolv into IPv4/v6 structure.", addr
);
625 switch(ai
->ai_family
) {
627 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
629 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
634 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
635 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
641 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
651 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
653 struct addrinfo
*ai
= NULL
;
656 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
658 if (rpc
->is_udp
== 0) {
659 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
663 sprintf(service
, "%d", port
);
664 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
665 rpc_set_error(rpc
, "Invalid address:%s. "
666 "Can not resolv into IPv4/v6 structure.", addr
);
672 rpc
->udp_dest
= NULL
;
674 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
675 if (rpc
->udp_dest
== NULL
) {
676 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
680 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
683 rpc
->is_broadcast
= is_broadcast
;
684 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
689 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
691 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
693 return (struct sockaddr
*)&rpc
->udp_src
;
696 int rpc_queue_length(struct rpc_context
*rpc
)
701 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
703 for(pdu
= rpc
->outqueue
; pdu
; pdu
= pdu
->next
) {
706 for(pdu
= rpc
->waitpdu
; pdu
; pdu
= pdu
->next
) {
712 void rpc_set_fd(struct rpc_context
*rpc
, int fd
)
714 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);