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