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"
21 #include <arpa/inet.h>
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
32 #include "aros_compat.h"
45 #ifdef HAVE_SYS_FILIO_H
46 #include <sys/filio.h>
48 #ifdef HAVE_SYS_SOCKIO_H
49 #include <sys/sockio.h>
51 #include <sys/types.h>
52 #include "libnfs-zdr.h"
54 #include "libnfs-raw.h"
55 #include "libnfs-private.h"
59 //has to be included after stdlib!!
60 #include "win32_errnowrapper.h"
64 static int rpc_reconnect_requeue(struct rpc_context
*rpc
);
65 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
);
67 static void set_nonblocking(int fd
)
72 v
= ioctlsocket(fd
, FIONBIO
,&nonblocking
);
74 v
= fcntl(fd
, F_GETFL
, 0);
75 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
79 int rpc_get_fd(struct rpc_context
*rpc
)
81 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
86 int rpc_which_events(struct rpc_context
*rpc
)
90 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
92 events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
94 if (rpc
->is_udp
!= 0) {
95 /* for udp sockets we only wait for pollin */
105 static int rpc_write_to_socket(struct rpc_context
*rpc
)
109 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
112 rpc_set_error(rpc
, "trying to write but not connected");
116 while (rpc
->outqueue
!= NULL
) {
119 total
= rpc
->outqueue
->outdata
.size
;
121 count
= send(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
, 0);
123 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
126 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
130 rpc
->outqueue
->written
+= count
;
131 if (rpc
->outqueue
->written
== total
) {
132 struct rpc_pdu
*pdu
= rpc
->outqueue
;
134 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
135 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
141 static int rpc_read_from_socket(struct rpc_context
*rpc
)
148 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
150 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
153 if (ioctlsocket(rpc
->fd
, FIONREAD
, &available
) != 0) {
155 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
157 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
161 if (available
== 0) {
162 rpc_set_error(rpc
, "Socket has been closed");
168 socklen_t socklen
= sizeof(rpc
->udp_src
);
170 buf
= malloc(available
);
172 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
175 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
177 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
180 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
181 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
189 /* read record marker, 4 bytes at the beginning of every pdu */
190 if (rpc
->inbuf
== NULL
) {
192 rpc
->inbuf
= malloc(rpc
->insize
);
193 if (rpc
->inbuf
== NULL
) {
194 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
198 if (rpc
->inpos
< 4) {
199 size
= 4 - rpc
->inpos
;
201 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
203 if (errno
== EINTR
) {
206 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
213 if (available
== 0) {
217 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
218 if (rpc
->insize
< pdu_size
) {
221 buf
= malloc(pdu_size
);
223 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
226 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
229 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
233 if (size
> rpc
->insize
- rpc
->inpos
) {
234 size
= rpc
->insize
- rpc
->inpos
;
237 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
239 if (errno
== EINTR
) {
242 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
248 if (rpc
->inpos
== rpc
->insize
) {
249 if (rpc_process_pdu(rpc
, rpc
->inbuf
, pdu_size
) != 0) {
250 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
264 int rpc_service(struct rpc_context
*rpc
, int revents
)
266 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
268 if (revents
& POLLERR
) {
274 socklen_t err_size
= sizeof(err
);
276 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
277 (char *)&err
, &err_size
) != 0 || err
!= 0) {
281 rpc_set_error(rpc
, "rpc_service: socket error "
285 rpc_set_error(rpc
, "rpc_service: POLLERR, "
286 "Unknown socket error.");
288 if (rpc
->connect_cb
!= NULL
) {
289 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
293 if (revents
& POLLHUP
) {
294 rpc_set_error(rpc
, "Socket failed with POLLHUP");
295 if (rpc
->connect_cb
!= NULL
) {
296 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
301 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
303 socklen_t err_size
= sizeof(err
);
305 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
306 (char *)&err
, &err_size
) != 0 || err
!= 0) {
310 rpc_set_error(rpc
, "rpc_service: socket error "
311 "%s(%d) while connecting.",
313 if (rpc
->connect_cb
!= NULL
) {
314 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
315 NULL
, rpc
->connect_data
);
320 rpc
->is_connected
= 1;
321 if (rpc
->connect_cb
!= NULL
) {
322 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
327 if (revents
& POLLIN
) {
328 if (rpc_read_from_socket(rpc
) != 0) {
329 rpc_reconnect_requeue(rpc
);
334 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
335 if (rpc_write_to_socket(rpc
) != 0) {
336 rpc_set_error(rpc
, "write to socket failed");
344 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
346 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
348 rpc
->auto_reconnect
= 1;
351 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
353 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
355 rpc
->auto_reconnect
= 0;
358 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
362 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
364 switch (s
->ss_family
) {
366 socksize
= sizeof(struct sockaddr_in
);
367 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
370 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
375 rpc_set_error(rpc
, "Failed to open socket");
379 /* Some systems allow you to set capabilities on an executable
380 * to allow the file to be executed with privilege to bind to
381 * privileged system ports, even if the user is not root.
383 * Opportunistically try to bind the socket to a low numbered
384 * system port in the hope that the user is either root or the
385 * executable has the CAP_NET_BIND_SERVICE.
387 * As soon as we fail the bind() with EACCES we know we will never
388 * be able to bind to a system port so we terminate the loop.
391 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
392 * to make the executable able to bind to a system port.
394 * On Windows, there is no concept of privileged ports. Thus
395 * binding will usually succeed.
398 struct sockaddr_in sin
;
399 static int portOfs
= 0;
400 const int firstPort
= 512; /* >= 512 according to Sun docs */
401 const int portCount
= IPPORT_RESERVED
- firstPort
;
402 int startOfs
= portOfs
, port
, rc
;
406 port
= htons(firstPort
+ portOfs
);
407 portOfs
= (portOfs
+ 1) % portCount
;
409 /* skip well-known ports */
410 if (!getservbyport(port
, "tcp")) {
411 memset(&sin
, 0, sizeof(sin
));
413 sin
.sin_family
= AF_INET
;
414 sin
.sin_addr
.s_addr
= 0;
416 rc
= bind(rpc
->fd
, (struct sockaddr
*)&sin
, sizeof(struct sockaddr_in
));
418 /* we got EACCES, so don't try again */
419 if (rc
!= 0 && errno
== EACCES
)
423 } while (rc
!= 0 && portOfs
!= startOfs
);
426 set_nonblocking(rpc
->fd
);
428 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
429 rpc_set_error(rpc
, "connect() to server failed. %s(%d)", strerror(errno
), errno
);
436 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
438 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&rpc
->s
;
440 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
443 rpc_set_error(rpc
, "Trying to connect while already connected");
447 if (rpc
->is_udp
!= 0) {
448 rpc_set_error(rpc
, "Trying to connect on UDP socket");
452 rpc
->auto_reconnect
= 0;
454 sin
->sin_family
= AF_INET
;
455 sin
->sin_port
= htons(port
);
456 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
457 rpc_set_error(rpc
, "Not a valid server ip address");
462 switch (rpc
->s
.ss_family
) {
464 #ifdef HAVE_SOCKADDR_LEN
465 sin
->sin_len
= sizeof(struct sockaddr_in
);
470 rpc
->connect_cb
= cb
;
471 rpc
->connect_data
= private_data
;
473 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
480 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
482 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
484 rpc_unset_autoreconnect(rpc
);
488 closesocket(rpc
->fd
);
495 rpc
->is_connected
= 0;
497 rpc_error_all_pdus(rpc
, error
);
502 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
504 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
506 if (status
!= RPC_STATUS_SUCCESS
) {
507 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
511 rpc
->is_connected
= 1;
512 rpc
->connect_cb
= NULL
;
515 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
516 static int rpc_reconnect_requeue(struct rpc_context
*rpc
)
520 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
524 closesocket(rpc
->fd
);
531 rpc
->is_connected
= 0;
533 /* socket is closed so we will not get any replies to any commands
534 * in flight. Move them all over from the waitpdu queue back to the out queue
536 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
537 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
538 SLIST_ADD(&rpc
->outqueue
, pdu
);
539 /* we have to re-send the whole pdu again */
543 if (rpc
->auto_reconnect
!= 0) {
544 rpc
->connect_cb
= reconnect_cb
;
546 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
547 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
556 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
558 struct addrinfo
*ai
= NULL
;
561 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
563 if (rpc
->is_udp
== 0) {
564 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
568 sprintf(service
, "%d", port
);
569 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
570 rpc_set_error(rpc
, "Invalid address:%s. "
571 "Can not resolv into IPv4/v6 structure.");
575 switch(ai
->ai_family
) {
577 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
579 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
584 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
585 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
591 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
601 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
603 struct addrinfo
*ai
= NULL
;
606 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
608 if (rpc
->is_udp
== 0) {
609 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
613 sprintf(service
, "%d", port
);
614 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
615 rpc_set_error(rpc
, "Invalid address:%s. "
616 "Can not resolv into IPv4/v6 structure.");
622 rpc
->udp_dest
= NULL
;
624 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
625 if (rpc
->udp_dest
== NULL
) {
626 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
630 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
633 rpc
->is_broadcast
= is_broadcast
;
634 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
639 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
641 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
643 return (struct sockaddr
*)&rpc
->udp_src
;
646 int rpc_queue_length(struct rpc_context
*rpc
)
651 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
653 for(pdu
= rpc
->outqueue
; pdu
; pdu
= pdu
->next
) {
656 for(pdu
= rpc
->waitpdu
; pdu
; pdu
= pdu
->next
) {