source: roaraudio/libroar/basic.c @ 6052:d48765b2475e

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

updated copyright headers

File size: 22.6 KB
Line 
1//basic.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2015
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#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
39#include <pwd.h>
40#endif
41
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
49int __get_daemonimage(char ** daemonimage, enum mode * mode, const char * server) {
50 const char * tmp = NULL;
51
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;
69   tmp = server;
70   *mode = NORMAL;
71  } else if ( server[0] == '!' ) {
72   server += 1;
73   tmp = server;
74   *mode = SYSTEM;
75  } else {
76   roar_err_set(ROAR_ERROR_ILLSEQ);
77   return -1;
78  }
79 }
80
81 if ( tmp == NULL || *tmp == 0 )
82  tmp = roar_libroar_get_config()->daemonimage;
83
84 // we keep this step to be compatible with older versions.
85 if ( tmp == NULL || *tmp == 0 ) {
86  tmp = roar_env_get("ROAR_DAEMONIMAGE");
87  if ( tmp != NULL ) {
88   ROAR_WARN("__get_daemonimage(*): Usage of $ROAR_DAEMONIMAGE is obsolete. Use ROAR_OPTIONS=daemonimage:...");
89  }
90 }
91
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;
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;
108 char * daemonimage = NULL;
109 char buf[64];
110 int port;
111 size_t i;
112 int fh;
113 int ret;
114 int err;
115
116 if ( __get_daemonimage(&daemonimage, &mode, server) == -1 )
117  return -1;
118
119 if ( mode != NORMAL ) {
120  roar_mm_free(daemonimage);
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();
127  if ( port < 1025 ) /* it seems on some strange systems the magic border is at 1025 not 1024. */
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 ) {
138  roar_mm_free(daemonimage);
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);
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);
158  roar_mm_free_noerror(daemonimage);
159  return -1;
160 }
161
162 roar_mm_free(daemonimage);
163
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;
186 char * daemonimage = NULL;
187 int socks[2];
188 int r;
189 char fhstr[12];
190 size_t len;
191 char * bin_sh;
192 int err;
193
194 if ( __get_daemonimage(&daemonimage, &mode, server) == -1 )
195  return -1;
196
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
203 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
204  roar_err_from_errno();
205  roar_mm_free_noerror(daemonimage);
206  return -1;
207 }
208
209 r = roar_fork(NULL);
210
211 if ( r == -1 ) { // error!
212  ROAR_ERR("_start_server(*): Can not fork: %s", roar_error2str(roar_error));
213  err = roar_error;
214  close(socks[0]);
215  close(socks[1]);
216  roar_mm_free(daemonimage);
217  roar_error = err;
218  return -1;
219 } else if ( r == 0 ) { // we are the child
220  roar_watchdog_stop();
221
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);
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     }
242     execlp("sh", "sh", "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL);
243    break;
244  }
245
246  roar_mm_free(daemonimage);
247
248  // we are still alive?
249  ROAR_ERR("_start_server(*): alive after exec(), that's bad!");
250  ROAR_U_EXIT(1);
251 } else { // we are the parent
252  close(socks[1]);
253  roar_mm_free(daemonimage);
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;
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)
269 return _start_server_win32(con, server, type, flags, timeout);
270#elif !defined(ROAR_TARGET_MICROCONTROLLER)
271 return _start_server_posix(con, server, type, flags, timeout);
272#else
273 ROAR_ERR("_start_server(*): There is no UNIX Domain Socket, download a real OS.");
274 roar_err_set(ROAR_ERROR_NOSYS);
275 return -1;
276#endif
277}
278
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
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;
309 int ret;
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;
323  con->server_name = roar_mm_strdup(server);
324  return 0;
325 } else if ( !strcmp(server, "+fork") || !strncmp(server, "+fork=", 6) ) {
326  return _start_server(con, server, type, flags, timeout);
327 } else if ( !strcmp(server, "+internal") || !strncmp(server, "+internal=", 10) ) {
328  ret = _connect_internal(con, server, type, flags, timeout);
329  if ( ret == 0 )
330   con->server_name = roar_mm_strdup(server);
331  return ret;
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  }
370
371  ROAR_DBG("roar_connect_raw(*): user_sock='%s'", user_sock);
372 }
373
374 if ( port || is_decnet ) {
375  fh = roar_socket_connect(type, user_sock, port);
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) ) {
383#ifdef O_NOCTTY
384     fh = open(user_sock, O_RDWR|O_NOCTTY, 0666);
385#else
386     fh = open(user_sock, O_RDWR, 0666);
387#endif
388    }
389   }
390  }
391#endif
392  if ( fh == -1 )
393   fh = roar_socket_connect(type, user_sock, ROAR_DEFAULT_PORT);
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
412 con->server_name = roar_mm_strdup(server);
413
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) {
419#ifdef ROAR_HAVE_LIBSLP
420 struct roar_libroar_config * config = roar_libroar_get_config();
421#endif
422#if defined(ROAR_HAVE_UNIX)
423 char user_sock[128];
424 const char * roar_server;
425#endif
426#if defined(ROAR_HAVE_LIBSLP) || !defined(ROAR_TARGET_MICROCONTROLLER)
427 int i = 0;
428#endif
429#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
430 struct passwd * pwd;
431 char * sysconf_roarserver;
432#endif
433#ifdef ROAR_HAVE_LIBDNET
434 struct stat decnet_stat;
435#endif
436#ifdef ROAR_HAVE_LIBX11
437 struct roar_x11_connection * x11con;
438#endif
439#ifdef ROAR_HAVE_LIBSLP
440 struct roar_server * list;
441 int workarounds_store;
442#endif
443
444 roar_err_set(ROAR_ERROR_UNKNOWN);
445
446 if ( timeout != 0 ) {
447  roar_err_set(ROAR_ERROR_INVAL);
448  return -1;
449 }
450
451 if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK )
452  flags |= ROAR_ENUM_FLAG_NONBLOCK;
453
454
455 if ( server == NULL || *server == 0 )
456  server = roar_libroar_get_server();
457
458 if ( server == NULL || *server == 0 )
459  server = roar_env_get("ROAR_SERVER");
460
461#ifdef ROAR_HAVE_LIBX11
462 if ( server == NULL || *server == 0 ) {
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
470#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
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);
478 }
479#endif
480
481 if ( server != NULL && !strcasecmp(server, "+slp") ) {
482  server = roar_slp_find_roard(0);
483  if ( server == NULL ) {
484   return -1;
485  }
486 }
487
488 // "+default" is an alias for NULL.
489 if ( server != NULL && !strcasecmp(server, "+default") ) {
490  server = NULL;
491 }
492
493 if ( server == NULL || *server == 0 ) {
494  /* connect via defaults */
495
496#ifdef ROAR_HAVE_UNIX
497#ifndef ROAR_TARGET_MICROCONTROLLER
498  roar_server = roar_env_get("HOME");
499#else
500  roar_server = NULL;
501#endif
502
503  if ( roar_server == NULL ) {
504#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
505   if ( (pwd = getpwuid(getuid())) != NULL ) {
506    roar_server = pwd->pw_dir;
507   }
508#endif
509  }
510
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;
514
515   if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 )
516    return 0;
517  }
518
519  if ( _connect_server(con, ROAR_DEFAULT_SOCK_GLOBAL, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 )
520   return 0;
521#endif
522
523  if ( _connect_server(con, ROAR_DEFAULT_HOSTPORT, ROAR_SOCKET_TYPE_TCP, flags, timeout) == 0 )
524   return 0;
525
526#ifdef ROAR_HAVE_LIBDNET
527  if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) {
528   if ( roar_socket_get_local_nodename() ) {
529    snprintf(user_sock, sizeof(user_sock)-1, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT);
530    if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_DECNET, flags, timeout) == 0 )
531     return 0;
532   }
533  }
534#endif
535
536  if ( _connect_server(con, "+abstract", ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) == 0 )
537   return 0;
538
539#ifdef ROAR_HAVE_LIBSLP
540 if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) &&
541      !(flags & ROAR_ENUM_FLAG_NONBLOCK) &&
542      !(flags & ROAR_ENUM_FLAG_LOCALONLY)
543    ) {
544  if ( (server = roar_slp_find_roard(0)) != NULL ) {
545   if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 )
546    return 0;
547
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                     */
550   ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache");
551   if ( (server = roar_slp_find_roard(1)) != NULL )
552    if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 )
553     return 0;
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++) {
563   if ( _connect_server(con, list[i].server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) {
564    roar_enum_servers_free(list);
565    return 0;
566   }
567  }
568  roar_enum_servers_free(list);
569 }
570#endif
571
572 return -1;
573
574 } else {
575  /* connect via (char*)server */
576  if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) != 0 )
577   return -1;
578  return 0;
579 }
580
581 roar_err_set(ROAR_ERROR_NODEV);
582 ROAR_DBG("roar_connect_raw(*) = -1 // error=NODEV");
583 return -1;
584}
585
586int roar_connect     (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) {
587 struct roar_libroar_config * config = roar_libroar_get_config();
588
589 if ( con == NULL ) {
590  roar_err_set(ROAR_ERROR_FAULT);
591  return -1;
592 }
593
594 if ( roar_connect_none(con) == -1 )
595  return -1;
596
597 if ( roar_connect_raw(con, server, flags | config->serverflags, timeout) == -1 )
598  return -1;
599
600 return 0;
601}
602
603int roar_connect_none (struct roar_connection * con) {
604 struct roar_libroar_config * config = roar_libroar_get_config();
605
606 if ( con == NULL ) {
607  roar_err_set(ROAR_ERROR_INVAL);
608  return -1;
609 }
610
611 memset(con, 0, sizeof(struct roar_connection));
612 con->refc        = 1;
613 con->flags       = ROAR_CON_FLAGS_ISCLIENT;
614 con->version     = _ROAR_MESSAGE_VERSION;
615 con->cb_userdata = NULL;
616 con->cb          = NULL;
617 con->server_stds = NULL;
618 con->server_name = NULL;
619
620 roar_err_init(&(con->errorframe));
621
622 con->viocon = &(con->viocon_store);
623
624// con->flags |= ROAR_CON_FLAGS_VIO;
625
626 if ( config->protocolversion != -1 )
627  roar_set_connection_version(con, config->protocolversion);
628
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
658 // special hack to set an illegal value used internally in libroar:
659 if ( fh == -2 )
660  fh = -1;
661
662 if ( roar_vio_open_fh_socket(con->viocon, fh) != -1 ) {
663  con->flags |= ROAR_CON_FLAGS_VIO;
664 } else {
665  return -1;
666 }
667
668 roar_err_set(ROAR_ERROR_NONE);
669 return 0;
670}
671
672int roar_get_connection_fh (struct roar_connection * con) {
673 int fh;
674
675 ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con);
676
677 roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL);
678
679 if ( con == NULL )
680  return -1;
681
682 ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con);
683
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
689  return -1;
690#endif
691 }
692
693 ROAR_DBG("roar_get_connection_fh(con=%p) = %i", con, fh);
694
695 return fh;
696}
697
698struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) {
699 if ( con == NULL ) {
700  roar_err_set(ROAR_ERROR_FAULT);
701  return NULL;
702 }
703
704 if ( con->flags & ROAR_CON_FLAGS_VIO )
705  return con->viocon;
706
707// TODO: try to open the VIO.
708
709 roar_err_set(ROAR_ERROR_BADFH);
710 return NULL;
711}
712
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) {
734 struct roar_vio_calls * vio;
735 struct roar_message m;
736
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
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);
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   }
753  }
754 }
755
756 memset(&m, 0, sizeof(m));
757
758 m.datalen =  0;
759 m.stream  = -1;
760 m.pos     =  0;
761 m.cmd     = ROAR_CMD_QUIT;
762
763 roar_req(con, &m, NULL);
764
765 if ( (vio = roar_get_connection_vio2(con)) != NULL ) {
766  roar_vio_close(vio);
767 }
768
769 if ( con->server_stds != NULL ) {
770  roar_stds_free(con->server_stds);
771  con->server_stds = NULL;
772 }
773
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 }
784
785 return 0;
786}
787
788int roar_set_connection_callback(struct roar_connection * con,
789                                 void (*cb)(struct roar_connection * con,
790                                            struct roar_message    * mes,
791                                            void                   * data,
792                                            void                   * userdata),
793                                 void * userdata) {
794 if ( con == NULL ) {
795  roar_err_set(ROAR_ERROR_FAULT);
796  return -1;
797 }
798
799 con->cb       = cb;
800 con->cb_userdata = userdata;
801
802 return 0;
803}
804
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
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
834 if ( ret == 0 )
835  ret = store++;
836
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
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) {
859 roar_err_set(ROAR_ERROR_NOSYS);
860 return -1;
861}
862
863int roar_noop         (struct roar_connection * con) {
864 struct roar_message mes;
865
866 if ( con == NULL ) {
867  roar_err_set(ROAR_ERROR_FAULT);
868  return -1;
869 }
870
871 memset(&mes, 0, sizeof(mes));
872
873 mes.cmd = ROAR_CMD_NOOP;
874 mes.stream = -1;
875
876 return roar_req3(con, &mes, NULL);
877}
878
879int roar_identify   (struct roar_connection * con, const char * name) {
880 struct roar_message mes;
881 uint32_t pid;
882 int max_len;
883
884 roar_err_set(ROAR_ERROR_UNKNOWN);
885
886 ROAR_DBG("roar_identify(*): try to identify myself...");
887
888 memset(&mes, 0, sizeof(mes));
889
890 mes.cmd    = ROAR_CMD_IDENTIFY;
891 mes.stream = -1;
892 mes.pos    =  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
901 max_len = roar_mm_strlen(name);
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();
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);
916
917 strncpy(mes.data+5, name, max_len);
918
919 return roar_req3(con, &mes, NULL);
920}
921
922//ll
Note: See TracBrowser for help on using the repository browser.