Imported Upstream version 1.2.0
[deb_libnfs.git] / lib / socket.c
CommitLineData
dabf4152
AM
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*/
5670ec6e
AM
17#ifdef WIN32
18#include "win32_compat.h"
19#else
20#include <unistd.h>
21#include <poll.h>
22#include <arpa/inet.h>
23#include <sys/ioctl.h>
24#include <sys/socket.h>
25#include <netdb.h>
26#endif/*WIN32*/
dabf4152
AM
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31#include <stdio.h>
32#include <stdlib.h>
dabf4152 33#include <fcntl.h>
dabf4152
AM
34#include <string.h>
35#include <errno.h>
36#include <rpc/rpc.h>
37#include <rpc/xdr.h>
dabf4152
AM
38#ifdef HAVE_SYS_FILIO_H
39#include <sys/filio.h>
40#endif
5670ec6e
AM
41#ifdef HAVE_SYS_SOCKIO_H
42#include <sys/sockio.h>
43#endif
dabf4152 44#include <sys/types.h>
dabf4152
AM
45#include "libnfs.h"
46#include "libnfs-raw.h"
47#include "libnfs-private.h"
48#include "slist.h"
49
5670ec6e
AM
50#ifdef WIN32
51//has to be included after stdlib!!
52#include "win32_errnowrapper.h"
53#endif
54
55
56static int rpc_reconnect_requeue(struct rpc_context *rpc);
57static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_storage *s);
58
dabf4152
AM
59static void set_nonblocking(int fd)
60{
5670ec6e
AM
61 int v = 0;
62#if defined(WIN32)
63 long nonblocking=1;
64 v = ioctlsocket(fd, FIONBIO,&nonblocking);
65#else
dabf4152
AM
66 v = fcntl(fd, F_GETFL, 0);
67 fcntl(fd, F_SETFL, v | O_NONBLOCK);
5670ec6e 68#endif //FIXME
dabf4152
AM
69}
70
71int rpc_get_fd(struct rpc_context *rpc)
72{
73 return rpc->fd;
74}
75
76int rpc_which_events(struct rpc_context *rpc)
77{
78 int events = rpc->is_connected ? POLLIN : POLLOUT;
79
80 if (rpc->is_udp != 0) {
81 /* for udp sockets we only wait for pollin */
82 return POLLIN;
83 }
84
85 if (rpc->outqueue) {
86 events |= POLLOUT;
87 }
88 return events;
89}
90
91static int rpc_write_to_socket(struct rpc_context *rpc)
92{
5670ec6e 93 int64_t count;
dabf4152
AM
94
95 if (rpc == NULL) {
96 return -1;
97 }
98 if (rpc->fd == -1) {
99 rpc_set_error(rpc, "trying to write but not connected");
100 return -1;
101 }
102
103 while (rpc->outqueue != NULL) {
5670ec6e 104 int64_t total;
dabf4152
AM
105
106 total = rpc->outqueue->outdata.size;
107
5670ec6e
AM
108#if defined(WIN32)
109 count = send(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written, 0);
110#else
dabf4152 111 count = write(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written);
5670ec6e 112#endif
dabf4152
AM
113 if (count == -1) {
114 if (errno == EAGAIN || errno == EWOULDBLOCK) {
115 return 0;
116 }
117 rpc_set_error(rpc, "Error when writing to socket :%s(%d)", strerror(errno), errno);
118 return -1;
119 }
120
121 rpc->outqueue->written += count;
122 if (rpc->outqueue->written == total) {
123 struct rpc_pdu *pdu = rpc->outqueue;
124
125 SLIST_REMOVE(&rpc->outqueue, pdu);
126 SLIST_ADD_END(&rpc->waitpdu, pdu);
127 }
128 }
129 return 0;
130}
131
132static int rpc_read_from_socket(struct rpc_context *rpc)
133{
134 int available;
135 int size;
136 int pdu_size;
5670ec6e 137 int64_t count;
dabf4152 138
5670ec6e
AM
139#if defined(WIN32)
140 if (ioctlsocket(rpc->fd, FIONREAD, &available) != 0) {
141#else
dabf4152 142 if (ioctl(rpc->fd, FIONREAD, &available) != 0) {
5670ec6e 143#endif
dabf4152
AM
144 rpc_set_error(rpc, "Ioctl FIONREAD returned error : %d. Closing socket.", errno);
145 return -1;
146 }
5670ec6e 147
dabf4152
AM
148 if (available == 0) {
149 rpc_set_error(rpc, "Socket has been closed");
150 return -1;
151 }
152
153 if (rpc->is_udp) {
154 char *buf;
155 socklen_t socklen = sizeof(rpc->udp_src);
156
157 buf = malloc(available);
158 if (buf == NULL) {
159 rpc_set_error(rpc, "Failed to malloc buffer for recvfrom");
160 return -1;
161 }
162 count = recvfrom(rpc->fd, buf, available, MSG_DONTWAIT, (struct sockaddr *)&rpc->udp_src, &socklen);
163 if (count < 0) {
164 rpc_set_error(rpc, "Failed recvfrom: %s", strerror(errno));
165 free(buf);
166 }
167 if (rpc_process_pdu(rpc, buf, count) != 0) {
168 rpc_set_error(rpc, "Invalid/garbage pdu received from server. Ignoring PDU");
169 free(buf);
170 return -1;
171 }
172 free(buf);
173 return 0;
174 }
175
176 /* read record marker, 4 bytes at the beginning of every pdu */
177 if (rpc->inbuf == NULL) {
178 rpc->insize = 4;
179 rpc->inbuf = malloc(rpc->insize);
180 if (rpc->inbuf == NULL) {
181 rpc_set_error(rpc, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno);
182 return -1;
183 }
184 }
185 if (rpc->inpos < 4) {
186 size = 4 - rpc->inpos;
187
5670ec6e
AM
188#if defined(WIN32)
189 count = recv(rpc->fd, rpc->inbuf + rpc->inpos, size, 0);
190#else
dabf4152 191 count = read(rpc->fd, rpc->inbuf + rpc->inpos, size);
5670ec6e 192#endif
dabf4152
AM
193 if (count == -1) {
194 if (errno == EINTR) {
195 return 0;
196 }
197 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
198 return -1;
199 }
200 available -= count;
201 rpc->inpos += count;
202 }
203
204 if (available == 0) {
205 return 0;
206 }
207
208 pdu_size = rpc_get_pdu_size(rpc->inbuf);
209 if (rpc->insize < pdu_size) {
210 unsigned char *buf;
211
212 buf = malloc(pdu_size);
213 if (buf == NULL) {
214 rpc_set_error(rpc, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size, errno);
215 return -1;
216 }
217 memcpy(buf, rpc->inbuf, rpc->insize);
218 free(rpc->inbuf);
219 rpc->inbuf = buf;
220 rpc->insize = rpc_get_pdu_size(rpc->inbuf);
221 }
222
223 size = available;
224 if (size > rpc->insize - rpc->inpos) {
225 size = rpc->insize - rpc->inpos;
226 }
227
5670ec6e
AM
228#if defined(WIN32)
229 count = recv(rpc->fd, rpc->inbuf + rpc->inpos, size, 0);
230#else
dabf4152 231 count = read(rpc->fd, rpc->inbuf + rpc->inpos, size);
5670ec6e 232#endif
dabf4152
AM
233 if (count == -1) {
234 if (errno == EINTR) {
235 return 0;
236 }
237 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
238 return -1;
239 }
240 available -= count;
241 rpc->inpos += count;
242
243 if (rpc->inpos == rpc->insize) {
244 if (rpc_process_pdu(rpc, rpc->inbuf, pdu_size) != 0) {
245 rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket");
246 return -1;
247 }
248 free(rpc->inbuf);
249 rpc->inbuf = NULL;
250 rpc->insize = 0;
251 rpc->inpos = 0;
252 }
253
254 return 0;
255}
256
257
258
259int rpc_service(struct rpc_context *rpc, int revents)
260{
261 if (revents & POLLERR) {
5670ec6e
AM
262#ifdef WIN32
263 char err = 0;
264#else
dabf4152 265 int err = 0;
5670ec6e 266#endif
dabf4152
AM
267 socklen_t err_size = sizeof(err);
268
269 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
5670ec6e 270 (char *)&err, &err_size) != 0 || err != 0) {
dabf4152
AM
271 if (err == 0) {
272 err = errno;
273 }
274 rpc_set_error(rpc, "rpc_service: socket error "
275 "%s(%d).",
276 strerror(err), err);
277 } else {
278 rpc_set_error(rpc, "rpc_service: POLLERR, "
279 "Unknown socket error.");
280 }
5670ec6e
AM
281 if (rpc->connect_cb != NULL) {
282 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
283 }
dabf4152
AM
284 return -1;
285 }
286 if (revents & POLLHUP) {
287 rpc_set_error(rpc, "Socket failed with POLLHUP");
5670ec6e
AM
288 if (rpc->connect_cb != NULL) {
289 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
290 }
dabf4152
AM
291 return -1;
292 }
293
294 if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) {
295 int err = 0;
296 socklen_t err_size = sizeof(err);
297
298 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
5670ec6e 299 (char *)&err, &err_size) != 0 || err != 0) {
dabf4152
AM
300 if (err == 0) {
301 err = errno;
302 }
303 rpc_set_error(rpc, "rpc_service: socket error "
304 "%s(%d) while connecting.",
305 strerror(err), err);
5670ec6e
AM
306 if (rpc->connect_cb != NULL) {
307 rpc->connect_cb(rpc, RPC_STATUS_ERROR,
dabf4152 308 NULL, rpc->connect_data);
5670ec6e 309 }
dabf4152
AM
310 return -1;
311 }
312
313 rpc->is_connected = 1;
5670ec6e
AM
314 if (rpc->connect_cb != NULL) {
315 rpc->connect_cb(rpc, RPC_STATUS_SUCCESS, NULL, rpc->connect_data);
316 }
dabf4152
AM
317 return 0;
318 }
319
5670ec6e
AM
320 if (revents & POLLIN) {
321 if (rpc_read_from_socket(rpc) != 0) {
322 rpc_reconnect_requeue(rpc);
323 return 0;
324 }
325 }
326
dabf4152
AM
327 if (revents & POLLOUT && rpc->outqueue != NULL) {
328 if (rpc_write_to_socket(rpc) != 0) {
329 rpc_set_error(rpc, "write to socket failed");
330 return -1;
331 }
332 }
333
5670ec6e
AM
334 return 0;
335}
336
337void rpc_set_autoreconnect(struct rpc_context *rpc)
338{
339 rpc->auto_reconnect = 1;
340}
341
342void rpc_unset_autoreconnect(struct rpc_context *rpc)
343{
344 rpc->auto_reconnect = 0;
345}
346
347static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_storage *s)
348{
349 int socksize;
350
351 switch (s->ss_family) {
352 case AF_INET:
353 socksize = sizeof(struct sockaddr_in);
354 rpc->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
355 break;
356 default:
357 rpc_set_error(rpc, "Can not handle AF_FAMILY:%d", s->ss_family);
358 return -1;
359 }
360
361 if (rpc->fd == -1) {
362 rpc_set_error(rpc, "Failed to open socket");
363 return -1;
364 }
365
366
367#if !defined(WIN32)
368 /* Some systems allow you to set capabilities on an executable
369 * to allow the file to be executed with privilege to bind to
370 * privileged system ports, even if the user is not root.
371 *
372 * Opportunistically try to bind the socket to a low numbered
373 * system port in the hope that the user is either root or the
374 * executable has the CAP_NET_BIND_SERVICE.
375 *
376 * As soon as we fail the bind() with EACCES we know we will never
377 * be able to bind to a system port so we terminate the loop.
378 *
379 * On linux, use
380 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
381 * to make the executable able to bind to a system port.
382 */
383 if (1) {
384 int port;
385 int one = 1;
386
387 setsockopt(rpc->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
388
389 for (port = 200; port < 500; port++) {
390 struct sockaddr_in sin;
391
392 memset(&sin, 0, sizeof(sin));
393 sin.sin_port = htons(port);
394 sin.sin_family = AF_INET;
395 sin.sin_addr.s_addr = 0;
396
397 if (bind(rpc->fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) != 0 && errno != EACCES) {
398 /* we didnt get EACCES, so try again */
399 continue;
400 }
401 break;
dabf4152
AM
402 }
403 }
5670ec6e 404#endif
dabf4152 405
5670ec6e 406 set_nonblocking(rpc->fd);
dabf4152 407
5670ec6e
AM
408#if defined(WIN32)
409 if (connect(rpc->fd, (struct sockaddr *)s, socksize) == 0 && errno != EINPROGRESS )
410#else
411 if (connect(rpc->fd, (struct sockaddr *)s, socksize) != 0 && errno != EINPROGRESS)
412#endif
413 {
414 rpc_set_error(rpc, "connect() to server failed");
415 return -1;
416 }
417
418 return 0;
419}
dabf4152
AM
420
421int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data)
422{
5670ec6e 423 struct sockaddr_in *sin = (struct sockaddr_in *)&rpc->s;
dabf4152
AM
424
425 if (rpc->fd != -1) {
426 rpc_set_error(rpc, "Trying to connect while already connected");
427 return -1;
428 }
429
430 if (rpc->is_udp != 0) {
431 rpc_set_error(rpc, "Trying to connect on UDP socket");
432 return -1;
433 }
434
5670ec6e
AM
435 rpc->auto_reconnect = 0;
436
dabf4152
AM
437 sin->sin_family = AF_INET;
438 sin->sin_port = htons(port);
439 if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) {
440 rpc_set_error(rpc, "Not a valid server ip address");
441 return -1;
442 }
443
5670ec6e
AM
444
445 switch (rpc->s.ss_family) {
dabf4152 446 case AF_INET:
dabf4152 447#ifdef HAVE_SOCKADDR_LEN
5670ec6e 448 sin->sin_len = sizeof(struct sockaddr_in);
dabf4152 449#endif
dabf4152
AM
450 break;
451 }
452
dabf4152
AM
453 rpc->connect_cb = cb;
454 rpc->connect_data = private_data;
455
5670ec6e 456 if (rpc_connect_sockaddr_async(rpc, &rpc->s) != 0) {
dabf4152 457 return -1;
5670ec6e 458 }
dabf4152
AM
459
460 return 0;
461}
462
463int rpc_disconnect(struct rpc_context *rpc, char *error)
464{
5670ec6e
AM
465 rpc_unset_autoreconnect(rpc);
466
dabf4152 467 if (rpc->fd != -1) {
5670ec6e
AM
468#if defined(WIN32)
469 closesocket(rpc->fd);
470#else
dabf4152 471 close(rpc->fd);
5670ec6e 472#endif
dabf4152
AM
473 }
474 rpc->fd = -1;
475
476 rpc->is_connected = 0;
477
478 rpc_error_all_pdus(rpc, error);
479
480 return 0;
481}
482
5670ec6e
AM
483static void reconnect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
484{
485 if (status != RPC_STATUS_SUCCESS) {
486 rpc_error_all_pdus(rpc, "RPC ERROR: Failed to reconnect async");
487 return;
488 }
489
490 rpc->is_connected = 1;
491 rpc->connect_cb = NULL;
492}
493
494/* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue and reconnect */
495static int rpc_reconnect_requeue(struct rpc_context *rpc)
496{
497 struct rpc_pdu *pdu;
498
499 if (rpc->fd != -1) {
500#if defined(WIN32)
501 closesocket(rpc->fd);
502#else
503 close(rpc->fd);
504#endif
505 }
506 rpc->fd = -1;
507
508 rpc->is_connected = 0;
509
510 /* socket is closed so we will not get any replies to any commands
511 * in flight. Move them all over from the waitpdu queue back to the out queue
512 */
513 for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
514 SLIST_REMOVE(&rpc->waitpdu, pdu);
515 SLIST_ADD(&rpc->outqueue, pdu);
516 /* we have to re-send the whole pdu again */
517 pdu->written = 0;
518 }
519
520 if (rpc->auto_reconnect != 0) {
521 rpc->connect_cb = reconnect_cb;
522
523 if (rpc_connect_sockaddr_async(rpc, &rpc->s) != 0) {
524 rpc_error_all_pdus(rpc, "RPC ERROR: Failed to reconnect async");
525 return -1;
526 }
527 }
528
529 return 0;
530}
531
dabf4152
AM
532
533int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port)
534{
535 struct addrinfo *ai = NULL;
536 char service[6];
537
538 if (rpc->is_udp == 0) {
539 rpc_set_error(rpc, "Cant not bind UDP. Not UDP context");
540 return -1;
541 }
542
5670ec6e 543 sprintf(service, "%d", port);
dabf4152
AM
544 if (getaddrinfo(addr, service, NULL, &ai) != 0) {
545 rpc_set_error(rpc, "Invalid address:%s. "
546 "Can not resolv into IPv4/v6 structure.");
547 return -1;
548 }
549
550 switch(ai->ai_family) {
551 case AF_INET:
552 rpc->fd = socket(ai->ai_family, SOCK_DGRAM, 0);
553 if (rpc->fd == -1) {
554 rpc_set_error(rpc, "Failed to create UDP socket: %s", strerror(errno));
555 freeaddrinfo(ai);
556 return -1;
557 }
558
559 if (bind(rpc->fd, (struct sockaddr *)ai->ai_addr, sizeof(struct sockaddr_in)) != 0) {
560 rpc_set_error(rpc, "Failed to bind to UDP socket: %s",strerror(errno));
561 freeaddrinfo(ai);
562 return -1;
563 }
564 break;
565 default:
566 rpc_set_error(rpc, "Can not handle UPD sockets of family %d yet", ai->ai_family);
567 freeaddrinfo(ai);
568 return -1;
569 }
570
571 freeaddrinfo(ai);
572
573 return 0;
574}
575
576int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int is_broadcast)
577{
578 struct addrinfo *ai = NULL;
579 char service[6];
580
581 if (rpc->is_udp == 0) {
582 rpc_set_error(rpc, "Can not set destination sockaddr. Not UDP context");
583 return -1;
584 }
585
5670ec6e 586 sprintf(service, "%d", port);
dabf4152
AM
587 if (getaddrinfo(addr, service, NULL, &ai) != 0) {
588 rpc_set_error(rpc, "Invalid address:%s. "
589 "Can not resolv into IPv4/v6 structure.");
590 return -1;
591 }
592
593 if (rpc->udp_dest) {
594 free(rpc->udp_dest);
595 rpc->udp_dest = NULL;
596 }
597 rpc->udp_dest = malloc(ai->ai_addrlen);
598 if (rpc->udp_dest == NULL) {
599 rpc_set_error(rpc, "Out of memory. Failed to allocate sockaddr structure");
600 return -1;
601 }
602 memcpy(rpc->udp_dest, ai->ai_addr, ai->ai_addrlen);
603 freeaddrinfo(ai);
604
605 rpc->is_broadcast = is_broadcast;
5670ec6e 606 setsockopt(rpc->fd, SOL_SOCKET, SO_BROADCAST, (char *)&is_broadcast, sizeof(is_broadcast));
dabf4152
AM
607
608 return 0;
609}
610
611struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc)
612{
613 return (struct sockaddr *)&rpc->udp_src;
614}
5670ec6e
AM
615
616int rpc_queue_length(struct rpc_context *rpc)
617{
618 int i=0;
619 struct rpc_pdu *pdu;
620
621 for(pdu = rpc->outqueue; pdu; pdu = pdu->next) {
622 i++;
623 }
624 for(pdu = rpc->waitpdu; pdu; pdu = pdu->next) {
625 i++;
626 }
627 return i;
628}