source: roaraudio/libroar/basic.c @ 2007:4eb7199520fb

Last change on this file since 2007:4eb7199520fb was 2007:4eb7199520fb, checked in by phi, 15 years ago

got client slp working, need cleanup

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