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/>.
22 #define ssize_t SSIZE_T
23 #define MSG_DONTWAIT 0
27 #include <arpa/inet.h>
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
43 #ifdef HAVE_SYS_FILIO_H
44 #include <sys/filio.h>
46 #ifdef HAVE_SYS_SOCKIO_H
47 #include <sys/sockio.h>
49 #include <sys/types.h>
51 #include "libnfs-raw.h"
52 #include "libnfs-private.h"
55 static int rpc_disconnect_requeue(struct rpc_context
*rpc
);
57 static void set_nonblocking(int fd
)
62 v
= fcntl(fd
, F_GETFL
, 0);
63 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
67 int rpc_get_fd(struct rpc_context
*rpc
)
72 int rpc_which_events(struct rpc_context
*rpc
)
74 int events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
76 if (rpc
->is_udp
!= 0) {
77 /* for udp sockets we only wait for pollin */
87 static int rpc_write_to_socket(struct rpc_context
*rpc
)
95 rpc_set_error(rpc
, "trying to write but not connected");
99 while (rpc
->outqueue
!= NULL
) {
102 total
= rpc
->outqueue
->outdata
.size
;
105 count
= send(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
, 0);
107 count
= write(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
);
110 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
113 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
117 rpc
->outqueue
->written
+= count
;
118 if (rpc
->outqueue
->written
== total
) {
119 struct rpc_pdu
*pdu
= rpc
->outqueue
;
121 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
122 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
128 static int rpc_read_from_socket(struct rpc_context
*rpc
)
136 if (ioctlsocket(rpc
->fd
, FIONREAD
, &available
) != 0) {
138 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
140 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
143 if (available
== 0) {
144 rpc_set_error(rpc
, "Socket has been closed");
150 socklen_t socklen
= sizeof(rpc
->udp_src
);
152 buf
= malloc(available
);
154 rpc_set_error(rpc
, "Failed to malloc buffer for recvfrom");
157 count
= recvfrom(rpc
->fd
, buf
, available
, MSG_DONTWAIT
, (struct sockaddr
*)&rpc
->udp_src
, &socklen
);
159 rpc_set_error(rpc
, "Failed recvfrom: %s", strerror(errno
));
162 if (rpc_process_pdu(rpc
, buf
, count
) != 0) {
163 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Ignoring PDU");
171 /* read record marker, 4 bytes at the beginning of every pdu */
172 if (rpc
->inbuf
== NULL
) {
174 rpc
->inbuf
= malloc(rpc
->insize
);
175 if (rpc
->inbuf
== NULL
) {
176 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
180 if (rpc
->inpos
< 4) {
181 size
= 4 - rpc
->inpos
;
184 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
186 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
189 if (errno
== EINTR
) {
192 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
199 if (available
== 0) {
203 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
204 if (rpc
->insize
< pdu_size
) {
207 buf
= malloc(pdu_size
);
209 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
212 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
215 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
219 if (size
> rpc
->insize
- rpc
->inpos
) {
220 size
= rpc
->insize
- rpc
->inpos
;
224 count
= recv(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
, 0);
226 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
229 if (errno
== EINTR
) {
232 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
238 if (rpc
->inpos
== rpc
->insize
) {
239 if (rpc_process_pdu(rpc
, rpc
->inbuf
, pdu_size
) != 0) {
240 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
254 int rpc_service(struct rpc_context
*rpc
, int revents
)
256 if (revents
& POLLERR
) {
258 socklen_t err_size
= sizeof(err
);
260 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
261 (char *)&err
, &err_size
) != 0 || err
!= 0) {
265 rpc_set_error(rpc
, "rpc_service: socket error "
269 rpc_set_error(rpc
, "rpc_service: POLLERR, "
270 "Unknown socket error.");
272 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
275 if (revents
& POLLHUP
) {
276 rpc_set_error(rpc
, "Socket failed with POLLHUP");
277 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
281 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
283 socklen_t err_size
= sizeof(err
);
285 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
286 (char *)&err
, &err_size
) != 0 || err
!= 0) {
290 rpc_set_error(rpc
, "rpc_service: socket error "
291 "%s(%d) while connecting.",
293 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
294 NULL
, rpc
->connect_data
);
298 rpc
->is_connected
= 1;
299 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
303 if (revents
& POLLIN
) {
304 if (rpc_read_from_socket(rpc
) != 0) {
305 rpc_disconnect_requeue(rpc
);
310 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
311 if (rpc_write_to_socket(rpc
) != 0) {
312 rpc_set_error(rpc
, "write to socket failed");
321 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
323 struct sockaddr_storage s
;
324 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&s
;
328 rpc_set_error(rpc
, "Trying to connect while already connected");
332 if (rpc
->is_udp
!= 0) {
333 rpc_set_error(rpc
, "Trying to connect on UDP socket");
337 sin
->sin_family
= AF_INET
;
338 sin
->sin_port
= htons(port
);
339 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
340 rpc_set_error(rpc
, "Not a valid server ip address");
344 switch (s
.ss_family
) {
346 socksize
= sizeof(struct sockaddr_in
);
347 #ifdef HAVE_SOCKADDR_LEN
348 sin
->sin_len
= socksize
;
350 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
355 rpc_set_error(rpc
, "Failed to open socket");
359 rpc
->connect_cb
= cb
;
360 rpc
->connect_data
= private_data
;
362 set_nonblocking(rpc
->fd
);
364 if (connect(rpc
->fd
, (struct sockaddr
*)&s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
365 rpc_set_error(rpc
, "connect() to server failed");
372 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
376 closesocket(rpc
->fd
);
383 rpc
->is_connected
= 0;
385 rpc_error_all_pdus(rpc
, error
);
390 /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue */
391 static int rpc_disconnect_requeue(struct rpc_context
*rpc
)
397 closesocket(rpc
->fd
);
404 rpc
->is_connected
= 0;
406 /* socket is closed so we will not get any replies to any commands
407 * in flight. Move them all over from the waitpdu queue back to the out queue
409 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
410 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
411 SLIST_ADD(&rpc
->outqueue
, pdu
);
418 int rpc_bind_udp(struct rpc_context
*rpc
, char *addr
, int port
)
420 struct addrinfo
*ai
= NULL
;
423 if (rpc
->is_udp
== 0) {
424 rpc_set_error(rpc
, "Cant not bind UDP. Not UDP context");
428 sprintf(service
, "%d", port
);
429 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
430 rpc_set_error(rpc
, "Invalid address:%s. "
431 "Can not resolv into IPv4/v6 structure.");
435 switch(ai
->ai_family
) {
437 rpc
->fd
= socket(ai
->ai_family
, SOCK_DGRAM
, 0);
439 rpc_set_error(rpc
, "Failed to create UDP socket: %s", strerror(errno
));
444 if (bind(rpc
->fd
, (struct sockaddr
*)ai
->ai_addr
, sizeof(struct sockaddr_in
)) != 0) {
445 rpc_set_error(rpc
, "Failed to bind to UDP socket: %s",strerror(errno
));
451 rpc_set_error(rpc
, "Can not handle UPD sockets of family %d yet", ai
->ai_family
);
461 int rpc_set_udp_destination(struct rpc_context
*rpc
, char *addr
, int port
, int is_broadcast
)
463 struct addrinfo
*ai
= NULL
;
466 if (rpc
->is_udp
== 0) {
467 rpc_set_error(rpc
, "Can not set destination sockaddr. Not UDP context");
471 sprintf(service
, "%d", port
);
472 if (getaddrinfo(addr
, service
, NULL
, &ai
) != 0) {
473 rpc_set_error(rpc
, "Invalid address:%s. "
474 "Can not resolv into IPv4/v6 structure.");
480 rpc
->udp_dest
= NULL
;
482 rpc
->udp_dest
= malloc(ai
->ai_addrlen
);
483 if (rpc
->udp_dest
== NULL
) {
484 rpc_set_error(rpc
, "Out of memory. Failed to allocate sockaddr structure");
487 memcpy(rpc
->udp_dest
, ai
->ai_addr
, ai
->ai_addrlen
);
490 rpc
->is_broadcast
= is_broadcast
;
491 setsockopt(rpc
->fd
, SOL_SOCKET
, SO_BROADCAST
, (char *)&is_broadcast
, sizeof(is_broadcast
));
496 struct sockaddr
*rpc_get_recv_sockaddr(struct rpc_context
*rpc
)
498 return (struct sockaddr
*)&rpc
->udp_src
;