2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
26 #include <arpa/inet.h>
27 #include <sys/ioctl.h>
29 #include "libnfs-raw.h"
30 #include "libnfs-private.h"
31 #include "dlinklist.h"
33 static void set_nonblocking(int fd)
36 v = fcntl(fd, F_GETFL, 0);
37 fcntl(fd, F_SETFL, v | O_NONBLOCK);
40 int rpc_get_fd(struct rpc_context *rpc)
45 int rpc_which_events(struct rpc_context *rpc)
49 if (rpc->is_connected == 0) {
59 static int rpc_write_to_socket(struct rpc_context *rpc)
64 printf("trying to write to socket for NULL context\n");
68 printf("trying to write but not connected\n");
72 while (rpc->outqueue != NULL) {
75 total = rpc->outqueue->outdata.size;
77 count = write(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written);
79 if (errno == EAGAIN || errno == EWOULDBLOCK) {
80 printf("socket would block, return from write to socket\n");
83 printf("Error when writing to socket :%s(%d)\n", strerror(errno), errno);
87 rpc->outqueue->written += count;
88 if (rpc->outqueue->written == total) {
89 struct rpc_pdu *pdu = rpc->outqueue;
91 DLIST_REMOVE(rpc->outqueue, pdu);
92 DLIST_ADD_END(rpc->waitpdu, pdu, NULL);
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");
113 size = rpc->insize - rpc->inpos + available;
116 rpc_set_error(rpc, "Out of memory: failed to allocate %d bytes for input buffer. Closing socket.", size);
119 if (rpc->insize > rpc->inpos) {
120 memcpy(buf, rpc->inbuf + rpc->inpos, rpc->insize - rpc->inpos);
121 rpc->insize -= rpc->inpos;
125 count = read(rpc->fd, buf + rpc->insize, available);
127 if (errno == EINTR) {
132 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
138 if (rpc->inbuf != NULL) {
141 rpc->inbuf = (char *)buf;
142 rpc->insize += count;
145 if (rpc->insize - rpc->inpos < 4) {
148 count = rpc_get_pdu_size(rpc->inbuf + rpc->inpos);
149 if (rpc->insize + rpc->inpos < count) {
152 if (rpc_process_pdu(rpc, rpc->inbuf + rpc->inpos, count) != 0) {
153 rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket");
157 if (rpc->inpos == rpc->insize) {
169 int rpc_service(struct rpc_context *rpc, int revents)
171 if (revents & POLLERR) {
172 printf("rpc_service: POLLERR, socket error\n");
173 if (rpc->is_connected == 0) {
174 rpc_set_error(rpc, "Failed to connect to server socket.");
176 rpc_set_error(rpc, "Socket closed with POLLERR");
178 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
181 if (revents & POLLHUP) {
182 printf("rpc_service: POLLHUP, socket error\n");
183 rpc_set_error(rpc, "Socket failed with POLLHUP");
184 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
188 if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) {
189 rpc->is_connected = 1;
190 rpc->connect_cb(rpc, RPC_STATUS_SUCCESS, NULL, rpc->connect_data);
194 if (revents & POLLOUT && rpc->outqueue != NULL) {
195 if (rpc_write_to_socket(rpc) != 0) {
196 printf("write to socket failed\n");
201 if (revents & POLLIN) {
202 if (rpc_read_from_socket(rpc) != 0) {
203 rpc_disconnect(rpc, rpc_get_error(rpc));
212 int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, int use_privileged_port, rpc_cb cb, void *private_data)
214 struct sockaddr_storage s;
215 struct sockaddr_in *sin = (struct sockaddr_in *)&s;
219 rpc_set_error(rpc, "Trying to connect while already connected");
220 printf("%s\n", rpc->error_string);
224 sin->sin_family = AF_INET;
225 sin->sin_port = htons(port);
226 if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) {
227 rpc_set_error(rpc, "Not a valid server ip address");
228 printf("%s\n", rpc->error_string);
232 switch (s.ss_family) {
234 rpc->fd = socket(AF_INET, SOCK_STREAM, 0);
235 socksize = sizeof(struct sockaddr_in);
240 rpc_set_error(rpc, "Failed to open socket");
241 printf("%s\n", rpc->error_string);
245 /* if we are root, try to find a privileged port to use (512 - 1023) */
246 if (geteuid() == 0 && use_privileged_port != 0) {
247 struct sockaddr_storage ls;
249 static int local_port = 0;
251 if (local_port == 0) {
252 srandom(getpid() ^ time(NULL));
253 local_port = random()%512 + 512;
258 if (local_port >= 1024) {
261 switch (s.ss_family) {
263 bzero(&ls, socksize);
264 ((struct sockaddr_in *)&ls)->sin_family = AF_INET;
265 ((struct sockaddr_in *)&ls)->sin_addr.s_addr = INADDR_ANY;
266 ((struct sockaddr_in *)&ls)->sin_port = htons(local_port++);
270 ret = bind(rpc->fd, (struct sockaddr *)&ls, socksize);
271 } while (ret != 0 && count < 50);
274 rpc->connect_cb = cb;
275 rpc->connect_data = private_data;
277 set_nonblocking(rpc->fd);
279 if (connect(rpc->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS) {
280 rpc_set_error(rpc, "connect() to server failed");
281 printf("%s\n", rpc->error_string);
288 int rpc_disconnect(struct rpc_context *rpc, char *error)
295 rpc->is_connected = 0;
297 rpc_error_all_pdus(rpc, error);