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
RevLine 
[0]1//basic.c:
2
[690]3/*
[5823]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013
[690]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[690]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
[0]36#include "libroar.h"
37
[5843]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
[5895]45int __get_daemonimage(char ** daemonimage, enum mode * mode, const char * server) {
46 const char * tmp = NULL;
47
[5843]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;
[5895]65   tmp = server;
[5843]66   *mode = NORMAL;
67  } else if ( server[0] == '!' ) {
68   server += 1;
[5895]69   tmp = server;
[5843]70   *mode = SYSTEM;
71  } else {
72   roar_err_set(ROAR_ERROR_ILLSEQ);
73   return -1;
74  }
75 }
76
[5895]77 if ( tmp == NULL || *tmp == 0 )
78  tmp = roar_libroar_get_config()->daemonimage;
[5843]79
80 // we keep this step to be compatible with older versions.
[5895]81 if ( tmp == NULL || *tmp == 0 ) {
82  tmp = roar_env_get("ROAR_DAEMONIMAGE");
83  if ( tmp != NULL ) {
[5843]84   ROAR_WARN("__get_daemonimage(*): Usage of $ROAR_DAEMONIMAGE is obsolete. Use ROAR_OPTIONS=daemonimage:...");
85  }
86 }
87
[5895]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;
[5843]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;
[5895]104 char * daemonimage = NULL;
[5843]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 ) {
[5895]115  roar_mm_free(daemonimage);
[5843]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();
[5848]122  if ( port < 1025 ) /* it seems on some strange systems the magic border is at 1025 not 1024. */
[5843]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 ) {
[5895]133  roar_mm_free(daemonimage);
[5843]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);
[5844]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);
[5895]153  err = roar_error;
154  roar_mm_free(daemonimage);
155  roar_error = err;
[5844]156  return -1;
157 }
[5843]158
[5895]159 roar_mm_free(daemonimage);
160
[5843]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;
[5895]183 char * daemonimage = NULL;
[5369]184 int socks[2];
185 int r;
186 char fhstr[12];
[5838]187 size_t len;
[5895]188 char * bin_sh;
189 int err;
[5369]190
[5843]191 if ( __get_daemonimage(&daemonimage, &mode, server) == -1 )
192  return -1;
[5369]193
[5838]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
[5369]200 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
201  roar_err_from_errno();
[5895]202  err = roar_error;
203  roar_mm_free(daemonimage);
204  roar_error = err;
[5369]205  return -1;
206 }
207
[5373]208 r = roar_fork(NULL);
[5369]209
210 if ( r == -1 ) { // error!
[5373]211  ROAR_ERR("_start_server(*): Can not fork: %s", roar_error2str(roar_error));
[5895]212  err = roar_error;
[5369]213  close(socks[0]);
214  close(socks[1]);
[5895]215  roar_mm_free(daemonimage);
216  roar_error = err;
[5369]217  return -1;
218 } else if ( r == 0 ) { // we are the child
[5619]219  roar_watchdog_stop();
220
[5369]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);
[5895]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     }
[5369]241     execlp("sh", "sh", "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL);
242    break;
243  }
244
[5895]245  roar_mm_free(daemonimage);
246
[5369]247  // we are still alive?
[5373]248  ROAR_ERR("_start_server(*): alive after exec(), that's bad!");
[5619]249  ROAR_U_EXIT(1);
[5369]250 } else { // we are the parent
251  close(socks[1]);
[5895]252  roar_mm_free(daemonimage);
[5369]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;
[5843]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)
[5847]268 return _start_server_win32(con, server, type, flags, timeout);
[5843]269#elif !defined(ROAR_TARGET_MICROCONTROLLER)
270 return _start_server_posix(con, server, type, flags, timeout);
[5369]271#else
[5843]272 ROAR_ERR("_start_server(*): There is no UNIX Domain Socket, download a real OS.");
273 roar_err_set(ROAR_ERROR_NOSYS);
[5369]274 return -1;
275#endif
276}
277
[5373]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
[5368]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;
[5838]308 int ret;
[5368]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;
[5838]322  con->server_name = roar_mm_strdup(server);
[5368]323  return 0;
[5369]324 } else if ( !strcmp(server, "+fork") || !strncmp(server, "+fork=", 6) ) {
[5847]325  return _start_server(con, server, type, flags, timeout);
[5373]326 } else if ( !strcmp(server, "+internal") || !strncmp(server, "+internal=", 10) ) {
[5838]327  ret = _connect_internal(con, server, type, flags, timeout);
328  if ( ret == 0 )
329   con->server_name = roar_mm_strdup(server);
330  return ret;
[5368]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  }
[5838]369
370  ROAR_DBG("roar_connect_raw(*): user_sock='%s'", user_sock);
[5368]371 }
372
373 if ( port || is_decnet ) {
[5375]374  fh = roar_socket_connect(type, user_sock, port);
[5368]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) ) {
[5406]382#ifdef O_NOCTTY
[5428]383     fh = open(user_sock, O_RDWR|O_NOCTTY, 0666);
[5406]384#else
[5428]385     fh = open(user_sock, O_RDWR, 0666);
[5406]386#endif
[5368]387    }
388   }
389  }
390#endif
[5428]391  if ( fh == -1 )
392   fh = roar_socket_connect(type, user_sock, ROAR_DEFAULT_PORT);
[5368]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
[5838]411 con->server_name = roar_mm_strdup(server);
412
[5368]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) {
[4780]418#ifdef ROAR_HAVE_LIBSLP
[4653]419 struct roar_libroar_config * config = roar_libroar_get_config();
[4780]420#endif
[5745]421#if defined(ROAR_HAVE_UNIX)
[5114]422 char user_sock[128];
[5754]423 const char * roar_server;
[5393]424#endif
425#if defined(ROAR_HAVE_LIBSLP) || !defined(ROAR_TARGET_MICROCONTROLLER)
[701]426 int i = 0;
[5393]427#endif
[1475]428#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[1026]429 struct passwd * pwd;
[5895]430 char * sysconf_roarserver;
[1393]431#endif
[828]432#ifdef ROAR_HAVE_LIBDNET
433 struct stat decnet_stat;
434#endif
[3372]435#ifdef ROAR_HAVE_LIBX11
436 struct roar_x11_connection * x11con;
437#endif
[4945]438#ifdef ROAR_HAVE_LIBSLP
[4806]439 struct roar_server * list;
440 int workarounds_store;
[4945]441#endif
[0]442
[4873]443 roar_err_set(ROAR_ERROR_UNKNOWN);
[807]444
[4806]445 if ( timeout != 0 ) {
[4873]446  roar_err_set(ROAR_ERROR_INVAL);
[4806]447  return -1;
448 }
449
450 if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK )
451  flags |= ROAR_ENUM_FLAG_NONBLOCK;
452
453
[5368]454 if ( server == NULL || *server == 0 )
[2567]455  server = roar_libroar_get_server();
456
[5368]457 if ( server == NULL || *server == 0 )
[5754]458  server = roar_env_get("ROAR_SERVER");
[0]459
[3372]460#ifdef ROAR_HAVE_LIBX11
[5368]461 if ( server == NULL || *server == 0 ) {
[3372]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
[1436]469#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[5895]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);
[448]477 }
[1093]478#endif
[448]479
[3077]480 if ( server != NULL && !strcasecmp(server, "+slp") ) {
[3076]481  server = roar_slp_find_roard(0);
482  if ( server == NULL ) {
483   return -1;
484  }
485 }
486
[5471]487 // "+default" is an alias for NULL.
488 if ( server != NULL && !strcasecmp(server, "+default") ) {
489  server = NULL;
490 }
491
[61]492 if ( server == NULL || *server == 0 ) {
[0]493  /* connect via defaults */
494
[1764]495#ifdef ROAR_HAVE_UNIX
[1436]496#ifndef ROAR_TARGET_MICROCONTROLLER
[5754]497  roar_server = roar_env_get("HOME");
[1435]498#else
499  roar_server = NULL;
500#endif
[1026]501
502  if ( roar_server == NULL ) {
[1436]503#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[5895]504   if ( (pwd = getpwuid(getuid())) != NULL ) {
[1026]505    roar_server = pwd->pw_dir;
506   }
[1078]507#endif
[1026]508  }
509
[5895]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;
[0]513
[5895]514   if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 )
515    return 0;
516  }
[0]517
[5368]518  if ( _connect_server(con, ROAR_DEFAULT_SOCK_GLOBAL, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 )
519   return 0;
[1764]520#endif
[237]521
[5368]522  if ( _connect_server(con, ROAR_DEFAULT_HOSTPORT, ROAR_SOCKET_TYPE_TCP, flags, timeout) == 0 )
523   return 0;
[0]524
[828]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);
[5368]529    if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_DECNET, flags, timeout) == 0 )
530     return 0;
[828]531   }
[522]532  }
[828]533#endif
[2007]534
[5375]535  if ( _connect_server(con, "+abstract", ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) == 0 )
[5368]536   return 0;
[4109]537
[2007]538#ifdef ROAR_HAVE_LIBSLP
[4806]539 if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) &&
[5373]540      !(flags & ROAR_ENUM_FLAG_NONBLOCK) &&
541      !(flags & ROAR_ENUM_FLAG_LOCALONLY)
[4806]542    ) {
543  if ( (server = roar_slp_find_roard(0)) != NULL ) {
[5375]544   if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 )
[5368]545    return 0;
[2014]546
[4806]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                     */
[5237]549   ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache");
[4806]550   if ( (server = roar_slp_find_roard(1)) != NULL )
[5375]551    if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 )
[5368]552     return 0;
[4806]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++) {
[5375]562   if ( _connect_server(con, list[i].server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) {
[4806]563    roar_enum_servers_free(list);
[5368]564    return 0;
[4806]565   }
566  }
567  roar_enum_servers_free(list);
[4653]568 }
[2007]569#endif
[522]570
[2014]571 return -1;
572
[0]573 } else {
574  /* connect via (char*)server */
[5375]575  if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) != 0 )
[5369]576   return -1;
577  return 0;
[0]578 }
579
[5368]580 roar_err_set(ROAR_ERROR_NODEV);
581 ROAR_DBG("roar_connect_raw(*) = -1 // error=NODEV");
582 return -1;
[0]583}
584
[5237]585int roar_connect     (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) {
[5373]586 struct roar_libroar_config * config = roar_libroar_get_config();
587
[1315]588 if ( con == NULL ) {
[4873]589  roar_err_set(ROAR_ERROR_FAULT);
[1315]590  return -1;
591 }
592
[5368]593 if ( roar_connect_none(con) == -1 )
[0]594  return -1;
595
[5373]596 if ( roar_connect_raw(con, server, flags | config->serverflags, timeout) == -1 )
[5296]597  return -1;
598
599 return 0;
[1315]600}
601
[5365]602int roar_connect_none (struct roar_connection * con) {
[5465]603 struct roar_libroar_config * config = roar_libroar_get_config();
604
[5365]605 if ( con == NULL ) {
[4873]606  roar_err_set(ROAR_ERROR_INVAL);
[1315]607  return -1;
608 }
609
610 memset(con, 0, sizeof(struct roar_connection));
[5296]611 con->refc        = 1;
[5467]612 con->flags       = ROAR_CON_FLAGS_ISCLIENT;
[5465]613 con->version     = _ROAR_MESSAGE_VERSION;
[5232]614 con->cb_userdata = NULL;
615 con->cb          = NULL;
[5296]616 con->server_stds = NULL;
617 con->server_name = NULL;
[5232]618
619 roar_err_init(&(con->errorframe));
[1315]620
[5296]621 con->viocon = &(con->viocon_store);
622
[5365]623// con->flags |= ROAR_CON_FLAGS_VIO;
624
[5465]625 if ( config->protocolversion != -1 )
626  roar_set_connection_version(con, config->protocolversion);
627
[5365]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
[5637]657 // special hack to set an illegal value used internally in libroar:
[5365]658 if ( fh == -2 )
659  fh = -1;
660
[5296]661 if ( roar_vio_open_fh_socket(con->viocon, fh) != -1 ) {
[3869]662  con->flags |= ROAR_CON_FLAGS_VIO;
[5608]663 } else {
664  return -1;
[5231]665 }
[1315]666
[4873]667 roar_err_set(ROAR_ERROR_NONE);
[0]668 return 0;
669}
670
[1660]671int roar_get_connection_fh (struct roar_connection * con) {
[5231]672 int fh;
673
674 ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con);
675
[3869]676 roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL);
[2809]677
[2043]678 if ( con == NULL )
679  return -1;
680
[5231]681 ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con);
682
[5792]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
[3869]688  return -1;
[5792]689#endif
690 }
[3869]691
[5231]692 ROAR_DBG("roar_get_connection_fh(con=%p) = %i", con, fh);
[3869]693
[5231]694 return fh;
[2043]695}
696
[3869]697struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) {
[5608]698 if ( con == NULL ) {
699  roar_err_set(ROAR_ERROR_FAULT);
[3869]700  return NULL;
[5608]701 }
[3869]702
703 if ( con->flags & ROAR_CON_FLAGS_VIO )
[5296]704  return con->viocon;
[3869]705
706// TODO: try to open the VIO.
707
[5608]708 roar_err_set(ROAR_ERROR_BADFH);
[3869]709 return NULL;
710}
711
[5296]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) {
[3869]733 struct roar_vio_calls * vio;
[0]734 struct roar_message m;
735
[5296]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
[5841]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);
[5843]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   }
[5841]752  }
753 }
754
[3875]755 memset(&m, 0, sizeof(m));
756
[5144]757 m.datalen =  0;
758 m.stream  = -1;
759 m.pos     =  0;
[0]760 m.cmd     = ROAR_CMD_QUIT;
761
762 roar_req(con, &m, NULL);
763
[3869]764 if ( (vio = roar_get_connection_vio2(con)) != NULL ) {
765  roar_vio_close(vio);
[2809]766 }
[0]767
[5296]768 if ( con->server_stds != NULL ) {
769  roar_stds_free(con->server_stds);
770  con->server_stds = NULL;
771 }
[0]772
[5296]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 }
[807]783
[0]784 return 0;
785}
786
[3913]787int roar_set_connection_callback(struct roar_connection * con,
788                                 void (*cb)(struct roar_connection * con,
789                                            struct roar_message    * mes,
[5231]790                                            void                   * data,
[3913]791                                            void                   * userdata),
792                                 void * userdata) {
[5465]793 if ( con == NULL ) {
794  roar_err_set(ROAR_ERROR_FAULT);
[3913]795  return -1;
[5465]796 }
[3913]797
798 con->cb       = cb;
[5231]799 con->cb_userdata = userdata;
[3913]800
801 return 0;
802}
803
[5465]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
[5467]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
[5468]833 if ( ret == 0 )
834  ret = store++;
835
[5467]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
[3914]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) {
[5148]858 roar_err_set(ROAR_ERROR_NOSYS);
[3914]859 return -1;
860}
[3913]861
[3836]862int roar_noop         (struct roar_connection * con) {
863 struct roar_message mes;
864
865 if ( con == NULL ) {
[5148]866  roar_err_set(ROAR_ERROR_FAULT);
[3836]867  return -1;
868 }
869
870 memset(&mes, 0, sizeof(mes));
871
872 mes.cmd = ROAR_CMD_NOOP;
[5144]873 mes.stream = -1;
[3836]874
[5146]875 return roar_req3(con, &mes, NULL);
[3836]876}
877
[5114]878int roar_identify   (struct roar_connection * con, const char * name) {
[0]879 struct roar_message mes;
[5132]880 uint32_t pid;
[0]881 int max_len;
882
[4873]883 roar_err_set(ROAR_ERROR_UNKNOWN);
[807]884
[0]885 ROAR_DBG("roar_identify(*): try to identify myself...");
886
[3875]887 memset(&mes, 0, sizeof(mes));
888
[0]889 mes.cmd    = ROAR_CMD_IDENTIFY;
[5144]890 mes.stream = -1;
891 mes.pos    =  0;
[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
[5114]900 max_len = roar_mm_strlen(name);
[0]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();
[5132]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);
[0]915
916 strncpy(mes.data+5, name, max_len);
917
[5146]918 return roar_req3(con, &mes, NULL);
[0]919}
920
921//ll
Note: See TracBrowser for help on using the repository browser.