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