source: roaraudio/libroar/basic.c @ 828:8b4d4f52ca50

Last change on this file since 828:8b4d4f52ca50 was 828:8b4d4f52ca50, checked in by phi, 16 years ago

do not try DECnet on non DECnet kernels...

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