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