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