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>
29 #include <sys/socket.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
51 #ifdef HAVE_SYS_FILIO_H
52 #include <sys/filio.h>
54 #ifdef HAVE_SYS_SOCKIO_H
55 #include <sys/sockio.h>
57 #include <sys/types.h>
58 #include "libnfs-zdr.h"
60 #include "libnfs-raw.h"
61 #include "libnfs-private.h"
65 //has to be included after stdlib!!
66 #include "win32_errnowrapper.h"
70 static int rpc_reconnect_requeue(struct rpc_context
*rpc
);
71 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
);
73 static void set_nonblocking(int fd
)
78 v
= ioctl(fd
, FIONBIO
, &nonblocking
);
80 v
= fcntl(fd
, F_GETFL
, 0);
81 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
85 int rpc_get_fd(struct rpc_context
*rpc
)
87 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
92 int rpc_which_events(struct rpc_context
*rpc
)
96 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
98 events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
100 if (rpc
->is_udp
!= 0) {
101 /* for udp sockets we only wait for pollin */
111 static int rpc_write_to_socket(struct rpc_context
*rpc
)
115 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
118 rpc_set_error(rpc
, "trying to write but not connected");
122 while (rpc
->outqueue
!= NULL
) {
125 total
= rpc
->outqueue
->outdata
.size
;
127 count
= send(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
, 0);
129 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
132 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
136 rpc
->outqueue
->written
+= count
;
137 if (rpc
->outqueue
->written
== total
) {
138 struct rpc_pdu
*pdu
= rpc
->outqueue
;
140 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
141 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
147 static int rpc_read_from_socket(struct rpc_context
*rpc
)
154 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
156 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
158 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
159 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
163 if (available
== 0) {
164 rpc_set_error(rpc
, "Socket has been closed");
170 socklen_t socklen
= sizeof(rpc
->udp_src
);
172 buf
= malloc(available
);
174 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
177 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
179 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
182 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
183 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
191 /* read record marker, 4 bytes at the beginning of every pdu */
192 if (rpc
->inbuf
== NULL
) {
194 rpc
->inbuf
= malloc(rpc
->insize
);
195 if (rpc
->inbuf
== NULL
) {
196 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
200 if (rpc
->inpos
< 4) {
201 size
= 4 - rpc
->inpos
;
203 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
205 if (errno
== EINTR
) {
208 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
215 if (available
== 0) {
219 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
220 if (rpc
->insize
< pdu_size
) {
223 buf
= malloc(pdu_size
);
225 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
228 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
231 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
235 if (size
> rpc
->insize
- rpc
->inpos
) {
236 size
= rpc
->insize
- rpc
->inpos
;
239 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
241 if (errno
== EINTR
) {
244 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
250 if (rpc
->inpos
== rpc
->insize
) {
251 if (rpc_process_pdu(rpc
, rpc
->inbuf
, pdu_size
) != 0) {
252 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
266 int rpc_service(struct rpc_context
*rpc
, int revents
)
268 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
270 if (revents
& POLLERR
) {
276 socklen_t err_size
= sizeof(err
);
278 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
279 (char *)&err
, &err_size
) != 0 || err
!= 0) {
283 rpc_set_error(rpc
, "rpc_service: socket error "
287 rpc_set_error(rpc
, "rpc_service: POLLERR, "
288 "Unknown socket error.");
290 if (rpc
->connect_cb
!= NULL
) {
291 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
295 if (revents
& POLLHUP
) {
296 rpc_set_error(rpc
, "Socket failed with POLLHUP");
297 if (rpc
->connect_cb
!= NULL
) {
298 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
303 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
305 socklen_t err_size
= sizeof(err
);
307 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
308 (char *)&err
, &err_size
) != 0 || err
!= 0) {
312 rpc_set_error(rpc
, "rpc_service: socket error "
313 "%s(%d) while connecting.",
315 if (rpc
->connect_cb
!= NULL
) {
316 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
317 NULL
, rpc
->connect_data
);
322 rpc
->is_connected
= 1;
323 if (rpc
->connect_cb
!= NULL
) {
324 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
329 if (revents
& POLLIN
) {
330 if (rpc_read_from_socket(rpc
) != 0) {
331 rpc_reconnect_requeue(rpc
);
336 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
337 if (rpc_write_to_socket(rpc
) != 0) {
338 rpc_set_error(rpc
, "write to socket failed");
346 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
348 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
350 rpc
->auto_reconnect
= 1;
353 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
355 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
357 rpc
->auto_reconnect
= 0;
360 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
364 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
366 switch (s
->ss_family
) {
368 socksize
= sizeof(struct sockaddr_in
);
369 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
372 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
377 rpc_set_error(rpc
, "Failed to open socket");
381 /* Some systems allow you to set capabilities on an executable
382 * to allow the file to be executed with privilege to bind to
383 * privileged system ports, even if the user is not root.
385 * Opportunistically try to bind the socket to a low numbered
386 * system port in the hope that the user is either root or the
387 * executable has the CAP_NET_BIND_SERVICE.
389 * As soon as we fail the bind() with EACCES we know we will never
390 * be able to bind to a system port so we terminate the loop.
393 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
394 * to make the executable able to bind to a system port.
396 * On Windows, there is no concept of privileged ports. Thus
397 * binding will usually succeed.
400 struct sockaddr_in sin
;
401 static int portOfs
= 0;
402 const int firstPort
= 512; /* >= 512 according to Sun docs */
403 const int portCount
= IPPORT_RESERVED
- firstPort
;
404 int startOfs
= portOfs
, port
, rc
;
408 port
= htons(firstPort
+ portOfs
);
409 portOfs
= (portOfs
+ 1) % portCount
;
411 /* skip well-known ports */
412 if (!getservbyport(port
, "tcp")) {
413 memset(&sin
, 0, sizeof(sin
));
415 sin
.sin_family
= AF_INET
;
416 sin
.sin_addr
.s_addr
= 0;
418 rc
= bind(rpc
->fd
, (struct sockaddr
*)&sin
, sizeof(struct sockaddr_in
));
420 /* we got EACCES, so don't try again */
421 if (rc
!= 0 && errno
== EACCES
)
425 } while (rc
!= 0 && portOfs
!= startOfs
);
428 set_nonblocking(rpc
->fd
);
430 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
431 rpc_set_error(rpc
, "connect() to server failed. %s(%d)", strerror(errno
), errno
);
438 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
440 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&rpc
->s
;
442 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
445 rpc_set_error(rpc
, "Trying to connect while already connected");
449 if (rpc
->is_udp
!= 0) {
450 rpc_set_error(rpc
, "Trying to connect on UDP socket");
454 rpc
->auto_reconnect
= 0;
456 sin
->sin_family
= AF_INET
;
457 sin
->sin_port
= htons(port
);
458 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
459 rpc_set_error(rpc
, "Not a valid server ip address");
464 switch (rpc
->s
.ss_family
) {
466 #ifdef HAVE_SOCKADDR_LEN
467 sin
->sin_len
= sizeof(struct sockaddr_in
);
472 rpc
->connect_cb
= cb
;
473 rpc
->connect_data
= private_data
;
475 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
482 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
484 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
486 rpc_unset_autoreconnect(rpc
);
493 rpc
->is_connected
= 0;
495 rpc_error_all_pdus(rpc
, error
);
500 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
502 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
504 if (status
!= RPC_STATUS_SUCCESS
) {
505 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
509 rpc
->is_connected
= 1;
510 rpc
->connect_cb
= NULL
;
513 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
514 static int rpc_reconnect_requeue(struct rpc_context
*rpc
)
518 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
525 rpc
->is_connected
= 0;
527 /* socket is closed so we will not get any replies to any commands
528 * in flight. Move them all over from the waitpdu queue back to the out queue
530 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
531 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
532 SLIST_ADD(&rpc
->outqueue
, pdu
);
533 /* we have to re-send the whole pdu again */
537 if (rpc
->auto_reconnect
!= 0) {
538 rpc
->connect_cb
= reconnect_cb
;
540 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
541 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
550 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
552 struct addrinfo
*ai
= NULL
;
555 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
557 if (rpc
->is_udp
== 0) {
558 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
562 sprintf(service
, "%d", port
);
563 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
564 rpc_set_error(rpc
, "Invalid address:%s. "
565 "Can not resolv into IPv4/v6 structure.");
569 switch(ai
->ai_family
) {
571 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
573 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
578 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
579 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
585 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
595 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
597 struct addrinfo
*ai
= NULL
;
600 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
602 if (rpc
->is_udp
== 0) {
603 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
607 sprintf(service
, "%d", port
);
608 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
609 rpc_set_error(rpc
, "Invalid address:%s. "
610 "Can not resolv into IPv4/v6 structure.");
616 rpc
->udp_dest
= NULL
;
618 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
619 if (rpc
->udp_dest
== NULL
) {
620 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
624 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
627 rpc
->is_broadcast
= is_broadcast
;
628 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
633 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
635 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
637 return (struct sockaddr
*)&rpc
->udp_src
;
640 int rpc_queue_length(struct rpc_context
*rpc
)
645 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
647 for(pdu
= rpc
->outqueue
; pdu
; pdu
= pdu
->next
) {
650 for(pdu
= rpc
->waitpdu
; pdu
; pdu
= pdu
->next
) {