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