[add] - implement set_non_blocking for socket
[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) {
318 rpc_disconnect_requeue(rpc);
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
333
334int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data)
335{
336 struct sockaddr_storage s;
337 struct sockaddr_in *sin = (struct sockaddr_in *)&s;
338 int socksize;
339
340 if (rpc->fd != -1) {
341 rpc_set_error(rpc, "Trying to connect while already connected");
84004dbf
RS
342 return -1;
343 }
344
070287e5
RS
345 if (rpc->is_udp != 0) {
346 rpc_set_error(rpc, "Trying to connect on UDP socket");
347 return -1;
348 }
349
84004dbf
RS
350 sin->sin_family = AF_INET;
351 sin->sin_port = htons(port);
352 if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) {
353 rpc_set_error(rpc, "Not a valid server ip address");
1896d37b 354 return -1;
84004dbf
RS
355 }
356
357 switch (s.ss_family) {
358 case AF_INET:
84004dbf 359 socksize = sizeof(struct sockaddr_in);
f7f931c7 360#ifdef HAVE_SOCKADDR_LEN
ea214e45
RS
361 sin->sin_len = socksize;
362#endif
6874f61e 363 rpc->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
84004dbf
RS
364 break;
365 }
366
367 if (rpc->fd == -1) {
368 rpc_set_error(rpc, "Failed to open socket");
1896d37b 369 return -1;
84004dbf
RS
370 }
371
372 rpc->connect_cb = cb;
373 rpc->connect_data = private_data;
374
375 set_nonblocking(rpc->fd);
376
377 if (connect(rpc->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS) {
378 rpc_set_error(rpc, "connect() to server failed");
1896d37b 379 return -1;
84004dbf
RS
380 }
381
382 return 0;
383}
384
385int rpc_disconnect(struct rpc_context *rpc, char *error)
386{
387 if (rpc->fd != -1) {
6874f61e
RS
388#if defined(WIN32)
389 closesocket(rpc->fd);
390#else
84004dbf 391 close(rpc->fd);
6874f61e 392#endif
84004dbf
RS
393 }
394 rpc->fd = -1;
395
396 rpc->is_connected = 0;
397
398 rpc_error_all_pdus(rpc, error);
399
400 return 0;
401}
485bc9b9 402
b077fdeb
RS
403/* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue */
404static int rpc_disconnect_requeue(struct rpc_context *rpc)
405{
406 struct rpc_pdu *pdu;
407
408 if (rpc->fd != -1) {
6874f61e
RS
409#if defined(WIN32)
410 closesocket(rpc->fd);
411#else
b077fdeb 412 close(rpc->fd);
6874f61e 413#endif
b077fdeb
RS
414 }
415 rpc->fd = -1;
416
417 rpc->is_connected = 0;
418
419 /* socket is closed so we will not get any replies to any commands
420 * in flight. Move them all over from the waitpdu queue back to the out queue
421 */
422 for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
423 SLIST_REMOVE(&rpc->waitpdu, pdu);
424 SLIST_ADD(&rpc->outqueue, pdu);
425 }
426
427 return 0;
428}
429
485bc9b9
RS
430
431int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port)
432{
433 struct addrinfo *ai = NULL;
434 char service[6];
435
436 if (rpc->is_udp == 0) {
437 rpc_set_error(rpc, "Cant not bind UDP. Not UDP context");
438 return -1;
439 }
440
6874f61e 441 sprintf(service, "%d", port);
485bc9b9
RS
442 if (getaddrinfo(addr, service, NULL, &ai) != 0) {
443 rpc_set_error(rpc, "Invalid address:%s. "
444 "Can not resolv into IPv4/v6 structure.");
445 return -1;
446 }
447
448 switch(ai->ai_family) {
449 case AF_INET:
450 rpc->fd = socket(ai->ai_family, SOCK_DGRAM, 0);
451 if (rpc->fd == -1) {
452 rpc_set_error(rpc, "Failed to create UDP socket: %s", strerror(errno));
453 freeaddrinfo(ai);
454 return -1;
455 }
456
457 if (bind(rpc->fd, (struct sockaddr *)ai->ai_addr, sizeof(struct sockaddr_in)) != 0) {
458 rpc_set_error(rpc, "Failed to bind to UDP socket: %s",strerror(errno));
459 freeaddrinfo(ai);
460 return -1;
461 }
462 break;
463 default:
464 rpc_set_error(rpc, "Can not handle UPD sockets of family %d yet", ai->ai_family);
465 freeaddrinfo(ai);
466 return -1;
467 }
468
469 freeaddrinfo(ai);
470
471 return 0;
472}
473
5bf60dc6
RS
474int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int is_broadcast)
475{
476 struct addrinfo *ai = NULL;
477 char service[6];
478
479 if (rpc->is_udp == 0) {
480 rpc_set_error(rpc, "Can not set destination sockaddr. Not UDP context");
481 return -1;
482 }
483
6874f61e 484 sprintf(service, "%d", port);
5bf60dc6
RS
485 if (getaddrinfo(addr, service, NULL, &ai) != 0) {
486 rpc_set_error(rpc, "Invalid address:%s. "
487 "Can not resolv into IPv4/v6 structure.");
488 return -1;
489 }
490
491 if (rpc->udp_dest) {
492 free(rpc->udp_dest);
493 rpc->udp_dest = NULL;
494 }
495 rpc->udp_dest = malloc(ai->ai_addrlen);
496 if (rpc->udp_dest == NULL) {
497 rpc_set_error(rpc, "Out of memory. Failed to allocate sockaddr structure");
498 return -1;
499 }
500 memcpy(rpc->udp_dest, ai->ai_addr, ai->ai_addrlen);
501 freeaddrinfo(ai);
502
503 rpc->is_broadcast = is_broadcast;
bb4e9ed6 504 setsockopt(rpc->fd, SOL_SOCKET, SO_BROADCAST, (char *)&is_broadcast, sizeof(is_broadcast));
5bf60dc6
RS
505
506 return 0;
507}
c481da67
RS
508
509struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc)
510{
511 return (struct sockaddr *)&rpc->udp_src;
512}