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"
28 #include <arpa/inet.h>
40 #ifdef HAVE_SYS_IOCTL_H
41 #include <sys/ioctl.h>
44 #ifdef HAVE_SYS_SOCKET_H
45 #include <sys/socket.h>
54 #ifdef HAVE_SYS_FILIO_H
55 #include <sys/filio.h>
57 #ifdef HAVE_SYS_SOCKIO_H
58 #include <sys/sockio.h>
60 #include <sys/types.h>
61 #include "libnfs-zdr.h"
63 #include "libnfs-raw.h"
64 #include "libnfs-private.h"
68 //has to be included after stdlib!!
69 #include "win32_errnowrapper.h"
73 static int rpc_reconnect_requeue(struct rpc_context
*rpc
);
74 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
);
76 static void set_nonblocking(int fd
)
81 v
= ioctl(fd
, FIONBIO
, &nonblocking
);
83 v
= fcntl(fd
, F_GETFL
, 0);
84 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
88 int rpc_get_fd(struct rpc_context
*rpc
)
90 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
95 int rpc_which_events(struct rpc_context
*rpc
)
99 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
101 events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
103 if (rpc
->is_udp
!= 0) {
104 /* for udp sockets we only wait for pollin */
114 static int rpc_write_to_socket(struct rpc_context
*rpc
)
118 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
121 rpc_set_error(rpc
, "trying to write but not connected");
125 while (rpc
->outqueue
!= NULL
) {
128 total
= rpc
->outqueue
->outdata
.size
;
130 count
= send(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
, 0);
132 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
135 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
139 rpc
->outqueue
->written
+= count
;
140 if (rpc
->outqueue
->written
== total
) {
141 struct rpc_pdu
*pdu
= rpc
->outqueue
;
143 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
144 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
150 static int rpc_read_from_socket(struct rpc_context
*rpc
)
157 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
159 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
161 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
162 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
166 if (available
== 0) {
167 rpc_set_error(rpc
, "Socket has been closed");
173 socklen_t socklen
= sizeof(rpc
->udp_src
);
175 buf
= malloc(available
);
177 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
180 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
182 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
185 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
186 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
194 /* read record marker, 4 bytes at the beginning of every pdu */
195 if (rpc
->inbuf
== NULL
) {
197 rpc
->inbuf
= malloc(rpc
->insize
);
198 if (rpc
->inbuf
== NULL
) {
199 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
203 if (rpc
->inpos
< 4) {
204 size
= 4 - rpc
->inpos
;
206 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
208 if (errno
== EINTR
) {
211 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
218 if (available
== 0) {
222 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
223 if (rpc
->insize
< pdu_size
) {
226 buf
= malloc(pdu_size
);
228 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
231 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
234 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
238 if (size
> rpc
->insize
- rpc
->inpos
) {
239 size
= rpc
->insize
- rpc
->inpos
;
242 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
244 if (errno
== EINTR
) {
247 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
253 if (rpc
->inpos
== rpc
->insize
) {
254 if (rpc_process_pdu(rpc
, rpc
->inbuf
, pdu_size
) != 0) {
255 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
269 int rpc_service(struct rpc_context
*rpc
, int revents
)
271 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
273 if (revents
& POLLERR
) {
279 socklen_t err_size
= sizeof(err
);
281 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
282 (char *)&err
, &err_size
) != 0 || err
!= 0) {
286 rpc_set_error(rpc
, "rpc_service: socket error "
290 rpc_set_error(rpc
, "rpc_service: POLLERR, "
291 "Unknown socket error.");
293 if (rpc
->connect_cb
!= NULL
) {
294 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
298 if (revents
& POLLHUP
) {
299 rpc_set_error(rpc
, "Socket failed with POLLHUP");
300 if (rpc
->connect_cb
!= NULL
) {
301 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
306 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
308 socklen_t err_size
= sizeof(err
);
310 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
311 (char *)&err
, &err_size
) != 0 || err
!= 0) {
315 rpc_set_error(rpc
, "rpc_service: socket error "
316 "%s(%d) while connecting.",
318 if (rpc
->connect_cb
!= NULL
) {
319 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
320 NULL
, rpc
->connect_data
);
325 rpc
->is_connected
= 1;
326 if (rpc
->connect_cb
!= NULL
) {
327 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
332 if (revents
& POLLIN
) {
333 if (rpc_read_from_socket(rpc
) != 0) {
334 rpc_reconnect_requeue(rpc
);
339 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
340 if (rpc_write_to_socket(rpc
) != 0) {
341 rpc_set_error(rpc
, "write to socket failed");
349 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
351 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
353 rpc
->auto_reconnect
= 1;
356 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
358 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
360 rpc
->auto_reconnect
= 0;
363 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
367 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
369 switch (s
->ss_family
) {
371 socksize
= sizeof(struct sockaddr_in
);
372 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
375 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
380 rpc_set_error(rpc
, "Failed to open socket");
384 /* Some systems allow you to set capabilities on an executable
385 * to allow the file to be executed with privilege to bind to
386 * privileged system ports, even if the user is not root.
388 * Opportunistically try to bind the socket to a low numbered
389 * system port in the hope that the user is either root or the
390 * executable has the CAP_NET_BIND_SERVICE.
392 * As soon as we fail the bind() with EACCES we know we will never
393 * be able to bind to a system port so we terminate the loop.
396 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
397 * to make the executable able to bind to a system port.
399 * On Windows, there is no concept of privileged ports. Thus
400 * binding will usually succeed.
403 struct sockaddr_in sin
;
404 static int portOfs
= 0;
405 const int firstPort
= 512; /* >= 512 according to Sun docs */
406 const int portCount
= IPPORT_RESERVED
- firstPort
;
407 int startOfs
, port
, rc
;
410 portOfs
= time(NULL
) % 400;
415 port
= htons(firstPort
+ portOfs
);
416 portOfs
= (portOfs
+ 1) % portCount
;
418 /* skip well-known ports */
419 if (!getservbyport(port
, "tcp")) {
420 memset(&sin
, 0, sizeof(sin
));
422 sin
.sin_family
= AF_INET
;
423 sin
.sin_addr
.s_addr
= 0;
425 rc
= bind(rpc
->fd
, (struct sockaddr
*)&sin
, sizeof(struct sockaddr_in
));
427 /* we got EACCES, so don't try again */
428 if (rc
!= 0 && errno
== EACCES
)
432 } while (rc
!= 0 && portOfs
!= startOfs
);
435 set_nonblocking(rpc
->fd
);
437 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
438 rpc_set_error(rpc
, "connect() to server failed. %s(%d)", strerror(errno
), errno
);
445 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
447 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&rpc
->s
;
449 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
452 rpc_set_error(rpc
, "Trying to connect while already connected");
456 if (rpc
->is_udp
!= 0) {
457 rpc_set_error(rpc
, "Trying to connect on UDP socket");
461 rpc
->auto_reconnect
= 0;
463 sin
->sin_family
= AF_INET
;
464 sin
->sin_port
= htons(port
);
465 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
466 rpc_set_error(rpc
, "Not a valid server ip address");
471 switch (rpc
->s
.ss_family
) {
473 #ifdef HAVE_SOCKADDR_LEN
474 sin
->sin_len
= sizeof(struct sockaddr_in
);
479 rpc
->connect_cb
= cb
;
480 rpc
->connect_data
= private_data
;
482 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
489 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
491 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
493 rpc_unset_autoreconnect(rpc
);
500 rpc
->is_connected
= 0;
502 rpc_error_all_pdus(rpc
, error
);
507 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
509 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
511 if (status
!= RPC_STATUS_SUCCESS
) {
512 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
516 rpc
->is_connected
= 1;
517 rpc
->connect_cb
= NULL
;
520 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
521 static int rpc_reconnect_requeue(struct rpc_context
*rpc
)
525 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
532 rpc
->is_connected
= 0;
534 /* socket is closed so we will not get any replies to any commands
535 * in flight. Move them all over from the waitpdu queue back to the out queue
537 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
538 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
539 SLIST_ADD(&rpc
->outqueue
, pdu
);
540 /* we have to re-send the whole pdu again */
544 if (rpc
->auto_reconnect
!= 0) {
545 rpc
->connect_cb
= reconnect_cb
;
547 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
548 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
557 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
559 struct addrinfo
*ai
= NULL
;
562 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
564 if (rpc
->is_udp
== 0) {
565 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
569 sprintf(service
, "%d", port
);
570 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
571 rpc_set_error(rpc
, "Invalid address:%s. "
572 "Can not resolv into IPv4/v6 structure.");
576 switch(ai
->ai_family
) {
578 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
580 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
585 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
586 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
592 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
602 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
604 struct addrinfo
*ai
= NULL
;
607 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
609 if (rpc
->is_udp
== 0) {
610 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
614 sprintf(service
, "%d", port
);
615 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
616 rpc_set_error(rpc
, "Invalid address:%s. "
617 "Can not resolv into IPv4/v6 structure.");
623 rpc
->udp_dest
= NULL
;
625 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
626 if (rpc
->udp_dest
== NULL
) {
627 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
631 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
634 rpc
->is_broadcast
= is_broadcast
;
635 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
640 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
642 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
644 return (struct sockaddr
*)&rpc
->udp_src
;
647 int rpc_queue_length(struct rpc_context
*rpc
)
652 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
654 for(pdu
= rpc
->outqueue
; pdu
; pdu
= pdu
->next
) {
657 for(pdu
= rpc
->waitpdu
; pdu
; pdu
= pdu
->next
) {