for UDP sockets, we ONLY wait for pollin events
[deb_libnfs.git] / lib / socket.c
1 /*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <poll.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <rpc/rpc.h>
26 #include <rpc/xdr.h>
27 #include <arpa/inet.h>
28 #include <sys/ioctl.h>
29 #include "libnfs.h"
30 #include "libnfs-raw.h"
31 #include "libnfs-private.h"
32 #include "slist.h"
33
34 static void set_nonblocking(int fd)
35 {
36 unsigned v;
37 v = fcntl(fd, F_GETFL, 0);
38 fcntl(fd, F_SETFL, v | O_NONBLOCK);
39 }
40
41 int rpc_get_fd(struct rpc_context *rpc)
42 {
43 return rpc->fd;
44 }
45
46 int rpc_which_events(struct rpc_context *rpc)
47 {
48 int events = rpc->is_connected ? POLLIN : POLLOUT;
49
50 if (rpc->is_udp != 0) {
51 /* for udp sockets we only wait for pollin */
52 return POLLIN;
53 }
54
55 if (rpc->outqueue) {
56 events |= POLLOUT;
57 }
58 return events;
59 }
60
61 static int rpc_write_to_socket(struct rpc_context *rpc)
62 {
63 ssize_t count;
64
65 if (rpc == NULL) {
66 return -1;
67 }
68 if (rpc->fd == -1) {
69 rpc_set_error(rpc, "trying to write but not connected");
70 return -1;
71 }
72
73 while (rpc->outqueue != NULL) {
74 ssize_t total;
75
76 total = rpc->outqueue->outdata.size;
77
78 count = write(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written);
79 if (count == -1) {
80 if (errno == EAGAIN || errno == EWOULDBLOCK) {
81 return 0;
82 }
83 rpc_set_error(rpc, "Error when writing to socket :%s(%d)", strerror(errno), errno);
84 return -1;
85 }
86
87 rpc->outqueue->written += count;
88 if (rpc->outqueue->written == total) {
89 struct rpc_pdu *pdu = rpc->outqueue;
90
91 SLIST_REMOVE(&rpc->outqueue, pdu);
92 SLIST_ADD_END(&rpc->waitpdu, pdu);
93 }
94 }
95 return 0;
96 }
97
98 static int rpc_read_from_socket(struct rpc_context *rpc)
99 {
100 int available;
101 int size;
102 int pdu_size;
103 ssize_t count;
104
105 if (ioctl(rpc->fd, FIONREAD, &available) != 0) {
106 rpc_set_error(rpc, "Ioctl FIONREAD returned error : %d. Closing socket.", errno);
107 return -1;
108 }
109 if (available == 0) {
110 rpc_set_error(rpc, "Socket has been closed");
111 return -1;
112 }
113
114 /* read record marker, 4 bytes at the beginning of every pdu */
115 if (rpc->inbuf == NULL) {
116 rpc->insize = 4;
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);
120 return -1;
121 }
122 }
123 if (rpc->inpos < 4) {
124 size = 4 - rpc->inpos;
125
126 count = read(rpc->fd, rpc->inbuf + rpc->inpos, size);
127 if (count == -1) {
128 if (errno == EINTR) {
129 return 0;
130 }
131 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
132 return -1;
133 }
134 available -= count;
135 rpc->inpos += count;
136 }
137
138 if (available == 0) {
139 return 0;
140 }
141
142 pdu_size = rpc_get_pdu_size(rpc->inbuf);
143 if (rpc->insize < pdu_size) {
144 unsigned char *buf;
145
146 buf = malloc(pdu_size);
147 if (buf == NULL) {
148 rpc_set_error(rpc, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size, errno);
149 return -1;
150 }
151 memcpy(buf, rpc->inbuf, rpc->insize);
152 free(rpc->inbuf);
153 rpc->inbuf = buf;
154 rpc->insize = rpc_get_pdu_size(rpc->inbuf);
155 }
156
157 size = available;
158 if (size > rpc->insize - rpc->inpos) {
159 size = rpc->insize - rpc->inpos;
160 }
161
162 count = read(rpc->fd, rpc->inbuf + rpc->inpos, size);
163 if (count == -1) {
164 if (errno == EINTR) {
165 return 0;
166 }
167 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
168 return -1;
169 }
170 available -= count;
171 rpc->inpos += count;
172
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");
176 return -1;
177 }
178 free(rpc->inbuf);
179 rpc->inbuf = NULL;
180 rpc->insize = 0;
181 rpc->inpos = 0;
182 }
183
184 return 0;
185 }
186
187
188
189 int rpc_service(struct rpc_context *rpc, int revents)
190 {
191 if (revents & POLLERR) {
192 int err = 0;
193 socklen_t err_size = sizeof(err);
194
195 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
196 &err, &err_size) != 0 || err != 0) {
197 if (err == 0) {
198 err = errno;
199 }
200 rpc_set_error(rpc, "rpc_service: socket error "
201 "%s(%d).",
202 strerror(err), err);
203 } else {
204 rpc_set_error(rpc, "rpc_service: POLLERR, "
205 "Unknown socket error.");
206 }
207 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
208 return -1;
209 }
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);
213 return -1;
214 }
215
216 if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) {
217 int err = 0;
218 socklen_t err_size = sizeof(err);
219
220 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
221 &err, &err_size) != 0 || err != 0) {
222 if (err == 0) {
223 err = errno;
224 }
225 rpc_set_error(rpc, "rpc_service: socket error "
226 "%s(%d) while connecting.",
227 strerror(err), err);
228 rpc->connect_cb(rpc, RPC_STATUS_ERROR,
229 NULL, rpc->connect_data);
230 return -1;
231 }
232
233 rpc->is_connected = 1;
234 rpc->connect_cb(rpc, RPC_STATUS_SUCCESS, NULL, rpc->connect_data);
235 return 0;
236 }
237
238 if (revents & POLLOUT && rpc->outqueue != NULL) {
239 if (rpc_write_to_socket(rpc) != 0) {
240 rpc_set_error(rpc, "write to socket failed");
241 return -1;
242 }
243 }
244
245 if (revents & POLLIN) {
246 if (rpc_read_from_socket(rpc) != 0) {
247 rpc_disconnect(rpc, rpc_get_error(rpc));
248 return -1;
249 }
250 }
251
252 return 0;
253 }
254
255
256 int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data)
257 {
258 struct sockaddr_storage s;
259 struct sockaddr_in *sin = (struct sockaddr_in *)&s;
260 int socksize;
261
262 if (rpc->fd != -1) {
263 rpc_set_error(rpc, "Trying to connect while already connected");
264 return -1;
265 }
266
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");
271 return -1;
272 }
273
274 switch (s.ss_family) {
275 case AF_INET:
276 socksize = sizeof(struct sockaddr_in);
277 #ifdef HAVE_SOCK_SIN_LEN
278 sin->sin_len = socksize;
279 #endif
280 rpc->fd = socket(AF_INET, SOCK_STREAM, 0);
281 break;
282 }
283
284 if (rpc->fd == -1) {
285 rpc_set_error(rpc, "Failed to open socket");
286 return -1;
287 }
288
289 rpc->connect_cb = cb;
290 rpc->connect_data = private_data;
291
292 set_nonblocking(rpc->fd);
293
294 if (connect(rpc->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS) {
295 rpc_set_error(rpc, "connect() to server failed");
296 return -1;
297 }
298
299 return 0;
300 }
301
302 int rpc_disconnect(struct rpc_context *rpc, char *error)
303 {
304 if (rpc->fd != -1) {
305 close(rpc->fd);
306 }
307 rpc->fd = -1;
308
309 rpc->is_connected = 0;
310
311 rpc_error_all_pdus(rpc, error);
312
313 return 0;
314 }