1 | //basic.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
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 | static int _start_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { |
---|
39 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
40 | enum { |
---|
41 | NORMAL = 0, |
---|
42 | SYSTEM = 1 |
---|
43 | } mode = NORMAL; |
---|
44 | const char * daemonimage = NULL; |
---|
45 | int socks[2]; |
---|
46 | int r; |
---|
47 | char fhstr[12]; |
---|
48 | |
---|
49 | if ( !strncmp(server, "+fork=", 6) ) { |
---|
50 | server += 6; |
---|
51 | if ( server[0] == 0 ) { |
---|
52 | // no special case, we just ignore it. |
---|
53 | } else if ( server[0] == 'd' && server[1] == ':' ) { |
---|
54 | server += 2; |
---|
55 | daemonimage = server; |
---|
56 | } else if ( server[0] == '!' ) { |
---|
57 | server += 1; |
---|
58 | daemonimage = server; |
---|
59 | mode = SYSTEM; |
---|
60 | } else { |
---|
61 | roar_err_set(ROAR_ERROR_ILLSEQ); |
---|
62 | return -1; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | if ( daemonimage == NULL ) |
---|
67 | daemonimage = roar_libroar_get_config()->daemonimage; |
---|
68 | |
---|
69 | // we keep this step to be compatible with older versions. |
---|
70 | if ( daemonimage == NULL || *daemonimage == 0 ) { |
---|
71 | daemonimage = roar_env_get("ROAR_DAEMONIMAGE"); |
---|
72 | if ( daemonimage != NULL ) { |
---|
73 | ROAR_WARN("_start_server(*): Usage of $ROAR_DAEMONIMAGE is obsolete. Use ROAR_OPTIONS=daemonimage:..."); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | if ( daemonimage == NULL || *daemonimage == 0 ) |
---|
78 | daemonimage = "roard"; |
---|
79 | |
---|
80 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
81 | roar_err_from_errno(); |
---|
82 | return -1; |
---|
83 | } |
---|
84 | |
---|
85 | r = roar_fork(NULL); |
---|
86 | |
---|
87 | if ( r == -1 ) { // error! |
---|
88 | ROAR_ERR("_start_server(*): Can not fork: %s", roar_error2str(roar_error)); |
---|
89 | close(socks[0]); |
---|
90 | close(socks[1]); |
---|
91 | return -1; |
---|
92 | } else if ( r == 0 ) { // we are the child |
---|
93 | roar_watchdog_stop(); |
---|
94 | |
---|
95 | close(socks[0]); |
---|
96 | |
---|
97 | close(ROAR_STDIN ); // we do not want roard to have any standard input |
---|
98 | close(ROAR_STDOUT); // STDOUT is also not needed, so we close it, |
---|
99 | // but STDERR we keep open for error messages. |
---|
100 | |
---|
101 | snprintf(fhstr, sizeof(fhstr), "%i", socks[1]); |
---|
102 | |
---|
103 | switch (mode) { |
---|
104 | case NORMAL: |
---|
105 | execlp(daemonimage, daemonimage, "--no-listen", "--client-fh", fhstr, (_LIBROAR_GOOD_CAST char*)NULL); |
---|
106 | break; |
---|
107 | case SYSTEM: |
---|
108 | dup2(socks[1], ROAR_STDIN ); |
---|
109 | dup2(socks[1], ROAR_STDOUT); |
---|
110 | execl("/bin/sh", "/bin/sh", "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL); |
---|
111 | execlp("sh", "sh", "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL); |
---|
112 | break; |
---|
113 | } |
---|
114 | |
---|
115 | // we are still alive? |
---|
116 | ROAR_ERR("_start_server(*): alive after exec(), that's bad!"); |
---|
117 | ROAR_U_EXIT(1); |
---|
118 | } else { // we are the parent |
---|
119 | close(socks[1]); |
---|
120 | if ( roar_vio_open_fh_socket(con->viocon, socks[0]) == -1 ) { |
---|
121 | close(socks[0]); |
---|
122 | return -1; |
---|
123 | } else { |
---|
124 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
125 | } |
---|
126 | return 0; |
---|
127 | } |
---|
128 | |
---|
129 | return -1; |
---|
130 | #else |
---|
131 | ROAR_ERR("_start_server(*): There is no UNIX Domain Socket support in win32, download a real OS."); |
---|
132 | return -1; |
---|
133 | #endif |
---|
134 | } |
---|
135 | |
---|
136 | static int _connect_internal(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { |
---|
137 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
138 | struct roar_vio_calls * vio; |
---|
139 | |
---|
140 | if ( config->connect_internal != NULL ) { |
---|
141 | vio = config->connect_internal(con, server, type, flags, timeout); |
---|
142 | if ( vio == NULL ) |
---|
143 | return -1; |
---|
144 | |
---|
145 | con->viocon = vio; |
---|
146 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
147 | |
---|
148 | return 0; |
---|
149 | } |
---|
150 | |
---|
151 | roar_err_set(ROAR_ERROR_BADHOST); |
---|
152 | return -1; |
---|
153 | } |
---|
154 | |
---|
155 | static int _connect_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { |
---|
156 | #if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT) |
---|
157 | struct stat sockstat; |
---|
158 | #endif |
---|
159 | const char * obj = NULL; |
---|
160 | char user_sock[128]; |
---|
161 | int is_decnet = 0; |
---|
162 | int port = 0; |
---|
163 | int i = 0; |
---|
164 | int fh = -1; |
---|
165 | int err; |
---|
166 | |
---|
167 | if ( con == NULL || server == NULL ) { |
---|
168 | roar_err_set(ROAR_ERROR_FAULT); |
---|
169 | return -1; |
---|
170 | } |
---|
171 | |
---|
172 | if ( !strcmp(server, "+invalid") ) { |
---|
173 | roar_err_set(ROAR_ERROR_CANCELED); |
---|
174 | return -1; |
---|
175 | } else if ( !strncmp(server, "+dstr=", 6) ) { |
---|
176 | if ( roar_vio_open_dstr_simple(con->viocon, server+6, ROAR_VIOF_READWRITE) == -1 ) |
---|
177 | return -1; |
---|
178 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
179 | return 0; |
---|
180 | } else if ( !strcmp(server, "+fork") || !strncmp(server, "+fork=", 6) ) { |
---|
181 | return _start_server(con, server, type, flags, timeout); |
---|
182 | } else if ( !strcmp(server, "+internal") || !strncmp(server, "+internal=", 10) ) { |
---|
183 | return _connect_internal(con, server, type, flags, timeout); |
---|
184 | } |
---|
185 | |
---|
186 | strncpy(user_sock, server, sizeof(user_sock)-1); |
---|
187 | user_sock[sizeof(user_sock)-1] = 0; |
---|
188 | |
---|
189 | |
---|
190 | if ( *user_sock != '/' ) { // don't test AF_UNIX sockets for ports |
---|
191 | for (i = 0; user_sock[i] != 0; i++) { |
---|
192 | if ( user_sock[i] == ':' ) { |
---|
193 | if ( user_sock[i+1] == ':' ) { // DECnet, leave unchanged |
---|
194 | is_decnet = 1; |
---|
195 | obj = &user_sock[i+2]; |
---|
196 | break; |
---|
197 | } |
---|
198 | |
---|
199 | port = atoi(&(user_sock[i+1])); |
---|
200 | user_sock[i] = 0; |
---|
201 | break; |
---|
202 | } |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | if ( is_decnet ) { |
---|
207 | if ( *user_sock == ':' ) { |
---|
208 | if ( roar_socket_get_local_nodename() != NULL ) { |
---|
209 | strncpy(user_sock, roar_socket_get_local_nodename(), sizeof(user_sock)-1); |
---|
210 | user_sock[sizeof(user_sock)-1] = 0; |
---|
211 | roar_mm_strlcat(user_sock, server, sizeof(user_sock)-1); |
---|
212 | user_sock[sizeof(user_sock)-1] = 0; |
---|
213 | obj = strstr(user_sock, "::"); |
---|
214 | obj += 2; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | if ( *obj == 0 ) { |
---|
219 | #ifdef DN_MAXOBJL |
---|
220 | roar_mm_strlcat(user_sock, ROAR_DEFAULT_OBJECT, sizeof(user_sock)-1); |
---|
221 | user_sock[sizeof(user_sock)-1] = 0; |
---|
222 | #else |
---|
223 | ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown."); |
---|
224 | #endif |
---|
225 | } |
---|
226 | ROAR_DBG("roar_connect_raw(*): user_sock='%s'", user_sock); |
---|
227 | } |
---|
228 | |
---|
229 | if ( port || is_decnet ) { |
---|
230 | fh = roar_socket_connect(type, user_sock, port); |
---|
231 | // restore the original string |
---|
232 | user_sock[i] = ':'; |
---|
233 | } else { |
---|
234 | #if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT) |
---|
235 | if ( user_sock[0] == '/' ) { |
---|
236 | if ( stat(user_sock, &sockstat) == 0 ) { |
---|
237 | if ( S_ISCHR(sockstat.st_mode) ) { |
---|
238 | #ifdef O_NOCTTY |
---|
239 | fh = open(user_sock, O_RDWR|O_NOCTTY, 0666); |
---|
240 | #else |
---|
241 | fh = open(user_sock, O_RDWR, 0666); |
---|
242 | #endif |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | #endif |
---|
247 | if ( fh == -1 ) |
---|
248 | fh = roar_socket_connect(type, user_sock, ROAR_DEFAULT_PORT); |
---|
249 | } |
---|
250 | |
---|
251 | if ( fh == -1 ) |
---|
252 | return -1; |
---|
253 | |
---|
254 | if ( roar_vio_open_fh_socket(con->viocon, fh) == -1 ) { |
---|
255 | err = roar_error; |
---|
256 | #ifdef ROAR_TARGET_WIN32 |
---|
257 | closesocket(fh); |
---|
258 | #else |
---|
259 | close(fh); |
---|
260 | #endif |
---|
261 | roar_error = err; |
---|
262 | return -1; |
---|
263 | } else { |
---|
264 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
265 | } |
---|
266 | |
---|
267 | roar_err_set(ROAR_ERROR_NONE); |
---|
268 | return 0; |
---|
269 | } |
---|
270 | |
---|
271 | static int roar_connect_raw (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) { |
---|
272 | #ifdef ROAR_HAVE_LIBSLP |
---|
273 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
274 | #endif |
---|
275 | #if defined(ROAR_HAVE_UNIX) |
---|
276 | char user_sock[128]; |
---|
277 | const char * roar_server; |
---|
278 | #endif |
---|
279 | #if defined(ROAR_HAVE_LIBSLP) || !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
280 | int i = 0; |
---|
281 | #endif |
---|
282 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
283 | struct passwd * pwd; |
---|
284 | #endif |
---|
285 | #ifdef ROAR_HAVE_LIBDNET |
---|
286 | struct stat decnet_stat; |
---|
287 | #endif |
---|
288 | #ifdef ROAR_HAVE_LIBX11 |
---|
289 | struct roar_x11_connection * x11con; |
---|
290 | #endif |
---|
291 | #ifdef ROAR_HAVE_LIBSLP |
---|
292 | struct roar_server * list; |
---|
293 | int workarounds_store; |
---|
294 | #endif |
---|
295 | |
---|
296 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
297 | |
---|
298 | if ( timeout != 0 ) { |
---|
299 | roar_err_set(ROAR_ERROR_INVAL); |
---|
300 | return -1; |
---|
301 | } |
---|
302 | |
---|
303 | if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK ) |
---|
304 | flags |= ROAR_ENUM_FLAG_NONBLOCK; |
---|
305 | |
---|
306 | |
---|
307 | if ( server == NULL || *server == 0 ) |
---|
308 | server = roar_libroar_get_server(); |
---|
309 | |
---|
310 | if ( server == NULL || *server == 0 ) |
---|
311 | server = roar_env_get("ROAR_SERVER"); |
---|
312 | |
---|
313 | #ifdef ROAR_HAVE_LIBX11 |
---|
314 | if ( server == NULL || *server == 0 ) { |
---|
315 | if ( (x11con = roar_x11_connect(NULL)) != NULL ) { |
---|
316 | server = roar_x11_get_prop(x11con, "ROAR_SERVER"); |
---|
317 | roar_x11_disconnect(x11con); |
---|
318 | } |
---|
319 | } |
---|
320 | #endif |
---|
321 | |
---|
322 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
323 | if ( (server == NULL || *server == 0) && (i = readlink("/etc/roarserver", user_sock, sizeof(user_sock)-1)) != -1 ) { |
---|
324 | user_sock[i] = 0; |
---|
325 | server = user_sock; |
---|
326 | } |
---|
327 | #endif |
---|
328 | |
---|
329 | if ( server != NULL && !strcasecmp(server, "+slp") ) { |
---|
330 | server = roar_slp_find_roard(0); |
---|
331 | if ( server == NULL ) { |
---|
332 | return -1; |
---|
333 | } |
---|
334 | } |
---|
335 | |
---|
336 | // "+default" is an alias for NULL. |
---|
337 | if ( server != NULL && !strcasecmp(server, "+default") ) { |
---|
338 | server = NULL; |
---|
339 | } |
---|
340 | |
---|
341 | if ( server == NULL || *server == 0 ) { |
---|
342 | /* connect via defaults */ |
---|
343 | |
---|
344 | #ifdef ROAR_HAVE_UNIX |
---|
345 | #ifndef ROAR_TARGET_MICROCONTROLLER |
---|
346 | roar_server = roar_env_get("HOME"); |
---|
347 | #else |
---|
348 | roar_server = NULL; |
---|
349 | #endif |
---|
350 | |
---|
351 | if ( roar_server == NULL ) { |
---|
352 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
353 | if ( (pwd = getpwuid(getuid())) == NULL ) { |
---|
354 | roar_server = "/NX-HOME-DIR"; |
---|
355 | } else { |
---|
356 | roar_server = pwd->pw_dir; |
---|
357 | } |
---|
358 | #else |
---|
359 | roar_server = "/WIN32-SUCKS"; |
---|
360 | #endif |
---|
361 | } |
---|
362 | |
---|
363 | snprintf(user_sock, sizeof(user_sock)-1, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER); |
---|
364 | user_sock[sizeof(user_sock)-1] = 0; |
---|
365 | |
---|
366 | if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 ) |
---|
367 | return 0; |
---|
368 | |
---|
369 | if ( _connect_server(con, ROAR_DEFAULT_SOCK_GLOBAL, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 ) |
---|
370 | return 0; |
---|
371 | #endif |
---|
372 | |
---|
373 | if ( _connect_server(con, ROAR_DEFAULT_HOSTPORT, ROAR_SOCKET_TYPE_TCP, flags, timeout) == 0 ) |
---|
374 | return 0; |
---|
375 | |
---|
376 | #ifdef ROAR_HAVE_LIBDNET |
---|
377 | if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) { |
---|
378 | if ( roar_socket_get_local_nodename() ) { |
---|
379 | snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT); |
---|
380 | if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_DECNET, flags, timeout) == 0 ) |
---|
381 | return 0; |
---|
382 | } |
---|
383 | } |
---|
384 | #endif |
---|
385 | |
---|
386 | if ( _connect_server(con, "+abstract", ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) == 0 ) |
---|
387 | return 0; |
---|
388 | |
---|
389 | #ifdef ROAR_HAVE_LIBSLP |
---|
390 | if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) && |
---|
391 | !(flags & ROAR_ENUM_FLAG_NONBLOCK) && |
---|
392 | !(flags & ROAR_ENUM_FLAG_LOCALONLY) |
---|
393 | ) { |
---|
394 | if ( (server = roar_slp_find_roard(0)) != NULL ) { |
---|
395 | if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) |
---|
396 | return 0; |
---|
397 | |
---|
398 | /* in case we can not connect to the server given this may be a cache problem, |
---|
399 | we do a new lookup with the cache disabled in this case */ |
---|
400 | ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache"); |
---|
401 | if ( (server = roar_slp_find_roard(1)) != NULL ) |
---|
402 | if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) |
---|
403 | return 0; |
---|
404 | } |
---|
405 | } |
---|
406 | |
---|
407 | workarounds_store = config->workaround.workarounds; |
---|
408 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP; |
---|
409 | list = roar_enum_servers(flags, -1, -1); |
---|
410 | config->workaround.workarounds = workarounds_store; |
---|
411 | if ( list != NULL ) { |
---|
412 | for (i = 0; list[i].server != NULL; i++) { |
---|
413 | if ( _connect_server(con, list[i].server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) { |
---|
414 | roar_enum_servers_free(list); |
---|
415 | return 0; |
---|
416 | } |
---|
417 | } |
---|
418 | roar_enum_servers_free(list); |
---|
419 | } |
---|
420 | #endif |
---|
421 | |
---|
422 | return -1; |
---|
423 | |
---|
424 | } else { |
---|
425 | /* connect via (char*)server */ |
---|
426 | if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) != 0 ) |
---|
427 | return -1; |
---|
428 | return 0; |
---|
429 | } |
---|
430 | |
---|
431 | roar_err_set(ROAR_ERROR_NODEV); |
---|
432 | ROAR_DBG("roar_connect_raw(*) = -1 // error=NODEV"); |
---|
433 | return -1; |
---|
434 | } |
---|
435 | |
---|
436 | int roar_connect (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) { |
---|
437 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
438 | |
---|
439 | if ( con == NULL ) { |
---|
440 | roar_err_set(ROAR_ERROR_FAULT); |
---|
441 | return -1; |
---|
442 | } |
---|
443 | |
---|
444 | if ( roar_connect_none(con) == -1 ) |
---|
445 | return -1; |
---|
446 | |
---|
447 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
448 | if ( roar_connect_raw(con, server, flags | config->serverflags, timeout) == -1 ) |
---|
449 | return -1; |
---|
450 | |
---|
451 | // TODO: this should be moved to be within _connect_server() so it also stores server names of auto located servers. |
---|
452 | if ( server != NULL ) { |
---|
453 | con->server_name = roar_mm_strdup(server); |
---|
454 | } |
---|
455 | |
---|
456 | return 0; |
---|
457 | } |
---|
458 | |
---|
459 | int roar_connect_none (struct roar_connection * con) { |
---|
460 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
461 | |
---|
462 | if ( con == NULL ) { |
---|
463 | roar_err_set(ROAR_ERROR_INVAL); |
---|
464 | return -1; |
---|
465 | } |
---|
466 | |
---|
467 | memset(con, 0, sizeof(struct roar_connection)); |
---|
468 | con->refc = 1; |
---|
469 | con->flags = ROAR_CON_FLAGS_ISCLIENT; |
---|
470 | con->version = _ROAR_MESSAGE_VERSION; |
---|
471 | con->cb_userdata = NULL; |
---|
472 | con->cb = NULL; |
---|
473 | con->server_stds = NULL; |
---|
474 | con->server_name = NULL; |
---|
475 | |
---|
476 | roar_err_init(&(con->errorframe)); |
---|
477 | |
---|
478 | con->viocon = &(con->viocon_store); |
---|
479 | |
---|
480 | // con->flags |= ROAR_CON_FLAGS_VIO; |
---|
481 | |
---|
482 | if ( config->protocolversion != -1 ) |
---|
483 | roar_set_connection_version(con, config->protocolversion); |
---|
484 | |
---|
485 | roar_err_set(ROAR_ERROR_NONE); |
---|
486 | return 0; |
---|
487 | } |
---|
488 | |
---|
489 | int roar_connect_vio (struct roar_connection * con, struct roar_vio_calls * vio) { |
---|
490 | if ( con == NULL || vio == NULL ) { |
---|
491 | roar_err_set(ROAR_ERROR_INVAL); |
---|
492 | return -1; |
---|
493 | } |
---|
494 | |
---|
495 | if ( roar_connect_none(con) == -1 ) |
---|
496 | return -1; |
---|
497 | |
---|
498 | con->viocon = vio; |
---|
499 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
500 | |
---|
501 | return -1; |
---|
502 | } |
---|
503 | |
---|
504 | int roar_connect_fh (struct roar_connection * con, int fh) { |
---|
505 | |
---|
506 | if ( con == NULL || fh == -1 ) { |
---|
507 | roar_err_set(ROAR_ERROR_INVAL); |
---|
508 | return -1; |
---|
509 | } |
---|
510 | |
---|
511 | if ( roar_connect_none(con) == -1 ) |
---|
512 | return -1; |
---|
513 | |
---|
514 | // special hack to set an illegal value used internally in libroar: |
---|
515 | if ( fh == -2 ) |
---|
516 | fh = -1; |
---|
517 | |
---|
518 | if ( roar_vio_open_fh_socket(con->viocon, fh) != -1 ) { |
---|
519 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
520 | } else { |
---|
521 | return -1; |
---|
522 | } |
---|
523 | |
---|
524 | roar_err_set(ROAR_ERROR_NONE); |
---|
525 | return 0; |
---|
526 | } |
---|
527 | |
---|
528 | int roar_get_connection_fh (struct roar_connection * con) { |
---|
529 | int fh; |
---|
530 | |
---|
531 | ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con); |
---|
532 | |
---|
533 | roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL); |
---|
534 | |
---|
535 | if ( con == NULL ) |
---|
536 | return -1; |
---|
537 | |
---|
538 | ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con); |
---|
539 | |
---|
540 | if ( roar_vio_ctl(con->viocon, ROAR_VIO_CTL_GET_FH, &fh) == -1 ) |
---|
541 | return -1; |
---|
542 | |
---|
543 | ROAR_DBG("roar_get_connection_fh(con=%p) = %i", con, fh); |
---|
544 | |
---|
545 | return fh; |
---|
546 | } |
---|
547 | |
---|
548 | struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) { |
---|
549 | if ( con == NULL ) { |
---|
550 | roar_err_set(ROAR_ERROR_FAULT); |
---|
551 | return NULL; |
---|
552 | } |
---|
553 | |
---|
554 | if ( con->flags & ROAR_CON_FLAGS_VIO ) |
---|
555 | return con->viocon; |
---|
556 | |
---|
557 | // TODO: try to open the VIO. |
---|
558 | |
---|
559 | roar_err_set(ROAR_ERROR_BADFH); |
---|
560 | return NULL; |
---|
561 | } |
---|
562 | |
---|
563 | const char * roar_get_connection_server(struct roar_connection * con) { |
---|
564 | if ( con == NULL ) { |
---|
565 | roar_err_set(ROAR_ERROR_FAULT); |
---|
566 | return NULL; |
---|
567 | } |
---|
568 | |
---|
569 | return con->server_name; |
---|
570 | } |
---|
571 | |
---|
572 | int roar_connectionref(struct roar_connection * con) { |
---|
573 | if ( con == NULL ) { |
---|
574 | roar_err_set(ROAR_ERROR_FAULT); |
---|
575 | return -1; |
---|
576 | } |
---|
577 | |
---|
578 | con->refc++; |
---|
579 | |
---|
580 | return 0; |
---|
581 | } |
---|
582 | |
---|
583 | int roar_connectionunref(struct roar_connection * con) { |
---|
584 | struct roar_vio_calls * vio; |
---|
585 | struct roar_message m; |
---|
586 | |
---|
587 | if ( con == NULL ) { |
---|
588 | roar_err_set(ROAR_ERROR_FAULT); |
---|
589 | return -1; |
---|
590 | } |
---|
591 | |
---|
592 | con->refc--; |
---|
593 | |
---|
594 | if ( con->refc ) |
---|
595 | return 0; |
---|
596 | |
---|
597 | memset(&m, 0, sizeof(m)); |
---|
598 | |
---|
599 | m.datalen = 0; |
---|
600 | m.stream = -1; |
---|
601 | m.pos = 0; |
---|
602 | m.cmd = ROAR_CMD_QUIT; |
---|
603 | |
---|
604 | roar_req(con, &m, NULL); |
---|
605 | |
---|
606 | if ( (vio = roar_get_connection_vio2(con)) != NULL ) { |
---|
607 | roar_vio_close(vio); |
---|
608 | } |
---|
609 | |
---|
610 | if ( con->server_stds != NULL ) { |
---|
611 | roar_stds_free(con->server_stds); |
---|
612 | con->server_stds = NULL; |
---|
613 | } |
---|
614 | |
---|
615 | if ( con->server_name != NULL ) { |
---|
616 | roar_mm_free(con->server_name); |
---|
617 | con->server_name = NULL; |
---|
618 | } |
---|
619 | |
---|
620 | if ( con->flags & ROAR_CON_FLAGS_FREESELF ) { |
---|
621 | roar_mm_free(con); |
---|
622 | } else { |
---|
623 | roar_connect_fh(con, -2); |
---|
624 | } |
---|
625 | |
---|
626 | return 0; |
---|
627 | } |
---|
628 | |
---|
629 | int roar_set_connection_callback(struct roar_connection * con, |
---|
630 | void (*cb)(struct roar_connection * con, |
---|
631 | struct roar_message * mes, |
---|
632 | void * data, |
---|
633 | void * userdata), |
---|
634 | void * userdata) { |
---|
635 | if ( con == NULL ) { |
---|
636 | roar_err_set(ROAR_ERROR_FAULT); |
---|
637 | return -1; |
---|
638 | } |
---|
639 | |
---|
640 | con->cb = cb; |
---|
641 | con->cb_userdata = userdata; |
---|
642 | |
---|
643 | return 0; |
---|
644 | } |
---|
645 | |
---|
646 | int roar_set_connection_version(struct roar_connection * con, int version) { |
---|
647 | if ( con == NULL ) { |
---|
648 | roar_err_set(ROAR_ERROR_FAULT); |
---|
649 | return -1; |
---|
650 | } |
---|
651 | |
---|
652 | if ( version == -1 ) |
---|
653 | version = _ROAR_MESSAGE_VERSION; |
---|
654 | |
---|
655 | if ( version < 0 ) { |
---|
656 | roar_err_set(ROAR_ERROR_INVAL); |
---|
657 | return -1; |
---|
658 | } |
---|
659 | |
---|
660 | con->version = version; |
---|
661 | |
---|
662 | if ( version == 2 ) { |
---|
663 | con->flags |= ROAR_CON_FLAGS_SUPPORT_V2; |
---|
664 | } |
---|
665 | |
---|
666 | return 0; |
---|
667 | } |
---|
668 | |
---|
669 | int16_t roar_message_genseq(struct roar_connection * con, int is_client) { |
---|
670 | static int16_t store = 0; |
---|
671 | int16_t ret = 0; |
---|
672 | |
---|
673 | ret = store++; |
---|
674 | |
---|
675 | if ( ret == 0 ) |
---|
676 | ret = store++; |
---|
677 | |
---|
678 | if ( is_client == -1 ) { |
---|
679 | if ( con != NULL ) { |
---|
680 | is_client = con->flags & ROAR_CON_FLAGS_ISCLIENT ? 1 : 0; |
---|
681 | } |
---|
682 | } |
---|
683 | |
---|
684 | if ( is_client == -1 ) |
---|
685 | is_client = 1; |
---|
686 | |
---|
687 | ret |= 0x8000U; |
---|
688 | if ( !is_client ) |
---|
689 | ret -= 0x8000U; |
---|
690 | |
---|
691 | return ret; |
---|
692 | } |
---|
693 | |
---|
694 | int roar_sync (struct roar_connection * con) { |
---|
695 | // wait for any non-client reqs |
---|
696 | return roar_wait_msg(con, 0x0000, 0x8000); |
---|
697 | } |
---|
698 | |
---|
699 | int roar_wait_msg (struct roar_connection * con, int16_t seq, int16_t seqmask) { |
---|
700 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
701 | return -1; |
---|
702 | } |
---|
703 | |
---|
704 | int roar_noop (struct roar_connection * con) { |
---|
705 | struct roar_message mes; |
---|
706 | |
---|
707 | if ( con == NULL ) { |
---|
708 | roar_err_set(ROAR_ERROR_FAULT); |
---|
709 | return -1; |
---|
710 | } |
---|
711 | |
---|
712 | memset(&mes, 0, sizeof(mes)); |
---|
713 | |
---|
714 | mes.cmd = ROAR_CMD_NOOP; |
---|
715 | mes.stream = -1; |
---|
716 | |
---|
717 | return roar_req3(con, &mes, NULL); |
---|
718 | } |
---|
719 | |
---|
720 | int roar_identify (struct roar_connection * con, const char * name) { |
---|
721 | struct roar_message mes; |
---|
722 | uint32_t pid; |
---|
723 | int max_len; |
---|
724 | |
---|
725 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
726 | |
---|
727 | ROAR_DBG("roar_identify(*): try to identify myself..."); |
---|
728 | |
---|
729 | memset(&mes, 0, sizeof(mes)); |
---|
730 | |
---|
731 | mes.cmd = ROAR_CMD_IDENTIFY; |
---|
732 | mes.stream = -1; |
---|
733 | mes.pos = 0; |
---|
734 | |
---|
735 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
736 | |
---|
737 | if ( name == NULL ) |
---|
738 | name = "libroar client"; |
---|
739 | |
---|
740 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
741 | |
---|
742 | max_len = roar_mm_strlen(name); |
---|
743 | ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len); |
---|
744 | |
---|
745 | if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) ) |
---|
746 | max_len = LIBROAR_BUFFER_MSGDATA - 5; |
---|
747 | |
---|
748 | mes.datalen = 5 + max_len; |
---|
749 | mes.data[0] = 1; |
---|
750 | |
---|
751 | pid = getpid(); |
---|
752 | mes.data[1] = (pid & 0xFF000000UL) >> 24; |
---|
753 | mes.data[2] = (pid & 0x00FF0000UL) >> 16; |
---|
754 | mes.data[3] = (pid & 0x0000FF00UL) >> 8; |
---|
755 | mes.data[4] = (pid & 0x000000FFUL) >> 0; |
---|
756 | ROAR_DBG("roar_identify(*): pid = %i", (int)pid); |
---|
757 | |
---|
758 | strncpy(mes.data+5, name, max_len); |
---|
759 | |
---|
760 | return roar_req3(con, &mes, NULL); |
---|
761 | } |
---|
762 | |
---|
763 | //ll |
---|