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>
32 #define ssize_t SSIZE_T
33 #define MSG_DONTWAIT 0
37 #include <arpa/inet.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
53 #ifdef HAVE_SYS_FILIO_H
54 #include <sys/filio.h>
56 #ifdef HAVE_SYS_SOCKIO_H
57 #include <sys/sockio.h>
59 #include <sys/types.h>
61 #include "libnfs-raw.h"
62 #include "libnfs-private.h"
65 static int rpc_disconnect_requeue(struct rpc_context
*rpc
);
67 static void set_nonblocking(int fd
)
72 v
= fcntl(fd
, F_GETFL
, 0);
73 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
77 int rpc_get_fd(struct rpc_context
*rpc
)
82 int rpc_which_events(struct rpc_context
*rpc
)
84 int events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
86 if (rpc
->is_udp
!= 0) {
87 /* for udp sockets we only wait for pollin */
97 static int rpc_write_to_socket(struct rpc_context
*rpc
)
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
)
146 if (ioctlsocket(rpc
->fd
, FIONREAD
, &available
) != 0) {
148 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
150 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
154 if (available
== 0) {
155 rpc_set_error(rpc
, "Socket has been closed");
161 socklen_t socklen
= sizeof(rpc
->udp_src
);
163 buf
= malloc(available
);
165 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
168 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
170 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
173 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
174 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
182 /* read record marker, 4 bytes at the beginning of every pdu */
183 if (rpc
->inbuf
== NULL
) {
185 rpc
->inbuf
= malloc(rpc
->insize
);
186 if (rpc
->inbuf
== NULL
) {
187 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
191 if (rpc
->inpos
< 4) {
192 size
= 4 - rpc
->inpos
;
195 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
197 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
200 if (errno
== EINTR
) {
203 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
210 if (available
== 0) {
214 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
215 if (rpc
->insize
< pdu_size
) {
218 buf
= malloc(pdu_size
);
220 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
223 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
226 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
230 if (size
> rpc
->insize
- rpc
->inpos
) {
231 size
= rpc
->insize
- rpc
->inpos
;
235 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
237 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
240 if (errno
== EINTR
) {
243 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
249 if (rpc
->inpos
== rpc
->insize
) {
250 if (rpc_process_pdu(rpc
, rpc
->inbuf
, pdu_size
) != 0) {
251 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
265 int rpc_service(struct rpc_context
*rpc
, int revents
)
267 if (revents
& POLLERR
) {
269 socklen_t err_size
= sizeof(err
);
271 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
272 (char *)&err
, &err_size
) != 0 || err
!= 0) {
276 rpc_set_error(rpc
, "rpc_service: socket error "
280 rpc_set_error(rpc
, "rpc_service: POLLERR, "
281 "Unknown socket error.");
283 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 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
292 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
294 socklen_t err_size
= sizeof(err
);
296 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
297 (char *)&err
, &err_size
) != 0 || err
!= 0) {
301 rpc_set_error(rpc
, "rpc_service: socket error "
302 "%s(%d) while connecting.",
304 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
305 NULL
, rpc
->connect_data
);
309 rpc
->is_connected
= 1;
310 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
314 if (revents
& POLLIN
) {
315 if (rpc_read_from_socket(rpc
) != 0) {
316 rpc_disconnect_requeue(rpc
);
321 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
322 if (rpc_write_to_socket(rpc
) != 0) {
323 rpc_set_error(rpc
, "write to socket failed");
332 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
334 struct sockaddr_storage s
;
335 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&s
;
339 rpc_set_error(rpc
, "Trying to connect while already connected");
343 if (rpc
->is_udp
!= 0) {
344 rpc_set_error(rpc
, "Trying to connect on UDP socket");
348 sin
->sin_family
= AF_INET
;
349 sin
->sin_port
= htons(port
);
350 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
351 rpc_set_error(rpc
, "Not a valid server ip address");
355 switch (s
.ss_family
) {
357 socksize
= sizeof(struct sockaddr_in
);
358 #ifdef HAVE_SOCKADDR_LEN
359 sin
->sin_len
= socksize
;
361 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
366 rpc_set_error(rpc
, "Failed to open socket");
370 rpc
->connect_cb
= cb
;
371 rpc
->connect_data
= private_data
;
373 set_nonblocking(rpc
->fd
);
375 if (connect(rpc
->fd
, (struct sockaddr
*)&s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
376 rpc_set_error(rpc
, "connect() to server failed");
383 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
387 closesocket(rpc
->fd
);
394 rpc
->is_connected
= 0;
396 rpc_error_all_pdus(rpc
, error
);
401 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue */
402 static int rpc_disconnect_requeue(struct rpc_context
*rpc
)
408 closesocket(rpc
->fd
);
415 rpc
->is_connected
= 0;
417 /* socket is closed so we will not get any replies to any commands
418 * in flight. Move them all over from the waitpdu queue back to the out queue
420 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
421 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
422 SLIST_ADD(&rpc
->outqueue
, pdu
);
429 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
431 struct addrinfo
*ai
= NULL
;
434 if (rpc
->is_udp
== 0) {
435 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
439 sprintf(service
, "%d", port
);
440 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
441 rpc_set_error(rpc
, "Invalid address:%s. "
442 "Can not resolv into IPv4/v6 structure.");
446 switch(ai
->ai_family
) {
448 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
450 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
455 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
456 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
462 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
472 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
474 struct addrinfo
*ai
= NULL
;
477 if (rpc
->is_udp
== 0) {
478 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
482 sprintf(service
, "%d", port
);
483 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
484 rpc_set_error(rpc
, "Invalid address:%s. "
485 "Can not resolv into IPv4/v6 structure.");
491 rpc
->udp_dest
= NULL
;
493 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
494 if (rpc
->udp_dest
== NULL
) {
495 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
498 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
501 rpc
->is_broadcast
= is_broadcast
;
502 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
507 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
509 return (struct sockaddr
*)&rpc
->udp_src
;