source: roaraudio/libroar/basic.c @ 1319:acdd2e21951b

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

big steps in direction to vio roar_connection object, roar_*_message() and roar_req() now use temp. VIO objects

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