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/>.
18 #include "win32_compat.h"
19 #define ioctl ioctlsocket
20 #define close closesocket
23 #include <arpa/inet.h>
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
34 #include "aros_compat.h"
47 #ifdef HAVE_SYS_FILIO_H
48 #include <sys/filio.h>
50 #ifdef HAVE_SYS_SOCKIO_H
51 #include <sys/sockio.h>
53 #include <sys/types.h>
54 #include "libnfs-zdr.h"
56 #include "libnfs-raw.h"
57 #include "libnfs-private.h"
61 //has to be included after stdlib!!
62 #include "win32_errnowrapper.h"
66 static int rpc_reconnect_requeue(struct rpc_context
*rpc
);
67 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
);
69 static void set_nonblocking(int fd
)
74 v
= ioctl(fd
, FIONBIO
, &nonblocking
);
76 v
= fcntl(fd
, F_GETFL
, 0);
77 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
81 int rpc_get_fd(struct rpc_context
*rpc
)
83 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
88 int rpc_which_events(struct rpc_context
*rpc
)
92 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
94 events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
96 if (rpc
->is_udp
!= 0) {
97 /* for udp sockets we only wait for pollin */
107 static int rpc_write_to_socket(struct rpc_context
*rpc
)
111 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
114 rpc_set_error(rpc
, "trying to write but not connected");
118 while (rpc
->outqueue
!= NULL
) {
121 total
= rpc
->outqueue
->outdata
.size
;
123 count
= send(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
, 0);
125 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
128 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
132 rpc
->outqueue
->written
+= count
;
133 if (rpc
->outqueue
->written
== total
) {
134 struct rpc_pdu
*pdu
= rpc
->outqueue
;
136 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
137 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
143 static int rpc_read_from_socket(struct rpc_context
*rpc
)
150 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
152 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
154 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
155 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
159 if (available
== 0) {
160 rpc_set_error(rpc
, "Socket has been closed");
166 socklen_t socklen
= sizeof(rpc
->udp_src
);
168 buf
= malloc(available
);
170 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
173 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
175 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
178 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
179 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
187 /* read record marker, 4 bytes at the beginning of every pdu */
188 if (rpc
->inbuf
== NULL
) {
190 rpc
->inbuf
= malloc(rpc
->insize
);
191 if (rpc
->inbuf
== NULL
) {
192 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
196 if (rpc
->inpos
< 4) {
197 size
= 4 - rpc
->inpos
;
199 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
201 if (errno
== EINTR
) {
204 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
211 if (available
== 0) {
215 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
216 if (rpc
->insize
< pdu_size
) {
219 buf
= malloc(pdu_size
);
221 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
224 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
227 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
231 if (size
> rpc
->insize
- rpc
->inpos
) {
232 size
= rpc
->insize
- 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
);
246 if (rpc
->inpos
== rpc
->insize
) {
247 if (rpc_process_pdu(rpc
, rpc
->inbuf
, pdu_size
) != 0) {
248 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
262 int rpc_service(struct rpc_context
*rpc
, int revents
)
264 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
266 if (revents
& POLLERR
) {
272 socklen_t err_size
= sizeof(err
);
274 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
275 (char *)&err
, &err_size
) != 0 || err
!= 0) {
279 rpc_set_error(rpc
, "rpc_service: socket error "
283 rpc_set_error(rpc
, "rpc_service: POLLERR, "
284 "Unknown socket error.");
286 if (rpc
->connect_cb
!= NULL
) {
287 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
291 if (revents
& POLLHUP
) {
292 rpc_set_error(rpc
, "Socket failed with POLLHUP");
293 if (rpc
->connect_cb
!= NULL
) {
294 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
299 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
301 socklen_t err_size
= sizeof(err
);
303 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
304 (char *)&err
, &err_size
) != 0 || err
!= 0) {
308 rpc_set_error(rpc
, "rpc_service: socket error "
309 "%s(%d) while connecting.",
311 if (rpc
->connect_cb
!= NULL
) {
312 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
313 NULL
, rpc
->connect_data
);
318 rpc
->is_connected
= 1;
319 if (rpc
->connect_cb
!= NULL
) {
320 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
325 if (revents
& POLLIN
) {
326 if (rpc_read_from_socket(rpc
) != 0) {
327 rpc_reconnect_requeue(rpc
);
332 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
333 if (rpc_write_to_socket(rpc
) != 0) {
334 rpc_set_error(rpc
, "write to socket failed");
342 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
344 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
346 rpc
->auto_reconnect
= 1;
349 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
351 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
353 rpc
->auto_reconnect
= 0;
356 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
360 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
362 switch (s
->ss_family
) {
364 socksize
= sizeof(struct sockaddr_in
);
365 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
368 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
373 rpc_set_error(rpc
, "Failed to open socket");
377 /* Some systems allow you to set capabilities on an executable
378 * to allow the file to be executed with privilege to bind to
379 * privileged system ports, even if the user is not root.
381 * Opportunistically try to bind the socket to a low numbered
382 * system port in the hope that the user is either root or the
383 * executable has the CAP_NET_BIND_SERVICE.
385 * As soon as we fail the bind() with EACCES we know we will never
386 * be able to bind to a system port so we terminate the loop.
389 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
390 * to make the executable able to bind to a system port.
392 * On Windows, there is no concept of privileged ports. Thus
393 * binding will usually succeed.
396 struct sockaddr_in sin
;
397 static int portOfs
= 0;
398 const int firstPort
= 512; /* >= 512 according to Sun docs */
399 const int portCount
= IPPORT_RESERVED
- firstPort
;
400 int startOfs
= portOfs
, port
, rc
;
404 port
= htons(firstPort
+ portOfs
);
405 portOfs
= (portOfs
+ 1) % portCount
;
407 /* skip well-known ports */
408 if (!getservbyport(port
, "tcp")) {
409 memset(&sin
, 0, sizeof(sin
));
411 sin
.sin_family
= AF_INET
;
412 sin
.sin_addr
.s_addr
= 0;
414 rc
= bind(rpc
->fd
, (struct sockaddr
*)&sin
, sizeof(struct sockaddr_in
));
416 /* we got EACCES, so don't try again */
417 if (rc
!= 0 && errno
== EACCES
)
421 } while (rc
!= 0 && portOfs
!= startOfs
);
424 set_nonblocking(rpc
->fd
);
426 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
427 rpc_set_error(rpc
, "connect() to server failed. %s(%d)", strerror(errno
), errno
);
434 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
436 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&rpc
->s
;
438 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
441 rpc_set_error(rpc
, "Trying to connect while already connected");
445 if (rpc
->is_udp
!= 0) {
446 rpc_set_error(rpc
, "Trying to connect on UDP socket");
450 rpc
->auto_reconnect
= 0;
452 sin
->sin_family
= AF_INET
;
453 sin
->sin_port
= htons(port
);
454 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
455 rpc_set_error(rpc
, "Not a valid server ip address");
460 switch (rpc
->s
.ss_family
) {
462 #ifdef HAVE_SOCKADDR_LEN
463 sin
->sin_len
= sizeof(struct sockaddr_in
);
468 rpc
->connect_cb
= cb
;
469 rpc
->connect_data
= private_data
;
471 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
478 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
480 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
482 rpc_unset_autoreconnect(rpc
);
489 rpc
->is_connected
= 0;
491 rpc_error_all_pdus(rpc
, error
);
496 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
498 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
500 if (status
!= RPC_STATUS_SUCCESS
) {
501 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
505 rpc
->is_connected
= 1;
506 rpc
->connect_cb
= NULL
;
509 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
510 static int rpc_reconnect_requeue(struct rpc_context
*rpc
)
514 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
521 rpc
->is_connected
= 0;
523 /* socket is closed so we will not get any replies to any commands
524 * in flight. Move them all over from the waitpdu queue back to the out queue
526 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
527 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
528 SLIST_ADD(&rpc
->outqueue
, pdu
);
529 /* we have to re-send the whole pdu again */
533 if (rpc
->auto_reconnect
!= 0) {
534 rpc
->connect_cb
= reconnect_cb
;
536 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
537 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
546 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
548 struct addrinfo
*ai
= NULL
;
551 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
553 if (rpc
->is_udp
== 0) {
554 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
558 sprintf(service
, "%d", port
);
559 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
560 rpc_set_error(rpc
, "Invalid address:%s. "
561 "Can not resolv into IPv4/v6 structure.");
565 switch(ai
->ai_family
) {
567 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
569 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
574 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
575 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
581 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
591 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
593 struct addrinfo
*ai
= NULL
;
596 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
598 if (rpc
->is_udp
== 0) {
599 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
603 sprintf(service
, "%d", port
);
604 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
605 rpc_set_error(rpc
, "Invalid address:%s. "
606 "Can not resolv into IPv4/v6 structure.");
612 rpc
->udp_dest
= NULL
;
614 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
615 if (rpc
->udp_dest
== NULL
) {
616 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
620 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
623 rpc
->is_broadcast
= is_broadcast
;
624 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
629 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
631 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
633 return (struct sockaddr
*)&rpc
->udp_src
;
636 int rpc_queue_length(struct rpc_context
*rpc
)
641 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
643 for(pdu
= rpc
->outqueue
; pdu
; pdu
= pdu
->next
) {
646 for(pdu
= rpc
->waitpdu
; pdu
; pdu
= pdu
->next
) {