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