413ebbe4c8e4009cca6f0ccbc506a5a02b657014
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>
39 #ifdef HAVE_SYS_FILIO_H
40 #include <sys/filio.h>
42 #ifdef HAVE_SYS_SOCKIO_H
43 #include <sys/sockio.h>
45 #include <sys/types.h>
47 #include "libnfs-raw.h"
48 #include "libnfs-private.h"
52 //has to be included after stdlib!!
53 #include "win32_errnowrapper.h"
57 static int rpc_reconnect_requeue(struct rpc_context
*rpc
);
58 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
);
60 static void set_nonblocking(int fd
)
65 v
= ioctlsocket(fd
, FIONBIO
,&nonblocking
);
67 v
= fcntl(fd
, F_GETFL
, 0);
68 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
72 int rpc_get_fd(struct rpc_context
*rpc
)
74 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
79 int rpc_which_events(struct rpc_context
*rpc
)
83 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
85 events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
87 if (rpc
->is_udp
!= 0) {
88 /* for udp sockets we only wait for pollin */
98 static int rpc_write_to_socket(struct rpc_context
*rpc
)
102 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
105 rpc_set_error(rpc
, "trying to write but not connected");
109 while (rpc
->outqueue
!= NULL
) {
112 total
= rpc
->outqueue
->outdata
.size
;
115 count
= send(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
, 0);
117 count
= write(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
);
120 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
123 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
127 rpc
->outqueue
->written
+= count
;
128 if (rpc
->outqueue
->written
== total
) {
129 struct rpc_pdu
*pdu
= rpc
->outqueue
;
131 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
132 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
138 static int rpc_read_from_socket(struct rpc_context
*rpc
)
145 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
148 if (ioctlsocket(rpc
->fd
, FIONREAD
, &available
) != 0) {
150 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
152 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
156 if (available
== 0) {
157 rpc_set_error(rpc
, "Socket has been closed");
163 socklen_t socklen
= sizeof(rpc
->udp_src
);
165 buf
= malloc(available
);
167 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
170 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
172 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
175 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
176 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
184 /* read record marker, 4 bytes at the beginning of every pdu */
185 if (rpc
->inbuf
== NULL
) {
187 rpc
->inbuf
= malloc(rpc
->insize
);
188 if (rpc
->inbuf
== NULL
) {
189 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
193 if (rpc
->inpos
< 4) {
194 size
= 4 - rpc
->inpos
;
197 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
199 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
202 if (errno
== EINTR
) {
205 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
212 if (available
== 0) {
216 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
217 if (rpc
->insize
< pdu_size
) {
220 buf
= malloc(pdu_size
);
222 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
225 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
228 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
232 if (size
> rpc
->insize
- rpc
->inpos
) {
233 size
= rpc
->insize
- rpc
->inpos
;
237 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
239 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
242 if (errno
== EINTR
) {
245 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
251 if (rpc
->inpos
== rpc
->insize
) {
252 if (rpc_process_pdu(rpc
, rpc
->inbuf
, pdu_size
) != 0) {
253 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
267 int rpc_service(struct rpc_context
*rpc
, int revents
)
269 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
271 if (revents
& POLLERR
) {
277 socklen_t err_size
= sizeof(err
);
279 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
280 (char *)&err
, &err_size
) != 0 || err
!= 0) {
284 rpc_set_error(rpc
, "rpc_service: socket error "
288 rpc_set_error(rpc
, "rpc_service: POLLERR, "
289 "Unknown socket error.");
291 if (rpc
->connect_cb
!= NULL
) {
292 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
296 if (revents
& POLLHUP
) {
297 rpc_set_error(rpc
, "Socket failed with POLLHUP");
298 if (rpc
->connect_cb
!= NULL
) {
299 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
304 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
306 socklen_t err_size
= sizeof(err
);
308 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
309 (char *)&err
, &err_size
) != 0 || err
!= 0) {
313 rpc_set_error(rpc
, "rpc_service: socket error "
314 "%s(%d) while connecting.",
316 if (rpc
->connect_cb
!= NULL
) {
317 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
318 NULL
, rpc
->connect_data
);
323 rpc
->is_connected
= 1;
324 if (rpc
->connect_cb
!= NULL
) {
325 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
330 if (revents
& POLLIN
) {
331 if (rpc_read_from_socket(rpc
) != 0) {
332 rpc_reconnect_requeue(rpc
);
337 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
338 if (rpc_write_to_socket(rpc
) != 0) {
339 rpc_set_error(rpc
, "write to socket failed");
347 void rpc_set_autoreconnect(struct rpc_context
*rpc
)
349 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
351 rpc
->auto_reconnect
= 1;
354 void rpc_unset_autoreconnect(struct rpc_context
*rpc
)
356 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
358 rpc
->auto_reconnect
= 0;
361 static int rpc_connect_sockaddr_async(struct rpc_context
*rpc
, struct sockaddr_storage
*s
)
365 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
367 switch (s
->ss_family
) {
369 socksize
= sizeof(struct sockaddr_in
);
370 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
373 rpc_set_error(rpc
, "Can not handle AF_FAMILY:%d", s
->ss_family
);
378 rpc_set_error(rpc
, "Failed to open socket");
382 /* Some systems allow you to set capabilities on an executable
383 * to allow the file to be executed with privilege to bind to
384 * privileged system ports, even if the user is not root.
386 * Opportunistically try to bind the socket to a low numbered
387 * system port in the hope that the user is either root or the
388 * executable has the CAP_NET_BIND_SERVICE.
390 * As soon as we fail the bind() with EACCES we know we will never
391 * be able to bind to a system port so we terminate the loop.
394 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
395 * to make the executable able to bind to a system port.
397 * On Windows, there is no concept of privileged ports. Thus
398 * binding will usually succeed.
401 struct sockaddr_in sin
;
402 static int portOfs
= 0;
403 const int firstPort
= 512; /* >= 512 according to Sun docs */
404 const int portCount
= IPPORT_RESERVED
- firstPort
;
405 int startOfs
= portOfs
, port
, rc
;
409 port
= htons(firstPort
+ portOfs
);
410 portOfs
= (portOfs
+ 1) % portCount
;
412 /* skip well-known ports */
413 if (!getservbyport(port
, "tcp")) {
414 memset(&sin
, 0, sizeof(sin
));
416 sin
.sin_family
= AF_INET
;
417 sin
.sin_addr
.s_addr
= 0;
419 rc
= bind(rpc
->fd
, (struct sockaddr
*)&sin
, sizeof(struct sockaddr_in
));
421 /* we got EACCES, so don't try again */
422 if (rc
!= 0 && errno
== EACCES
)
426 } while (rc
!= 0 && portOfs
!= startOfs
);
429 set_nonblocking(rpc
->fd
);
431 if (connect(rpc
->fd
, (struct sockaddr
*)s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
432 rpc_set_error(rpc
, "connect() to server failed. %s(%d)", strerror(errno
), errno
);
439 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
441 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&rpc
->s
;
443 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
446 rpc_set_error(rpc
, "Trying to connect while already connected");
450 if (rpc
->is_udp
!= 0) {
451 rpc_set_error(rpc
, "Trying to connect on UDP socket");
455 rpc
->auto_reconnect
= 0;
457 sin
->sin_family
= AF_INET
;
458 sin
->sin_port
= htons(port
);
459 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
460 rpc_set_error(rpc
, "Not a valid server ip address");
465 switch (rpc
->s
.ss_family
) {
467 #ifdef HAVE_SOCKADDR_LEN
468 sin
->sin_len
= sizeof(struct sockaddr_in
);
473 rpc
->connect_cb
= cb
;
474 rpc
->connect_data
= private_data
;
476 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
483 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
485 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
487 rpc_unset_autoreconnect(rpc
);
491 closesocket(rpc
->fd
);
498 rpc
->is_connected
= 0;
500 rpc_error_all_pdus(rpc
, error
);
505 static void reconnect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
507 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
509 if (status
!= RPC_STATUS_SUCCESS
) {
510 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
514 rpc
->is_connected
= 1;
515 rpc
->connect_cb
= NULL
;
518 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
519 static int rpc_reconnect_requeue(struct rpc_context
*rpc
)
523 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
527 closesocket(rpc
->fd
);
534 rpc
->is_connected
= 0;
536 /* socket is closed so we will not get any replies to any commands
537 * in flight. Move them all over from the waitpdu queue back to the out queue
539 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
540 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
541 SLIST_ADD(&rpc
->outqueue
, pdu
);
542 /* we have to re-send the whole pdu again */
546 if (rpc
->auto_reconnect
!= 0) {
547 rpc
->connect_cb
= reconnect_cb
;
549 if (rpc_connect_sockaddr_async(rpc
, &rpc
->s
) != 0) {
550 rpc_error_all_pdus(rpc
, "RPC ERROR: Failed to reconnect async");
559 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
561 struct addrinfo
*ai
= NULL
;
564 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
566 if (rpc
->is_udp
== 0) {
567 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
571 sprintf(service
, "%d", port
);
572 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
573 rpc_set_error(rpc
, "Invalid address:%s. "
574 "Can not resolv into IPv4/v6 structure.");
578 switch(ai
->ai_family
) {
580 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
582 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
587 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
588 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
594 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
604 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
606 struct addrinfo
*ai
= NULL
;
609 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
611 if (rpc
->is_udp
== 0) {
612 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
616 sprintf(service
, "%d", port
);
617 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
618 rpc_set_error(rpc
, "Invalid address:%s. "
619 "Can not resolv into IPv4/v6 structure.");
625 rpc
->udp_dest
= NULL
;
627 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
628 if (rpc
->udp_dest
== NULL
) {
629 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
633 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
636 rpc
->is_broadcast
= is_broadcast
;
637 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
642 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
644 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
646 return (struct sockaddr
*)&rpc
->udp_src
;
649 int rpc_queue_length(struct rpc_context
*rpc
)
654 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
656 for(pdu
= rpc
->outqueue
; pdu
; pdu
= pdu
->next
) {
659 for(pdu
= rpc
->waitpdu
; pdu
; pdu
= pdu
->next
) {