source: roaraudio/libroar/basic.c @ 5368:1c35d975f8fe

Last change on this file since 5368:1c35d975f8fe was 5368:1c35d975f8fe, checked in by phi, 12 years ago

converted roar_connect_raw() to a static function, mad it a more universal API, including using VIO more directly

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