source: roaraudio/libroar/basic.c @ 5843:62465e3791bc

Last change on this file since 5843:62465e3791bc was 5843:62465e3791bc, checked in by phi, 11 years ago

Added support for +fork on win32 (Closes: #337)

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