source: roaraudio/libroar/basic.c @ 6076:b81c397aee90

Last change on this file since 6076:b81c397aee90 was 6052:d48765b2475e, checked in by phi, 9 years ago

updated copyright headers

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