[win32] - make it compile on win32
[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{
6874f61e
RS
69#if defined(WIN32)
70#else
84004dbf
RS
71 unsigned v;
72 v = fcntl(fd, F_GETFL, 0);
73 fcntl(fd, F_SETFL, v | O_NONBLOCK);
a8a1b858 74#endif //FIXME
84004dbf
RS
75}
76
77int rpc_get_fd(struct rpc_context *rpc)
78{
79 return rpc->fd;
80}
81
82int rpc_which_events(struct rpc_context *rpc)
83{
912f7ad5 84 int events = rpc->is_connected ? POLLIN : POLLOUT;
84004dbf 85
5911f3e8
RS
86 if (rpc->is_udp != 0) {
87 /* for udp sockets we only wait for pollin */
88 return POLLIN;
89 }
90
84004dbf
RS
91 if (rpc->outqueue) {
92 events |= POLLOUT;
93 }
94 return events;
95}
96
97static int rpc_write_to_socket(struct rpc_context *rpc)
98{
99 ssize_t count;
100
101 if (rpc == NULL) {
84004dbf
RS
102 return -1;
103 }
104 if (rpc->fd == -1) {
1896d37b
RS
105 rpc_set_error(rpc, "trying to write but not connected");
106 return -1;
84004dbf
RS
107 }
108
109 while (rpc->outqueue != NULL) {
110 ssize_t total;
111
112 total = rpc->outqueue->outdata.size;
113
6874f61e
RS
114#if defined(WIN32)
115 count = send(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written, 0);
116#else
84004dbf 117 count = write(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written);
6874f61e 118#endif
84004dbf
RS
119 if (count == -1) {
120 if (errno == EAGAIN || errno == EWOULDBLOCK) {
84004dbf
RS
121 return 0;
122 }
1896d37b
RS
123 rpc_set_error(rpc, "Error when writing to socket :%s(%d)", strerror(errno), errno);
124 return -1;
84004dbf
RS
125 }
126
127 rpc->outqueue->written += count;
128 if (rpc->outqueue->written == total) {
129 struct rpc_pdu *pdu = rpc->outqueue;
130
131 SLIST_REMOVE(&rpc->outqueue, pdu);
132 SLIST_ADD_END(&rpc->waitpdu, pdu);
133 }
134 }
135 return 0;
136}
137
138static int rpc_read_from_socket(struct rpc_context *rpc)
139{
140 int available;
141 int size;
cdb19ec1 142 int pdu_size;
84004dbf
RS
143 ssize_t count;
144
6874f61e
RS
145#if defined(WIN32)
146 if (ioctlsocket(rpc->fd, FIONREAD, &available) != 0) {
147#else
84004dbf 148 if (ioctl(rpc->fd, FIONREAD, &available) != 0) {
6874f61e 149#endif
84004dbf
RS
150 rpc_set_error(rpc, "Ioctl FIONREAD returned error : %d. Closing socket.", errno);
151 return -1;
152 }
a8a1b858 153
84004dbf
RS
154 if (available == 0) {
155 rpc_set_error(rpc, "Socket has been closed");
1896d37b 156 return -1;
84004dbf 157 }
cdb19ec1 158
0268794f
RS
159 if (rpc->is_udp) {
160 char *buf;
161 socklen_t socklen = sizeof(rpc->udp_src);
162
163 buf = malloc(available);
164 if (buf == NULL) {
165 rpc_set_error(rpc, "Failed to malloc buffer for recvfrom");
166 return -1;
167 }
168 count = recvfrom(rpc->fd, buf, available, MSG_DONTWAIT, (struct sockaddr *)&rpc->udp_src, &socklen);
169 if (count < 0) {
170 rpc_set_error(rpc, "Failed recvfrom: %s", strerror(errno));
171 free(buf);
172 }
173 if (rpc_process_pdu(rpc, buf, count) != 0) {
174 rpc_set_error(rpc, "Invalid/garbage pdu received from server. Ignoring PDU");
175 free(buf);
176 return -1;
177 }
178 free(buf);
179 return 0;
180 }
181
cdb19ec1
RS
182 /* read record marker, 4 bytes at the beginning of every pdu */
183 if (rpc->inbuf == NULL) {
184 rpc->insize = 4;
185 rpc->inbuf = malloc(rpc->insize);
186 if (rpc->inbuf == NULL) {
187 rpc_set_error(rpc, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno);
188 return -1;
189 }
190 }
191 if (rpc->inpos < 4) {
192 size = 4 - rpc->inpos;
193
6874f61e
RS
194#if defined(WIN32)
195 count = recv(rpc->fd, rpc->inbuf + rpc->inpos, size, 0);
196#else
cdb19ec1 197 count = read(rpc->fd, rpc->inbuf + rpc->inpos, size);
6874f61e 198#endif
cdb19ec1
RS
199 if (count == -1) {
200 if (errno == EINTR) {
201 return 0;
202 }
203 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
204 return -1;
205 }
206 available -= count;
207 rpc->inpos += count;
208 }
209
210 if (available == 0) {
211 return 0;
212 }
213
214 pdu_size = rpc_get_pdu_size(rpc->inbuf);
215 if (rpc->insize < pdu_size) {
216 unsigned char *buf;
217
218 buf = malloc(pdu_size);
219 if (buf == NULL) {
220 rpc_set_error(rpc, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size, errno);
221 return -1;
222 }
223 memcpy(buf, rpc->inbuf, rpc->insize);
224 free(rpc->inbuf);
225 rpc->inbuf = buf;
226 rpc->insize = rpc_get_pdu_size(rpc->inbuf);
84004dbf 227 }
cdb19ec1
RS
228
229 size = available;
230 if (size > rpc->insize - rpc->inpos) {
231 size = rpc->insize - rpc->inpos;
84004dbf
RS
232 }
233
6874f61e
RS
234#if defined(WIN32)
235 count = recv(rpc->fd, rpc->inbuf + rpc->inpos, size, 0);
236#else
cdb19ec1 237 count = read(rpc->fd, rpc->inbuf + rpc->inpos, size);
6874f61e 238#endif
84004dbf
RS
239 if (count == -1) {
240 if (errno == EINTR) {
84004dbf
RS
241 return 0;
242 }
243 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
1896d37b 244 return -1;
84004dbf 245 }
cdb19ec1
RS
246 available -= count;
247 rpc->inpos += count;
84004dbf 248
cdb19ec1
RS
249 if (rpc->inpos == rpc->insize) {
250 if (rpc_process_pdu(rpc, rpc->inbuf, pdu_size) != 0) {
84004dbf 251 rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket");
1896d37b 252 return -1;
84004dbf 253 }
cdb19ec1
RS
254 free(rpc->inbuf);
255 rpc->inbuf = NULL;
256 rpc->insize = 0;
257 rpc->inpos = 0;
84004dbf 258 }
cdb19ec1 259
84004dbf
RS
260 return 0;
261}
262
263
264
265int rpc_service(struct rpc_context *rpc, int revents)
266{
267 if (revents & POLLERR) {
a8a1b858 268 char err = 0;
912f7ad5
RS
269 socklen_t err_size = sizeof(err);
270
271 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
bb4e9ed6 272 (char *)&err, &err_size) != 0 || err != 0) {
912f7ad5
RS
273 if (err == 0) {
274 err = errno;
275 }
276 rpc_set_error(rpc, "rpc_service: socket error "
277 "%s(%d).",
278 strerror(err), err);
84004dbf 279 } else {
912f7ad5
RS
280 rpc_set_error(rpc, "rpc_service: POLLERR, "
281 "Unknown socket error.");
84004dbf
RS
282 }
283 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
284 return -1;
285 }
286 if (revents & POLLHUP) {
84004dbf
RS
287 rpc_set_error(rpc, "Socket failed with POLLHUP");
288 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
1896d37b 289 return -1;
84004dbf
RS
290 }
291
292 if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) {
912f7ad5
RS
293 int err = 0;
294 socklen_t err_size = sizeof(err);
295
296 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
bb4e9ed6 297 (char *)&err, &err_size) != 0 || err != 0) {
912f7ad5
RS
298 if (err == 0) {
299 err = errno;
300 }
301 rpc_set_error(rpc, "rpc_service: socket error "
302 "%s(%d) while connecting.",
303 strerror(err), err);
304 rpc->connect_cb(rpc, RPC_STATUS_ERROR,
305 NULL, rpc->connect_data);
306 return -1;
307 }
308
84004dbf
RS
309 rpc->is_connected = 1;
310 rpc->connect_cb(rpc, RPC_STATUS_SUCCESS, NULL, rpc->connect_data);
311 return 0;
312 }
313
b077fdeb
RS
314 if (revents & POLLIN) {
315 if (rpc_read_from_socket(rpc) != 0) {
316 rpc_disconnect_requeue(rpc);
317 return 0;
84004dbf
RS
318 }
319 }
320
b077fdeb
RS
321 if (revents & POLLOUT && rpc->outqueue != NULL) {
322 if (rpc_write_to_socket(rpc) != 0) {
323 rpc_set_error(rpc, "write to socket failed");
1896d37b 324 return -1;
84004dbf
RS
325 }
326 }
327
328 return 0;
329}
330
331
332int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data)
333{
334 struct sockaddr_storage s;
335 struct sockaddr_in *sin = (struct sockaddr_in *)&s;
336 int socksize;
337
338 if (rpc->fd != -1) {
339 rpc_set_error(rpc, "Trying to connect while already connected");
84004dbf
RS
340 return -1;
341 }
342
070287e5
RS
343 if (rpc->is_udp != 0) {
344 rpc_set_error(rpc, "Trying to connect on UDP socket");
345 return -1;
346 }
347
84004dbf
RS
348 sin->sin_family = AF_INET;
349 sin->sin_port = htons(port);
350 if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) {
351 rpc_set_error(rpc, "Not a valid server ip address");
1896d37b 352 return -1;
84004dbf
RS
353 }
354
355 switch (s.ss_family) {
356 case AF_INET:
84004dbf 357 socksize = sizeof(struct sockaddr_in);
f7f931c7 358#ifdef HAVE_SOCKADDR_LEN
ea214e45
RS
359 sin->sin_len = socksize;
360#endif
6874f61e 361 rpc->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
84004dbf
RS
362 break;
363 }
364
365 if (rpc->fd == -1) {
366 rpc_set_error(rpc, "Failed to open socket");
1896d37b 367 return -1;
84004dbf
RS
368 }
369
370 rpc->connect_cb = cb;
371 rpc->connect_data = private_data;
372
373 set_nonblocking(rpc->fd);
374
375 if (connect(rpc->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS) {
376 rpc_set_error(rpc, "connect() to server failed");
1896d37b 377 return -1;
84004dbf
RS
378 }
379
380 return 0;
381}
382
383int rpc_disconnect(struct rpc_context *rpc, char *error)
384{
385 if (rpc->fd != -1) {
6874f61e
RS
386#if defined(WIN32)
387 closesocket(rpc->fd);
388#else
84004dbf 389 close(rpc->fd);
6874f61e 390#endif
84004dbf
RS
391 }
392 rpc->fd = -1;
393
394 rpc->is_connected = 0;
395
396 rpc_error_all_pdus(rpc, error);
397
398 return 0;
399}
485bc9b9 400
b077fdeb
RS
401/* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue */
402static int rpc_disconnect_requeue(struct rpc_context *rpc)
403{
404 struct rpc_pdu *pdu;
405
406 if (rpc->fd != -1) {
6874f61e
RS
407#if defined(WIN32)
408 closesocket(rpc->fd);
409#else
b077fdeb 410 close(rpc->fd);
6874f61e 411#endif
b077fdeb
RS
412 }
413 rpc->fd = -1;
414
415 rpc->is_connected = 0;
416
417 /* socket is closed so we will not get any replies to any commands
418 * in flight. Move them all over from the waitpdu queue back to the out queue
419 */
420 for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
421 SLIST_REMOVE(&rpc->waitpdu, pdu);
422 SLIST_ADD(&rpc->outqueue, pdu);
423 }
424
425 return 0;
426}
427
485bc9b9
RS
428
429int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port)
430{
431 struct addrinfo *ai = NULL;
432 char service[6];
433
434 if (rpc->is_udp == 0) {
435 rpc_set_error(rpc, "Cant not bind UDP. Not UDP context");
436 return -1;
437 }
438
6874f61e 439 sprintf(service, "%d", port);
485bc9b9
RS
440 if (getaddrinfo(addr, service, NULL, &ai) != 0) {
441 rpc_set_error(rpc, "Invalid address:%s. "
442 "Can not resolv into IPv4/v6 structure.");
443 return -1;
444 }
445
446 switch(ai->ai_family) {
447 case AF_INET:
448 rpc->fd = socket(ai->ai_family, SOCK_DGRAM, 0);
449 if (rpc->fd == -1) {
450 rpc_set_error(rpc, "Failed to create UDP socket: %s", strerror(errno));
451 freeaddrinfo(ai);
452 return -1;
453 }
454
455 if (bind(rpc->fd, (struct sockaddr *)ai->ai_addr, sizeof(struct sockaddr_in)) != 0) {
456 rpc_set_error(rpc, "Failed to bind to UDP socket: %s",strerror(errno));
457 freeaddrinfo(ai);
458 return -1;
459 }
460 break;
461 default:
462 rpc_set_error(rpc, "Can not handle UPD sockets of family %d yet", ai->ai_family);
463 freeaddrinfo(ai);
464 return -1;
465 }
466
467 freeaddrinfo(ai);
468
469 return 0;
470}
471
5bf60dc6
RS
472int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int is_broadcast)
473{
474 struct addrinfo *ai = NULL;
475 char service[6];
476
477 if (rpc->is_udp == 0) {
478 rpc_set_error(rpc, "Can not set destination sockaddr. Not UDP context");
479 return -1;
480 }
481
6874f61e 482 sprintf(service, "%d", port);
5bf60dc6
RS
483 if (getaddrinfo(addr, service, NULL, &ai) != 0) {
484 rpc_set_error(rpc, "Invalid address:%s. "
485 "Can not resolv into IPv4/v6 structure.");
486 return -1;
487 }
488
489 if (rpc->udp_dest) {
490 free(rpc->udp_dest);
491 rpc->udp_dest = NULL;
492 }
493 rpc->udp_dest = malloc(ai->ai_addrlen);
494 if (rpc->udp_dest == NULL) {
495 rpc_set_error(rpc, "Out of memory. Failed to allocate sockaddr structure");
496 return -1;
497 }
498 memcpy(rpc->udp_dest, ai->ai_addr, ai->ai_addrlen);
499 freeaddrinfo(ai);
500
501 rpc->is_broadcast = is_broadcast;
bb4e9ed6 502 setsockopt(rpc->fd, SOL_SOCKET, SO_BROADCAST, (char *)&is_broadcast, sizeof(is_broadcast));
5bf60dc6
RS
503
504 return 0;
505}
c481da67
RS
506
507struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc)
508{
509 return (struct sockaddr *)&rpc->udp_src;
510}