source: roaraudio/libroar/basic.c @ 3516:2becb65c23b6

Last change on this file since 3516:2becb65c23b6 was 3372:92c4d8a5d8b7, checked in by phi, 14 years ago

support to read props from X11, use ROAR_SERVER prop to find a roard

File size: 12.1 KB
RevLine 
[0]1//basic.c:
2
[690]3/*
[3372]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
[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
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
[0]35#include "libroar.h"
36
37int roar_connect_raw (char * server) {
38 char user_sock[80];
39 char * roar_server;
[701]40 int i = 0;
[0]41 int port = 0;
42 int fh = -1;
[501]43 int is_decnet = 0;
[516]44 char * obj = NULL;
[1475]45#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[1026]46 struct passwd * pwd;
[1393]47#endif
[828]48#ifdef ROAR_HAVE_LIBDNET
49 struct stat decnet_stat;
50#endif
[3372]51#ifdef ROAR_HAVE_LIBX11
52 struct roar_x11_connection * x11con;
53#endif
[0]54
[807]55 roar_errno = ROAR_ERROR_UNKNOWN;
56
[2897]57 // load config
58 roar_libroar_get_config();
59
[2567]60 if ( server == NULL )
61  server = roar_libroar_get_server();
62
[61]63 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
[0]64  server = roar_server;
65
[3372]66#ifdef ROAR_HAVE_LIBX11
67 if ( server == NULL ) {
68  if ( (x11con = roar_x11_connect(NULL)) != NULL ) {
69   server = roar_x11_get_prop(x11con, "ROAR_SERVER");
70   roar_x11_disconnect(x11con);
71  }
72 }
73#endif
74
[1436]75#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[448]76 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) {
77   user_sock[i] = 0;
78   server = user_sock;
79 }
[1093]80#endif
[448]81
[3077]82 if ( server != NULL && !strcasecmp(server, "+slp") ) {
[3076]83  server = roar_slp_find_roard(0);
84  if ( server == NULL ) {
85   return -1;
86  }
87 }
88
[61]89 if ( server == NULL || *server == 0 ) {
[0]90  /* connect via defaults */
91
[1764]92#ifdef ROAR_HAVE_UNIX
[1436]93#ifndef ROAR_TARGET_MICROCONTROLLER
[1026]94  roar_server = getenv("HOME");
[1435]95#else
96  roar_server = NULL;
97#endif
[1026]98
99  if ( roar_server == NULL ) {
[1436]100#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[1026]101   if ( (pwd = getpwuid(getuid())) == NULL ) {
102    roar_server = "/NX-HOME-DIR";
103   } else {
104    roar_server = pwd->pw_dir;
105   }
[1078]106#else
107   roar_server = "/WIN32-SUCKS";
108#endif
[1026]109  }
110
111  snprintf(user_sock, 79, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER);
[0]112
113  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
114   return fh;
115
[237]116  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
117   return fh;
[1764]118#endif
[237]119
[0]120  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
121   return fh;
122
[828]123#ifdef ROAR_HAVE_LIBDNET
124  if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) {
125   if ( roar_socket_get_local_nodename() ) {
126    snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT);
[2010]127    if ( (fh = roar_socket_connect(user_sock, ROAR_DEFAULT_NUM)) != -1 )
128     return fh;
[828]129   }
[522]130  }
[828]131#endif
[2007]132
133#ifdef ROAR_HAVE_LIBSLP
[2014]134 if ( (server = roar_slp_find_roard(0)) != NULL )
135  if ( (fh = roar_connect_raw(server)) != -1 )
136   return fh;
137
138 /* in case we can not connect to the server given this may be a cache problem,
139    we do a new lookup with the cache disabled in this case                     */
140 ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache");
141 if ( (server = roar_slp_find_roard(1)) != NULL )
142  if ( (fh = roar_connect_raw(server)) != -1 )
143   return fh;
[2007]144#endif
[522]145
[2014]146 return -1;
147
[0]148 } else {
149  /* connect via (char*)server */
[501]150  // find a port:
151  if ( *server != '/' ) { // don't test AF_UNIX sockets for ports
152   for (i = 0; server[i] != 0; i++) {
153    if ( server[i] == ':' ) {
154     if ( server[i+1] == ':' ) { // DECnet, leave unchanged
155      is_decnet = 1;
[516]156      obj = &server[i+2];
[501]157      break;
158     }
159
160     port = atoi(server+i+1);
161     server[i] = 0;
162     break;
163    }
[0]164   }
165  }
166
[521]167  if ( is_decnet ) {
168    *user_sock = 0;
169   if ( *server == ':' ) {
170    if ( roar_socket_get_local_nodename() )
[1066]171     strncat(user_sock, roar_socket_get_local_nodename(), 6);
[521]172   }
173
[1066]174   strncat(user_sock, server, 79);
[516]175   server = user_sock;
[521]176   if ( *obj == 0 ) {
[1066]177#ifdef DN_MAXOBJL
178    strncat(user_sock, ROAR_DEFAULT_OBJECT, DN_MAXOBJL+2);
179#else
180    ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown.");
181#endif
[521]182   }
[516]183  }
184
[501]185  if ( port || is_decnet ) {
[0]186   fh = roar_socket_connect(server, port);
187   // restore the original string
188   server[i] = ':';
189  } else {
190   fh = roar_socket_connect(server, ROAR_DEFAULT_PORT);
191  }
192 }
193
[807]194 if ( fh == -1 )
195  roar_errno = ROAR_ERROR_CONNREFUSED;
196
[0]197 ROAR_DBG("roar_connect_raw(*) = %i", fh);
198
199 return fh;
200}
201
202int roar_connect    (struct roar_connection * con, char * server) {
[1315]203 int fh;
204
205 if ( con == NULL ) {
206  roar_errno = ROAR_ERROR_INVAL;
207  return -1;
208 }
209
[807]210 roar_errno = ROAR_ERROR_UNKNOWN;
[1315]211 fh = roar_connect_raw(server);
[0]212
[1315]213 if ( fh == -1 )
[0]214  return -1;
215
[1315]216 return roar_connect_fh(con, fh);
217}
218
219int roar_connect_fh (struct roar_connection * con, int fh) {
220
221 if ( con == NULL || fh == -1 ) {
222  roar_errno = ROAR_ERROR_INVAL;
223  return -1;
224 }
225
226 // specal hack to set an ilegal value used internaly in libroar:
227 if ( fh == -2 )
228  fh = -1;
229
230 memset(con, 0, sizeof(struct roar_connection));
231
[1660]232 con->__fh = fh;
[1315]233
[807]234 roar_errno = ROAR_ERROR_NONE;
[0]235 return 0;
236}
237
[1660]238int roar_get_connection_fh (struct roar_connection * con) {
[2809]239 roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio", NULL);
240
[2043]241 if ( con == NULL )
242  return -1;
243
[1660]244 return con->__fh;
245}
246
[2043]247int roar_get_connection_vio (struct roar_connection * con, struct roar_vio_calls * vio) {
248 if ( con == NULL || vio == NULL )
249  return -1;
250
[2809]251 return roar_vio_open_fh_socket(vio, con->__fh);
[2043]252}
253
[0]254int roar_disconnect (struct roar_connection * con) {
[2809]255 struct roar_vio_calls vio;
[0]256 struct roar_message m;
257
258 m.datalen = 0;
259 m.stream  = 0;
[133]260 m.pos     = 0;
[0]261 m.cmd     = ROAR_CMD_QUIT;
262
263 roar_req(con, &m, NULL);
264
[2809]265 if ( roar_get_connection_vio(con, &vio) != -1 ) {
266  roar_vio_close(&vio);
267 }
[0]268
[1660]269 roar_connect_fh(con, -2);
[0]270
[807]271 roar_errno = ROAR_ERROR_NONE;
272
[0]273 return 0;
274}
275
276int roar_identify   (struct roar_connection * con, char * name) {
277 struct roar_message mes;
278 pid_t pid;
279 int max_len;
280
[807]281 roar_errno = ROAR_ERROR_UNKNOWN;
282
[0]283 ROAR_DBG("roar_identify(*): try to identify myself...");
284
285 mes.cmd    = ROAR_CMD_IDENTIFY;
286 mes.stream = 0;
287 mes.pos    = 0;
288
289 ROAR_DBG("roar_identify(*): name=%p", name);
290
291 if ( name == NULL )
292  name = "libroar client";
293
294 ROAR_DBG("roar_identify(*): name=%p", name);
295
296 max_len = strlen(name);
297 ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len);
298
299 if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) )
300  max_len = LIBROAR_BUFFER_MSGDATA - 5;
301
302 mes.datalen = 5 + max_len;
303 mes.data[0] = 1;
304
305 pid = getpid();
306 *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid);
307 ROAR_DBG("roar_identify(*): pid = %i", pid);
308
309 strncpy(mes.data+5, name, max_len);
310
311 return roar_req(con, &mes, NULL);
312}
313
[8]314#define _ROAR_MESS_BUF_LEN (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */)
[0]315int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) {
[1319]316 struct roar_vio_calls vio;
317
[2809]318 if ( roar_get_connection_vio(con, &vio) == -1 )
[1319]319  return -1;
320
321 return roar_vsend_message(&vio, mes, data);
322}
323
324int roar_vsend_message(struct roar_vio_calls * vio, struct roar_message * mes, char *  data) {
[0]325 char buf[_ROAR_MESS_BUF_LEN];
326
[807]327 roar_errno = ROAR_ERROR_UNKNOWN;
328
[0]329 ROAR_DBG("roar_send_message(*): try to send an request...");
330
[8]331 buf[0] = _ROAR_MESSAGE_VERSION;
332 buf[1] = (unsigned char) mes->cmd;
333 *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream);
334 *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos);
335 *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen);
[0]336
[1319]337 if ( roar_vio_write(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
[807]338  roar_errno = ROAR_ERROR_PIPE;
[0]339  return -1;
[807]340 }
[0]341
[807]342 if ( mes->datalen != 0 ) {
[1319]343  if ( roar_vio_write(vio, data == NULL ? mes->data : data, mes->datalen) != mes->datalen ) {
[807]344   roar_errno = ROAR_ERROR_PIPE;
[0]345   return -1;
[807]346  }
347 }
348
349 roar_errno = ROAR_ERROR_NONE;
[0]350
351 ROAR_DBG("roar_send_message(*) = 0");
352 return 0;
353}
354
355int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) {
[1319]356 struct roar_vio_calls vio;
357
[2809]358 if ( roar_get_connection_vio(con, &vio) == -1 )
[1319]359  return -1;
360
361 return roar_vrecv_message(&vio, mes, data);
362}
363
364int roar_vrecv_message(struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
[0]365 char buf[_ROAR_MESS_BUF_LEN];
366
[807]367 roar_errno = ROAR_ERROR_UNKNOWN;
368
[0]369 ROAR_DBG("roar_recv_message(*): try to get a response form the server...");
370
371 if ( data )
372  *data = NULL;
373
[1319]374 if ( roar_vio_read(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
[807]375  roar_errno = ROAR_ERROR_PROTO;
[0]376  return -1;
[807]377 }
[0]378
379 ROAR_DBG("roar_recv_message(*): Got a header");
380
[807]381 if ( buf[0] != _ROAR_MESSAGE_VERSION ) {
382  roar_errno = ROAR_ERROR_PROTO;
[8]383  return -1;
[807]384 }
[8]385
386 mes->cmd     = (unsigned char)buf[1];
387 mes->stream  = ROAR_NET2HOST16(*(uint16_t*)(buf+2));
388 mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+4));
389 mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8));
[0]390
391 ROAR_DBG("roar_recv_message(*): command=%i(%s)", mes->cmd,
392           mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN"));
393
[104]394 if ( mes->datalen == 0 ) {
395  ROAR_DBG("roar_recv_message(*): no data in this pkg");
396  ROAR_DBG("roar_recv_message(*) = 0");
[807]397  roar_errno = ROAR_ERROR_NONE;
[104]398  return 0;
399 }
400
[0]401 if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) {
[1319]402  if ( roar_vio_read(vio, mes->data, mes->datalen) == mes->datalen ) {
[0]403   ROAR_DBG("roar_recv_message(*): Got data!");
404   ROAR_DBG("roar_recv_message(*) = 0");
[807]405   roar_errno = ROAR_ERROR_NONE;
[0]406   return 0;
407  }
[807]408
409  roar_errno = ROAR_ERROR_PIPE;
[0]410  return -1;
411 } else {
[807]412  if ( data == NULL ) {
413   roar_errno = ROAR_ERROR_MSGSIZE;
[0]414   return -1;
[807]415  }
[0]416
[807]417  if ( (*data = malloc(mes->datalen)) == NULL ) {
418   roar_errno = ROAR_ERROR_NOMEM;
[0]419   return -1;
[807]420  }
[0]421
[807]422  if ( mes->datalen == 0 ) {
423   roar_errno = ROAR_ERROR_NONE;
[0]424   return 0;
[807]425  }
[0]426
[1319]427  if ( roar_vio_read(vio, *data, mes->datalen) == mes->datalen ) {
[0]428   ROAR_DBG("roar_recv_message(*): Got data!");
429   ROAR_DBG("roar_recv_message(*) = 0");
[807]430   roar_errno = ROAR_ERROR_NONE;
[0]431   return 0;
432  }
[807]433
434  roar_errno = ROAR_ERROR_PIPE;
[0]435  return -1;
436 }
437
[807]438 // what happened here?
[0]439 return -1;
440}
441
442int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) {
[1319]443 struct roar_vio_calls vio;
444
[2809]445 if ( roar_get_connection_vio(con, &vio) == -1 )
[1319]446  return -1;
447
448 return roar_vreq(&vio, mes, data);
449}
450
451int roar_vreq         (struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
452 if ( roar_vsend_message(vio, mes, data ? *data : NULL) != 0 )
[0]453  return -1;
454
455 if ( data )
456  free(*data);
457
[2053]458 roar_vio_sync(vio); // we need to do this becasue of ssl/compressed links
459
[1319]460 return roar_vrecv_message(vio, mes, data);
[0]461}
462
463int roar_debug_message_print (struct roar_message * mes) {
464 if ( mes == NULL )
465  return -1;
466
467 ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd);
468 ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream);
469 ROAR_DBG("roar_debug_message_print(*): Pos    : %u", mes->pos);
470 ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen);
471
472 ROAR_DBG("roar_debug_message_print(*) = 0");
473 return 0;
474}
475
476int roar_debug_audio_info_print (struct roar_audio_info * info) {
477 if ( info == NULL )
478  return -1;
479
480 ROAR_DBG("roar_debug_audio_info_print(*): Rate    : %i", info->rate);
481 ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels);
482 ROAR_DBG("roar_debug_audio_info_print(*): Bits    : %i", info->bits);
483 ROAR_DBG("roar_debug_audio_info_print(*): Codec   : %i", info->codec);
484
485 ROAR_DBG("roar_debug_audio_info_print(*) = 0");
486 return 0;
487}
488
489//ll
Note: See TracBrowser for help on using the repository browser.