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 | */ | |
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 | |
fc01d2a9 TN |
28 | #ifdef HAVE_CONFIG_H |
29 | #include "config.h" | |
30 | #endif | |
84004dbf | 31 | #include <stdio.h> |
98f5fee8 | 32 | #include <stdlib.h> |
f3a75078 | 33 | #include <assert.h> |
84004dbf | 34 | #include <fcntl.h> |
84004dbf RS |
35 | #include <string.h> |
36 | #include <errno.h> | |
fc01d2a9 TN |
37 | #ifdef HAVE_SYS_FILIO_H |
38 | #include <sys/filio.h> | |
39 | #endif | |
647d2ea1 RS |
40 | #ifdef HAVE_SYS_SOCKIO_H |
41 | #include <sys/sockio.h> | |
42 | #endif | |
485bc9b9 | 43 | #include <sys/types.h> |
763cd6e3 | 44 | #include "libnfs-zdr.h" |
84004dbf RS |
45 | #include "libnfs.h" |
46 | #include "libnfs-raw.h" | |
47 | #include "libnfs-private.h" | |
48 | #include "slist.h" | |
49 | ||
fcc42bfe M |
50 | #ifdef WIN32 |
51 | //has to be included after stdlib!! | |
52 | #include "win32_errnowrapper.h" | |
53 | #endif | |
54 | ||
55 | ||
1744ef90 RS |
56 | static int rpc_reconnect_requeue(struct rpc_context *rpc); |
57 | static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_storage *s); | |
b077fdeb | 58 | |
84004dbf RS |
59 | static void set_nonblocking(int fd) |
60 | { | |
99c14c9b | 61 | int v = 0; |
6874f61e | 62 | #if defined(WIN32) |
99c14c9b | 63 | long nonblocking=1; |
64 | v = ioctlsocket(fd, FIONBIO,&nonblocking); | |
6874f61e | 65 | #else |
84004dbf RS |
66 | v = fcntl(fd, F_GETFL, 0); |
67 | fcntl(fd, F_SETFL, v | O_NONBLOCK); | |
a8a1b858 | 68 | #endif //FIXME |
84004dbf RS |
69 | } |
70 | ||
71 | int rpc_get_fd(struct rpc_context *rpc) | |
72 | { | |
f3a75078 RS |
73 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
74 | ||
84004dbf RS |
75 | return rpc->fd; |
76 | } | |
77 | ||
78 | int rpc_which_events(struct rpc_context *rpc) | |
79 | { | |
f3a75078 RS |
80 | int events; |
81 | ||
82 | assert(rpc->magic == RPC_CONTEXT_MAGIC); | |
83 | ||
84 | events = rpc->is_connected ? POLLIN : POLLOUT; | |
84004dbf | 85 | |
5911f3e8 RS |
86 | if (rpc->is_udp != 0) { |
87 | /* for udp sockets we only wait for pollin */ | |
88 | return POLLIN; | |
89 | } | |
90 | ||
84004dbf RS |
91 | if (rpc->outqueue) { |
92 | events |= POLLOUT; | |
93 | } | |
94 | return events; | |
95 | } | |
96 | ||
97 | static int rpc_write_to_socket(struct rpc_context *rpc) | |
98 | { | |
d14e2838 | 99 | int32_t count; |
84004dbf | 100 | |
f3a75078 RS |
101 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
102 | ||
84004dbf | 103 | if (rpc->fd == -1) { |
1896d37b RS |
104 | rpc_set_error(rpc, "trying to write but not connected"); |
105 | return -1; | |
84004dbf RS |
106 | } |
107 | ||
108 | while (rpc->outqueue != NULL) { | |
183451cf | 109 | int64_t total; |
84004dbf RS |
110 | |
111 | total = rpc->outqueue->outdata.size; | |
112 | ||
6874f61e RS |
113 | #if defined(WIN32) |
114 | count = send(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written, 0); | |
115 | #else | |
84004dbf | 116 | count = write(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written); |
6874f61e | 117 | #endif |
84004dbf RS |
118 | if (count == -1) { |
119 | if (errno == EAGAIN || errno == EWOULDBLOCK) { | |
84004dbf RS |
120 | return 0; |
121 | } | |
1896d37b RS |
122 | rpc_set_error(rpc, "Error when writing to socket :%s(%d)", strerror(errno), errno); |
123 | return -1; | |
84004dbf RS |
124 | } |
125 | ||
126 | rpc->outqueue->written += count; | |
127 | if (rpc->outqueue->written == total) { | |
128 | struct rpc_pdu *pdu = rpc->outqueue; | |
129 | ||
130 | SLIST_REMOVE(&rpc->outqueue, pdu); | |
131 | SLIST_ADD_END(&rpc->waitpdu, pdu); | |
132 | } | |
133 | } | |
134 | return 0; | |
135 | } | |
136 | ||
137 | static int rpc_read_from_socket(struct rpc_context *rpc) | |
138 | { | |
139 | int available; | |
140 | int size; | |
cdb19ec1 | 141 | int pdu_size; |
d14e2838 | 142 | int32_t count; |
84004dbf | 143 | |
f3a75078 RS |
144 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
145 | ||
6874f61e RS |
146 | #if defined(WIN32) |
147 | if (ioctlsocket(rpc->fd, FIONREAD, &available) != 0) { | |
148 | #else | |
84004dbf | 149 | if (ioctl(rpc->fd, FIONREAD, &available) != 0) { |
6874f61e | 150 | #endif |
84004dbf RS |
151 | rpc_set_error(rpc, "Ioctl FIONREAD returned error : %d. Closing socket.", errno); |
152 | return -1; | |
153 | } | |
a8a1b858 | 154 | |
84004dbf RS |
155 | if (available == 0) { |
156 | rpc_set_error(rpc, "Socket has been closed"); | |
1896d37b | 157 | return -1; |
84004dbf | 158 | } |
cdb19ec1 | 159 | |
0268794f RS |
160 | if (rpc->is_udp) { |
161 | char *buf; | |
162 | socklen_t socklen = sizeof(rpc->udp_src); | |
163 | ||
164 | buf = malloc(available); | |
165 | if (buf == NULL) { | |
166 | rpc_set_error(rpc, "Failed to malloc buffer for recvfrom"); | |
167 | return -1; | |
168 | } | |
169 | count = recvfrom(rpc->fd, buf, available, MSG_DONTWAIT, (struct sockaddr *)&rpc->udp_src, &socklen); | |
170 | if (count < 0) { | |
171 | rpc_set_error(rpc, "Failed recvfrom: %s", strerror(errno)); | |
172 | free(buf); | |
173 | } | |
174 | if (rpc_process_pdu(rpc, buf, count) != 0) { | |
175 | rpc_set_error(rpc, "Invalid/garbage pdu received from server. Ignoring PDU"); | |
176 | free(buf); | |
177 | return -1; | |
178 | } | |
179 | free(buf); | |
180 | return 0; | |
181 | } | |
182 | ||
cdb19ec1 RS |
183 | /* read record marker, 4 bytes at the beginning of every pdu */ |
184 | if (rpc->inbuf == NULL) { | |
185 | rpc->insize = 4; | |
186 | rpc->inbuf = malloc(rpc->insize); | |
187 | if (rpc->inbuf == NULL) { | |
188 | rpc_set_error(rpc, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno); | |
189 | return -1; | |
190 | } | |
191 | } | |
192 | if (rpc->inpos < 4) { | |
193 | size = 4 - rpc->inpos; | |
194 | ||
6874f61e RS |
195 | #if defined(WIN32) |
196 | count = recv(rpc->fd, rpc->inbuf + rpc->inpos, size, 0); | |
197 | #else | |
cdb19ec1 | 198 | count = read(rpc->fd, rpc->inbuf + rpc->inpos, size); |
6874f61e | 199 | #endif |
cdb19ec1 RS |
200 | if (count == -1) { |
201 | if (errno == EINTR) { | |
202 | return 0; | |
203 | } | |
204 | rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno); | |
205 | return -1; | |
206 | } | |
207 | available -= count; | |
208 | rpc->inpos += count; | |
209 | } | |
210 | ||
211 | if (available == 0) { | |
212 | return 0; | |
213 | } | |
214 | ||
215 | pdu_size = rpc_get_pdu_size(rpc->inbuf); | |
216 | if (rpc->insize < pdu_size) { | |
217 | unsigned char *buf; | |
218 | ||
219 | buf = malloc(pdu_size); | |
220 | if (buf == NULL) { | |
221 | rpc_set_error(rpc, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size, errno); | |
222 | return -1; | |
223 | } | |
224 | memcpy(buf, rpc->inbuf, rpc->insize); | |
225 | free(rpc->inbuf); | |
226 | rpc->inbuf = buf; | |
227 | rpc->insize = rpc_get_pdu_size(rpc->inbuf); | |
84004dbf | 228 | } |
cdb19ec1 RS |
229 | |
230 | size = available; | |
231 | if (size > rpc->insize - rpc->inpos) { | |
232 | size = rpc->insize - rpc->inpos; | |
84004dbf RS |
233 | } |
234 | ||
6874f61e RS |
235 | #if defined(WIN32) |
236 | count = recv(rpc->fd, rpc->inbuf + rpc->inpos, size, 0); | |
237 | #else | |
cdb19ec1 | 238 | count = read(rpc->fd, rpc->inbuf + rpc->inpos, size); |
6874f61e | 239 | #endif |
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 | { | |
f3a75078 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 | { |
f3a75078 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 | { | |
f3a75078 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 | |
f3a75078 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. | |
2521374f RI |
395 | * |
396 | * On Windows, there is no concept of privileged ports. Thus | |
397 | * binding will usually succeed. | |
07fd0cbc | 398 | */ |
2521374f 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; | |
404 | int startOfs = portOfs, port, rc; | |
405 | ||
406 | do { | |
407 | rc = -1; | |
408 | port = htons(firstPort + portOfs); | |
409 | portOfs = (portOfs + 1) % portCount; | |
410 | ||
411 | /* skip well-known ports */ | |
412 | if (!getservbyport(port, "tcp")) { | |
413 | memset(&sin, 0, sizeof(sin)); | |
414 | sin.sin_port = port; | |
415 | sin.sin_family = AF_INET; | |
416 | sin.sin_addr.s_addr = 0; | |
417 | ||
418 | rc = bind(rpc->fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)); | |
419 | #if !defined(WIN32) | |
420 | /* we got EACCES, so don't try again */ | |
421 | if (rc != 0 && errno == EACCES) | |
422 | break; | |
423 | #endif | |
07fd0cbc | 424 | } |
2521374f | 425 | } while (rc != 0 && portOfs != startOfs); |
07fd0cbc | 426 | } |
07fd0cbc | 427 | |
84004dbf | 428 | set_nonblocking(rpc->fd); |
07fd0cbc | 429 | |
456c0b14 RI |
430 | if (connect(rpc->fd, (struct sockaddr *)s, socksize) != 0 && errno != EINPROGRESS) { |
431 | rpc_set_error(rpc, "connect() to server failed. %s(%d)", strerror(errno), errno); | |
1896d37b | 432 | return -1; |
84004dbf RS |
433 | } |
434 | ||
435 | return 0; | |
436 | } | |
437 | ||
1744ef90 RS |
438 | int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data) |
439 | { | |
440 | struct sockaddr_in *sin = (struct sockaddr_in *)&rpc->s; | |
441 | ||
f3a75078 RS |
442 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
443 | ||
1744ef90 RS |
444 | if (rpc->fd != -1) { |
445 | rpc_set_error(rpc, "Trying to connect while already connected"); | |
446 | return -1; | |
447 | } | |
448 | ||
449 | if (rpc->is_udp != 0) { | |
450 | rpc_set_error(rpc, "Trying to connect on UDP socket"); | |
451 | return -1; | |
452 | } | |
453 | ||
454 | rpc->auto_reconnect = 0; | |
455 | ||
456 | sin->sin_family = AF_INET; | |
457 | sin->sin_port = htons(port); | |
458 | if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) { | |
459 | rpc_set_error(rpc, "Not a valid server ip address"); | |
460 | return -1; | |
461 | } | |
462 | ||
463 | ||
464 | switch (rpc->s.ss_family) { | |
465 | case AF_INET: | |
466 | #ifdef HAVE_SOCKADDR_LEN | |
467 | sin->sin_len = sizeof(struct sockaddr_in); | |
468 | #endif | |
469 | break; | |
470 | } | |
471 | ||
472 | rpc->connect_cb = cb; | |
473 | rpc->connect_data = private_data; | |
474 | ||
475 | if (rpc_connect_sockaddr_async(rpc, &rpc->s) != 0) { | |
476 | return -1; | |
477 | } | |
478 | ||
479 | return 0; | |
480 | } | |
481 | ||
84004dbf RS |
482 | int rpc_disconnect(struct rpc_context *rpc, char *error) |
483 | { | |
f3a75078 RS |
484 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
485 | ||
1744ef90 RS |
486 | rpc_unset_autoreconnect(rpc); |
487 | ||
84004dbf | 488 | if (rpc->fd != -1) { |
6874f61e RS |
489 | #if defined(WIN32) |
490 | closesocket(rpc->fd); | |
491 | #else | |
84004dbf | 492 | close(rpc->fd); |
6874f61e | 493 | #endif |
84004dbf RS |
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 | { | |
f3a75078 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 | ||
f3a75078 RS |
522 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
523 | ||
b077fdeb | 524 | if (rpc->fd != -1) { |
6874f61e RS |
525 | #if defined(WIN32) |
526 | closesocket(rpc->fd); | |
527 | #else | |
b077fdeb | 528 | close(rpc->fd); |
6874f61e | 529 | #endif |
b077fdeb RS |
530 | } |
531 | rpc->fd = -1; | |
532 | ||
533 | rpc->is_connected = 0; | |
534 | ||
535 | /* socket is closed so we will not get any replies to any commands | |
536 | * in flight. Move them all over from the waitpdu queue back to the out queue | |
537 | */ | |
538 | for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) { | |
539 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
540 | SLIST_ADD(&rpc->outqueue, pdu); | |
1744ef90 RS |
541 | /* we have to re-send the whole pdu again */ |
542 | pdu->written = 0; | |
543 | } | |
544 | ||
545 | if (rpc->auto_reconnect != 0) { | |
546 | rpc->connect_cb = reconnect_cb; | |
547 | ||
548 | if (rpc_connect_sockaddr_async(rpc, &rpc->s) != 0) { | |
549 | rpc_error_all_pdus(rpc, "RPC ERROR: Failed to reconnect async"); | |
550 | return -1; | |
551 | } | |
b077fdeb RS |
552 | } |
553 | ||
554 | return 0; | |
555 | } | |
556 | ||
485bc9b9 RS |
557 | |
558 | int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port) | |
559 | { | |
560 | struct addrinfo *ai = NULL; | |
561 | char service[6]; | |
562 | ||
f3a75078 RS |
563 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
564 | ||
485bc9b9 RS |
565 | if (rpc->is_udp == 0) { |
566 | rpc_set_error(rpc, "Cant not bind UDP. Not UDP context"); | |
567 | return -1; | |
568 | } | |
569 | ||
6874f61e | 570 | sprintf(service, "%d", port); |
485bc9b9 RS |
571 | if (getaddrinfo(addr, service, NULL, &ai) != 0) { |
572 | rpc_set_error(rpc, "Invalid address:%s. " | |
573 | "Can not resolv into IPv4/v6 structure."); | |
574 | return -1; | |
575 | } | |
576 | ||
577 | switch(ai->ai_family) { | |
578 | case AF_INET: | |
579 | rpc->fd = socket(ai->ai_family, SOCK_DGRAM, 0); | |
580 | if (rpc->fd == -1) { | |
581 | rpc_set_error(rpc, "Failed to create UDP socket: %s", strerror(errno)); | |
582 | freeaddrinfo(ai); | |
583 | return -1; | |
584 | } | |
585 | ||
586 | if (bind(rpc->fd, (struct sockaddr *)ai->ai_addr, sizeof(struct sockaddr_in)) != 0) { | |
587 | rpc_set_error(rpc, "Failed to bind to UDP socket: %s",strerror(errno)); | |
588 | freeaddrinfo(ai); | |
589 | return -1; | |
590 | } | |
591 | break; | |
592 | default: | |
593 | rpc_set_error(rpc, "Can not handle UPD sockets of family %d yet", ai->ai_family); | |
594 | freeaddrinfo(ai); | |
595 | return -1; | |
596 | } | |
597 | ||
598 | freeaddrinfo(ai); | |
599 | ||
600 | return 0; | |
601 | } | |
602 | ||
5bf60dc6 RS |
603 | int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int is_broadcast) |
604 | { | |
605 | struct addrinfo *ai = NULL; | |
606 | char service[6]; | |
607 | ||
f3a75078 RS |
608 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
609 | ||
5bf60dc6 RS |
610 | if (rpc->is_udp == 0) { |
611 | rpc_set_error(rpc, "Can not set destination sockaddr. Not UDP context"); | |
612 | return -1; | |
613 | } | |
614 | ||
6874f61e | 615 | sprintf(service, "%d", port); |
5bf60dc6 RS |
616 | if (getaddrinfo(addr, service, NULL, &ai) != 0) { |
617 | rpc_set_error(rpc, "Invalid address:%s. " | |
618 | "Can not resolv into IPv4/v6 structure."); | |
619 | return -1; | |
620 | } | |
621 | ||
622 | if (rpc->udp_dest) { | |
623 | free(rpc->udp_dest); | |
624 | rpc->udp_dest = NULL; | |
625 | } | |
626 | rpc->udp_dest = malloc(ai->ai_addrlen); | |
627 | if (rpc->udp_dest == NULL) { | |
628 | rpc_set_error(rpc, "Out of memory. Failed to allocate sockaddr structure"); | |
2811861b | 629 | freeaddrinfo(ai); |
5bf60dc6 RS |
630 | return -1; |
631 | } | |
632 | memcpy(rpc->udp_dest, ai->ai_addr, ai->ai_addrlen); | |
633 | freeaddrinfo(ai); | |
634 | ||
635 | rpc->is_broadcast = is_broadcast; | |
bb4e9ed6 | 636 | setsockopt(rpc->fd, SOL_SOCKET, SO_BROADCAST, (char *)&is_broadcast, sizeof(is_broadcast)); |
5bf60dc6 RS |
637 | |
638 | return 0; | |
639 | } | |
c481da67 RS |
640 | |
641 | struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc) | |
642 | { | |
f3a75078 RS |
643 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
644 | ||
c481da67 RS |
645 | return (struct sockaddr *)&rpc->udp_src; |
646 | } | |
83aa785d RS |
647 | |
648 | int rpc_queue_length(struct rpc_context *rpc) | |
649 | { | |
650 | int i=0; | |
651 | struct rpc_pdu *pdu; | |
652 | ||
f3a75078 RS |
653 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
654 | ||
83aa785d RS |
655 | for(pdu = rpc->outqueue; pdu; pdu = pdu->next) { |
656 | i++; | |
657 | } | |
658 | for(pdu = rpc->waitpdu; pdu; pdu = pdu->next) { | |
659 | i++; | |
660 | } | |
661 | return i; | |
662 | } |