Commit | Line | Data |
---|---|---|
84004dbf RS |
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 | ||
6874f61e RS |
18 | #if defined(WIN32) |
19 | #include <winsock2.h> | |
20 | #include <ws2tcpip.h> | |
21 | #include <basetsd.h> | |
22 | #define ssize_t SSIZE_T | |
23 | #define MSG_DONTWAIT 0 | |
24 | #else | |
25 | #include <unistd.h> | |
26 | #include <poll.h> | |
27 | #include <arpa/inet.h> | |
28 | #include <sys/ioctl.h> | |
29 | #include <sys/socket.h> | |
30 | #include <netdb.h> | |
31 | #endif | |
32 | ||
fc01d2a9 TN |
33 | #ifdef HAVE_CONFIG_H |
34 | #include "config.h" | |
35 | #endif | |
84004dbf | 36 | #include <stdio.h> |
98f5fee8 | 37 | #include <stdlib.h> |
84004dbf | 38 | #include <fcntl.h> |
84004dbf RS |
39 | #include <string.h> |
40 | #include <errno.h> | |
98f5fee8 | 41 | #include <rpc/rpc.h> |
84004dbf | 42 | #include <rpc/xdr.h> |
fc01d2a9 TN |
43 | #ifdef HAVE_SYS_FILIO_H |
44 | #include <sys/filio.h> | |
45 | #endif | |
647d2ea1 RS |
46 | #ifdef HAVE_SYS_SOCKIO_H |
47 | #include <sys/sockio.h> | |
48 | #endif | |
485bc9b9 | 49 | #include <sys/types.h> |
84004dbf RS |
50 | #include "libnfs.h" |
51 | #include "libnfs-raw.h" | |
52 | #include "libnfs-private.h" | |
53 | #include "slist.h" | |
54 | ||
b077fdeb RS |
55 | static int rpc_disconnect_requeue(struct rpc_context *rpc); |
56 | ||
84004dbf RS |
57 | static void set_nonblocking(int fd) |
58 | { | |
6874f61e RS |
59 | #if defined(WIN32) |
60 | #else | |
84004dbf RS |
61 | unsigned v; |
62 | v = fcntl(fd, F_GETFL, 0); | |
63 | fcntl(fd, F_SETFL, v | O_NONBLOCK); | |
6874f61e | 64 | #endif |
84004dbf RS |
65 | } |
66 | ||
67 | int rpc_get_fd(struct rpc_context *rpc) | |
68 | { | |
69 | return rpc->fd; | |
70 | } | |
71 | ||
72 | int rpc_which_events(struct rpc_context *rpc) | |
73 | { | |
912f7ad5 | 74 | int events = rpc->is_connected ? POLLIN : POLLOUT; |
84004dbf | 75 | |
5911f3e8 RS |
76 | if (rpc->is_udp != 0) { |
77 | /* for udp sockets we only wait for pollin */ | |
78 | return POLLIN; | |
79 | } | |
80 | ||
84004dbf RS |
81 | if (rpc->outqueue) { |
82 | events |= POLLOUT; | |
83 | } | |
84 | return events; | |
85 | } | |
86 | ||
87 | static int rpc_write_to_socket(struct rpc_context *rpc) | |
88 | { | |
89 | ssize_t count; | |
90 | ||
91 | if (rpc == NULL) { | |
84004dbf RS |
92 | return -1; |
93 | } | |
94 | if (rpc->fd == -1) { | |
1896d37b RS |
95 | rpc_set_error(rpc, "trying to write but not connected"); |
96 | return -1; | |
84004dbf RS |
97 | } |
98 | ||
99 | while (rpc->outqueue != NULL) { | |
100 | ssize_t total; | |
101 | ||
102 | total = rpc->outqueue->outdata.size; | |
103 | ||
6874f61e RS |
104 | #if defined(WIN32) |
105 | count = send(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written, 0); | |
106 | #else | |
84004dbf | 107 | count = write(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written); |
6874f61e | 108 | #endif |
84004dbf RS |
109 | if (count == -1) { |
110 | if (errno == EAGAIN || errno == EWOULDBLOCK) { | |
84004dbf RS |
111 | return 0; |
112 | } | |
1896d37b RS |
113 | rpc_set_error(rpc, "Error when writing to socket :%s(%d)", strerror(errno), errno); |
114 | return -1; | |
84004dbf RS |
115 | } |
116 | ||
117 | rpc->outqueue->written += count; | |
118 | if (rpc->outqueue->written == total) { | |
119 | struct rpc_pdu *pdu = rpc->outqueue; | |
120 | ||
121 | SLIST_REMOVE(&rpc->outqueue, pdu); | |
122 | SLIST_ADD_END(&rpc->waitpdu, pdu); | |
123 | } | |
124 | } | |
125 | return 0; | |
126 | } | |
127 | ||
128 | static int rpc_read_from_socket(struct rpc_context *rpc) | |
129 | { | |
130 | int available; | |
131 | int size; | |
cdb19ec1 | 132 | int pdu_size; |
84004dbf RS |
133 | ssize_t count; |
134 | ||
6874f61e RS |
135 | #if defined(WIN32) |
136 | if (ioctlsocket(rpc->fd, FIONREAD, &available) != 0) { | |
137 | #else | |
84004dbf | 138 | if (ioctl(rpc->fd, FIONREAD, &available) != 0) { |
6874f61e | 139 | #endif |
84004dbf RS |
140 | rpc_set_error(rpc, "Ioctl FIONREAD returned error : %d. Closing socket.", errno); |
141 | return -1; | |
142 | } | |
143 | if (available == 0) { | |
144 | rpc_set_error(rpc, "Socket has been closed"); | |
1896d37b | 145 | return -1; |
84004dbf | 146 | } |
cdb19ec1 | 147 | |
0268794f RS |
148 | if (rpc->is_udp) { |
149 | char *buf; | |
150 | socklen_t socklen = sizeof(rpc->udp_src); | |
151 | ||
152 | buf = malloc(available); | |
153 | if (buf == NULL) { | |
154 | rpc_set_error(rpc, "Failed to malloc buffer for recvfrom"); | |
155 | return -1; | |
156 | } | |
157 | count = recvfrom(rpc->fd, buf, available, MSG_DONTWAIT, (struct sockaddr *)&rpc->udp_src, &socklen); | |
158 | if (count < 0) { | |
159 | rpc_set_error(rpc, "Failed recvfrom: %s", strerror(errno)); | |
160 | free(buf); | |
161 | } | |
162 | if (rpc_process_pdu(rpc, buf, count) != 0) { | |
163 | rpc_set_error(rpc, "Invalid/garbage pdu received from server. Ignoring PDU"); | |
164 | free(buf); | |
165 | return -1; | |
166 | } | |
167 | free(buf); | |
168 | return 0; | |
169 | } | |
170 | ||
cdb19ec1 RS |
171 | /* read record marker, 4 bytes at the beginning of every pdu */ |
172 | if (rpc->inbuf == NULL) { | |
173 | rpc->insize = 4; | |
174 | rpc->inbuf = malloc(rpc->insize); | |
175 | if (rpc->inbuf == NULL) { | |
176 | rpc_set_error(rpc, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno); | |
177 | return -1; | |
178 | } | |
179 | } | |
180 | if (rpc->inpos < 4) { | |
181 | size = 4 - rpc->inpos; | |
182 | ||
6874f61e RS |
183 | #if defined(WIN32) |
184 | count = recv(rpc->fd, rpc->inbuf + rpc->inpos, size, 0); | |
185 | #else | |
cdb19ec1 | 186 | count = read(rpc->fd, rpc->inbuf + rpc->inpos, size); |
6874f61e | 187 | #endif |
cdb19ec1 RS |
188 | if (count == -1) { |
189 | if (errno == EINTR) { | |
190 | return 0; | |
191 | } | |
192 | rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno); | |
193 | return -1; | |
194 | } | |
195 | available -= count; | |
196 | rpc->inpos += count; | |
197 | } | |
198 | ||
199 | if (available == 0) { | |
200 | return 0; | |
201 | } | |
202 | ||
203 | pdu_size = rpc_get_pdu_size(rpc->inbuf); | |
204 | if (rpc->insize < pdu_size) { | |
205 | unsigned char *buf; | |
206 | ||
207 | buf = malloc(pdu_size); | |
208 | if (buf == NULL) { | |
209 | rpc_set_error(rpc, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size, errno); | |
210 | return -1; | |
211 | } | |
212 | memcpy(buf, rpc->inbuf, rpc->insize); | |
213 | free(rpc->inbuf); | |
214 | rpc->inbuf = buf; | |
215 | rpc->insize = rpc_get_pdu_size(rpc->inbuf); | |
84004dbf | 216 | } |
cdb19ec1 RS |
217 | |
218 | size = available; | |
219 | if (size > rpc->insize - rpc->inpos) { | |
220 | size = rpc->insize - rpc->inpos; | |
84004dbf RS |
221 | } |
222 | ||
6874f61e RS |
223 | #if defined(WIN32) |
224 | count = recv(rpc->fd, rpc->inbuf + rpc->inpos, size, 0); | |
225 | #else | |
cdb19ec1 | 226 | count = read(rpc->fd, rpc->inbuf + rpc->inpos, size); |
6874f61e | 227 | #endif |
84004dbf RS |
228 | if (count == -1) { |
229 | if (errno == EINTR) { | |
84004dbf RS |
230 | return 0; |
231 | } | |
232 | rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno); | |
1896d37b | 233 | return -1; |
84004dbf | 234 | } |
cdb19ec1 RS |
235 | available -= count; |
236 | rpc->inpos += count; | |
84004dbf | 237 | |
cdb19ec1 RS |
238 | if (rpc->inpos == rpc->insize) { |
239 | if (rpc_process_pdu(rpc, rpc->inbuf, pdu_size) != 0) { | |
84004dbf | 240 | rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket"); |
1896d37b | 241 | return -1; |
84004dbf | 242 | } |
cdb19ec1 RS |
243 | free(rpc->inbuf); |
244 | rpc->inbuf = NULL; | |
245 | rpc->insize = 0; | |
246 | rpc->inpos = 0; | |
84004dbf | 247 | } |
cdb19ec1 | 248 | |
84004dbf RS |
249 | return 0; |
250 | } | |
251 | ||
252 | ||
253 | ||
254 | int rpc_service(struct rpc_context *rpc, int revents) | |
255 | { | |
256 | if (revents & POLLERR) { | |
912f7ad5 RS |
257 | int err = 0; |
258 | socklen_t err_size = sizeof(err); | |
259 | ||
260 | if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR, | |
261 | &err, &err_size) != 0 || err != 0) { | |
262 | if (err == 0) { | |
263 | err = errno; | |
264 | } | |
265 | rpc_set_error(rpc, "rpc_service: socket error " | |
266 | "%s(%d).", | |
267 | strerror(err), err); | |
84004dbf | 268 | } else { |
912f7ad5 RS |
269 | rpc_set_error(rpc, "rpc_service: POLLERR, " |
270 | "Unknown socket error."); | |
84004dbf RS |
271 | } |
272 | rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data); | |
273 | return -1; | |
274 | } | |
275 | if (revents & POLLHUP) { | |
84004dbf RS |
276 | rpc_set_error(rpc, "Socket failed with POLLHUP"); |
277 | rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data); | |
1896d37b | 278 | return -1; |
84004dbf RS |
279 | } |
280 | ||
281 | if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) { | |
912f7ad5 RS |
282 | int err = 0; |
283 | socklen_t err_size = sizeof(err); | |
284 | ||
285 | if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR, | |
286 | &err, &err_size) != 0 || err != 0) { | |
287 | if (err == 0) { | |
288 | err = errno; | |
289 | } | |
290 | rpc_set_error(rpc, "rpc_service: socket error " | |
291 | "%s(%d) while connecting.", | |
292 | strerror(err), err); | |
293 | rpc->connect_cb(rpc, RPC_STATUS_ERROR, | |
294 | NULL, rpc->connect_data); | |
295 | return -1; | |
296 | } | |
297 | ||
84004dbf RS |
298 | rpc->is_connected = 1; |
299 | rpc->connect_cb(rpc, RPC_STATUS_SUCCESS, NULL, rpc->connect_data); | |
300 | return 0; | |
301 | } | |
302 | ||
b077fdeb RS |
303 | if (revents & POLLIN) { |
304 | if (rpc_read_from_socket(rpc) != 0) { | |
305 | rpc_disconnect_requeue(rpc); | |
306 | return 0; | |
84004dbf RS |
307 | } |
308 | } | |
309 | ||
b077fdeb RS |
310 | if (revents & POLLOUT && rpc->outqueue != NULL) { |
311 | if (rpc_write_to_socket(rpc) != 0) { | |
312 | rpc_set_error(rpc, "write to socket failed"); | |
1896d37b | 313 | return -1; |
84004dbf RS |
314 | } |
315 | } | |
316 | ||
317 | return 0; | |
318 | } | |
319 | ||
320 | ||
321 | int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data) | |
322 | { | |
323 | struct sockaddr_storage s; | |
324 | struct sockaddr_in *sin = (struct sockaddr_in *)&s; | |
325 | int socksize; | |
326 | ||
327 | if (rpc->fd != -1) { | |
328 | rpc_set_error(rpc, "Trying to connect while already connected"); | |
84004dbf RS |
329 | return -1; |
330 | } | |
331 | ||
070287e5 RS |
332 | if (rpc->is_udp != 0) { |
333 | rpc_set_error(rpc, "Trying to connect on UDP socket"); | |
334 | return -1; | |
335 | } | |
336 | ||
84004dbf RS |
337 | sin->sin_family = AF_INET; |
338 | sin->sin_port = htons(port); | |
339 | if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) { | |
340 | rpc_set_error(rpc, "Not a valid server ip address"); | |
1896d37b | 341 | return -1; |
84004dbf RS |
342 | } |
343 | ||
344 | switch (s.ss_family) { | |
345 | case AF_INET: | |
84004dbf | 346 | socksize = sizeof(struct sockaddr_in); |
f7f931c7 | 347 | #ifdef HAVE_SOCKADDR_LEN |
ea214e45 RS |
348 | sin->sin_len = socksize; |
349 | #endif | |
6874f61e | 350 | rpc->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
84004dbf RS |
351 | break; |
352 | } | |
353 | ||
354 | if (rpc->fd == -1) { | |
355 | rpc_set_error(rpc, "Failed to open socket"); | |
1896d37b | 356 | return -1; |
84004dbf RS |
357 | } |
358 | ||
359 | rpc->connect_cb = cb; | |
360 | rpc->connect_data = private_data; | |
361 | ||
362 | set_nonblocking(rpc->fd); | |
363 | ||
364 | if (connect(rpc->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS) { | |
365 | rpc_set_error(rpc, "connect() to server failed"); | |
1896d37b | 366 | return -1; |
84004dbf RS |
367 | } |
368 | ||
369 | return 0; | |
370 | } | |
371 | ||
372 | int rpc_disconnect(struct rpc_context *rpc, char *error) | |
373 | { | |
374 | if (rpc->fd != -1) { | |
6874f61e RS |
375 | #if defined(WIN32) |
376 | closesocket(rpc->fd); | |
377 | #else | |
84004dbf | 378 | close(rpc->fd); |
6874f61e | 379 | #endif |
84004dbf RS |
380 | } |
381 | rpc->fd = -1; | |
382 | ||
383 | rpc->is_connected = 0; | |
384 | ||
385 | rpc_error_all_pdus(rpc, error); | |
386 | ||
387 | return 0; | |
388 | } | |
485bc9b9 | 389 | |
b077fdeb RS |
390 | /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue */ |
391 | static int rpc_disconnect_requeue(struct rpc_context *rpc) | |
392 | { | |
393 | struct rpc_pdu *pdu; | |
394 | ||
395 | if (rpc->fd != -1) { | |
6874f61e RS |
396 | #if defined(WIN32) |
397 | closesocket(rpc->fd); | |
398 | #else | |
b077fdeb | 399 | close(rpc->fd); |
6874f61e | 400 | #endif |
b077fdeb RS |
401 | } |
402 | rpc->fd = -1; | |
403 | ||
404 | rpc->is_connected = 0; | |
405 | ||
406 | /* socket is closed so we will not get any replies to any commands | |
407 | * in flight. Move them all over from the waitpdu queue back to the out queue | |
408 | */ | |
409 | for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) { | |
410 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
411 | SLIST_ADD(&rpc->outqueue, pdu); | |
412 | } | |
413 | ||
414 | return 0; | |
415 | } | |
416 | ||
485bc9b9 RS |
417 | |
418 | int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port) | |
419 | { | |
420 | struct addrinfo *ai = NULL; | |
421 | char service[6]; | |
422 | ||
423 | if (rpc->is_udp == 0) { | |
424 | rpc_set_error(rpc, "Cant not bind UDP. Not UDP context"); | |
425 | return -1; | |
426 | } | |
427 | ||
6874f61e | 428 | sprintf(service, "%d", port); |
485bc9b9 RS |
429 | if (getaddrinfo(addr, service, NULL, &ai) != 0) { |
430 | rpc_set_error(rpc, "Invalid address:%s. " | |
431 | "Can not resolv into IPv4/v6 structure."); | |
432 | return -1; | |
433 | } | |
434 | ||
435 | switch(ai->ai_family) { | |
436 | case AF_INET: | |
437 | rpc->fd = socket(ai->ai_family, SOCK_DGRAM, 0); | |
438 | if (rpc->fd == -1) { | |
439 | rpc_set_error(rpc, "Failed to create UDP socket: %s", strerror(errno)); | |
440 | freeaddrinfo(ai); | |
441 | return -1; | |
442 | } | |
443 | ||
444 | if (bind(rpc->fd, (struct sockaddr *)ai->ai_addr, sizeof(struct sockaddr_in)) != 0) { | |
445 | rpc_set_error(rpc, "Failed to bind to UDP socket: %s",strerror(errno)); | |
446 | freeaddrinfo(ai); | |
447 | return -1; | |
448 | } | |
449 | break; | |
450 | default: | |
451 | rpc_set_error(rpc, "Can not handle UPD sockets of family %d yet", ai->ai_family); | |
452 | freeaddrinfo(ai); | |
453 | return -1; | |
454 | } | |
455 | ||
456 | freeaddrinfo(ai); | |
457 | ||
458 | return 0; | |
459 | } | |
460 | ||
5bf60dc6 RS |
461 | int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int is_broadcast) |
462 | { | |
463 | struct addrinfo *ai = NULL; | |
464 | char service[6]; | |
465 | ||
466 | if (rpc->is_udp == 0) { | |
467 | rpc_set_error(rpc, "Can not set destination sockaddr. Not UDP context"); | |
468 | return -1; | |
469 | } | |
470 | ||
6874f61e | 471 | sprintf(service, "%d", port); |
5bf60dc6 RS |
472 | if (getaddrinfo(addr, service, NULL, &ai) != 0) { |
473 | rpc_set_error(rpc, "Invalid address:%s. " | |
474 | "Can not resolv into IPv4/v6 structure."); | |
475 | return -1; | |
476 | } | |
477 | ||
478 | if (rpc->udp_dest) { | |
479 | free(rpc->udp_dest); | |
480 | rpc->udp_dest = NULL; | |
481 | } | |
482 | rpc->udp_dest = malloc(ai->ai_addrlen); | |
483 | if (rpc->udp_dest == NULL) { | |
484 | rpc_set_error(rpc, "Out of memory. Failed to allocate sockaddr structure"); | |
485 | return -1; | |
486 | } | |
487 | memcpy(rpc->udp_dest, ai->ai_addr, ai->ai_addrlen); | |
488 | freeaddrinfo(ai); | |
489 | ||
490 | rpc->is_broadcast = is_broadcast; | |
491 | setsockopt(rpc->fd, SOL_SOCKET, SO_BROADCAST, &is_broadcast, sizeof(is_broadcast)); | |
492 | ||
493 | return 0; | |
494 | } | |
c481da67 RS |
495 | |
496 | struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc) | |
497 | { | |
498 | return (struct sockaddr *)&rpc->udp_src; | |
499 | } |