1 | //basic.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | enum mode { |
---|
39 | NORMAL = 0, |
---|
40 | SYSTEM = 1 |
---|
41 | }; |
---|
42 | |
---|
43 | static int _connect_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout); |
---|
44 | |
---|
45 | int __get_daemonimage(char ** daemonimage, enum mode * mode, const char * server) { |
---|
46 | const char * tmp = NULL; |
---|
47 | |
---|
48 | *daemonimage = NULL; |
---|
49 | *mode = NORMAL; |
---|
50 | |
---|
51 | if ( !!strncmp(server, "+fork", 5) ) { |
---|
52 | roar_err_set(ROAR_ERROR_INVAL); |
---|
53 | return -1; |
---|
54 | } |
---|
55 | |
---|
56 | server += 5; |
---|
57 | |
---|
58 | if ( server[0] == '=' ) { |
---|
59 | server++; |
---|
60 | |
---|
61 | if ( server[0] == 0 ) { |
---|
62 | // no special case, we just ignore it. |
---|
63 | } else if ( server[0] == 'd' && server[1] == ':' ) { |
---|
64 | server += 2; |
---|
65 | tmp = server; |
---|
66 | *mode = NORMAL; |
---|
67 | } else if ( server[0] == '!' ) { |
---|
68 | server += 1; |
---|
69 | tmp = server; |
---|
70 | *mode = SYSTEM; |
---|
71 | } else { |
---|
72 | roar_err_set(ROAR_ERROR_ILLSEQ); |
---|
73 | return -1; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | if ( tmp == NULL || *tmp == 0 ) |
---|
78 | tmp = roar_libroar_get_config()->daemonimage; |
---|
79 | |
---|
80 | // we keep this step to be compatible with older versions. |
---|
81 | if ( tmp == NULL || *tmp == 0 ) { |
---|
82 | tmp = roar_env_get("ROAR_DAEMONIMAGE"); |
---|
83 | if ( tmp != NULL ) { |
---|
84 | ROAR_WARN("__get_daemonimage(*): Usage of $ROAR_DAEMONIMAGE is obsolete. Use ROAR_OPTIONS=daemonimage:..."); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | if ( tmp != NULL && *tmp != 0 ) { |
---|
89 | *daemonimage = roar_mm_strdup(tmp); |
---|
90 | return 0; |
---|
91 | } |
---|
92 | |
---|
93 | *daemonimage = roar_libroar_get_path("bin-default-daemonimage", 0, NULL, NULL); |
---|
94 | if ( *daemonimage == NULL ) |
---|
95 | return -1; |
---|
96 | |
---|
97 | return 0; |
---|
98 | } |
---|
99 | |
---|
100 | #if defined(ROAR_TARGET_WIN32) |
---|
101 | #define NUM_TRIES 16 |
---|
102 | static int _start_server_win32(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { |
---|
103 | enum mode mode = NORMAL; |
---|
104 | char * daemonimage = NULL; |
---|
105 | char buf[64]; |
---|
106 | int port; |
---|
107 | size_t i; |
---|
108 | int fh; |
---|
109 | int ret; |
---|
110 | int err; |
---|
111 | |
---|
112 | if ( __get_daemonimage(&daemonimage, &mode, server) == -1 ) |
---|
113 | return -1; |
---|
114 | |
---|
115 | if ( mode != NORMAL ) { |
---|
116 | roar_mm_free(daemonimage); |
---|
117 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
118 | return -1; |
---|
119 | } |
---|
120 | |
---|
121 | for (i = 0; i < NUM_TRIES; i++) { |
---|
122 | port = roar_random_uint16(); |
---|
123 | if ( port < 1025 ) /* it seems on some strange systems the magic border is at 1025 not 1024. */ |
---|
124 | continue; |
---|
125 | |
---|
126 | fh = roar_socket_connect(ROAR_SOCKET_TYPE_TCP, "localhost", port); |
---|
127 | if ( fh == -1 ) |
---|
128 | break; |
---|
129 | |
---|
130 | closesocket(fh); |
---|
131 | } |
---|
132 | |
---|
133 | if ( i == NUM_TRIES ) { |
---|
134 | roar_mm_free(daemonimage); |
---|
135 | roar_err_set(ROAR_ERROR_LOOP); |
---|
136 | return -1; |
---|
137 | } |
---|
138 | |
---|
139 | // port is the port to use. It seems to be 'free'. |
---|
140 | |
---|
141 | #if defined(_P_DETACH) |
---|
142 | #define MODE _P_DETACH |
---|
143 | #elif defined(_P_NOWAIT) |
---|
144 | #define MODE _P_NOWAIT |
---|
145 | #else |
---|
146 | #define MODE _P_NOWAITO |
---|
147 | #endif |
---|
148 | |
---|
149 | snprintf(buf, sizeof(buf), "%i", port); |
---|
150 | roar_err_clear_all(); |
---|
151 | if ( _spawnlp(MODE, daemonimage, daemonimage, "--tcp", "--bind", "localhost", "--port", buf, (const char*)NULL) < 0 ) { |
---|
152 | roar_err_update(); |
---|
153 | ROAR_ERR("_start_server_win32(server='%s', ...): Can not start server: %s: %s", server, daemonimage, roar_errorstring); |
---|
154 | roar_mm_free_noerror(daemonimage); |
---|
155 | return -1; |
---|
156 | } |
---|
157 | |
---|
158 | roar_mm_free(daemonimage); |
---|
159 | |
---|
160 | snprintf(buf, sizeof(buf), "localhost:%i", port); |
---|
161 | |
---|
162 | // give the daemon some time to come up: |
---|
163 | roar_usleep(25000); |
---|
164 | |
---|
165 | ret = -1; |
---|
166 | for (i = 0; ret == -1 && i < NUM_TRIES; i++) { |
---|
167 | ret = _connect_server(con, buf, type, flags, timeout); |
---|
168 | if ( ret == -1 && i < NUM_TRIES ) |
---|
169 | roar_sleep(1); |
---|
170 | } |
---|
171 | |
---|
172 | if ( ret == -1 ) |
---|
173 | return -1; |
---|
174 | |
---|
175 | con->flags |= ROAR_CON_FLAGS_TERMINATE; |
---|
176 | |
---|
177 | return 0; |
---|
178 | } |
---|
179 | #elif !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
180 | static int _start_server_posix(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { |
---|
181 | enum mode mode = NORMAL; |
---|
182 | char * daemonimage = NULL; |
---|
183 | int socks[2]; |
---|
184 | int r; |
---|
185 | char fhstr[12]; |
---|
186 | size_t len; |
---|
187 | char * bin_sh; |
---|
188 | int err; |
---|
189 | |
---|
190 | if ( __get_daemonimage(&daemonimage, &mode, server) == -1 ) |
---|
191 | return -1; |
---|
192 | |
---|
193 | len = roar_mm_strlen(daemonimage) + 9; // 9 = '+fork=' + 'X:' + '\0' |
---|
194 | con->server_name = roar_mm_malloc(len); |
---|
195 | if ( con->server_name != NULL ) { |
---|
196 | snprintf(con->server_name, len, "+fork=%s%s", mode == NORMAL ? "d:" : "!", daemonimage); |
---|
197 | } |
---|
198 | |
---|
199 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
200 | roar_err_from_errno(); |
---|
201 | roar_mm_free_noerror(daemonimage); |
---|
202 | return -1; |
---|
203 | } |
---|
204 | |
---|
205 | r = roar_fork(NULL); |
---|
206 | |
---|
207 | if ( r == -1 ) { // error! |
---|
208 | ROAR_ERR("_start_server(*): Can not fork: %s", roar_error2str(roar_error)); |
---|
209 | err = roar_error; |
---|
210 | close(socks[0]); |
---|
211 | close(socks[1]); |
---|
212 | roar_mm_free(daemonimage); |
---|
213 | roar_error = err; |
---|
214 | return -1; |
---|
215 | } else if ( r == 0 ) { // we are the child |
---|
216 | roar_watchdog_stop(); |
---|
217 | |
---|
218 | close(socks[0]); |
---|
219 | |
---|
220 | close(ROAR_STDIN ); // we do not want roard to have any standard input |
---|
221 | close(ROAR_STDOUT); // STDOUT is also not needed, so we close it, |
---|
222 | // but STDERR we keep open for error messages. |
---|
223 | |
---|
224 | snprintf(fhstr, sizeof(fhstr), "%i", socks[1]); |
---|
225 | |
---|
226 | switch (mode) { |
---|
227 | case NORMAL: |
---|
228 | execlp(daemonimage, daemonimage, "--no-listen", "--client-fh", fhstr, (_LIBROAR_GOOD_CAST char*)NULL); |
---|
229 | break; |
---|
230 | case SYSTEM: |
---|
231 | dup2(socks[1], ROAR_STDIN ); |
---|
232 | dup2(socks[1], ROAR_STDOUT); |
---|
233 | bin_sh = roar_libroar_get_path("bin-sh", 0, NULL, NULL); |
---|
234 | if ( bin_sh != NULL ) { |
---|
235 | execl(bin_sh, bin_sh, "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL); |
---|
236 | roar_mm_free(bin_sh); |
---|
237 | } |
---|
238 | execlp("sh", "sh", "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL); |
---|
239 | break; |
---|
240 | } |
---|
241 | |
---|
242 | roar_mm_free(daemonimage); |
---|
243 | |
---|
244 | // we are still alive? |
---|
245 | ROAR_ERR("_start_server(*): alive after exec(), that's bad!"); |
---|
246 | ROAR_U_EXIT(1); |
---|
247 | } else { // we are the parent |
---|
248 | close(socks[1]); |
---|
249 | roar_mm_free(daemonimage); |
---|
250 | if ( roar_vio_open_fh_socket(con->viocon, socks[0]) == -1 ) { |
---|
251 | close(socks[0]); |
---|
252 | return -1; |
---|
253 | } else { |
---|
254 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
255 | } |
---|
256 | return 0; |
---|
257 | } |
---|
258 | |
---|
259 | return -1; |
---|
260 | } |
---|
261 | #endif |
---|
262 | |
---|
263 | static int _start_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { |
---|
264 | #if defined(ROAR_TARGET_WIN32) |
---|
265 | return _start_server_win32(con, server, type, flags, timeout); |
---|
266 | #elif !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
267 | return _start_server_posix(con, server, type, flags, timeout); |
---|
268 | #else |
---|
269 | ROAR_ERR("_start_server(*): There is no UNIX Domain Socket, download a real OS."); |
---|
270 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
271 | return -1; |
---|
272 | #endif |
---|
273 | } |
---|
274 | |
---|
275 | static int _connect_internal(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { |
---|
276 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
277 | struct roar_vio_calls * vio; |
---|
278 | |
---|
279 | if ( config->connect_internal != NULL ) { |
---|
280 | vio = config->connect_internal(con, server, type, flags, timeout); |
---|
281 | if ( vio == NULL ) |
---|
282 | return -1; |
---|
283 | |
---|
284 | con->viocon = vio; |
---|
285 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
286 | |
---|
287 | return 0; |
---|
288 | } |
---|
289 | |
---|
290 | roar_err_set(ROAR_ERROR_BADHOST); |
---|
291 | return -1; |
---|
292 | } |
---|
293 | |
---|
294 | static int _connect_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { |
---|
295 | #if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT) |
---|
296 | struct stat sockstat; |
---|
297 | #endif |
---|
298 | const char * obj = NULL; |
---|
299 | char user_sock[128]; |
---|
300 | int is_decnet = 0; |
---|
301 | int port = 0; |
---|
302 | int i = 0; |
---|
303 | int fh = -1; |
---|
304 | int err; |
---|
305 | int ret; |
---|
306 | |
---|
307 | if ( con == NULL || server == NULL ) { |
---|
308 | roar_err_set(ROAR_ERROR_FAULT); |
---|
309 | return -1; |
---|
310 | } |
---|
311 | |
---|
312 | if ( !strcmp(server, "+invalid") ) { |
---|
313 | roar_err_set(ROAR_ERROR_CANCELED); |
---|
314 | return -1; |
---|
315 | } else if ( !strncmp(server, "+dstr=", 6) ) { |
---|
316 | if ( roar_vio_open_dstr_simple(con->viocon, server+6, ROAR_VIOF_READWRITE) == -1 ) |
---|
317 | return -1; |
---|
318 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
319 | con->server_name = roar_mm_strdup(server); |
---|
320 | return 0; |
---|
321 | } else if ( !strcmp(server, "+fork") || !strncmp(server, "+fork=", 6) ) { |
---|
322 | return _start_server(con, server, type, flags, timeout); |
---|
323 | } else if ( !strcmp(server, "+internal") || !strncmp(server, "+internal=", 10) ) { |
---|
324 | ret = _connect_internal(con, server, type, flags, timeout); |
---|
325 | if ( ret == 0 ) |
---|
326 | con->server_name = roar_mm_strdup(server); |
---|
327 | return ret; |
---|
328 | } |
---|
329 | |
---|
330 | strncpy(user_sock, server, sizeof(user_sock)-1); |
---|
331 | user_sock[sizeof(user_sock)-1] = 0; |
---|
332 | |
---|
333 | |
---|
334 | if ( *user_sock != '/' ) { // don't test AF_UNIX sockets for ports |
---|
335 | for (i = 0; user_sock[i] != 0; i++) { |
---|
336 | if ( user_sock[i] == ':' ) { |
---|
337 | if ( user_sock[i+1] == ':' ) { // DECnet, leave unchanged |
---|
338 | is_decnet = 1; |
---|
339 | obj = &user_sock[i+2]; |
---|
340 | break; |
---|
341 | } |
---|
342 | |
---|
343 | port = atoi(&(user_sock[i+1])); |
---|
344 | user_sock[i] = 0; |
---|
345 | break; |
---|
346 | } |
---|
347 | } |
---|
348 | } |
---|
349 | |
---|
350 | if ( is_decnet ) { |
---|
351 | if ( *user_sock == ':' ) { |
---|
352 | if ( roar_socket_get_local_nodename() != NULL ) { |
---|
353 | strncpy(user_sock, roar_socket_get_local_nodename(), sizeof(user_sock)-1); |
---|
354 | user_sock[sizeof(user_sock)-1] = 0; |
---|
355 | roar_mm_strlcat(user_sock, server, sizeof(user_sock)-1); |
---|
356 | user_sock[sizeof(user_sock)-1] = 0; |
---|
357 | obj = strstr(user_sock, "::"); |
---|
358 | obj += 2; |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | if ( *obj == 0 ) { |
---|
363 | roar_mm_strlcat(user_sock, ROAR_DEFAULT_OBJECT, sizeof(user_sock)-1); |
---|
364 | user_sock[sizeof(user_sock)-1] = 0; |
---|
365 | } |
---|
366 | |
---|
367 | ROAR_DBG("roar_connect_raw(*): user_sock='%s'", user_sock); |
---|
368 | } |
---|
369 | |
---|
370 | if ( port || is_decnet ) { |
---|
371 | fh = roar_socket_connect(type, user_sock, port); |
---|
372 | // restore the original string |
---|
373 | user_sock[i] = ':'; |
---|
374 | } else { |
---|
375 | #if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT) |
---|
376 | if ( user_sock[0] == '/' ) { |
---|
377 | if ( stat(user_sock, &sockstat) == 0 ) { |
---|
378 | if ( S_ISCHR(sockstat.st_mode) ) { |
---|
379 | #ifdef O_NOCTTY |
---|
380 | fh = open(user_sock, O_RDWR|O_NOCTTY, 0666); |
---|
381 | #else |
---|
382 | fh = open(user_sock, O_RDWR, 0666); |
---|
383 | #endif |
---|
384 | } |
---|
385 | } |
---|
386 | } |
---|
387 | #endif |
---|
388 | if ( fh == -1 ) |
---|
389 | fh = roar_socket_connect(type, user_sock, ROAR_DEFAULT_PORT); |
---|
390 | } |
---|
391 | |
---|
392 | if ( fh == -1 ) |
---|
393 | return -1; |
---|
394 | |
---|
395 | if ( roar_vio_open_fh_socket(con->viocon, fh) == -1 ) { |
---|
396 | err = roar_error; |
---|
397 | #ifdef ROAR_TARGET_WIN32 |
---|
398 | closesocket(fh); |
---|
399 | #else |
---|
400 | close(fh); |
---|
401 | #endif |
---|
402 | roar_error = err; |
---|
403 | return -1; |
---|
404 | } else { |
---|
405 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
406 | } |
---|
407 | |
---|
408 | con->server_name = roar_mm_strdup(server); |
---|
409 | |
---|
410 | roar_err_set(ROAR_ERROR_NONE); |
---|
411 | return 0; |
---|
412 | } |
---|
413 | |
---|
414 | static int roar_connect_raw (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) { |
---|
415 | #ifdef ROAR_HAVE_LIBSLP |
---|
416 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
417 | #endif |
---|
418 | #if defined(ROAR_HAVE_UNIX) |
---|
419 | char user_sock[128]; |
---|
420 | const char * roar_server; |
---|
421 | #endif |
---|
422 | #if defined(ROAR_HAVE_LIBSLP) || !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
423 | int i = 0; |
---|
424 | #endif |
---|
425 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
426 | struct passwd * pwd; |
---|
427 | char * sysconf_roarserver; |
---|
428 | #endif |
---|
429 | #ifdef ROAR_HAVE_LIBDNET |
---|
430 | struct stat decnet_stat; |
---|
431 | #endif |
---|
432 | #ifdef ROAR_HAVE_LIBX11 |
---|
433 | struct roar_x11_connection * x11con; |
---|
434 | #endif |
---|
435 | #ifdef ROAR_HAVE_LIBSLP |
---|
436 | struct roar_server * list; |
---|
437 | int workarounds_store; |
---|
438 | #endif |
---|
439 | |
---|
440 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
441 | |
---|
442 | if ( timeout != 0 ) { |
---|
443 | roar_err_set(ROAR_ERROR_INVAL); |
---|
444 | return -1; |
---|
445 | } |
---|
446 | |
---|
447 | if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK ) |
---|
448 | flags |= ROAR_ENUM_FLAG_NONBLOCK; |
---|
449 | |
---|
450 | |
---|
451 | if ( server == NULL || *server == 0 ) |
---|
452 | server = roar_libroar_get_server(); |
---|
453 | |
---|
454 | if ( server == NULL || *server == 0 ) |
---|
455 | server = roar_env_get("ROAR_SERVER"); |
---|
456 | |
---|
457 | #ifdef ROAR_HAVE_LIBX11 |
---|
458 | if ( server == NULL || *server == 0 ) { |
---|
459 | if ( (x11con = roar_x11_connect(NULL)) != NULL ) { |
---|
460 | server = roar_x11_get_prop(x11con, "ROAR_SERVER"); |
---|
461 | roar_x11_disconnect(x11con); |
---|
462 | } |
---|
463 | } |
---|
464 | #endif |
---|
465 | |
---|
466 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
467 | sysconf_roarserver = roar_libroar_get_path("sysconf-roarserver", 0, NULL, NULL); |
---|
468 | if ( sysconf_roarserver != NULL ) { |
---|
469 | if ( (server == NULL || *server == 0) && (i = readlink(sysconf_roarserver, user_sock, sizeof(user_sock)-1)) != -1 ) { |
---|
470 | user_sock[i] = 0; |
---|
471 | server = user_sock; |
---|
472 | } |
---|
473 | roar_mm_free(sysconf_roarserver); |
---|
474 | } |
---|
475 | #endif |
---|
476 | |
---|
477 | if ( server != NULL && !strcasecmp(server, "+slp") ) { |
---|
478 | server = roar_slp_find_roard(0); |
---|
479 | if ( server == NULL ) { |
---|
480 | return -1; |
---|
481 | } |
---|
482 | } |
---|
483 | |
---|
484 | // "+default" is an alias for NULL. |
---|
485 | if ( server != NULL && !strcasecmp(server, "+default") ) { |
---|
486 | server = NULL; |
---|
487 | } |
---|
488 | |
---|
489 | if ( server == NULL || *server == 0 ) { |
---|
490 | /* connect via defaults */ |
---|
491 | |
---|
492 | #ifdef ROAR_HAVE_UNIX |
---|
493 | #ifndef ROAR_TARGET_MICROCONTROLLER |
---|
494 | roar_server = roar_env_get("HOME"); |
---|
495 | #else |
---|
496 | roar_server = NULL; |
---|
497 | #endif |
---|
498 | |
---|
499 | if ( roar_server == NULL ) { |
---|
500 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
501 | if ( (pwd = getpwuid(getuid())) != NULL ) { |
---|
502 | roar_server = pwd->pw_dir; |
---|
503 | } |
---|
504 | #endif |
---|
505 | } |
---|
506 | |
---|
507 | if ( roar_server != NULL ) { |
---|
508 | snprintf(user_sock, sizeof(user_sock)-1, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER); |
---|
509 | user_sock[sizeof(user_sock)-1] = 0; |
---|
510 | |
---|
511 | if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 ) |
---|
512 | return 0; |
---|
513 | } |
---|
514 | |
---|
515 | if ( _connect_server(con, ROAR_DEFAULT_SOCK_GLOBAL, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 ) |
---|
516 | return 0; |
---|
517 | #endif |
---|
518 | |
---|
519 | if ( _connect_server(con, ROAR_DEFAULT_HOSTPORT, ROAR_SOCKET_TYPE_TCP, flags, timeout) == 0 ) |
---|
520 | return 0; |
---|
521 | |
---|
522 | #ifdef ROAR_HAVE_LIBDNET |
---|
523 | if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) { |
---|
524 | if ( roar_socket_get_local_nodename() ) { |
---|
525 | snprintf(user_sock, sizeof(user_sock)-1, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT); |
---|
526 | if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_DECNET, flags, timeout) == 0 ) |
---|
527 | return 0; |
---|
528 | } |
---|
529 | } |
---|
530 | #endif |
---|
531 | |
---|
532 | if ( _connect_server(con, "+abstract", ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) == 0 ) |
---|
533 | return 0; |
---|
534 | |
---|
535 | #ifdef ROAR_HAVE_LIBSLP |
---|
536 | if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) && |
---|
537 | !(flags & ROAR_ENUM_FLAG_NONBLOCK) && |
---|
538 | !(flags & ROAR_ENUM_FLAG_LOCALONLY) |
---|
539 | ) { |
---|
540 | if ( (server = roar_slp_find_roard(0)) != NULL ) { |
---|
541 | if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) |
---|
542 | return 0; |
---|
543 | |
---|
544 | /* in case we can not connect to the server given this may be a cache problem, |
---|
545 | we do a new lookup with the cache disabled in this case */ |
---|
546 | ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache"); |
---|
547 | if ( (server = roar_slp_find_roard(1)) != NULL ) |
---|
548 | if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) |
---|
549 | return 0; |
---|
550 | } |
---|
551 | } |
---|
552 | |
---|
553 | workarounds_store = config->workaround.workarounds; |
---|
554 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP; |
---|
555 | list = roar_enum_servers(flags, -1, -1); |
---|
556 | config->workaround.workarounds = workarounds_store; |
---|
557 | if ( list != NULL ) { |
---|
558 | for (i = 0; list[i].server != NULL; i++) { |
---|
559 | if ( _connect_server(con, list[i].server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) { |
---|
560 | roar_enum_servers_free(list); |
---|
561 | return 0; |
---|
562 | } |
---|
563 | } |
---|
564 | roar_enum_servers_free(list); |
---|
565 | } |
---|
566 | #endif |
---|
567 | |
---|
568 | return -1; |
---|
569 | |
---|
570 | } else { |
---|
571 | /* connect via (char*)server */ |
---|
572 | if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) != 0 ) |
---|
573 | return -1; |
---|
574 | return 0; |
---|
575 | } |
---|
576 | |
---|
577 | roar_err_set(ROAR_ERROR_NODEV); |
---|
578 | ROAR_DBG("roar_connect_raw(*) = -1 // error=NODEV"); |
---|
579 | return -1; |
---|
580 | } |
---|
581 | |
---|
582 | int roar_connect (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) { |
---|
583 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
584 | |
---|
585 | if ( con == NULL ) { |
---|
586 | roar_err_set(ROAR_ERROR_FAULT); |
---|
587 | return -1; |
---|
588 | } |
---|
589 | |
---|
590 | if ( roar_connect_none(con) == -1 ) |
---|
591 | return -1; |
---|
592 | |
---|
593 | if ( roar_connect_raw(con, server, flags | config->serverflags, timeout) == -1 ) |
---|
594 | return -1; |
---|
595 | |
---|
596 | return 0; |
---|
597 | } |
---|
598 | |
---|
599 | int roar_connect_none (struct roar_connection * con) { |
---|
600 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
601 | |
---|
602 | if ( con == NULL ) { |
---|
603 | roar_err_set(ROAR_ERROR_INVAL); |
---|
604 | return -1; |
---|
605 | } |
---|
606 | |
---|
607 | memset(con, 0, sizeof(struct roar_connection)); |
---|
608 | con->refc = 1; |
---|
609 | con->flags = ROAR_CON_FLAGS_ISCLIENT; |
---|
610 | con->version = _ROAR_MESSAGE_VERSION; |
---|
611 | con->cb_userdata = NULL; |
---|
612 | con->cb = NULL; |
---|
613 | con->server_stds = NULL; |
---|
614 | con->server_name = NULL; |
---|
615 | |
---|
616 | roar_err_init(&(con->errorframe)); |
---|
617 | |
---|
618 | con->viocon = &(con->viocon_store); |
---|
619 | |
---|
620 | // con->flags |= ROAR_CON_FLAGS_VIO; |
---|
621 | |
---|
622 | if ( config->protocolversion != -1 ) |
---|
623 | roar_set_connection_version(con, config->protocolversion); |
---|
624 | |
---|
625 | roar_err_set(ROAR_ERROR_NONE); |
---|
626 | return 0; |
---|
627 | } |
---|
628 | |
---|
629 | int roar_connect_vio (struct roar_connection * con, struct roar_vio_calls * vio) { |
---|
630 | if ( con == NULL || vio == NULL ) { |
---|
631 | roar_err_set(ROAR_ERROR_INVAL); |
---|
632 | return -1; |
---|
633 | } |
---|
634 | |
---|
635 | if ( roar_connect_none(con) == -1 ) |
---|
636 | return -1; |
---|
637 | |
---|
638 | con->viocon = vio; |
---|
639 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
640 | |
---|
641 | return -1; |
---|
642 | } |
---|
643 | |
---|
644 | int roar_connect_fh (struct roar_connection * con, int fh) { |
---|
645 | |
---|
646 | if ( con == NULL || fh == -1 ) { |
---|
647 | roar_err_set(ROAR_ERROR_INVAL); |
---|
648 | return -1; |
---|
649 | } |
---|
650 | |
---|
651 | if ( roar_connect_none(con) == -1 ) |
---|
652 | return -1; |
---|
653 | |
---|
654 | // special hack to set an illegal value used internally in libroar: |
---|
655 | if ( fh == -2 ) |
---|
656 | fh = -1; |
---|
657 | |
---|
658 | if ( roar_vio_open_fh_socket(con->viocon, fh) != -1 ) { |
---|
659 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
660 | } else { |
---|
661 | return -1; |
---|
662 | } |
---|
663 | |
---|
664 | roar_err_set(ROAR_ERROR_NONE); |
---|
665 | return 0; |
---|
666 | } |
---|
667 | |
---|
668 | int roar_get_connection_fh (struct roar_connection * con) { |
---|
669 | int fh; |
---|
670 | |
---|
671 | ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con); |
---|
672 | |
---|
673 | roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL); |
---|
674 | |
---|
675 | if ( con == NULL ) |
---|
676 | return -1; |
---|
677 | |
---|
678 | ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con); |
---|
679 | |
---|
680 | if ( roar_vio_ctl(con->viocon, ROAR_VIO_CTL_GET_FH, &fh) == -1 ) { |
---|
681 | #ifdef ROAR_TARGET_WIN32 |
---|
682 | if ( roar_vio_ctl(con->viocon, ROAR_VIO_CTL_GET_SELECT_FH, &fh) == -1 ) |
---|
683 | return -1; |
---|
684 | #else |
---|
685 | return -1; |
---|
686 | #endif |
---|
687 | } |
---|
688 | |
---|
689 | ROAR_DBG("roar_get_connection_fh(con=%p) = %i", con, fh); |
---|
690 | |
---|
691 | return fh; |
---|
692 | } |
---|
693 | |
---|
694 | struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) { |
---|
695 | if ( con == NULL ) { |
---|
696 | roar_err_set(ROAR_ERROR_FAULT); |
---|
697 | return NULL; |
---|
698 | } |
---|
699 | |
---|
700 | if ( con->flags & ROAR_CON_FLAGS_VIO ) |
---|
701 | return con->viocon; |
---|
702 | |
---|
703 | // TODO: try to open the VIO. |
---|
704 | |
---|
705 | roar_err_set(ROAR_ERROR_BADFH); |
---|
706 | return NULL; |
---|
707 | } |
---|
708 | |
---|
709 | const char * roar_get_connection_server(struct roar_connection * con) { |
---|
710 | if ( con == NULL ) { |
---|
711 | roar_err_set(ROAR_ERROR_FAULT); |
---|
712 | return NULL; |
---|
713 | } |
---|
714 | |
---|
715 | return con->server_name; |
---|
716 | } |
---|
717 | |
---|
718 | int roar_connectionref(struct roar_connection * con) { |
---|
719 | if ( con == NULL ) { |
---|
720 | roar_err_set(ROAR_ERROR_FAULT); |
---|
721 | return -1; |
---|
722 | } |
---|
723 | |
---|
724 | con->refc++; |
---|
725 | |
---|
726 | return 0; |
---|
727 | } |
---|
728 | |
---|
729 | int roar_connectionunref(struct roar_connection * con) { |
---|
730 | struct roar_vio_calls * vio; |
---|
731 | struct roar_message m; |
---|
732 | |
---|
733 | if ( con == NULL ) { |
---|
734 | roar_err_set(ROAR_ERROR_FAULT); |
---|
735 | return -1; |
---|
736 | } |
---|
737 | |
---|
738 | con->refc--; |
---|
739 | |
---|
740 | if ( con->refc ) |
---|
741 | return 0; |
---|
742 | |
---|
743 | if ( con->flags & ROAR_CON_FLAGS_TERMINATE ) { |
---|
744 | if ( roar_terminate(con, 1) == -1 ) { |
---|
745 | ROAR_WARN("roar_connectionunref(con=%p{.server_name='%s'}): Can not terminate server as requested.", con, con->server_name); |
---|
746 | if ( roar_terminate(con, 0) == -1 ) { |
---|
747 | ROAR_ERR("roar_connectionunref(con=%p{.server_name='%s'}): Can not exit server. BAD.", con, con->server_name); |
---|
748 | } |
---|
749 | } |
---|
750 | } |
---|
751 | |
---|
752 | memset(&m, 0, sizeof(m)); |
---|
753 | |
---|
754 | m.datalen = 0; |
---|
755 | m.stream = -1; |
---|
756 | m.pos = 0; |
---|
757 | m.cmd = ROAR_CMD_QUIT; |
---|
758 | |
---|
759 | roar_req(con, &m, NULL); |
---|
760 | |
---|
761 | if ( (vio = roar_get_connection_vio2(con)) != NULL ) { |
---|
762 | roar_vio_close(vio); |
---|
763 | } |
---|
764 | |
---|
765 | if ( con->server_stds != NULL ) { |
---|
766 | roar_stds_free(con->server_stds); |
---|
767 | con->server_stds = NULL; |
---|
768 | } |
---|
769 | |
---|
770 | if ( con->server_name != NULL ) { |
---|
771 | roar_mm_free(con->server_name); |
---|
772 | con->server_name = NULL; |
---|
773 | } |
---|
774 | |
---|
775 | if ( con->flags & ROAR_CON_FLAGS_FREESELF ) { |
---|
776 | roar_mm_free(con); |
---|
777 | } else { |
---|
778 | roar_connect_fh(con, -2); |
---|
779 | } |
---|
780 | |
---|
781 | return 0; |
---|
782 | } |
---|
783 | |
---|
784 | int roar_set_connection_callback(struct roar_connection * con, |
---|
785 | void (*cb)(struct roar_connection * con, |
---|
786 | struct roar_message * mes, |
---|
787 | void * data, |
---|
788 | void * userdata), |
---|
789 | void * userdata) { |
---|
790 | if ( con == NULL ) { |
---|
791 | roar_err_set(ROAR_ERROR_FAULT); |
---|
792 | return -1; |
---|
793 | } |
---|
794 | |
---|
795 | con->cb = cb; |
---|
796 | con->cb_userdata = userdata; |
---|
797 | |
---|
798 | return 0; |
---|
799 | } |
---|
800 | |
---|
801 | int roar_set_connection_version(struct roar_connection * con, int version) { |
---|
802 | if ( con == NULL ) { |
---|
803 | roar_err_set(ROAR_ERROR_FAULT); |
---|
804 | return -1; |
---|
805 | } |
---|
806 | |
---|
807 | if ( version == -1 ) |
---|
808 | version = _ROAR_MESSAGE_VERSION; |
---|
809 | |
---|
810 | if ( version < 0 ) { |
---|
811 | roar_err_set(ROAR_ERROR_INVAL); |
---|
812 | return -1; |
---|
813 | } |
---|
814 | |
---|
815 | con->version = version; |
---|
816 | |
---|
817 | if ( version == 2 ) { |
---|
818 | con->flags |= ROAR_CON_FLAGS_SUPPORT_V2; |
---|
819 | } |
---|
820 | |
---|
821 | return 0; |
---|
822 | } |
---|
823 | |
---|
824 | int16_t roar_message_genseq(struct roar_connection * con, int is_client) { |
---|
825 | static int16_t store = 0; |
---|
826 | int16_t ret = 0; |
---|
827 | |
---|
828 | ret = store++; |
---|
829 | |
---|
830 | if ( ret == 0 ) |
---|
831 | ret = store++; |
---|
832 | |
---|
833 | if ( is_client == -1 ) { |
---|
834 | if ( con != NULL ) { |
---|
835 | is_client = con->flags & ROAR_CON_FLAGS_ISCLIENT ? 1 : 0; |
---|
836 | } |
---|
837 | } |
---|
838 | |
---|
839 | if ( is_client == -1 ) |
---|
840 | is_client = 1; |
---|
841 | |
---|
842 | ret |= 0x8000U; |
---|
843 | if ( !is_client ) |
---|
844 | ret -= 0x8000U; |
---|
845 | |
---|
846 | return ret; |
---|
847 | } |
---|
848 | |
---|
849 | int roar_sync (struct roar_connection * con) { |
---|
850 | // wait for any non-client reqs |
---|
851 | return roar_wait_msg(con, 0x0000, 0x8000); |
---|
852 | } |
---|
853 | |
---|
854 | int roar_wait_msg (struct roar_connection * con, int16_t seq, int16_t seqmask) { |
---|
855 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
856 | return -1; |
---|
857 | } |
---|
858 | |
---|
859 | int roar_noop (struct roar_connection * con) { |
---|
860 | struct roar_message mes; |
---|
861 | |
---|
862 | if ( con == NULL ) { |
---|
863 | roar_err_set(ROAR_ERROR_FAULT); |
---|
864 | return -1; |
---|
865 | } |
---|
866 | |
---|
867 | memset(&mes, 0, sizeof(mes)); |
---|
868 | |
---|
869 | mes.cmd = ROAR_CMD_NOOP; |
---|
870 | mes.stream = -1; |
---|
871 | |
---|
872 | return roar_req3(con, &mes, NULL); |
---|
873 | } |
---|
874 | |
---|
875 | int roar_identify (struct roar_connection * con, const char * name) { |
---|
876 | struct roar_message mes; |
---|
877 | uint32_t pid; |
---|
878 | int max_len; |
---|
879 | |
---|
880 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
881 | |
---|
882 | ROAR_DBG("roar_identify(*): try to identify myself..."); |
---|
883 | |
---|
884 | memset(&mes, 0, sizeof(mes)); |
---|
885 | |
---|
886 | mes.cmd = ROAR_CMD_IDENTIFY; |
---|
887 | mes.stream = -1; |
---|
888 | mes.pos = 0; |
---|
889 | |
---|
890 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
891 | |
---|
892 | if ( name == NULL ) |
---|
893 | name = "libroar client"; |
---|
894 | |
---|
895 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
896 | |
---|
897 | max_len = roar_mm_strlen(name); |
---|
898 | ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len); |
---|
899 | |
---|
900 | if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) ) |
---|
901 | max_len = LIBROAR_BUFFER_MSGDATA - 5; |
---|
902 | |
---|
903 | mes.datalen = 5 + max_len; |
---|
904 | mes.data[0] = 1; |
---|
905 | |
---|
906 | pid = getpid(); |
---|
907 | mes.data[1] = (pid & 0xFF000000UL) >> 24; |
---|
908 | mes.data[2] = (pid & 0x00FF0000UL) >> 16; |
---|
909 | mes.data[3] = (pid & 0x0000FF00UL) >> 8; |
---|
910 | mes.data[4] = (pid & 0x000000FFUL) >> 0; |
---|
911 | ROAR_DBG("roar_identify(*): pid = %i", (int)pid); |
---|
912 | |
---|
913 | strncpy(mes.data+5, name, max_len); |
---|
914 | |
---|
915 | return roar_req3(con, &mes, NULL); |
---|
916 | } |
---|
917 | |
---|
918 | //ll |
---|