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"
22 #include <arpa/inet.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
36 #ifdef HAVE_SYS_FILIO_H
37 #include <sys/filio.h>
39 #ifdef HAVE_SYS_SOCKIO_H
40 #include <sys/sockio.h>
42 #include <sys/types.h>
43 #include "libnfs-zdr.h"
45 #include "libnfs-raw.h"
46 #include "libnfs-private.h"
50 //has to be included after stdlib!!
51 #include "win32_errnowrapper.h"
55 static int rpc_reconnect_requeue(struct rpc_context
*rpc
);
56 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
);
58 static void set_nonblocking(int fd
)
63 v
= ioctlsocket(fd
, FIONBIO
,&nonblocking
);
65 v
= fcntl(fd
, F_GETFL
, 0);
66 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
70 int rpc_get_fd(struct rpc_context
*rpc
)
75 int rpc_which_events(struct rpc_context
*rpc
)
77 int events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
79 if (rpc
->is_udp
!= 0) {
80 /* for udp sockets we only wait for pollin */
90 static int rpc_write_to_socket(struct rpc_context
*rpc
)
98 rpc_set_error(rpc
, "trying to write but not connected");
102 while (rpc
->outqueue
!= NULL
) {
105 total
= rpc
->outqueue
->outdata
.size
;
108 count
= send(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
, 0);
110 count
= write(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
);
113 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
116 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
120 rpc
->outqueue
->written
+= count
;
121 if (rpc
->outqueue
->written
== total
) {
122 struct rpc_pdu
*pdu
= rpc
->outqueue
;
124 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
125 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
131 static int rpc_read_from_socket(struct rpc_context
*rpc
)
139 if (ioctlsocket(rpc
->fd
, FIONREAD
, &available
) != 0) {
141 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
143 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
147 if (available
== 0) {
148 rpc_set_error(rpc
, "Socket has been closed");
154 socklen_t socklen
= sizeof(rpc
->udp_src
);
156 buf
= malloc(available
);
158 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
161 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
163 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
166 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
167 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
175 /* read record marker, 4 bytes at the beginning of every pdu */
176 if (rpc
->inbuf
== NULL
) {
178 rpc
->inbuf
= malloc(rpc
->insize
);
179 if (rpc
->inbuf
== NULL
) {
180 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
184 if (rpc
->inpos
< 4) {
185 size
= 4 - rpc
->inpos
;
188 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
190 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
193 if (errno
== EINTR
) {
196 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
203 if (available
== 0) {
207 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
208 if (rpc
->insize
< pdu_size
) {
211 buf
= malloc(pdu_size
);
213 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
216 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
219 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
223 if (size
> rpc
->insize
- rpc
->inpos
) {
224 size
= rpc
->insize
- rpc
->inpos
;
228 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
230 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
233 if (errno
== EINTR
) {
236 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
242 if (rpc
->inpos
== rpc
->insize
) {
243 if (rpc_process_pdu(rpc
, rpc
->inbuf
, pdu_size
) != 0) {
244 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
258 int rpc_service(struct rpc_context
*rpc
, int revents
)
260 if (revents
& POLLERR
) {
266 socklen_t err_size
= sizeof(err
);
268 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
269 (char *)&err
, &err_size
) != 0 || err
!= 0) {
273 rpc_set_error(rpc
, "rpc_service: socket error "
277 rpc_set_error(rpc
, "rpc_service: POLLERR, "
278 "Unknown socket error.");
280 if (rpc
->connect_cb
!= NULL
) {
281 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
285 if (revents
& POLLHUP
) {
286 rpc_set_error(rpc
, "Socket failed with POLLHUP");
287 if (rpc
->connect_cb
!= NULL
) {
288 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
293 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
295 socklen_t err_size
= sizeof(err
);
297 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
298 (char *)&err
, &err_size
) != 0 || err
!= 0) {
302 rpc_set_error(rpc
, "rpc_service: socket error "
303 "%s(%d) while connecting.",
305 if (rpc
->connect_cb
!= NULL
) {
306 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
307 NULL
, rpc
->connect_data
);
312 rpc
->is_connected
= 1;
313 if (rpc
->connect_cb
!= NULL
) {
314 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
319 if (revents
& POLLIN
) {
320 if (rpc_read_from_socket(rpc
) != 0) {
321 rpc_reconnect_requeue(rpc
);
326 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
327 if (rpc_write_to_socket(rpc
) != 0) {
328 rpc_set_error(rpc
, "write to socket failed");
336 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
338 rpc
->auto_reconnect
= 1;
341 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
343 rpc
->auto_reconnect
= 0;
346 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
350 switch (s
->ss_family
) {
352 socksize
= sizeof(struct sockaddr_in
);
353 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
356 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
361 rpc_set_error(rpc
, "Failed to open socket");
367 /* Some systems allow you to set capabilities on an executable
368 * to allow the file to be executed with privilege to bind to
369 * privileged system ports, even if the user is not root.
371 * Opportunistically try to bind the socket to a low numbered
372 * system port in the hope that the user is either root or the
373 * executable has the CAP_NET_BIND_SERVICE.
375 * As soon as we fail the bind() with EACCES we know we will never
376 * be able to bind to a system port so we terminate the loop.
379 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
380 * to make the executable able to bind to a system port.
383 static int port
= 200;
387 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&one
, sizeof(one
));
389 for (i
= 0; i
< 500; i
++) {
390 struct sockaddr_in sin
;
392 if(++port
> 700) port
= 200;
394 memset(&sin
, 0, sizeof(sin
));
395 sin
.sin_port
= htons(port
);
396 sin
.sin_family
= AF_INET
;
397 sin
.sin_addr
.s_addr
= 0;
399 if (bind(rpc
->fd
, (struct sockaddr
*)&sin
, sizeof(struct sockaddr_in
)) != 0 && errno
!= EACCES
) {
400 /* we didnt get EACCES, so try again */
408 set_nonblocking(rpc
->fd
);
411 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) == 0 && errno
!= EINPROGRESS
)
413 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) != 0 && errno
!= EINPROGRESS
)
416 rpc_set_error(rpc
, "connect() to server failed. %s(%d)", strerror(errno
), errno
);
423 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
425 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&rpc
->s
;
428 rpc_set_error(rpc
, "Trying to connect while already connected");
432 if (rpc
->is_udp
!= 0) {
433 rpc_set_error(rpc
, "Trying to connect on UDP socket");
437 rpc
->auto_reconnect
= 0;
439 sin
->sin_family
= AF_INET
;
440 sin
->sin_port
= htons(port
);
441 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
442 rpc_set_error(rpc
, "Not a valid server ip address");
447 switch (rpc
->s
.ss_family
) {
449 #ifdef HAVE_SOCKADDR_LEN
450 sin
->sin_len
= sizeof(struct sockaddr_in
);
455 rpc
->connect_cb
= cb
;
456 rpc
->connect_data
= private_data
;
458 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
465 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
467 rpc_unset_autoreconnect(rpc
);
471 closesocket(rpc
->fd
);
478 rpc
->is_connected
= 0;
480 rpc_error_all_pdus(rpc
, error
);
485 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
487 if (status
!= RPC_STATUS_SUCCESS
) {
488 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
492 rpc
->is_connected
= 1;
493 rpc
->connect_cb
= NULL
;
496 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
497 static int rpc_reconnect_requeue(struct rpc_context
*rpc
)
503 closesocket(rpc
->fd
);
510 rpc
->is_connected
= 0;
512 /* socket is closed so we will not get any replies to any commands
513 * in flight. Move them all over from the waitpdu queue back to the out queue
515 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
516 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
517 SLIST_ADD(&rpc
->outqueue
, pdu
);
518 /* we have to re-send the whole pdu again */
522 if (rpc
->auto_reconnect
!= 0) {
523 rpc
->connect_cb
= reconnect_cb
;
525 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
526 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
535 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
537 struct addrinfo
*ai
= NULL
;
540 if (rpc
->is_udp
== 0) {
541 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
545 sprintf(service
, "%d", port
);
546 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
547 rpc_set_error(rpc
, "Invalid address:%s. "
548 "Can not resolv into IPv4/v6 structure.");
552 switch(ai
->ai_family
) {
554 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
556 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
561 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
562 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
568 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
578 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
580 struct addrinfo
*ai
= NULL
;
583 if (rpc
->is_udp
== 0) {
584 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
588 sprintf(service
, "%d", port
);
589 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
590 rpc_set_error(rpc
, "Invalid address:%s. "
591 "Can not resolv into IPv4/v6 structure.");
597 rpc
->udp_dest
= NULL
;
599 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
600 if (rpc
->udp_dest
== NULL
) {
601 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
604 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
607 rpc
->is_broadcast
= is_broadcast
;
608 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
613 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
615 return (struct sockaddr
*)&rpc
->udp_src
;
618 int rpc_queue_length(struct rpc_context
*rpc
)
623 for(pdu
= rpc
->outqueue
; pdu
; pdu
= pdu
->next
) {
626 for(pdu
= rpc
->waitpdu
; pdu
; pdu
= pdu
->next
) {