[win32] - completed win32 port
[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
b077fdeb
RS
56static int rpc_disconnect_requeue(struct rpc_context *rpc);
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{
92 ssize_t count;
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) {
103 ssize_t total;
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;
84004dbf
RS
136 ssize_t count;
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
RS
279 }
280 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
281 return -1;
282 }
283 if (revents & POLLHUP) {
84004dbf
RS
284 rpc_set_error(rpc, "Socket failed with POLLHUP");
285 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
1896d37b 286 return -1;
84004dbf
RS
287 }
288
289 if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) {
912f7ad5
RS
290 int err = 0;
291 socklen_t err_size = sizeof(err);
292
293 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
bb4e9ed6 294 (char *)&err, &err_size) != 0 || err != 0) {
912f7ad5
RS
295 if (err == 0) {
296 err = errno;
297 }
298 rpc_set_error(rpc, "rpc_service: socket error "
299 "%s(%d) while connecting.",
300 strerror(err), err);
301 rpc->connect_cb(rpc, RPC_STATUS_ERROR,
302 NULL, rpc->connect_data);
303 return -1;
304 }
305
84004dbf
RS
306 rpc->is_connected = 1;
307 rpc->connect_cb(rpc, RPC_STATUS_SUCCESS, NULL, rpc->connect_data);
308 return 0;
309 }
310
b077fdeb
RS
311 if (revents & POLLIN) {
312 if (rpc_read_from_socket(rpc) != 0) {
83aa785d 313 rpc_disconnect_requeue(rpc);
b077fdeb 314 return 0;
84004dbf
RS
315 }
316 }
317
b077fdeb
RS
318 if (revents & POLLOUT && rpc->outqueue != NULL) {
319 if (rpc_write_to_socket(rpc) != 0) {
320 rpc_set_error(rpc, "write to socket failed");
1896d37b 321 return -1;
84004dbf
RS
322 }
323 }
324
325 return 0;
326}
327
84004dbf
RS
328int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data)
329{
330 struct sockaddr_storage s;
331 struct sockaddr_in *sin = (struct sockaddr_in *)&s;
332 int socksize;
333
334 if (rpc->fd != -1) {
335 rpc_set_error(rpc, "Trying to connect while already connected");
84004dbf
RS
336 return -1;
337 }
338
070287e5
RS
339 if (rpc->is_udp != 0) {
340 rpc_set_error(rpc, "Trying to connect on UDP socket");
341 return -1;
342 }
343
84004dbf
RS
344 sin->sin_family = AF_INET;
345 sin->sin_port = htons(port);
346 if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) {
347 rpc_set_error(rpc, "Not a valid server ip address");
1896d37b 348 return -1;
84004dbf
RS
349 }
350
351 switch (s.ss_family) {
352 case AF_INET:
84004dbf 353 socksize = sizeof(struct sockaddr_in);
f7f931c7 354#ifdef HAVE_SOCKADDR_LEN
ea214e45
RS
355 sin->sin_len = socksize;
356#endif
6874f61e 357 rpc->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
84004dbf
RS
358 break;
359 }
360
361 if (rpc->fd == -1) {
362 rpc_set_error(rpc, "Failed to open socket");
1896d37b 363 return -1;
84004dbf
RS
364 }
365
366 rpc->connect_cb = cb;
367 rpc->connect_data = private_data;
368
07fd0cbc
RS
369
370#if !defined(WIN32)
371 /* Some systems allow you to set capabilities on an executable
372 * to allow the file to be executed with privilege to bind to
373 * privileged system ports, even if the user is not root.
374 *
375 * Opportunistically try to bind the socket to a low numbered
376 * system port in the hope that the user is either root or the
377 * executable has the CAP_NET_BIND_SERVICE.
378 *
379 * As soon as we fail the bind() with EACCES we know we will never
380 * be able to bind to a system port so we terminate the loop.
381 *
382 * On linux, use
383 * sudo setcap 'cap_net_bind_service=+ep' /path/executable
384 * to make the executable able to bind to a system port.
385 */
386 if (1) {
387 int port;
388 int one = 1;
389
390 setsockopt(rpc->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
391
392 for (port = 200; port < 500; port++) {
393 struct sockaddr_in sin;
394
07fd0cbc
RS
395 memset(&sin, 0, sizeof(sin));
396 sin.sin_port = htons(port);
397 sin.sin_family = AF_INET;
398 sin.sin_addr.s_addr = 0;
399
400 if (bind(rpc->fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) != 0 && errno != EACCES) {
401 /* we didnt get EACCES, so try again */
402 continue;
403 }
404 break;
405 }
406 }
407#endif
408
84004dbf 409 set_nonblocking(rpc->fd);
07fd0cbc 410
4b0e8df9 411#if defined(WIN32)
fcc42bfe 412 if (connect(rpc->fd, (struct sockaddr *)&s, socksize) == 0 && errno != EINPROGRESS )
4b0e8df9 413#else
414 if (connect(rpc->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS)
415#endif
416 {
84004dbf 417 rpc_set_error(rpc, "connect() to server failed");
1896d37b 418 return -1;
84004dbf
RS
419 }
420
421 return 0;
422}
423
424int rpc_disconnect(struct rpc_context *rpc, char *error)
425{
426 if (rpc->fd != -1) {
6874f61e
RS
427#if defined(WIN32)
428 closesocket(rpc->fd);
429#else
84004dbf 430 close(rpc->fd);
6874f61e 431#endif
84004dbf
RS
432 }
433 rpc->fd = -1;
434
435 rpc->is_connected = 0;
436
437 rpc_error_all_pdus(rpc, error);
438
439 return 0;
440}
485bc9b9 441
b077fdeb
RS
442/* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue */
443static int rpc_disconnect_requeue(struct rpc_context *rpc)
444{
445 struct rpc_pdu *pdu;
446
447 if (rpc->fd != -1) {
6874f61e
RS
448#if defined(WIN32)
449 closesocket(rpc->fd);
450#else
b077fdeb 451 close(rpc->fd);
6874f61e 452#endif
b077fdeb
RS
453 }
454 rpc->fd = -1;
455
456 rpc->is_connected = 0;
457
458 /* socket is closed so we will not get any replies to any commands
459 * in flight. Move them all over from the waitpdu queue back to the out queue
460 */
461 for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
462 SLIST_REMOVE(&rpc->waitpdu, pdu);
463 SLIST_ADD(&rpc->outqueue, pdu);
464 }
465
466 return 0;
467}
468
485bc9b9
RS
469
470int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port)
471{
472 struct addrinfo *ai = NULL;
473 char service[6];
474
475 if (rpc->is_udp == 0) {
476 rpc_set_error(rpc, "Cant not bind UDP. Not UDP context");
477 return -1;
478 }
479
6874f61e 480 sprintf(service, "%d", port);
485bc9b9
RS
481 if (getaddrinfo(addr, service, NULL, &ai) != 0) {
482 rpc_set_error(rpc, "Invalid address:%s. "
483 "Can not resolv into IPv4/v6 structure.");
484 return -1;
485 }
486
487 switch(ai->ai_family) {
488 case AF_INET:
489 rpc->fd = socket(ai->ai_family, SOCK_DGRAM, 0);
490 if (rpc->fd == -1) {
491 rpc_set_error(rpc, "Failed to create UDP socket: %s", strerror(errno));
492 freeaddrinfo(ai);
493 return -1;
494 }
495
496 if (bind(rpc->fd, (struct sockaddr *)ai->ai_addr, sizeof(struct sockaddr_in)) != 0) {
497 rpc_set_error(rpc, "Failed to bind to UDP socket: %s",strerror(errno));
498 freeaddrinfo(ai);
499 return -1;
500 }
501 break;
502 default:
503 rpc_set_error(rpc, "Can not handle UPD sockets of family %d yet", ai->ai_family);
504 freeaddrinfo(ai);
505 return -1;
506 }
507
508 freeaddrinfo(ai);
509
510 return 0;
511}
512
5bf60dc6
RS
513int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int is_broadcast)
514{
515 struct addrinfo *ai = NULL;
516 char service[6];
517
518 if (rpc->is_udp == 0) {
519 rpc_set_error(rpc, "Can not set destination sockaddr. Not UDP context");
520 return -1;
521 }
522
6874f61e 523 sprintf(service, "%d", port);
5bf60dc6
RS
524 if (getaddrinfo(addr, service, NULL, &ai) != 0) {
525 rpc_set_error(rpc, "Invalid address:%s. "
526 "Can not resolv into IPv4/v6 structure.");
527 return -1;
528 }
529
530 if (rpc->udp_dest) {
531 free(rpc->udp_dest);
532 rpc->udp_dest = NULL;
533 }
534 rpc->udp_dest = malloc(ai->ai_addrlen);
535 if (rpc->udp_dest == NULL) {
536 rpc_set_error(rpc, "Out of memory. Failed to allocate sockaddr structure");
537 return -1;
538 }
539 memcpy(rpc->udp_dest, ai->ai_addr, ai->ai_addrlen);
540 freeaddrinfo(ai);
541
542 rpc->is_broadcast = is_broadcast;
bb4e9ed6 543 setsockopt(rpc->fd, SOL_SOCKET, SO_BROADCAST, (char *)&is_broadcast, sizeof(is_broadcast));
5bf60dc6
RS
544
545 return 0;
546}
c481da67
RS
547
548struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc)
549{
550 return (struct sockaddr *)&rpc->udp_src;
551}
83aa785d
RS
552
553int rpc_queue_length(struct rpc_context *rpc)
554{
555 int i=0;
556 struct rpc_pdu *pdu;
557
558 for(pdu = rpc->outqueue; pdu; pdu = pdu->next) {
559 i++;
560 }
561 for(pdu = rpc->waitpdu; pdu; pdu = pdu->next) {
562 i++;
563 }
564 return i;
565}