source: roaraudio/libroar/basic.c @ 5365:fa05e52423d9

Last change on this file since 5365:fa05e52423d9 was 5365:fa05e52423d9, checked in by phi, 12 years ago

added roar_connect_none() and roar_connect_vio()

File size: 12.7 KB
RevLine 
[0]1//basic.c:
2
[690]3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
[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
[5237]38int roar_connect_raw (const char * server, int flags, uint_least32_t timeout) {
[4780]39#ifdef ROAR_HAVE_LIBSLP
[4653]40 struct roar_libroar_config * config = roar_libroar_get_config();
[4780]41#endif
[5114]42 char user_sock[128];
[0]43 char * roar_server;
[701]44 int i = 0;
[0]45 int port = 0;
46 int fh = -1;
[501]47 int is_decnet = 0;
[5114]48 const char * obj = NULL;
[1475]49#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[1026]50 struct passwd * pwd;
[1393]51#endif
[828]52#ifdef ROAR_HAVE_LIBDNET
53 struct stat decnet_stat;
54#endif
[3372]55#ifdef ROAR_HAVE_LIBX11
56 struct roar_x11_connection * x11con;
57#endif
[4945]58#ifdef ROAR_HAVE_LIBSLP
[4806]59 struct roar_server * list;
60 int workarounds_store;
[4945]61#endif
[5267]62#if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT)
63 struct stat sockstat;
64#endif
[0]65
[4873]66 roar_err_set(ROAR_ERROR_UNKNOWN);
[807]67
[4806]68 if ( timeout != 0 ) {
[4873]69  roar_err_set(ROAR_ERROR_INVAL);
[4806]70  return -1;
71 }
72
73 if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK )
74  flags |= ROAR_ENUM_FLAG_NONBLOCK;
75
76
[2567]77 if ( server == NULL )
78  server = roar_libroar_get_server();
79
[61]80 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
[0]81  server = roar_server;
82
[3372]83#ifdef ROAR_HAVE_LIBX11
84 if ( server == NULL ) {
85  if ( (x11con = roar_x11_connect(NULL)) != NULL ) {
86   server = roar_x11_get_prop(x11con, "ROAR_SERVER");
87   roar_x11_disconnect(x11con);
88  }
89 }
90#endif
91
[1436]92#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[5114]93 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, sizeof(user_sock)-1)) != -1 ) {
[448]94   user_sock[i] = 0;
95   server = user_sock;
96 }
[1093]97#endif
[448]98
[3077]99 if ( server != NULL && !strcasecmp(server, "+slp") ) {
[3076]100  server = roar_slp_find_roard(0);
101  if ( server == NULL ) {
102   return -1;
103  }
104 }
105
[61]106 if ( server == NULL || *server == 0 ) {
[0]107  /* connect via defaults */
108
[1764]109#ifdef ROAR_HAVE_UNIX
[1436]110#ifndef ROAR_TARGET_MICROCONTROLLER
[1026]111  roar_server = getenv("HOME");
[1435]112#else
113  roar_server = NULL;
114#endif
[1026]115
116  if ( roar_server == NULL ) {
[1436]117#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[1026]118   if ( (pwd = getpwuid(getuid())) == NULL ) {
119    roar_server = "/NX-HOME-DIR";
120   } else {
121    roar_server = pwd->pw_dir;
122   }
[1078]123#else
124   roar_server = "/WIN32-SUCKS";
125#endif
[1026]126  }
127
[5114]128  snprintf(user_sock, sizeof(user_sock)-1, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER);
129  user_sock[sizeof(user_sock)-1] = 0;
[0]130
131  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
132   return fh;
133
[237]134  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
135   return fh;
[1764]136#endif
[237]137
[0]138  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
139   return fh;
140
[828]141#ifdef ROAR_HAVE_LIBDNET
142  if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) {
143   if ( roar_socket_get_local_nodename() ) {
144    snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT);
[2010]145    if ( (fh = roar_socket_connect(user_sock, ROAR_DEFAULT_NUM)) != -1 )
146     return fh;
[828]147   }
[522]148  }
[828]149#endif
[2007]150
[4109]151  if ( (fh = roar_socket_connect("+abstract", 0)) != -1 )
152   return fh;
153
[2007]154#ifdef ROAR_HAVE_LIBSLP
[4806]155 if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) &&
156      !(flags & ROAR_ENUM_FLAG_NONBLOCK)
157    ) {
158  if ( (server = roar_slp_find_roard(0)) != NULL ) {
[5237]159   if ( (fh = roar_connect_raw(server, 0, 0)) != -1 )
[4653]160    return fh;
[2014]161
[4806]162   /* in case we can not connect to the server given this may be a cache problem,
163      we do a new lookup with the cache disabled in this case                     */
[5237]164   ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache");
[4806]165   if ( (server = roar_slp_find_roard(1)) != NULL )
[5237]166    if ( (fh = roar_connect_raw(server, 0, 0)) != -1 )
[4806]167     return fh;
168  }
169 }
170
171 workarounds_store = config->workaround.workarounds;
172 config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP;
173 list = roar_enum_servers(flags, -1, -1);
174 config->workaround.workarounds = workarounds_store;
175 if ( list != NULL ) {
176  for (i = 0; list[i].server != NULL; i++) {
[5360]177   if ( (fh = roar_connect_raw(list[i].server, 0, 0)) != -1 ) {
[4806]178    roar_enum_servers_free(list);
[4653]179    return fh;
[4806]180   }
181  }
182  roar_enum_servers_free(list);
[4653]183 }
[2007]184#endif
[522]185
[2014]186 return -1;
187
[0]188 } else {
189  /* connect via (char*)server */
[501]190  // find a port:
[5114]191
[5185]192  if ( !strcmp(server, "+invalid") ) {
193   roar_err_set(ROAR_ERROR_CANCELED);
194   return -1;
195  }
196
[5120]197  strncpy(user_sock, server, sizeof(user_sock)-1);
[5114]198  user_sock[sizeof(user_sock)-1] = 0;
199
200  if ( *user_sock != '/' ) { // don't test AF_UNIX sockets for ports
201   for (i = 0; user_sock[i] != 0; i++) {
202    if ( user_sock[i] == ':' ) {
203     if ( user_sock[i+1] == ':' ) { // DECnet, leave unchanged
[501]204      is_decnet = 1;
[5114]205      obj = &user_sock[i+2];
[501]206      break;
207     }
208
[5114]209     port = atoi(&(user_sock[i+1]));
210     user_sock[i] = 0;
[501]211     break;
212    }
[0]213   }
214  }
215
[521]216  if ( is_decnet ) {
[5114]217   if ( *user_sock == ':' ) {
[5120]218    if ( roar_socket_get_local_nodename() != NULL ) {
219     strncpy(user_sock, roar_socket_get_local_nodename(), sizeof(user_sock)-1);
[5114]220     user_sock[sizeof(user_sock)-1] = 0;
[5120]221     roar_mm_strlcat(user_sock, server, sizeof(user_sock)-1);
222     user_sock[sizeof(user_sock)-1] = 0;
223     obj  = strstr(user_sock, "::");
224     obj += 2;
225    }
[521]226   }
227
228   if ( *obj == 0 ) {
[1066]229#ifdef DN_MAXOBJL
[5114]230    roar_mm_strlcat(user_sock, ROAR_DEFAULT_OBJECT, sizeof(user_sock)-1);
231    user_sock[sizeof(user_sock)-1] = 0;
[1066]232#else
[5237]233    ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown.");
[1066]234#endif
[521]235   }
[5237]236    ROAR_DBG("roar_connect_raw(*): user_sock='%s'", user_sock);
[516]237  }
238
[501]239  if ( port || is_decnet ) {
[5114]240   fh = roar_socket_connect(user_sock, port);
[0]241   // restore the original string
[5114]242   user_sock[i] = ':';
[0]243  } else {
[5267]244#if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT)
245   if ( user_sock[0] == '/' ) {
246    if ( stat(user_sock, &sockstat) == 0 ) {
247     if ( S_ISCHR(sockstat.st_mode) ) {
248      return open(user_sock, O_RDWR|O_NOCTTY, 0666);
249     }
250    }
251   }
252#endif
[5114]253   fh = roar_socket_connect(user_sock, ROAR_DEFAULT_PORT);
[0]254  }
255 }
256
[5237]257 ROAR_DBG("roar_connect_raw(*) = %i", fh);
[0]258
259 return fh;
260}
261
[5237]262int roar_connect     (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) {
[1315]263 int fh;
[5296]264 int err;
[1315]265
266 if ( con == NULL ) {
[4873]267  roar_err_set(ROAR_ERROR_FAULT);
[1315]268  return -1;
269 }
270
[4873]271 roar_err_set(ROAR_ERROR_UNKNOWN);
[5237]272 fh = roar_connect_raw(server, flags, timeout);
[0]273
[1315]274 if ( fh == -1 )
[0]275  return -1;
276
[5296]277 if ( roar_connect_fh(con, fh) == -1 ) {
278  err = roar_error;
279#ifdef ROAR_TARGET_WIN32
280  closesocket(fh);
281#else
282  close(fh);
283#endif
284  roar_error = err;
285  return -1;
286 }
287
288 if ( server != NULL ) {
289  con->server_name = roar_mm_strdup(server);
290 }
291
292 return 0;
[1315]293}
294
[5365]295int roar_connect_none (struct roar_connection * con) {
296 if ( con == NULL ) {
[4873]297  roar_err_set(ROAR_ERROR_INVAL);
[1315]298  return -1;
299 }
300
301 memset(con, 0, sizeof(struct roar_connection));
[5296]302 con->refc        = 1;
[5232]303 con->flags       = ROAR_CON_FLAGS_NONE;
304 con->version     = 0;
305 con->cb_userdata = NULL;
306 con->cb          = NULL;
[5296]307 con->server_stds = NULL;
308 con->server_name = NULL;
[5232]309
310 roar_err_init(&(con->errorframe));
[1315]311
[5296]312 con->viocon = &(con->viocon_store);
313
[5365]314// con->flags |= ROAR_CON_FLAGS_VIO;
315
316 roar_err_set(ROAR_ERROR_NONE);
317 return 0;
318}
319
320int roar_connect_vio (struct roar_connection * con, struct roar_vio_calls * vio) {
321 if ( con == NULL || vio == NULL ) {
322  roar_err_set(ROAR_ERROR_INVAL);
323  return -1;
324 }
325
326 if ( roar_connect_none(con) == -1 )
327  return -1;
328
329 con->viocon = vio;
330 con->flags |= ROAR_CON_FLAGS_VIO;
331
332 return -1;
333}
334
335int roar_connect_fh (struct roar_connection * con, int fh) {
336
337 if ( con == NULL || fh == -1 ) {
338  roar_err_set(ROAR_ERROR_INVAL);
339  return -1;
340 }
341
342 if ( roar_connect_none(con) == -1 )
343  return -1;
344
345 // specal hack to set an ilegal value used internaly in libroar:
346 if ( fh == -2 )
347  fh = -1;
348
[5296]349 if ( roar_vio_open_fh_socket(con->viocon, fh) != -1 ) {
[3869]350  con->flags |= ROAR_CON_FLAGS_VIO;
[5231]351 }
[1315]352
[4873]353 roar_err_set(ROAR_ERROR_NONE);
[0]354 return 0;
355}
356
[1660]357int roar_get_connection_fh (struct roar_connection * con) {
[5231]358 int fh;
359
360 ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con);
361
[3869]362 roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL);
[2809]363
[2043]364 if ( con == NULL )
365  return -1;
366
[5231]367 ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con);
368
[5296]369 if ( roar_vio_ctl(con->viocon, ROAR_VIO_CTL_GET_FH, &fh) == -1 )
[3869]370  return -1;
371
[5231]372 ROAR_DBG("roar_get_connection_fh(con=%p) = %i", con, fh);
[3869]373
[5231]374 return fh;
[2043]375}
376
[3869]377struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) {
378 if ( con == NULL )
379  return NULL;
380
381 if ( con->flags & ROAR_CON_FLAGS_VIO )
[5296]382  return con->viocon;
[3869]383
384// TODO: try to open the VIO.
385
386 return NULL;
387}
388
[5296]389const char * roar_get_connection_server(struct roar_connection * con) {
390 if ( con == NULL ) {
391  roar_err_set(ROAR_ERROR_FAULT);
392  return NULL;
393 }
394
395 return con->server_name;
396}
397
398int roar_connectionref(struct roar_connection * con) {
399 if ( con == NULL ) {
400  roar_err_set(ROAR_ERROR_FAULT);
401  return -1;
402 }
403
404 con->refc++;
405
406 return 0;
407}
408
409int roar_connectionunref(struct roar_connection * con) {
[3869]410 struct roar_vio_calls * vio;
[0]411 struct roar_message m;
412
[5296]413 if ( con == NULL ) {
414  roar_err_set(ROAR_ERROR_FAULT);
415  return -1;
416 }
417
418 con->refc--;
419
420 if ( con->refc )
421  return 0;
422
[3875]423 memset(&m, 0, sizeof(m));
424
[5144]425 m.datalen =  0;
426 m.stream  = -1;
427 m.pos     =  0;
[0]428 m.cmd     = ROAR_CMD_QUIT;
429
430 roar_req(con, &m, NULL);
431
[3869]432 if ( (vio = roar_get_connection_vio2(con)) != NULL ) {
433  roar_vio_close(vio);
[2809]434 }
[0]435
[5296]436 if ( con->server_stds != NULL ) {
437  roar_stds_free(con->server_stds);
438  con->server_stds = NULL;
439 }
[0]440
[5296]441 if ( con->server_name != NULL ) {
442  roar_mm_free(con->server_name);
443  con->server_name = NULL;
444 }
445
446 if ( con->flags & ROAR_CON_FLAGS_FREESELF ) {
447  roar_mm_free(con);
448 } else {
449  roar_connect_fh(con, -2);
450 }
[807]451
[0]452 return 0;
453}
454
[3913]455int roar_set_connection_callback(struct roar_connection * con,
456                                 void (*cb)(struct roar_connection * con,
457                                            struct roar_message    * mes,
[5231]458                                            void                   * data,
[3913]459                                            void                   * userdata),
460                                 void * userdata) {
461 if ( con == NULL )
462  return -1;
463
464 con->cb       = cb;
[5231]465 con->cb_userdata = userdata;
[3913]466
467 return 0;
468}
469
[3914]470int roar_sync         (struct roar_connection * con) {
471 // wait for any non-client reqs
472 return roar_wait_msg(con, 0x0000, 0x8000);
473}
474
475int roar_wait_msg     (struct roar_connection * con, int16_t seq, int16_t seqmask) {
[5148]476 roar_err_set(ROAR_ERROR_NOSYS);
[3914]477 return -1;
478}
[3913]479
[3836]480int roar_noop         (struct roar_connection * con) {
481 struct roar_message mes;
482
483 if ( con == NULL ) {
[5148]484  roar_err_set(ROAR_ERROR_FAULT);
[3836]485  return -1;
486 }
487
488 memset(&mes, 0, sizeof(mes));
489
490 mes.cmd = ROAR_CMD_NOOP;
[5144]491 mes.stream = -1;
[3836]492
[5146]493 return roar_req3(con, &mes, NULL);
[3836]494}
495
[5114]496int roar_identify   (struct roar_connection * con, const char * name) {
[0]497 struct roar_message mes;
[5132]498 uint32_t pid;
[0]499 int max_len;
500
[4873]501 roar_err_set(ROAR_ERROR_UNKNOWN);
[807]502
[0]503 ROAR_DBG("roar_identify(*): try to identify myself...");
504
[3875]505 memset(&mes, 0, sizeof(mes));
506
[0]507 mes.cmd    = ROAR_CMD_IDENTIFY;
[5144]508 mes.stream = -1;
509 mes.pos    =  0;
[0]510
511 ROAR_DBG("roar_identify(*): name=%p", name);
512
513 if ( name == NULL )
514  name = "libroar client";
515
516 ROAR_DBG("roar_identify(*): name=%p", name);
517
[5114]518 max_len = roar_mm_strlen(name);
[0]519 ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len);
520
521 if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) )
522  max_len = LIBROAR_BUFFER_MSGDATA - 5;
523
524 mes.datalen = 5 + max_len;
525 mes.data[0] = 1;
526
527 pid = getpid();
[5132]528 mes.data[1] = (pid & 0xFF000000UL) >> 24;
529 mes.data[2] = (pid & 0x00FF0000UL) >> 16;
530 mes.data[3] = (pid & 0x0000FF00UL) >>  8;
531 mes.data[4] = (pid & 0x000000FFUL) >>  0;
532 ROAR_DBG("roar_identify(*): pid = %i", (int)pid);
[0]533
534 strncpy(mes.data+5, name, max_len);
535
[5146]536 return roar_req3(con, &mes, NULL);
[0]537}
538
539//ll
Note: See TracBrowser for help on using the repository browser.