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