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