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