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/>.
25 #include <arpa/inet.h>
26 #include <sys/ioctl.h>
28 #include "libnfs-raw.h"
29 #include "libnfs-private.h"
32 static void set_nonblocking(int fd
)
35 v
= fcntl(fd
, F_GETFL
, 0);
36 fcntl(fd
, F_SETFL
, v
| O_NONBLOCK
);
39 int rpc_get_fd(struct rpc_context
*rpc
)
44 int rpc_which_events(struct rpc_context
*rpc
)
48 if (rpc
->is_connected
== 0) {
58 static int rpc_write_to_socket(struct rpc_context
*rpc
)
63 printf("trying to write to socket for NULL context\n");
67 printf("trying to write but not connected\n");
71 while (rpc
->outqueue
!= NULL
) {
74 total
= rpc
->outqueue
->outdata
.size
;
76 count
= write(rpc
->fd
, rpc
->outqueue
->outdata
.data
+ rpc
->outqueue
->written
, total
- rpc
->outqueue
->written
);
78 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
79 printf("socket would block, return from write to socket\n");
82 printf("Error when writing to socket :%s(%d)\n", strerror(errno
), errno
);
86 rpc
->outqueue
->written
+= count
;
87 if (rpc
->outqueue
->written
== total
) {
88 struct rpc_pdu
*pdu
= rpc
->outqueue
;
90 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
91 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
97 static int rpc_read_from_socket(struct rpc_context
*rpc
)
104 if (ioctl(rpc
->fd
, FIONREAD
, &available
) != 0) {
105 rpc_set_error(rpc
, "Ioctl FIONREAD returned error : %d. Closing socket.", errno
);
108 if (available
== 0) {
109 rpc_set_error(rpc
, "Socket has been closed");
112 size
= rpc
->insize
- rpc
->inpos
+ available
;
115 rpc_set_error(rpc
, "Out of memory: failed to allocate %d bytes for input buffer. Closing socket.", size
);
118 if (rpc
->insize
> rpc
->inpos
) {
119 memcpy(buf
, rpc
->inbuf
+ rpc
->inpos
, rpc
->insize
- rpc
->inpos
);
120 rpc
->insize
-= rpc
->inpos
;
124 count
= read(rpc
->fd
, buf
+ rpc
->insize
, available
);
126 if (errno
== EINTR
) {
131 rpc_set_error(rpc
, "Read from socket failed, errno:%d. Closing socket.", errno
);
137 if (rpc
->inbuf
!= NULL
) {
140 rpc
->inbuf
= (char *)buf
;
141 rpc
->insize
+= count
;
144 if (rpc
->insize
- rpc
->inpos
< 4) {
147 count
= rpc_get_pdu_size(rpc
->inbuf
+ rpc
->inpos
);
148 if (rpc
->insize
+ rpc
->inpos
< count
) {
151 if (rpc_process_pdu(rpc
, rpc
->inbuf
+ rpc
->inpos
, count
) != 0) {
152 rpc_set_error(rpc
, "Invalid/garbage pdu received from server. Closing socket");
156 if (rpc
->inpos
== rpc
->insize
) {
168 int rpc_service(struct rpc_context
*rpc
, int revents
)
170 if (revents
& POLLERR
) {
171 printf("rpc_service: POLLERR, socket error\n");
172 if (rpc
->is_connected
== 0) {
173 rpc_set_error(rpc
, "Failed to connect to server socket.");
175 rpc_set_error(rpc
, "Socket closed with POLLERR");
177 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
180 if (revents
& POLLHUP
) {
181 printf("rpc_service: POLLHUP, socket error\n");
182 rpc_set_error(rpc
, "Socket failed with POLLHUP");
183 rpc
->connect_cb(rpc
, RPC_STATUS_ERROR
, rpc
->error_string
, rpc
->connect_data
);
187 if (rpc
->is_connected
== 0 && rpc
->fd
!= -1 && revents
&POLLOUT
) {
188 rpc
->is_connected
= 1;
189 rpc
->connect_cb(rpc
, RPC_STATUS_SUCCESS
, NULL
, rpc
->connect_data
);
193 if (revents
& POLLOUT
&& rpc
->outqueue
!= NULL
) {
194 if (rpc_write_to_socket(rpc
) != 0) {
195 printf("write to socket failed\n");
200 if (revents
& POLLIN
) {
201 if (rpc_read_from_socket(rpc
) != 0) {
202 rpc_disconnect(rpc
, rpc_get_error(rpc
));
211 int rpc_connect_async(struct rpc_context
*rpc
, const char *server
, int port
, rpc_cb cb
, void *private_data
)
213 struct sockaddr_storage s
;
214 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&s
;
218 rpc_set_error(rpc
, "Trying to connect while already connected");
219 printf("%s\n", rpc
->error_string
);
223 sin
->sin_family
= AF_INET
;
224 sin
->sin_port
= htons(port
);
225 if (inet_pton(AF_INET
, server
, &sin
->sin_addr
) != 1) {
226 rpc_set_error(rpc
, "Not a valid server ip address");
227 printf("%s\n", rpc
->error_string
);
231 switch (s
.ss_family
) {
233 rpc
->fd
= socket(AF_INET
, SOCK_STREAM
, 0);
234 socksize
= sizeof(struct sockaddr_in
);
239 rpc_set_error(rpc
, "Failed to open socket");
240 printf("%s\n", rpc
->error_string
);
244 rpc
->connect_cb
= cb
;
245 rpc
->connect_data
= private_data
;
247 set_nonblocking(rpc
->fd
);
249 if (connect(rpc
->fd
, (struct sockaddr
*)&s
, socksize
) != 0 && errno
!= EINPROGRESS
) {
250 rpc_set_error(rpc
, "connect() to server failed");
251 printf("%s\n", rpc
->error_string
);
258 int rpc_disconnect(struct rpc_context
*rpc
, char *error
)
265 rpc
->is_connected
= 0;
267 rpc_error_all_pdus(rpc
, error
);