source: roaraudio/libroar/basic.c @ 5792:bfe694357a8a

Last change on this file since 5792:bfe694357a8a was 5792:bfe694357a8a, checked in by phi, 11 years ago

hopefully fix win32 bug with execed workaround

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