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