source: roaraudio/libroar/basic.c @ 1998:031c501513e1

Last change on this file since 1998:031c501513e1 was 1764:711314636e32, checked in by phi, 15 years ago

only try unix default sockets if we have unix support

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