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