source: roaraudio/libroar/basic.c @ 5889:d866fb1213d6

Last change on this file since 5889:d866fb1213d6 was 5848:49bec75c55fe, checked in by phi, 11 years ago

do not use low/prio/root ports

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