source: roaraudio/libroar/basic.c @ 3875:8731d0c3e04a

Last change on this file since 3875:8731d0c3e04a was 3875:8731d0c3e04a, checked in by phi, 14 years ago

init/zero messages with memset()

File size: 13.2 KB
Line 
1//basic.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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 (char * server) {
39 char user_sock[80];
40 char * roar_server;
41 int i = 0;
42 int port = 0;
43 int fh = -1;
44 int is_decnet = 0;
45 char * obj = NULL;
46#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
47 struct passwd * pwd;
48#endif
49#ifdef ROAR_HAVE_LIBDNET
50 struct stat decnet_stat;
51#endif
52#ifdef ROAR_HAVE_LIBX11
53 struct roar_x11_connection * x11con;
54#endif
55
56 roar_errno = ROAR_ERROR_UNKNOWN;
57
58 // load config
59 roar_libroar_get_config();
60
61 if ( server == NULL )
62  server = roar_libroar_get_server();
63
64 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
65  server = roar_server;
66
67#ifdef ROAR_HAVE_LIBX11
68 if ( server == NULL ) {
69  if ( (x11con = roar_x11_connect(NULL)) != NULL ) {
70   server = roar_x11_get_prop(x11con, "ROAR_SERVER");
71   roar_x11_disconnect(x11con);
72  }
73 }
74#endif
75
76#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
77 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) {
78   user_sock[i] = 0;
79   server = user_sock;
80 }
81#endif
82
83 if ( server != NULL && !strcasecmp(server, "+slp") ) {
84  server = roar_slp_find_roard(0);
85  if ( server == NULL ) {
86   return -1;
87  }
88 }
89
90 if ( server == NULL || *server == 0 ) {
91  /* connect via defaults */
92
93#ifdef ROAR_HAVE_UNIX
94#ifndef ROAR_TARGET_MICROCONTROLLER
95  roar_server = getenv("HOME");
96#else
97  roar_server = NULL;
98#endif
99
100  if ( roar_server == NULL ) {
101#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
102   if ( (pwd = getpwuid(getuid())) == NULL ) {
103    roar_server = "/NX-HOME-DIR";
104   } else {
105    roar_server = pwd->pw_dir;
106   }
107#else
108   roar_server = "/WIN32-SUCKS";
109#endif
110  }
111
112  snprintf(user_sock, 79, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER);
113
114  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
115   return fh;
116
117  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
118   return fh;
119#endif
120
121  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
122   return fh;
123
124#ifdef ROAR_HAVE_LIBDNET
125  if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) {
126   if ( roar_socket_get_local_nodename() ) {
127    snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT);
128    if ( (fh = roar_socket_connect(user_sock, ROAR_DEFAULT_NUM)) != -1 )
129     return fh;
130   }
131  }
132#endif
133
134#ifdef ROAR_HAVE_LIBSLP
135 if ( (server = roar_slp_find_roard(0)) != NULL )
136  if ( (fh = roar_connect_raw(server)) != -1 )
137   return fh;
138
139 /* in case we can not connect to the server given this may be a cache problem,
140    we do a new lookup with the cache disabled in this case                     */
141 ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache");
142 if ( (server = roar_slp_find_roard(1)) != NULL )
143  if ( (fh = roar_connect_raw(server)) != -1 )
144   return fh;
145#endif
146
147 return -1;
148
149 } else {
150  /* connect via (char*)server */
151  // find a port:
152  if ( *server != '/' ) { // don't test AF_UNIX sockets for ports
153   for (i = 0; server[i] != 0; i++) {
154    if ( server[i] == ':' ) {
155     if ( server[i+1] == ':' ) { // DECnet, leave unchanged
156      is_decnet = 1;
157      obj = &server[i+2];
158      break;
159     }
160
161     port = atoi(server+i+1);
162     server[i] = 0;
163     break;
164    }
165   }
166  }
167
168  if ( is_decnet ) {
169    *user_sock = 0;
170   if ( *server == ':' ) {
171    if ( roar_socket_get_local_nodename() )
172     strncat(user_sock, roar_socket_get_local_nodename(), 6);
173   }
174
175   strncat(user_sock, server, 79);
176   server = user_sock;
177   if ( *obj == 0 ) {
178#ifdef DN_MAXOBJL
179    strncat(user_sock, ROAR_DEFAULT_OBJECT, DN_MAXOBJL+2);
180#else
181    ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown.");
182#endif
183   }
184  }
185
186  if ( port || is_decnet ) {
187   fh = roar_socket_connect(server, port);
188   // restore the original string
189   server[i] = ':';
190  } else {
191   fh = roar_socket_connect(server, ROAR_DEFAULT_PORT);
192  }
193 }
194
195 if ( fh == -1 )
196  roar_errno = ROAR_ERROR_CONNREFUSED;
197
198 ROAR_DBG("roar_connect_raw(*) = %i", fh);
199
200 return fh;
201}
202
203int roar_connect    (struct roar_connection * con, char * server) {
204 int fh;
205
206 if ( con == NULL ) {
207  roar_errno = ROAR_ERROR_INVAL;
208  return -1;
209 }
210
211 roar_errno = ROAR_ERROR_UNKNOWN;
212 fh = roar_connect_raw(server);
213
214 if ( fh == -1 )
215  return -1;
216
217 return roar_connect_fh(con, fh);
218}
219
220int roar_connect_fh (struct roar_connection * con, int fh) {
221
222 if ( con == NULL || fh == -1 ) {
223  roar_errno = ROAR_ERROR_INVAL;
224  return -1;
225 }
226
227 // specal hack to set an ilegal value used internaly in libroar:
228 if ( fh == -2 )
229  fh = -1;
230
231 memset(con, 0, sizeof(struct roar_connection));
232
233 con->flags = ROAR_CON_FLAGS_FH;
234 con->__fh  = fh;
235
236 if ( roar_vio_open_fh_socket(&(con->viocon), fh) != -1 )
237  con->flags |= ROAR_CON_FLAGS_VIO;
238
239 roar_errno = ROAR_ERROR_NONE;
240 return 0;
241}
242
243int roar_get_connection_fh (struct roar_connection * con) {
244 roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL);
245
246 if ( con == NULL )
247  return -1;
248
249 if ( !(con->flags & ROAR_CON_FLAGS_FH) )
250  return -1;
251
252 return con->__fh;
253}
254
255int roar_get_connection_vio (struct roar_connection * con, struct roar_vio_calls * vio) {
256 roar_debug_warn_obsolete("roar_get_connection_vio", "roar_get_connection_vio2", NULL);
257
258 if ( con == NULL || vio == NULL )
259  return -1;
260
261 if ( !(con->flags & ROAR_CON_FLAGS_FH) )
262  return -1;
263
264 return roar_vio_open_fh_socket(vio, con->__fh);
265}
266
267struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) {
268 if ( con == NULL )
269  return NULL;
270
271 if ( con->flags & ROAR_CON_FLAGS_VIO )
272  return &(con->viocon);
273
274// TODO: try to open the VIO.
275
276 return NULL;
277}
278
279int roar_disconnect (struct roar_connection * con) {
280 struct roar_vio_calls * vio;
281 struct roar_message m;
282
283 memset(&m, 0, sizeof(m));
284
285 m.datalen = 0;
286 m.stream  = 0;
287 m.pos     = 0;
288 m.cmd     = ROAR_CMD_QUIT;
289
290 roar_req(con, &m, NULL);
291
292 if ( (vio = roar_get_connection_vio2(con)) != NULL ) {
293  roar_vio_close(vio);
294 }
295
296 roar_connect_fh(con, -2);
297
298 roar_errno = ROAR_ERROR_NONE;
299
300 return 0;
301}
302
303int roar_noop         (struct roar_connection * con) {
304 struct roar_message mes;
305
306 if ( con == NULL ) {
307  return -1;
308 }
309
310 memset(&mes, 0, sizeof(mes));
311
312 mes.cmd = ROAR_CMD_NOOP;
313
314 return roar_req(con, &mes, NULL);
315}
316
317int roar_identify   (struct roar_connection * con, char * name) {
318 struct roar_message mes;
319 pid_t pid;
320 int max_len;
321
322 roar_errno = ROAR_ERROR_UNKNOWN;
323
324 ROAR_DBG("roar_identify(*): try to identify myself...");
325
326 memset(&mes, 0, sizeof(mes));
327
328 mes.cmd    = ROAR_CMD_IDENTIFY;
329 mes.stream = 0;
330 mes.pos    = 0;
331
332 ROAR_DBG("roar_identify(*): name=%p", name);
333
334 if ( name == NULL )
335  name = "libroar client";
336
337 ROAR_DBG("roar_identify(*): name=%p", name);
338
339 max_len = strlen(name);
340 ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len);
341
342 if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) )
343  max_len = LIBROAR_BUFFER_MSGDATA - 5;
344
345 mes.datalen = 5 + max_len;
346 mes.data[0] = 1;
347
348 pid = getpid();
349 *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid);
350 ROAR_DBG("roar_identify(*): pid = %i", pid);
351
352 strncpy(mes.data+5, name, max_len);
353
354 return roar_req(con, &mes, NULL);
355}
356
357#define _ROAR_MESS_BUF_LEN (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */)
358int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) {
359 struct roar_vio_calls * vio;
360
361 if ( (vio = roar_get_connection_vio2(con)) == NULL )
362  return -1;
363
364 return roar_vsend_message(vio, mes, data);
365}
366
367int roar_vsend_message(struct roar_vio_calls * vio, struct roar_message * mes, char *  data) {
368 char buf[_ROAR_MESS_BUF_LEN];
369
370 roar_errno = ROAR_ERROR_UNKNOWN;
371
372 ROAR_DBG("roar_send_message(*): try to send an request...");
373
374 buf[0] = _ROAR_MESSAGE_VERSION;
375 buf[1] = (unsigned char) mes->cmd;
376 *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream);
377 *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos);
378 *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen);
379
380 if ( roar_vio_write(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
381  roar_errno = ROAR_ERROR_PIPE;
382  return -1;
383 }
384
385 if ( mes->datalen != 0 ) {
386  if ( roar_vio_write(vio, data == NULL ? mes->data : data, mes->datalen) != mes->datalen ) {
387   roar_errno = ROAR_ERROR_PIPE;
388   return -1;
389  }
390 }
391
392 roar_errno = ROAR_ERROR_NONE;
393
394 ROAR_DBG("roar_send_message(*) = 0");
395 return 0;
396}
397
398int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) {
399 struct roar_vio_calls * vio;
400
401 if ( (vio = roar_get_connection_vio2(con)) == NULL )
402  return -1;
403
404 return roar_vrecv_message(vio, mes, data);
405}
406
407int roar_vrecv_message(struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
408 char buf[_ROAR_MESS_BUF_LEN];
409
410 roar_errno = ROAR_ERROR_UNKNOWN;
411
412 ROAR_DBG("roar_recv_message(*): try to get a response form the server...");
413
414 if ( mes == NULL )
415  return -1;
416
417 if ( data != NULL )
418  *data = NULL;
419
420 memset(mes, 0, sizeof(struct roar_message));
421
422 if ( roar_vio_read(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) {
423  roar_errno = ROAR_ERROR_PROTO;
424  return -1;
425 }
426
427 ROAR_DBG("roar_recv_message(*): Got a header");
428
429 if ( buf[0] != _ROAR_MESSAGE_VERSION ) {
430  roar_errno = ROAR_ERROR_PROTO;
431  return -1;
432 }
433
434 mes->cmd     = (unsigned char)buf[1];
435 mes->stream  = ROAR_NET2HOST16(*(uint16_t*)(buf+2));
436 mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+4));
437 mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8));
438
439 if ( (int16_t)mes->stream == -1 )
440  mes->stream = -1;
441
442 ROAR_DBG("roar_recv_message(*): command=%i(%s)", mes->cmd,
443           mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN"));
444
445 if ( mes->datalen == 0 ) {
446  ROAR_DBG("roar_recv_message(*): no data in this pkg");
447  ROAR_DBG("roar_recv_message(*) = 0");
448  roar_errno = ROAR_ERROR_NONE;
449  return 0;
450 }
451
452 if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) {
453  if ( roar_vio_read(vio, mes->data, mes->datalen) == mes->datalen ) {
454   ROAR_DBG("roar_recv_message(*): Got data!");
455   ROAR_DBG("roar_recv_message(*) = 0");
456   roar_errno = ROAR_ERROR_NONE;
457   return 0;
458  }
459
460  roar_errno = ROAR_ERROR_PIPE;
461  return -1;
462 } else {
463  if ( data == NULL ) {
464   roar_errno = ROAR_ERROR_MSGSIZE;
465   return -1;
466  }
467
468  if ( (*data = malloc(mes->datalen)) == NULL ) {
469   roar_errno = ROAR_ERROR_NOMEM;
470   return -1;
471  }
472
473  if ( mes->datalen == 0 ) {
474   roar_errno = ROAR_ERROR_NONE;
475   return 0;
476  }
477
478  if ( roar_vio_read(vio, *data, mes->datalen) == mes->datalen ) {
479   ROAR_DBG("roar_recv_message(*): Got data!");
480   ROAR_DBG("roar_recv_message(*) = 0");
481   roar_errno = ROAR_ERROR_NONE;
482   return 0;
483  }
484
485  roar_errno = ROAR_ERROR_PIPE;
486  return -1;
487 }
488
489 // what happened here?
490 return -1;
491}
492
493int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) {
494 struct roar_vio_calls * vio;
495
496 if ( (vio = roar_get_connection_vio2(con)) == NULL )
497  return -1;
498
499 return roar_vreq(vio, mes, data);
500}
501
502int roar_vreq         (struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
503 if ( roar_vsend_message(vio, mes, data ? *data : NULL) != 0 )
504  return -1;
505
506 if ( data )
507  free(*data);
508
509 roar_vio_sync(vio); // we need to do this becasue of ssl/compressed links
510
511 return roar_vrecv_message(vio, mes, data);
512}
513
514int roar_debug_message_print (struct roar_message * mes) {
515 if ( mes == NULL )
516  return -1;
517
518 ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd);
519 ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream);
520 ROAR_DBG("roar_debug_message_print(*): Pos    : %u", mes->pos);
521 ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen);
522
523 ROAR_DBG("roar_debug_message_print(*) = 0");
524 return 0;
525}
526
527int roar_debug_audio_info_print (struct roar_audio_info * info) {
528 if ( info == NULL )
529  return -1;
530
531 ROAR_DBG("roar_debug_audio_info_print(*): Rate    : %i", info->rate);
532 ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels);
533 ROAR_DBG("roar_debug_audio_info_print(*): Bits    : %i", info->bits);
534 ROAR_DBG("roar_debug_audio_info_print(*): Codec   : %i", info->codec);
535
536 ROAR_DBG("roar_debug_audio_info_print(*) = 0");
537 return 0;
538}
539
540//ll
Note: See TracBrowser for help on using the repository browser.