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/>.
27 #include <arpa/inet.h>
28 #include <sys/ioctl.h>
30 #include "libnfs-raw.h"
31 #include "libnfs-private.h"
34 static void set_nonblocking(int fd
)
37 v
= fcntl(fd
, F_GETFL
, 0);
38 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
41 int rpc_get_fd(struct rpc_context
*rpc
)
46 int rpc_which_events(struct rpc_context
*rpc
)
48 int events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
56 static int rpc_write_to_socket(struct rpc_context
*rpc
)
64 rpc_set_error(rpc
, "trying to write but not connected");
68 while (rpc
->outqueue
!= NULL
) {
71 total
= rpc
->outqueue
->outdata
.size
;
73 count
= write(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
);
75 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
78 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
82 rpc
->outqueue
->written
+= count
;
83 if (rpc
->outqueue
->written
== total
) {
84 struct rpc_pdu
*pdu
= rpc
->outqueue
;
86 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
87 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
93 static int rpc_read_from_socket(struct rpc_context
*rpc
)
100 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
101 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
104 if (available
== 0) {
105 rpc_set_error(rpc
, "Socket has been closed");
108 size
= rpc
->insize
- rpc
->inpos
+ available
;
111 rpc_set_error(rpc
, "Out of memory: failed to allocate %d bytes for input buffer. Closing socket.", size
);
114 if (rpc
->insize
> rpc
->inpos
) {
115 memcpy(buf
, rpc
->inbuf
+ rpc
->inpos
, rpc
->insize
- rpc
->inpos
);
116 rpc
->insize
-= rpc
->inpos
;
120 count
= read(rpc
->fd
, buf
+ rpc
->insize
, available
);
122 if (errno
== EINTR
) {
127 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
133 if (rpc
->inbuf
!= NULL
) {
136 rpc
->inbuf
= (char *)buf
;
137 rpc
->insize
+= count
;
140 if (rpc
->insize
- rpc
->inpos
< 4) {
143 count
= rpc_get_pdu_size(rpc
->inbuf
+ rpc
->inpos
);
144 if (rpc
->insize
+ rpc
->inpos
< count
) {
147 if (rpc_process_pdu(rpc
, rpc
->inbuf
+ rpc
->inpos
, count
) != 0) {
148 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
152 if (rpc
->inpos
== rpc
->insize
) {
164 int rpc_service(struct rpc_context
*rpc
, int revents
)
166 if (revents
& POLLERR
) {
168 socklen_t err_size
= sizeof(err
);
170 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
171 &err
, &err_size
) != 0 || err
!= 0) {
175 rpc_set_error(rpc
, "rpc_service: socket error "
179 rpc_set_error(rpc
, "rpc_service: POLLERR, "
180 "Unknown socket error.");
182 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
185 if (revents
& POLLHUP
) {
186 rpc_set_error(rpc
, "Socket failed with POLLHUP");
187 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
191 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
193 socklen_t err_size
= sizeof(err
);
195 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
196 &err
, &err_size
) != 0 || err
!= 0) {
200 rpc_set_error(rpc
, "rpc_service: socket error "
201 "%s(%d) while connecting.",
203 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
204 NULL
, rpc
->connect_data
);
208 rpc
->is_connected
= 1;
209 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
213 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
214 if (rpc_write_to_socket(rpc
) != 0) {
215 rpc_set_error(rpc
, "write to socket failed");
220 if (revents
& POLLIN
) {
221 if (rpc_read_from_socket(rpc
) != 0) {
222 rpc_disconnect(rpc
, rpc_get_error(rpc
));
231 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
233 struct sockaddr_storage s
;
234 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&s
;
238 rpc_set_error(rpc
, "Trying to connect while already connected");
242 sin
->sin_family
= AF_INET
;
243 sin
->sin_port
= htons(port
);
244 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
245 rpc_set_error(rpc
, "Not a valid server ip address");
249 switch (s
.ss_family
) {
251 socksize
= sizeof(struct sockaddr_in
);
252 #ifdef HAVE_SOCK_SIN_LEN
253 sin
->sin_len
= socksize
;
255 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, 0);
260 rpc_set_error(rpc
, "Failed to open socket");
264 rpc
->connect_cb
= cb
;
265 rpc
->connect_data
= private_data
;
267 set_nonblocking(rpc
->fd
);
269 if (connect(rpc
->fd
, (struct sockaddr
*)&s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
270 rpc_set_error(rpc
, "connect() to server failed");
277 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
284 rpc
->is_connected
= 0;
286 rpc_error_all_pdus(rpc
, error
);