source: roaraudio/libroar/basic.c @ 5132:1c10be48298e

Last change on this file since 5132:1c10be48298e was 5132:1c10be48298e, checked in by phi, 13 years ago

Fixed invalid pointer aliasing (Closes: #178)

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