source: roaraudio/libroar/basic.c @ 5895:2bcffab4cd73

Last change on this file since 5895:2bcffab4cd73 was 5895:2bcffab4cd73, checked in by phi, 11 years ago

Moved away from roar_libroar_get_path_static()

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