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