source: roaraudio/libroar/basic.c @ 2010:f8413ff1a265

Last change on this file since 2010:f8413ff1a265 was 2010:f8413ff1a265, checked in by phi, 15 years ago

corrected bug with DECnet.

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