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>
53 #ifdef HAVE_SYS_FILIO_H
54 #include <sys/filio.h>
57 #ifdef HAVE_SYS_SOCKIO_H
58 #include <sys/sockio.h>
67 #include <sys/types.h>
68 #include "libnfs-zdr.h"
70 #include "libnfs-raw.h"
71 #include "libnfs-private.h"
75 //has to be included after stdlib!!
76 #include "win32_errnowrapper.h"
80 static int rpc_reconnect_requeue(struct rpc_context
*rpc
);
81 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
);
83 static void set_nonblocking(int fd
)
88 v
= ioctl(fd
, FIONBIO
, &nonblocking
);
90 v
= fcntl(fd
, F_GETFL
, 0);
91 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
95 int rpc_get_fd(struct rpc_context
*rpc
)
97 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
102 int rpc_which_events(struct rpc_context
*rpc
)
106 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
108 events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
110 if (rpc
->is_udp
!= 0) {
111 /* for udp sockets we only wait for pollin */
121 static int rpc_write_to_socket(struct rpc_context
*rpc
)
125 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
128 rpc_set_error(rpc
, "trying to write but not connected");
132 while (rpc
->outqueue
!= NULL
) {
135 total
= rpc
->outqueue
->outdata
.size
;
137 count
= send(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
, 0);
139 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
142 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
146 rpc
->outqueue
->written
+= count
;
147 if (rpc
->outqueue
->written
== total
) {
148 struct rpc_pdu
*pdu
= rpc
->outqueue
;
150 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
151 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
157 static int rpc_read_from_socket(struct rpc_context
*rpc
)
164 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
166 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
167 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
171 if (available
== 0) {
172 rpc_set_error(rpc
, "Socket has been closed");
178 socklen_t socklen
= sizeof(rpc
->udp_src
);
180 buf
= malloc(available
);
182 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
185 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
187 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
190 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
191 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
199 /* read record marker, 4 bytes at the beginning of every pdu */
200 if (rpc
->inbuf
== NULL
) {
202 rpc
->inbuf
= malloc(rpc
->insize
);
203 if (rpc
->inbuf
== NULL
) {
204 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
208 if (rpc
->inpos
< 4) {
209 size
= 4 - rpc
->inpos
;
211 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
213 if (errno
== EINTR
) {
216 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
223 if (available
== 0) {
227 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
228 if (rpc
->insize
< pdu_size
) {
231 buf
= malloc(pdu_size
);
233 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
236 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
239 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
243 if (size
> rpc
->insize
- rpc
->inpos
) {
244 size
= rpc
->insize
- rpc
->inpos
;
247 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
249 if (errno
== EINTR
) {
252 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
258 if (rpc
->inpos
== rpc
->insize
) {
259 char *buf
= rpc
->inbuf
;
265 if (rpc_process_pdu(rpc
, buf
, pdu_size
) != 0) {
266 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
277 int rpc_service(struct rpc_context
*rpc
, int revents
)
279 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
281 if (revents
& POLLERR
) {
287 socklen_t err_size
= sizeof(err
);
289 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
290 (char *)&err
, &err_size
) != 0 || err
!= 0) {
294 rpc_set_error(rpc
, "rpc_service: socket error "
298 rpc_set_error(rpc
, "rpc_service: POLLERR, "
299 "Unknown socket error.");
301 if (rpc
->connect_cb
!= NULL
) {
302 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
306 if (revents
& POLLHUP
) {
307 rpc_set_error(rpc
, "Socket failed with POLLHUP");
308 if (rpc
->connect_cb
!= NULL
) {
309 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
314 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
316 socklen_t err_size
= sizeof(err
);
318 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
319 (char *)&err
, &err_size
) != 0 || err
!= 0) {
323 rpc_set_error(rpc
, "rpc_service: socket error "
324 "%s(%d) while connecting.",
326 if (rpc
->connect_cb
!= NULL
) {
327 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
328 NULL
, rpc
->connect_data
);
333 rpc
->is_connected
= 1;
334 if (rpc
->connect_cb
!= NULL
) {
335 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
340 if (revents
& POLLIN
) {
341 if (rpc_read_from_socket(rpc
) != 0) {
342 rpc_reconnect_requeue(rpc
);
347 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
348 if (rpc_write_to_socket(rpc
) != 0) {
349 rpc_set_error(rpc
, "write to socket failed");
357 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
359 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
361 rpc
->auto_reconnect
= 1;
364 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
366 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
368 rpc
->auto_reconnect
= 0;
371 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
375 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
377 switch (s
->ss_family
) {
379 socksize
= sizeof(struct sockaddr_in
);
380 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
383 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
388 rpc_set_error(rpc
, "Failed to open socket");
392 /* Some systems allow you to set capabilities on an executable
393 * to allow the file to be executed with privilege to bind to
394 * privileged system ports, even if the user is not root.
396 * Opportunistically try to bind the socket to a low numbered
397 * system port in the hope that the user is either root or the
398 * executable has the CAP_NET_BIND_SERVICE.
400 * As soon as we fail the bind() with EACCES we know we will never
401 * be able to bind to a system port so we terminate the loop.
404 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
405 * to make the executable able to bind to a system port.
407 * On Windows, there is no concept of privileged ports. Thus
408 * binding will usually succeed.
411 struct sockaddr_in sin
;
412 static int portOfs
= 0;
413 const int firstPort
= 512; /* >= 512 according to Sun docs */
414 const int portCount
= IPPORT_RESERVED
- firstPort
;
415 int startOfs
, port
, rc
;
418 portOfs
= time(NULL
) % 400;
423 port
= htons(firstPort
+ portOfs
);
424 portOfs
= (portOfs
+ 1) % portCount
;
426 /* skip well-known ports */
427 if (!getservbyport(port
, "tcp")) {
428 memset(&sin
, 0, sizeof(sin
));
430 sin
.sin_family
= AF_INET
;
431 sin
.sin_addr
.s_addr
= 0;
433 rc
= bind(rpc
->fd
, (struct sockaddr
*)&sin
, sizeof(struct sockaddr_in
));
435 /* we got EACCES, so don't try again */
436 if (rc
!= 0 && errno
== EACCES
)
440 } while (rc
!= 0 && portOfs
!= startOfs
);
443 set_nonblocking(rpc
->fd
);
445 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
446 rpc_set_error(rpc
, "connect() to server failed. %s(%d)", strerror(errno
), errno
);
453 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
455 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&rpc
->s
;
457 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
460 rpc_set_error(rpc
, "Trying to connect while already connected");
464 if (rpc
->is_udp
!= 0) {
465 rpc_set_error(rpc
, "Trying to connect on UDP socket");
469 rpc
->auto_reconnect
= 0;
471 sin
->sin_family
= AF_INET
;
472 sin
->sin_port
= htons(port
);
473 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
474 rpc_set_error(rpc
, "Not a valid server ip address");
479 switch (rpc
->s
.ss_family
) {
481 #ifdef HAVE_SOCKADDR_LEN
482 sin
->sin_len
= sizeof(struct sockaddr_in
);
487 rpc
->connect_cb
= cb
;
488 rpc
->connect_data
= private_data
;
490 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
497 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
499 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
501 rpc_unset_autoreconnect(rpc
);
508 rpc
->is_connected
= 0;
510 rpc_error_all_pdus(rpc
, error
);
515 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
517 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
519 if (status
!= RPC_STATUS_SUCCESS
) {
520 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
524 rpc
->is_connected
= 1;
525 rpc
->connect_cb
= NULL
;
528 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
529 static int rpc_reconnect_requeue(struct rpc_context
*rpc
)
533 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
540 rpc
->is_connected
= 0;
542 /* socket is closed so we will not get any replies to any commands
543 * in flight. Move them all over from the waitpdu queue back to the out queue
545 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
546 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
547 SLIST_ADD(&rpc
->outqueue
, pdu
);
548 /* we have to re-send the whole pdu again */
552 if (rpc
->auto_reconnect
!= 0) {
553 rpc
->connect_cb
= reconnect_cb
;
555 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
556 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
565 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
567 struct addrinfo
*ai
= NULL
;
570 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
572 if (rpc
->is_udp
== 0) {
573 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
577 sprintf(service
, "%d", port
);
578 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
579 rpc_set_error(rpc
, "Invalid address:%s. "
580 "Can not resolv into IPv4/v6 structure.");
584 switch(ai
->ai_family
) {
586 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
588 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
593 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
594 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
600 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
610 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
612 struct addrinfo
*ai
= NULL
;
615 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
617 if (rpc
->is_udp
== 0) {
618 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
622 sprintf(service
, "%d", port
);
623 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
624 rpc_set_error(rpc
, "Invalid address:%s. "
625 "Can not resolv into IPv4/v6 structure.");
631 rpc
->udp_dest
= NULL
;
633 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
634 if (rpc
->udp_dest
== NULL
) {
635 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
639 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
642 rpc
->is_broadcast
= is_broadcast
;
643 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
648 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
650 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
652 return (struct sockaddr
*)&rpc
->udp_src
;
655 int rpc_queue_length(struct rpc_context
*rpc
)
660 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
662 for(pdu
= rpc
->outqueue
; pdu
; pdu
= pdu
->next
) {
665 for(pdu
= rpc
->waitpdu
; pdu
; pdu
= pdu
->next
) {