source: roaraudio/libroar/basic.c @ 5838:52ca1a88734e

Last change on this file since 5838:52ca1a88734e was 5838:52ca1a88734e, checked in by phi, 11 years ago

handle setting of server name in connection object in a better way. also added new command "servername" to roarctl to debug this a bit. This is one little step needed by ##337

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