source: roaraudio/libroar/basic.c @ 3076:71ec405258b7

Last change on this file since 3076:71ec405258b7 was 3076:71ec405258b7, checked in by phi, 14 years ago

small updates to SLP support, added +slp address schemata

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