source: roaraudio/libroar/basic.c @ 1475:a43095c6fc0e

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

only use POSIX file IO if we have POSIX file IO ;)

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