source: roaraudio/libroar/basic.c @ 5845:43d648524685

Last change on this file since 5845:43d648524685 was 5845:43d648524685, checked in by phi, 11 years ago

small error value correction/debug lion update

File size: 21.9 KB
RevLine 
[0]1//basic.c:
2
[690]3/*
[5823]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013
[690]5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[690]23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
[0]36#include "libroar.h"
37
[5843]38enum mode {
39 NORMAL = 0,
40 SYSTEM = 1
41};
42
43static int _connect_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout);
44
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);
[5844]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 }
[5843]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;
[5369]168 const char * daemonimage = NULL;
169 int socks[2];
170 int r;
171 char fhstr[12];
[5838]172 size_t len;
[5369]173
[5843]174 if ( __get_daemonimage(&daemonimage, &mode, server) == -1 )
175  return -1;
[5369]176
[5838]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
[5369]183 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
184  roar_err_from_errno();
185  return -1;
186 }
187
[5373]188 r = roar_fork(NULL);
[5369]189
190 if ( r == -1 ) { // error!
[5373]191  ROAR_ERR("_start_server(*): Can not fork: %s", roar_error2str(roar_error));
[5369]192  close(socks[0]);
193  close(socks[1]);
194  return -1;
195 } else if ( r == 0 ) { // we are the child
[5619]196  roar_watchdog_stop();
197
[5369]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);
[5832]213     execl(roar_libroar_get_path_static("bin-sh"), roar_libroar_get_path_static("bin-sh"), "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL);
[5369]214     execlp("sh", "sh", "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL);
215    break;
216  }
217
218  // we are still alive?
[5373]219  ROAR_ERR("_start_server(*): alive after exec(), that's bad!");
[5619]220  ROAR_U_EXIT(1);
[5369]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;
[5843]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);
[5369]241#else
[5843]242 ROAR_ERR("_start_server(*): There is no UNIX Domain Socket, download a real OS.");
243 roar_err_set(ROAR_ERROR_NOSYS);
[5369]244 return -1;
245#endif
246}
247
[5373]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
[5368]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;
[5838]278 int ret;
[5368]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;
[5838]292  con->server_name = roar_mm_strdup(server);
[5368]293  return 0;
[5369]294 } else if ( !strcmp(server, "+fork") || !strncmp(server, "+fork=", 6) ) {
295  return _start_server(con, server, type, flags, timeout);
[5373]296 } else if ( !strcmp(server, "+internal") || !strncmp(server, "+internal=", 10) ) {
[5838]297  ret = _connect_internal(con, server, type, flags, timeout);
298  if ( ret == 0 )
299   con->server_name = roar_mm_strdup(server);
300  return ret;
[5368]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  }
[5838]339
340  ROAR_DBG("roar_connect_raw(*): user_sock='%s'", user_sock);
[5368]341 }
342
343 if ( port || is_decnet ) {
[5375]344  fh = roar_socket_connect(type, user_sock, port);
[5368]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) ) {
[5406]352#ifdef O_NOCTTY
[5428]353     fh = open(user_sock, O_RDWR|O_NOCTTY, 0666);
[5406]354#else
[5428]355     fh = open(user_sock, O_RDWR, 0666);
[5406]356#endif
[5368]357    }
358   }
359  }
360#endif
[5428]361  if ( fh == -1 )
362   fh = roar_socket_connect(type, user_sock, ROAR_DEFAULT_PORT);
[5368]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
[5838]381 con->server_name = roar_mm_strdup(server);
382
[5368]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) {
[4780]388#ifdef ROAR_HAVE_LIBSLP
[4653]389 struct roar_libroar_config * config = roar_libroar_get_config();
[4780]390#endif
[5745]391#if defined(ROAR_HAVE_UNIX)
[5114]392 char user_sock[128];
[5754]393 const char * roar_server;
[5393]394#endif
395#if defined(ROAR_HAVE_LIBSLP) || !defined(ROAR_TARGET_MICROCONTROLLER)
[701]396 int i = 0;
[5393]397#endif
[1475]398#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[1026]399 struct passwd * pwd;
[1393]400#endif
[828]401#ifdef ROAR_HAVE_LIBDNET
402 struct stat decnet_stat;
403#endif
[3372]404#ifdef ROAR_HAVE_LIBX11
405 struct roar_x11_connection * x11con;
406#endif
[4945]407#ifdef ROAR_HAVE_LIBSLP
[4806]408 struct roar_server * list;
409 int workarounds_store;
[4945]410#endif
[0]411
[4873]412 roar_err_set(ROAR_ERROR_UNKNOWN);
[807]413
[4806]414 if ( timeout != 0 ) {
[4873]415  roar_err_set(ROAR_ERROR_INVAL);
[4806]416  return -1;
417 }
418
419 if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK )
420  flags |= ROAR_ENUM_FLAG_NONBLOCK;
421
422
[5368]423 if ( server == NULL || *server == 0 )
[2567]424  server = roar_libroar_get_server();
425
[5368]426 if ( server == NULL || *server == 0 )
[5754]427  server = roar_env_get("ROAR_SERVER");
[0]428
[3372]429#ifdef ROAR_HAVE_LIBX11
[5368]430 if ( server == NULL || *server == 0 ) {
[3372]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
[1436]438#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[5832]439 if ( (server == NULL || *server == 0) && (i = readlink(roar_libroar_get_path_static("sysconf-roarserver"), user_sock, sizeof(user_sock)-1)) != -1 ) {
[448]440   user_sock[i] = 0;
441   server = user_sock;
442 }
[1093]443#endif
[448]444
[3077]445 if ( server != NULL && !strcasecmp(server, "+slp") ) {
[3076]446  server = roar_slp_find_roard(0);
447  if ( server == NULL ) {
448   return -1;
449  }
450 }
451
[5471]452 // "+default" is an alias for NULL.
453 if ( server != NULL && !strcasecmp(server, "+default") ) {
454  server = NULL;
455 }
456
[61]457 if ( server == NULL || *server == 0 ) {
[0]458  /* connect via defaults */
459
[1764]460#ifdef ROAR_HAVE_UNIX
[1436]461#ifndef ROAR_TARGET_MICROCONTROLLER
[5754]462  roar_server = roar_env_get("HOME");
[1435]463#else
464  roar_server = NULL;
465#endif
[1026]466
467  if ( roar_server == NULL ) {
[1436]468#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[1026]469   if ( (pwd = getpwuid(getuid())) == NULL ) {
[5832]470    roar_server = roar_libroar_get_path_static("dir-nx-home");
[1026]471   } else {
472    roar_server = pwd->pw_dir;
473   }
[1078]474#else
475   roar_server = "/WIN32-SUCKS";
476#endif
[1026]477  }
478
[5114]479  snprintf(user_sock, sizeof(user_sock)-1, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER);
480  user_sock[sizeof(user_sock)-1] = 0;
[0]481
[5368]482  if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 )
483   return 0;
[0]484
[5368]485  if ( _connect_server(con, ROAR_DEFAULT_SOCK_GLOBAL, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 )
486   return 0;
[1764]487#endif
[237]488
[5368]489  if ( _connect_server(con, ROAR_DEFAULT_HOSTPORT, ROAR_SOCKET_TYPE_TCP, flags, timeout) == 0 )
490   return 0;
[0]491
[828]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);
[5368]496    if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_DECNET, flags, timeout) == 0 )
497     return 0;
[828]498   }
[522]499  }
[828]500#endif
[2007]501
[5375]502  if ( _connect_server(con, "+abstract", ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) == 0 )
[5368]503   return 0;
[4109]504
[2007]505#ifdef ROAR_HAVE_LIBSLP
[4806]506 if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) &&
[5373]507      !(flags & ROAR_ENUM_FLAG_NONBLOCK) &&
508      !(flags & ROAR_ENUM_FLAG_LOCALONLY)
[4806]509    ) {
510  if ( (server = roar_slp_find_roard(0)) != NULL ) {
[5375]511   if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 )
[5368]512    return 0;
[2014]513
[4806]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                     */
[5237]516   ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache");
[4806]517   if ( (server = roar_slp_find_roard(1)) != NULL )
[5375]518    if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 )
[5368]519     return 0;
[4806]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++) {
[5375]529   if ( _connect_server(con, list[i].server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) {
[4806]530    roar_enum_servers_free(list);
[5368]531    return 0;
[4806]532   }
533  }
534  roar_enum_servers_free(list);
[4653]535 }
[2007]536#endif
[522]537
[2014]538 return -1;
539
[0]540 } else {
541  /* connect via (char*)server */
[5375]542  if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) != 0 )
[5369]543   return -1;
544  return 0;
[0]545 }
546
[5368]547 roar_err_set(ROAR_ERROR_NODEV);
548 ROAR_DBG("roar_connect_raw(*) = -1 // error=NODEV");
549 return -1;
[0]550}
551
[5237]552int roar_connect     (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) {
[5373]553 struct roar_libroar_config * config = roar_libroar_get_config();
554
[1315]555 if ( con == NULL ) {
[4873]556  roar_err_set(ROAR_ERROR_FAULT);
[1315]557  return -1;
558 }
559
[5368]560 if ( roar_connect_none(con) == -1 )
[0]561  return -1;
562
[5373]563 if ( roar_connect_raw(con, server, flags | config->serverflags, timeout) == -1 )
[5296]564  return -1;
565
566 return 0;
[1315]567}
568
[5365]569int roar_connect_none (struct roar_connection * con) {
[5465]570 struct roar_libroar_config * config = roar_libroar_get_config();
571
[5365]572 if ( con == NULL ) {
[4873]573  roar_err_set(ROAR_ERROR_INVAL);
[1315]574  return -1;
575 }
576
577 memset(con, 0, sizeof(struct roar_connection));
[5296]578 con->refc        = 1;
[5467]579 con->flags       = ROAR_CON_FLAGS_ISCLIENT;
[5465]580 con->version     = _ROAR_MESSAGE_VERSION;
[5232]581 con->cb_userdata = NULL;
582 con->cb          = NULL;
[5296]583 con->server_stds = NULL;
584 con->server_name = NULL;
[5232]585
586 roar_err_init(&(con->errorframe));
[1315]587
[5296]588 con->viocon = &(con->viocon_store);
589
[5365]590// con->flags |= ROAR_CON_FLAGS_VIO;
591
[5465]592 if ( config->protocolversion != -1 )
593  roar_set_connection_version(con, config->protocolversion);
594
[5365]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
[5637]624 // special hack to set an illegal value used internally in libroar:
[5365]625 if ( fh == -2 )
626  fh = -1;
627
[5296]628 if ( roar_vio_open_fh_socket(con->viocon, fh) != -1 ) {
[3869]629  con->flags |= ROAR_CON_FLAGS_VIO;
[5608]630 } else {
631  return -1;
[5231]632 }
[1315]633
[4873]634 roar_err_set(ROAR_ERROR_NONE);
[0]635 return 0;
636}
637
[1660]638int roar_get_connection_fh (struct roar_connection * con) {
[5231]639 int fh;
640
641 ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con);
642
[3869]643 roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL);
[2809]644
[2043]645 if ( con == NULL )
646  return -1;
647
[5231]648 ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con);
649
[5792]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
[3869]655  return -1;
[5792]656#endif
657 }
[3869]658
[5231]659 ROAR_DBG("roar_get_connection_fh(con=%p) = %i", con, fh);
[3869]660
[5231]661 return fh;
[2043]662}
663
[3869]664struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) {
[5608]665 if ( con == NULL ) {
666  roar_err_set(ROAR_ERROR_FAULT);
[3869]667  return NULL;
[5608]668 }
[3869]669
670 if ( con->flags & ROAR_CON_FLAGS_VIO )
[5296]671  return con->viocon;
[3869]672
673// TODO: try to open the VIO.
674
[5608]675 roar_err_set(ROAR_ERROR_BADFH);
[3869]676 return NULL;
677}
678
[5296]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) {
[3869]700 struct roar_vio_calls * vio;
[0]701 struct roar_message m;
702
[5296]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
[5841]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);
[5843]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   }
[5841]719  }
720 }
721
[3875]722 memset(&m, 0, sizeof(m));
723
[5144]724 m.datalen =  0;
725 m.stream  = -1;
726 m.pos     =  0;
[0]727 m.cmd     = ROAR_CMD_QUIT;
728
729 roar_req(con, &m, NULL);
730
[3869]731 if ( (vio = roar_get_connection_vio2(con)) != NULL ) {
732  roar_vio_close(vio);
[2809]733 }
[0]734
[5296]735 if ( con->server_stds != NULL ) {
736  roar_stds_free(con->server_stds);
737  con->server_stds = NULL;
738 }
[0]739
[5296]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 }
[807]750
[0]751 return 0;
752}
753
[3913]754int roar_set_connection_callback(struct roar_connection * con,
755                                 void (*cb)(struct roar_connection * con,
756                                            struct roar_message    * mes,
[5231]757                                            void                   * data,
[3913]758                                            void                   * userdata),
759                                 void * userdata) {
[5465]760 if ( con == NULL ) {
761  roar_err_set(ROAR_ERROR_FAULT);
[3913]762  return -1;
[5465]763 }
[3913]764
765 con->cb       = cb;
[5231]766 con->cb_userdata = userdata;
[3913]767
768 return 0;
769}
770
[5465]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
[5467]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
[5468]800 if ( ret == 0 )
801  ret = store++;
802
[5467]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
[3914]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) {
[5148]825 roar_err_set(ROAR_ERROR_NOSYS);
[3914]826 return -1;
827}
[3913]828
[3836]829int roar_noop         (struct roar_connection * con) {
830 struct roar_message mes;
831
832 if ( con == NULL ) {
[5148]833  roar_err_set(ROAR_ERROR_FAULT);
[3836]834  return -1;
835 }
836
837 memset(&mes, 0, sizeof(mes));
838
839 mes.cmd = ROAR_CMD_NOOP;
[5144]840 mes.stream = -1;
[3836]841
[5146]842 return roar_req3(con, &mes, NULL);
[3836]843}
844
[5114]845int roar_identify   (struct roar_connection * con, const char * name) {
[0]846 struct roar_message mes;
[5132]847 uint32_t pid;
[0]848 int max_len;
849
[4873]850 roar_err_set(ROAR_ERROR_UNKNOWN);
[807]851
[0]852 ROAR_DBG("roar_identify(*): try to identify myself...");
853
[3875]854 memset(&mes, 0, sizeof(mes));
855
[0]856 mes.cmd    = ROAR_CMD_IDENTIFY;
[5144]857 mes.stream = -1;
858 mes.pos    =  0;
[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
[5114]867 max_len = roar_mm_strlen(name);
[0]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();
[5132]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);
[0]882
883 strncpy(mes.data+5, name, max_len);
884
[5146]885 return roar_req3(con, &mes, NULL);
[0]886}
887
888//ll
Note: See TracBrowser for help on using the repository browser.