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