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
;
50 if (rpc
->is_udp
!= 0) {
51 /* for udp sockets we only wait for pollin */
61 static int rpc_write_to_socket(struct rpc_context
*rpc
)
69 rpc_set_error(rpc
, "trying to write but not connected");
73 while (rpc
->outqueue
!= NULL
) {
76 total
= rpc
->outqueue
->outdata
.size
;
78 count
= write(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
);
80 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
83 rpc_set_error(rpc
, "Error when writing to socket :%s(%d)", strerror(errno
), errno
);
87 rpc
->outqueue
->written
+= count
;
88 if (rpc
->outqueue
->written
== total
) {
89 struct rpc_pdu
*pdu
= rpc
->outqueue
;
91 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
92 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
98 static int rpc_read_from_socket(struct rpc_context
*rpc
)
105 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
106 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
109 if (available
== 0) {
110 rpc_set_error(rpc
, "Socket has been closed");
114 /* read record marker, 4 bytes at the beginning of every pdu */
115 if (rpc
->inbuf
== NULL
) {
117 rpc
->inbuf
= malloc(rpc
->insize
);
118 if (rpc
->inbuf
== NULL
) {
119 rpc_set_error(rpc
, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno
);
123 if (rpc
->inpos
< 4) {
124 size
= 4 - rpc
->inpos
;
126 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
128 if (errno
== EINTR
) {
131 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
138 if (available
== 0) {
142 pdu_size
= rpc_get_pdu_size(rpc
->inbuf
);
143 if (rpc
->insize
< pdu_size
) {
146 buf
= malloc(pdu_size
);
148 rpc_set_error(rpc
, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size
, errno
);
151 memcpy(buf
, rpc
->inbuf
, rpc
->insize
);
154 rpc
->insize
= rpc_get_pdu_size(rpc
->inbuf
);
158 if (size
> rpc
->insize
- rpc
->inpos
) {
159 size
= rpc
->insize
- rpc
->inpos
;
162 count
= read(rpc
->fd
, rpc
->inbuf
+ rpc
->inpos
, size
);
164 if (errno
== EINTR
) {
167 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
173 if (rpc
->inpos
== rpc
->insize
) {
174 if (rpc_process_pdu(rpc
, rpc
->inbuf
, pdu_size
) != 0) {
175 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
189 int rpc_service(struct rpc_context
*rpc
, int revents
)
191 if (revents
& POLLERR
) {
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 "
204 rpc_set_error(rpc
, "rpc_service: POLLERR, "
205 "Unknown socket error.");
207 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
210 if (revents
& POLLHUP
) {
211 rpc_set_error(rpc
, "Socket failed with POLLHUP");
212 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
216 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
218 socklen_t err_size
= sizeof(err
);
220 if (getsockopt(rpc
->fd
, SOL_SOCKET
, SO_ERROR
,
221 &err
, &err_size
) != 0 || err
!= 0) {
225 rpc_set_error(rpc
, "rpc_service: socket error "
226 "%s(%d) while connecting.",
228 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
,
229 NULL
, rpc
->connect_data
);
233 rpc
->is_connected
= 1;
234 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
238 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
239 if (rpc_write_to_socket(rpc
) != 0) {
240 rpc_set_error(rpc
, "write to socket failed");
245 if (revents
& POLLIN
) {
246 if (rpc_read_from_socket(rpc
) != 0) {
247 rpc_disconnect(rpc
, rpc_get_error(rpc
));
256 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
258 struct sockaddr_storage s
;
259 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&s
;
263 rpc_set_error(rpc
, "Trying to connect while already connected");
267 sin
->sin_family
= AF_INET
;
268 sin
->sin_port
= htons(port
);
269 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
270 rpc_set_error(rpc
, "Not a valid server ip address");
274 switch (s
.ss_family
) {
276 socksize
= sizeof(struct sockaddr_in
);
277 #ifdef HAVE_SOCK_SIN_LEN
278 sin
->sin_len
= socksize
;
280 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, 0);
285 rpc_set_error(rpc
, "Failed to open socket");
289 rpc
->connect_cb
= cb
;
290 rpc
->connect_data
= private_data
;
292 set_nonblocking(rpc
->fd
);
294 if (connect(rpc
->fd
, (struct sockaddr
*)&s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
295 rpc_set_error(rpc
, "connect() to server failed");
302 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
309 rpc
->is_connected
= 0;
311 rpc_error_all_pdus(rpc
, error
);