source: roaraudio/libroar/basic.c @ 5921:2fa512f52879

Last change on this file since 5921:2fa512f52879 was 5901:64d1f534671b, checked in by phi, 11 years ago

added roar_mm_free_noerror() and make some use out of it

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