source: roaraudio/libroar/basic.c @ 5289:ddb3677af4d0

Last change on this file since 5289:ddb3677af4d0 was 5267:b48fff0bf120, checked in by phi, 12 years ago

experimental support to connect to roard via PTYs

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