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/>.
30 #include <arpa/inet.h>
31 #ifdef HAVE_SYS_FILIO_H
32 #include <sys/filio.h>
34 #include <sys/ioctl.h>
36 #include "libnfs-raw.h"
37 #include "libnfs-private.h"
40 static void set_nonblocking(int fd
)
43 v
= fcntl(fd
, F_GETFL
, 0);
44 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
47 int rpc_get_fd(struct rpc_context
*rpc
)
52 int rpc_which_events(struct rpc_context
*rpc
)
54 int events
= rpc
->is_connected
? POLLIN
: POLLOUT
;
62 static int rpc_write_to_socket(struct rpc_context
*rpc
)
70 rpc_set_error(rpc
, "trying to write but not connected");
74 while (rpc
->outqueue
!= NULL
) {
77 total
= rpc
->outqueue
->outdata
.size
;
79 count
= write(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
);
81 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
84 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
88 rpc
->outqueue
->written
+= count
;
89 if (rpc
->outqueue
->written
== total
) {
90 struct rpc_pdu
*pdu
= rpc
->outqueue
;
92 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
93 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
99 static int rpc_read_from_socket(struct rpc_context
*rpc
)
106 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
107 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
110 if (available
== 0) {
111 rpc_set_error(rpc
, "Socket has been closed");
115 /* read record marker, 4 bytes at the beginning of every pdu */
116 if (rpc
->inbuf
== NULL
) {
118 rpc
->inbuf
= malloc(rpc
->insize
);
119 if (rpc
->inbuf
== NULL
) {
120 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
124 if (rpc
->inpos
< 4) {
125 size
= 4 - rpc
->inpos
;
127 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
129 if (errno
== EINTR
) {
132 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
139 if (available
== 0) {
143 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
144 if (rpc
->insize
< pdu_size
) {
147 buf
= malloc(pdu_size
);
149 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
152 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
155 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
159 if (size
> rpc
->insize
- rpc
->inpos
) {
160 size
= rpc
->insize
- rpc
->inpos
;
163 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
165 if (errno
== EINTR
) {
168 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
174 if (rpc
->inpos
== rpc
->insize
) {
175 if (rpc_process_pdu(rpc
, rpc
->inbuf
, pdu_size
) != 0) {
176 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
190 int rpc_service(struct rpc_context
*rpc
, int revents
)
192 if (revents
& POLLERR
) {
194 socklen_t err_size
= sizeof(err
);
196 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
197 &err
, &err_size
) != 0 || err
!= 0) {
201 rpc_set_error(rpc
, "rpc_service: socket error "
205 rpc_set_error(rpc
, "rpc_service: POLLERR, "
206 "Unknown socket error.");
208 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
211 if (revents
& POLLHUP
) {
212 rpc_set_error(rpc
, "Socket failed with POLLHUP");
213 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
217 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
219 socklen_t err_size
= sizeof(err
);
221 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
222 &err
, &err_size
) != 0 || err
!= 0) {
226 rpc_set_error(rpc
, "rpc_service: socket error "
227 "%s(%d) while connecting.",
229 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
230 NULL
, rpc
->connect_data
);
234 rpc
->is_connected
= 1;
235 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
239 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
240 if (rpc_write_to_socket(rpc
) != 0) {
241 rpc_set_error(rpc
, "write to socket failed");
246 if (revents
& POLLIN
) {
247 if (rpc_read_from_socket(rpc
) != 0) {
248 rpc_disconnect(rpc
, rpc_get_error(rpc
));
257 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
259 struct sockaddr_storage s
;
260 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&s
;
264 rpc_set_error(rpc
, "Trying to connect while already connected");
268 sin
->sin_family
= AF_INET
;
269 sin
->sin_port
= htons(port
);
270 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
271 rpc_set_error(rpc
, "Not a valid server ip address");
275 switch (s
.ss_family
) {
277 socksize
= sizeof(struct sockaddr_in
);
278 #ifdef HAVE_SOCK_SIN_LEN
279 sin
->sin_len
= socksize
;
281 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, 0);
286 rpc_set_error(rpc
, "Failed to open socket");
290 rpc
->connect_cb
= cb
;
291 rpc
->connect_data
= private_data
;
293 set_nonblocking(rpc
->fd
);
295 if (connect(rpc
->fd
, (struct sockaddr
*)&s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
296 rpc_set_error(rpc
, "connect() to server failed");
303 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
310 rpc
->is_connected
= 0;
312 rpc_error_all_pdus(rpc
, error
);