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>
38 #ifdef HAVE_SYS_FILIO_H
39 #include <sys/filio.h>
41 #ifdef HAVE_SYS_SOCKIO_H
42 #include <sys/sockio.h>
44 #include <sys/types.h>
46 #include "libnfs-raw.h"
47 #include "libnfs-private.h"
51 //has to be included after stdlib!!
52 #include "win32_errnowrapper.h"
56 static int rpc_reconnect_requeue(struct rpc_context
*rpc
);
57 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
);
59 static void set_nonblocking(int fd
)
64 v
= ioctlsocket(fd
, FIONBIO
,&nonblocking
);
66 v
= fcntl(fd
, F_GETFL
, 0);
67 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
71 int rpc_get_fd(struct rpc_context
*rpc
)
76 int rpc_which_events(struct rpc_context
*rpc
)
78 int events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
80 if (rpc
->is_udp
!= 0) {
81 /* for udp sockets we only wait for pollin */
91 static int rpc_write_to_socket(struct rpc_context
*rpc
)
99 rpc_set_error(rpc
, "trying to write but not connected");
103 while (rpc
->outqueue
!= NULL
) {
106 total
= rpc
->outqueue
->outdata
.size
;
109 count
= send(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
, 0);
111 count
= write(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
);
114 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
117 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
121 rpc
->outqueue
->written
+= count
;
122 if (rpc
->outqueue
->written
== total
) {
123 struct rpc_pdu
*pdu
= rpc
->outqueue
;
125 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
126 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
132 static int rpc_read_from_socket(struct rpc_context
*rpc
)
140 if (ioctlsocket(rpc
->fd
, FIONREAD
, &available
) != 0) {
142 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
144 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
148 if (available
== 0) {
149 rpc_set_error(rpc
, "Socket has been closed");
155 socklen_t socklen
= sizeof(rpc
->udp_src
);
157 buf
= malloc(available
);
159 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
162 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
164 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
167 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
168 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
176 /* read record marker, 4 bytes at the beginning of every pdu */
177 if (rpc
->inbuf
== NULL
) {
179 rpc
->inbuf
= malloc(rpc
->insize
);
180 if (rpc
->inbuf
== NULL
) {
181 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
185 if (rpc
->inpos
< 4) {
186 size
= 4 - rpc
->inpos
;
189 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
191 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
194 if (errno
== EINTR
) {
197 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
204 if (available
== 0) {
208 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
209 if (rpc
->insize
< pdu_size
) {
212 buf
= malloc(pdu_size
);
214 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
217 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
220 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
224 if (size
> rpc
->insize
- rpc
->inpos
) {
225 size
= rpc
->insize
- rpc
->inpos
;
229 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
231 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
234 if (errno
== EINTR
) {
237 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
243 if (rpc
->inpos
== rpc
->insize
) {
244 if (rpc_process_pdu(rpc
, rpc
->inbuf
, pdu_size
) != 0) {
245 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
259 int rpc_service(struct rpc_context
*rpc
, int revents
)
261 if (revents
& POLLERR
) {
267 socklen_t err_size
= sizeof(err
);
269 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
270 (char *)&err
, &err_size
) != 0 || err
!= 0) {
274 rpc_set_error(rpc
, "rpc_service: socket error "
278 rpc_set_error(rpc
, "rpc_service: POLLERR, "
279 "Unknown socket error.");
281 if (rpc
->connect_cb
!= NULL
) {
282 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
286 if (revents
& POLLHUP
) {
287 rpc_set_error(rpc
, "Socket failed with POLLHUP");
288 if (rpc
->connect_cb
!= NULL
) {
289 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
294 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
296 socklen_t err_size
= sizeof(err
);
298 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
299 (char *)&err
, &err_size
) != 0 || err
!= 0) {
303 rpc_set_error(rpc
, "rpc_service: socket error "
304 "%s(%d) while connecting.",
306 if (rpc
->connect_cb
!= NULL
) {
307 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
308 NULL
, rpc
->connect_data
);
313 rpc
->is_connected
= 1;
314 if (rpc
->connect_cb
!= NULL
) {
315 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
320 if (revents
& POLLIN
) {
321 if (rpc_read_from_socket(rpc
) != 0) {
322 rpc_reconnect_requeue(rpc
);
327 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
328 if (rpc_write_to_socket(rpc
) != 0) {
329 rpc_set_error(rpc
, "write to socket failed");
337 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
339 rpc
->auto_reconnect
= 1;
342 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
344 rpc
->auto_reconnect
= 0;
347 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
351 switch (s
->ss_family
) {
353 socksize
= sizeof(struct sockaddr_in
);
354 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
357 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
362 rpc_set_error(rpc
, "Failed to open socket");
368 /* Some systems allow you to set capabilities on an executable
369 * to allow the file to be executed with privilege to bind to
370 * privileged system ports, even if the user is not root.
372 * Opportunistically try to bind the socket to a low numbered
373 * system port in the hope that the user is either root or the
374 * executable has the CAP_NET_BIND_SERVICE.
376 * As soon as we fail the bind() with EACCES we know we will never
377 * be able to bind to a system port so we terminate the loop.
380 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
381 * to make the executable able to bind to a system port.
387 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&one
, sizeof(one
));
389 for (port
= 200; port
< 500; port
++) {
390 struct sockaddr_in sin
;
392 memset(&sin
, 0, sizeof(sin
));
393 sin
.sin_port
= htons(port
);
394 sin
.sin_family
= AF_INET
;
395 sin
.sin_addr
.s_addr
= 0;
397 if (bind(rpc
->fd
, (struct sockaddr
*)&sin
, sizeof(struct sockaddr_in
)) != 0 && errno
!= EACCES
) {
398 /* we didnt get EACCES, so try again */
406 set_nonblocking(rpc
->fd
);
409 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) == 0 && errno
!= EINPROGRESS
)
411 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) != 0 && errno
!= EINPROGRESS
)
414 rpc_set_error(rpc
, "connect() to server failed");
421 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
423 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&rpc
->s
;
426 rpc_set_error(rpc
, "Trying to connect while already connected");
430 if (rpc
->is_udp
!= 0) {
431 rpc_set_error(rpc
, "Trying to connect on UDP socket");
435 rpc
->auto_reconnect
= 0;
437 sin
->sin_family
= AF_INET
;
438 sin
->sin_port
= htons(port
);
439 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
440 rpc_set_error(rpc
, "Not a valid server ip address");
445 switch (rpc
->s
.ss_family
) {
447 #ifdef HAVE_SOCKADDR_LEN
448 sin
->sin_len
= sizeof(struct sockaddr_in
);
453 rpc
->connect_cb
= cb
;
454 rpc
->connect_data
= private_data
;
456 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
463 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
465 rpc_unset_autoreconnect(rpc
);
469 closesocket(rpc
->fd
);
476 rpc
->is_connected
= 0;
478 rpc_error_all_pdus(rpc
, error
);
483 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
485 if (status
!= RPC_STATUS_SUCCESS
) {
486 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
490 rpc
->is_connected
= 1;
491 rpc
->connect_cb
= NULL
;
494 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
495 static int rpc_reconnect_requeue(struct rpc_context
*rpc
)
501 closesocket(rpc
->fd
);
508 rpc
->is_connected
= 0;
510 /* socket is closed so we will not get any replies to any commands
511 * in flight. Move them all over from the waitpdu queue back to the out queue
513 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
514 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
515 SLIST_ADD(&rpc
->outqueue
, pdu
);
516 /* we have to re-send the whole pdu again */
520 if (rpc
->auto_reconnect
!= 0) {
521 rpc
->connect_cb
= reconnect_cb
;
523 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
524 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
533 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
535 struct addrinfo
*ai
= NULL
;
538 if (rpc
->is_udp
== 0) {
539 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
543 sprintf(service
, "%d", port
);
544 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
545 rpc_set_error(rpc
, "Invalid address:%s. "
546 "Can not resolv into IPv4/v6 structure.");
550 switch(ai
->ai_family
) {
552 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
554 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
559 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
560 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
566 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
576 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
578 struct addrinfo
*ai
= NULL
;
581 if (rpc
->is_udp
== 0) {
582 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
586 sprintf(service
, "%d", port
);
587 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
588 rpc_set_error(rpc
, "Invalid address:%s. "
589 "Can not resolv into IPv4/v6 structure.");
595 rpc
->udp_dest
= NULL
;
597 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
598 if (rpc
->udp_dest
== NULL
) {
599 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
602 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
605 rpc
->is_broadcast
= is_broadcast
;
606 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
611 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
613 return (struct sockaddr
*)&rpc
->udp_src
;
616 int rpc_queue_length(struct rpc_context
*rpc
)
621 for(pdu
= rpc
->outqueue
; pdu
; pdu
= pdu
->next
) {
624 for(pdu
= rpc
->waitpdu
; pdu
; pdu
= pdu
->next
) {