source: roaraudio/libroar/basic.c @ 5119:66203ccc8f12

Last change on this file since 5119:66203ccc8f12 was 5114:82426e175a9d, checked in by phi, 13 years ago

added extra const keywords for server names as well as client names

File size: 11.4 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
38int roar_connect_raw (const char * server) {
39 return roar_connect_raw2(server, 0, 0);
40}
41
42int roar_connect_raw2 (const char * server, int flags, uint_least32_t timeout) {
43#ifdef ROAR_HAVE_LIBSLP
44 struct roar_libroar_config * config = roar_libroar_get_config();
45#endif
46 char user_sock[128];
47 char * roar_server;
48 int i = 0;
49 int port = 0;
50 int fh = -1;
51 int is_decnet = 0;
52 const char * obj = NULL;
53#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
54 struct passwd * pwd;
55#endif
56#ifdef ROAR_HAVE_LIBDNET
57 struct stat decnet_stat;
58#endif
59#ifdef ROAR_HAVE_LIBX11
60 struct roar_x11_connection * x11con;
61#endif
62#ifdef ROAR_HAVE_LIBSLP
63 struct roar_server * list;
64 int workarounds_store;
65#endif
66
67 roar_err_set(ROAR_ERROR_UNKNOWN);
68
69 if ( timeout != 0 ) {
70  roar_err_set(ROAR_ERROR_INVAL);
71  return -1;
72 }
73
74 if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK )
75  flags |= ROAR_ENUM_FLAG_NONBLOCK;
76
77
78 if ( server == NULL )
79  server = roar_libroar_get_server();
80
81 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
82  server = roar_server;
83
84#ifdef ROAR_HAVE_LIBX11
85 if ( server == NULL ) {
86  if ( (x11con = roar_x11_connect(NULL)) != NULL ) {
87   server = roar_x11_get_prop(x11con, "ROAR_SERVER");
88   roar_x11_disconnect(x11con);
89  }
90 }
91#endif
92
93#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
94 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, sizeof(user_sock)-1)) != -1 ) {
95   user_sock[i] = 0;
96   server = user_sock;
97 }
98#endif
99
100 if ( server != NULL && !strcasecmp(server, "+slp") ) {
101  server = roar_slp_find_roard(0);
102  if ( server == NULL ) {
103   return -1;
104  }
105 }
106
107 if ( server == NULL || *server == 0 ) {
108  /* connect via defaults */
109
110#ifdef ROAR_HAVE_UNIX
111#ifndef ROAR_TARGET_MICROCONTROLLER
112  roar_server = getenv("HOME");
113#else
114  roar_server = NULL;
115#endif
116
117  if ( roar_server == NULL ) {
118#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
119   if ( (pwd = getpwuid(getuid())) == NULL ) {
120    roar_server = "/NX-HOME-DIR";
121   } else {
122    roar_server = pwd->pw_dir;
123   }
124#else
125   roar_server = "/WIN32-SUCKS";
126#endif
127  }
128
129  snprintf(user_sock, sizeof(user_sock)-1, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER);
130  user_sock[sizeof(user_sock)-1] = 0;
131
132  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
133   return fh;
134
135  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
136   return fh;
137#endif
138
139  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
140   return fh;
141
142#ifdef ROAR_HAVE_LIBDNET
143  if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) {
144   if ( roar_socket_get_local_nodename() ) {
145    snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT);
146    if ( (fh = roar_socket_connect(user_sock, ROAR_DEFAULT_NUM)) != -1 )
147     return fh;
148   }
149  }
150#endif
151
152  if ( (fh = roar_socket_connect("+abstract", 0)) != -1 )
153   return fh;
154
155#ifdef ROAR_HAVE_LIBSLP
156 if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) &&
157      !(flags & ROAR_ENUM_FLAG_NONBLOCK)
158    ) {
159  if ( (server = roar_slp_find_roard(0)) != NULL ) {
160   if ( (fh = roar_connect_raw2(server, 0, 0)) != -1 )
161    return fh;
162
163   /* in case we can not connect to the server given this may be a cache problem,
164      we do a new lookup with the cache disabled in this case                     */
165   ROAR_WARN("roar_connect_raw2(*): Can not connect to SLP located server, disabling cache");
166   if ( (server = roar_slp_find_roard(1)) != NULL )
167    if ( (fh = roar_connect_raw2(server, 0, 0)) != -1 )
168     return fh;
169  }
170 }
171
172 workarounds_store = config->workaround.workarounds;
173 config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP;
174 list = roar_enum_servers(flags, -1, -1);
175 config->workaround.workarounds = workarounds_store;
176 if ( list != NULL ) {
177  for (i = 0; list[i].server != NULL; i++) {
178   if ( (fh = roar_connect_raw2((char*)list[i].server, 0, 0)) != -1 ) {
179    roar_enum_servers_free(list);
180    return fh;
181   }
182  }
183  roar_enum_servers_free(list);
184 }
185#endif
186
187 return -1;
188
189 } else {
190  /* connect via (char*)server */
191  // find a port:
192
193  strncat(user_sock, server, sizeof(user_sock)-1);
194  user_sock[sizeof(user_sock)-1] = 0;
195  server = user_sock;
196
197  if ( *user_sock != '/' ) { // don't test AF_UNIX sockets for ports
198   for (i = 0; user_sock[i] != 0; i++) {
199    if ( user_sock[i] == ':' ) {
200     if ( user_sock[i+1] == ':' ) { // DECnet, leave unchanged
201      is_decnet = 1;
202      obj = &user_sock[i+2];
203      break;
204     }
205
206     port = atoi(&(user_sock[i+1]));
207     user_sock[i] = 0;
208     break;
209    }
210   }
211  }
212
213  if ( is_decnet ) {
214   if ( *user_sock == ':' ) {
215    if ( roar_socket_get_local_nodename() )
216     roar_mm_strlcat(user_sock, roar_socket_get_local_nodename(), sizeof(user_sock)-1);
217     user_sock[sizeof(user_sock)-1] = 0;
218   }
219
220   if ( *obj == 0 ) {
221#ifdef DN_MAXOBJL
222    roar_mm_strlcat(user_sock, ROAR_DEFAULT_OBJECT, sizeof(user_sock)-1);
223    user_sock[sizeof(user_sock)-1] = 0;
224#else
225    ROAR_ERR("roar_connect_raw2(*): size of DECnet object unknown.");
226#endif
227   }
228  }
229
230  if ( port || is_decnet ) {
231   fh = roar_socket_connect(user_sock, port);
232   // restore the original string
233   user_sock[i] = ':';
234  } else {
235   fh = roar_socket_connect(user_sock, ROAR_DEFAULT_PORT);
236  }
237 }
238
239 if ( fh == -1 )
240  roar_err_set(ROAR_ERROR_CONNREFUSED);
241
242 ROAR_DBG("roar_connect_raw2(*) = %i", fh);
243
244 return fh;
245}
246
247int roar_connect    (struct roar_connection * con, const char * server) {
248 return roar_connect2(con, server, 0, 0);
249}
250
251int roar_connect2     (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) {
252 int fh;
253
254 if ( con == NULL ) {
255  roar_err_set(ROAR_ERROR_FAULT);
256  return -1;
257 }
258
259 roar_err_set(ROAR_ERROR_UNKNOWN);
260 fh = roar_connect_raw2(server, flags, timeout);
261
262 if ( fh == -1 )
263  return -1;
264
265 return roar_connect_fh(con, fh);
266}
267
268int roar_connect_fh (struct roar_connection * con, int fh) {
269
270 if ( con == NULL || fh == -1 ) {
271  roar_err_set(ROAR_ERROR_INVAL);
272  return -1;
273 }
274
275 // specal hack to set an ilegal value used internaly in libroar:
276 if ( fh == -2 )
277  fh = -1;
278
279 memset(con, 0, sizeof(struct roar_connection));
280
281 con->flags = ROAR_CON_FLAGS_FH;
282 con->__fh  = fh;
283
284 if ( roar_vio_open_fh_socket(&(con->viocon), fh) != -1 )
285  con->flags |= ROAR_CON_FLAGS_VIO;
286
287 roar_err_set(ROAR_ERROR_NONE);
288 return 0;
289}
290
291int roar_get_connection_fh (struct roar_connection * con) {
292 roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL);
293
294 if ( con == NULL )
295  return -1;
296
297 if ( !(con->flags & ROAR_CON_FLAGS_FH) )
298  return -1;
299
300 return con->__fh;
301}
302
303int roar_get_connection_vio (struct roar_connection * con, struct roar_vio_calls * vio) {
304 roar_debug_warn_obsolete("roar_get_connection_vio", "roar_get_connection_vio2", NULL);
305
306 if ( con == NULL || vio == NULL )
307  return -1;
308
309 if ( !(con->flags & ROAR_CON_FLAGS_FH) )
310  return -1;
311
312 return roar_vio_open_fh_socket(vio, con->__fh);
313}
314
315struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) {
316 if ( con == NULL )
317  return NULL;
318
319 if ( con->flags & ROAR_CON_FLAGS_VIO )
320  return &(con->viocon);
321
322// TODO: try to open the VIO.
323
324 return NULL;
325}
326
327int roar_disconnect (struct roar_connection * con) {
328 struct roar_vio_calls * vio;
329 struct roar_message m;
330
331 memset(&m, 0, sizeof(m));
332
333 m.datalen = 0;
334 m.stream  = 0;
335 m.pos     = 0;
336 m.cmd     = ROAR_CMD_QUIT;
337
338 roar_req(con, &m, NULL);
339
340 if ( (vio = roar_get_connection_vio2(con)) != NULL ) {
341  roar_vio_close(vio);
342 }
343
344 roar_connect_fh(con, -2);
345
346 roar_err_set(ROAR_ERROR_NONE);
347
348 return 0;
349}
350
351int roar_set_connection_callback(struct roar_connection * con,
352                                 void (*cb)(struct roar_connection * con,
353                                            struct roar_message    * mes,
354                                            void                   * userdata),
355                                 void * userdata) {
356 if ( con == NULL )
357  return -1;
358
359 con->cb       = cb;
360 con->userdata = userdata;
361
362 return 0;
363}
364
365int roar_sync         (struct roar_connection * con) {
366 // wait for any non-client reqs
367 return roar_wait_msg(con, 0x0000, 0x8000);
368}
369
370int roar_wait_msg     (struct roar_connection * con, int16_t seq, int16_t seqmask) {
371 return -1;
372}
373
374int roar_noop         (struct roar_connection * con) {
375 struct roar_message mes;
376
377 if ( con == NULL ) {
378  return -1;
379 }
380
381 memset(&mes, 0, sizeof(mes));
382
383 mes.cmd = ROAR_CMD_NOOP;
384
385 return roar_req(con, &mes, NULL);
386}
387
388int roar_identify   (struct roar_connection * con, const char * name) {
389 struct roar_message mes;
390 pid_t pid;
391 int max_len;
392
393 roar_err_set(ROAR_ERROR_UNKNOWN);
394
395 ROAR_DBG("roar_identify(*): try to identify myself...");
396
397 memset(&mes, 0, sizeof(mes));
398
399 mes.cmd    = ROAR_CMD_IDENTIFY;
400 mes.stream = 0;
401 mes.pos    = 0;
402
403 ROAR_DBG("roar_identify(*): name=%p", name);
404
405 if ( name == NULL )
406  name = "libroar client";
407
408 ROAR_DBG("roar_identify(*): name=%p", name);
409
410 max_len = roar_mm_strlen(name);
411 ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len);
412
413 if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) )
414  max_len = LIBROAR_BUFFER_MSGDATA - 5;
415
416 mes.datalen = 5 + max_len;
417 mes.data[0] = 1;
418
419 pid = getpid();
420 *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid);
421 ROAR_DBG("roar_identify(*): pid = %i", pid);
422
423 strncpy(mes.data+5, name, max_len);
424
425 return roar_req(con, &mes, NULL);
426}
427
428int roar_debug_message_print (struct roar_message * mes) {
429 if ( mes == NULL )
430  return -1;
431
432 ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd);
433 ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream);
434 ROAR_DBG("roar_debug_message_print(*): Pos    : %u", mes->pos);
435 ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen);
436
437 ROAR_DBG("roar_debug_message_print(*) = 0");
438 return 0;
439}
440
441int roar_debug_audio_info_print (struct roar_audio_info * info) {
442 if ( info == NULL )
443  return -1;
444
445 ROAR_DBG("roar_debug_audio_info_print(*): Rate    : %i", info->rate);
446 ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels);
447 ROAR_DBG("roar_debug_audio_info_print(*): Bits    : %i", info->bits);
448 ROAR_DBG("roar_debug_audio_info_print(*): Codec   : %i", info->codec);
449
450 ROAR_DBG("roar_debug_audio_info_print(*) = 0");
451 return 0;
452}
453
454//ll
Note: See TracBrowser for help on using the repository browser.