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 | |
---|
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 | 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 | |
---|
154 | int 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 | |
---|
171 | int 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 | |
---|
190 | int 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 | |
---|
209 | int 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 */) |
---|
248 | int 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 | |
---|
257 | int 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_vio_sync(vio); // we need to do this becasue of ssl/compressed links |
---|
283 | |
---|
284 | roar_errno = ROAR_ERROR_NONE; |
---|
285 | |
---|
286 | ROAR_DBG("roar_send_message(*) = 0"); |
---|
287 | return 0; |
---|
288 | } |
---|
289 | |
---|
290 | int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) { |
---|
291 | struct roar_vio_calls vio; |
---|
292 | |
---|
293 | if ( roar_vio_open_fh_socket(&vio, con->fh) == -1 ) |
---|
294 | return -1; |
---|
295 | |
---|
296 | return roar_vrecv_message(&vio, mes, data); |
---|
297 | } |
---|
298 | |
---|
299 | int roar_vrecv_message(struct roar_vio_calls * vio, struct roar_message * mes, char ** data) { |
---|
300 | char buf[_ROAR_MESS_BUF_LEN]; |
---|
301 | |
---|
302 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
303 | |
---|
304 | ROAR_DBG("roar_recv_message(*): try to get a response form the server..."); |
---|
305 | |
---|
306 | if ( data ) |
---|
307 | *data = NULL; |
---|
308 | |
---|
309 | if ( roar_vio_read(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) { |
---|
310 | roar_errno = ROAR_ERROR_PROTO; |
---|
311 | return -1; |
---|
312 | } |
---|
313 | |
---|
314 | ROAR_DBG("roar_recv_message(*): Got a header"); |
---|
315 | |
---|
316 | if ( buf[0] != _ROAR_MESSAGE_VERSION ) { |
---|
317 | roar_errno = ROAR_ERROR_PROTO; |
---|
318 | return -1; |
---|
319 | } |
---|
320 | |
---|
321 | mes->cmd = (unsigned char)buf[1]; |
---|
322 | mes->stream = ROAR_NET2HOST16(*(uint16_t*)(buf+2)); |
---|
323 | mes->pos = ROAR_NET2HOST32(*(uint32_t*)(buf+4)); |
---|
324 | mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8)); |
---|
325 | |
---|
326 | ROAR_DBG("roar_recv_message(*): command=%i(%s)", mes->cmd, |
---|
327 | mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN")); |
---|
328 | |
---|
329 | if ( mes->datalen == 0 ) { |
---|
330 | ROAR_DBG("roar_recv_message(*): no data in this pkg"); |
---|
331 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
332 | roar_errno = ROAR_ERROR_NONE; |
---|
333 | return 0; |
---|
334 | } |
---|
335 | |
---|
336 | if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) { |
---|
337 | if ( roar_vio_read(vio, mes->data, mes->datalen) == mes->datalen ) { |
---|
338 | ROAR_DBG("roar_recv_message(*): Got data!"); |
---|
339 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
340 | roar_errno = ROAR_ERROR_NONE; |
---|
341 | return 0; |
---|
342 | } |
---|
343 | |
---|
344 | roar_errno = ROAR_ERROR_PIPE; |
---|
345 | return -1; |
---|
346 | } else { |
---|
347 | if ( data == NULL ) { |
---|
348 | roar_errno = ROAR_ERROR_MSGSIZE; |
---|
349 | return -1; |
---|
350 | } |
---|
351 | |
---|
352 | if ( (*data = malloc(mes->datalen)) == NULL ) { |
---|
353 | roar_errno = ROAR_ERROR_NOMEM; |
---|
354 | return -1; |
---|
355 | } |
---|
356 | |
---|
357 | if ( mes->datalen == 0 ) { |
---|
358 | roar_errno = ROAR_ERROR_NONE; |
---|
359 | return 0; |
---|
360 | } |
---|
361 | |
---|
362 | if ( roar_vio_read(vio, *data, mes->datalen) == mes->datalen ) { |
---|
363 | ROAR_DBG("roar_recv_message(*): Got data!"); |
---|
364 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
365 | roar_errno = ROAR_ERROR_NONE; |
---|
366 | return 0; |
---|
367 | } |
---|
368 | |
---|
369 | roar_errno = ROAR_ERROR_PIPE; |
---|
370 | return -1; |
---|
371 | } |
---|
372 | |
---|
373 | // what happened here? |
---|
374 | return -1; |
---|
375 | } |
---|
376 | |
---|
377 | int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) { |
---|
378 | struct roar_vio_calls vio; |
---|
379 | |
---|
380 | if ( roar_vio_open_fh_socket(&vio, con->fh) == -1 ) |
---|
381 | return -1; |
---|
382 | |
---|
383 | return roar_vreq(&vio, mes, data); |
---|
384 | } |
---|
385 | |
---|
386 | int roar_vreq (struct roar_vio_calls * vio, struct roar_message * mes, char ** data) { |
---|
387 | if ( roar_vsend_message(vio, mes, data ? *data : NULL) != 0 ) |
---|
388 | return -1; |
---|
389 | |
---|
390 | if ( data ) |
---|
391 | free(*data); |
---|
392 | |
---|
393 | return roar_vrecv_message(vio, mes, data); |
---|
394 | } |
---|
395 | |
---|
396 | int roar_debug_message_print (struct roar_message * mes) { |
---|
397 | if ( mes == NULL ) |
---|
398 | return -1; |
---|
399 | |
---|
400 | ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd); |
---|
401 | ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream); |
---|
402 | ROAR_DBG("roar_debug_message_print(*): Pos : %u", mes->pos); |
---|
403 | ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen); |
---|
404 | |
---|
405 | ROAR_DBG("roar_debug_message_print(*) = 0"); |
---|
406 | return 0; |
---|
407 | } |
---|
408 | |
---|
409 | int roar_debug_audio_info_print (struct roar_audio_info * info) { |
---|
410 | if ( info == NULL ) |
---|
411 | return -1; |
---|
412 | |
---|
413 | ROAR_DBG("roar_debug_audio_info_print(*): Rate : %i", info->rate); |
---|
414 | ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels); |
---|
415 | ROAR_DBG("roar_debug_audio_info_print(*): Bits : %i", info->bits); |
---|
416 | ROAR_DBG("roar_debug_audio_info_print(*): Codec : %i", info->codec); |
---|
417 | |
---|
418 | ROAR_DBG("roar_debug_audio_info_print(*) = 0"); |
---|
419 | return 0; |
---|
420 | } |
---|
421 | |
---|
422 | //ll |
---|