[0] | 1 | //basic.c: |
---|
| 2 | |
---|
[690] | 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 | |
---|
[0] | 35 | #include "libroar.h" |
---|
| 36 | |
---|
| 37 | int roar_connect_raw (char * server) { |
---|
| 38 | char user_sock[80]; |
---|
| 39 | char * roar_server; |
---|
[701] | 40 | int i = 0; |
---|
[0] | 41 | int port = 0; |
---|
| 42 | int fh = -1; |
---|
[501] | 43 | int is_decnet = 0; |
---|
[516] | 44 | char * obj = NULL; |
---|
[1475] | 45 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[1026] | 46 | struct passwd * pwd; |
---|
[1393] | 47 | #endif |
---|
[828] | 48 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 49 | struct stat decnet_stat; |
---|
| 50 | #endif |
---|
[0] | 51 | |
---|
[807] | 52 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 53 | |
---|
[61] | 54 | if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL ) |
---|
[0] | 55 | server = roar_server; |
---|
| 56 | |
---|
[1436] | 57 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[448] | 58 | if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) { |
---|
| 59 | user_sock[i] = 0; |
---|
| 60 | server = user_sock; |
---|
| 61 | } |
---|
[1093] | 62 | #endif |
---|
[448] | 63 | |
---|
[61] | 64 | if ( server == NULL || *server == 0 ) { |
---|
[0] | 65 | /* connect via defaults */ |
---|
| 66 | |
---|
[1436] | 67 | #ifndef ROAR_TARGET_MICROCONTROLLER |
---|
[1026] | 68 | roar_server = getenv("HOME"); |
---|
[1435] | 69 | #else |
---|
| 70 | roar_server = NULL; |
---|
| 71 | #endif |
---|
[1026] | 72 | |
---|
| 73 | if ( roar_server == NULL ) { |
---|
[1436] | 74 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[1026] | 75 | if ( (pwd = getpwuid(getuid())) == NULL ) { |
---|
| 76 | roar_server = "/NX-HOME-DIR"; |
---|
| 77 | } else { |
---|
| 78 | roar_server = pwd->pw_dir; |
---|
| 79 | } |
---|
[1078] | 80 | #else |
---|
| 81 | roar_server = "/WIN32-SUCKS"; |
---|
| 82 | #endif |
---|
[1026] | 83 | } |
---|
| 84 | |
---|
| 85 | snprintf(user_sock, 79, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER); |
---|
[0] | 86 | |
---|
| 87 | if ( (fh = roar_socket_connect(user_sock, 0)) != -1 ) |
---|
| 88 | return fh; |
---|
| 89 | |
---|
[237] | 90 | if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 ) |
---|
| 91 | return fh; |
---|
| 92 | |
---|
[0] | 93 | if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 ) |
---|
| 94 | return fh; |
---|
| 95 | |
---|
[828] | 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 | } |
---|
[522] | 102 | } |
---|
[828] | 103 | #endif |
---|
[522] | 104 | |
---|
[0] | 105 | } else { |
---|
| 106 | /* connect via (char*)server */ |
---|
[501] | 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; |
---|
[516] | 113 | obj = &server[i+2]; |
---|
[501] | 114 | break; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | port = atoi(server+i+1); |
---|
| 118 | server[i] = 0; |
---|
| 119 | break; |
---|
| 120 | } |
---|
[0] | 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
[521] | 124 | if ( is_decnet ) { |
---|
| 125 | *user_sock = 0; |
---|
| 126 | if ( *server == ':' ) { |
---|
| 127 | if ( roar_socket_get_local_nodename() ) |
---|
[1066] | 128 | strncat(user_sock, roar_socket_get_local_nodename(), 6); |
---|
[521] | 129 | } |
---|
| 130 | |
---|
[1066] | 131 | strncat(user_sock, server, 79); |
---|
[516] | 132 | server = user_sock; |
---|
[521] | 133 | if ( *obj == 0 ) { |
---|
[1066] | 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 |
---|
[521] | 139 | } |
---|
[516] | 140 | } |
---|
| 141 | |
---|
[501] | 142 | if ( port || is_decnet ) { |
---|
[0] | 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 | |
---|
[807] | 151 | if ( fh == -1 ) |
---|
| 152 | roar_errno = ROAR_ERROR_CONNREFUSED; |
---|
| 153 | |
---|
[0] | 154 | ROAR_DBG("roar_connect_raw(*) = %i", fh); |
---|
| 155 | |
---|
| 156 | return fh; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | int roar_connect (struct roar_connection * con, char * server) { |
---|
[1315] | 160 | int fh; |
---|
| 161 | |
---|
| 162 | if ( con == NULL ) { |
---|
| 163 | roar_errno = ROAR_ERROR_INVAL; |
---|
| 164 | return -1; |
---|
| 165 | } |
---|
| 166 | |
---|
[807] | 167 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
[1315] | 168 | fh = roar_connect_raw(server); |
---|
[0] | 169 | |
---|
[1315] | 170 | if ( fh == -1 ) |
---|
[0] | 171 | return -1; |
---|
| 172 | |
---|
[1315] | 173 | return roar_connect_fh(con, fh); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | int 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 | |
---|
[1660] | 189 | con->__fh = fh; |
---|
[1315] | 190 | |
---|
[807] | 191 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 192 | return 0; |
---|
| 193 | } |
---|
| 194 | |
---|
[1660] | 195 | int roar_get_connection_fh (struct roar_connection * con) { |
---|
| 196 | return con->__fh; |
---|
| 197 | } |
---|
| 198 | |
---|
[0] | 199 | int roar_disconnect (struct roar_connection * con) { |
---|
| 200 | struct roar_message m; |
---|
| 201 | |
---|
| 202 | m.datalen = 0; |
---|
| 203 | m.stream = 0; |
---|
[133] | 204 | m.pos = 0; |
---|
[0] | 205 | m.cmd = ROAR_CMD_QUIT; |
---|
| 206 | |
---|
| 207 | roar_req(con, &m, NULL); |
---|
| 208 | |
---|
[1475] | 209 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[1660] | 210 | close(roar_get_connection_fh(con)); |
---|
[1475] | 211 | #endif |
---|
[0] | 212 | |
---|
[1660] | 213 | roar_connect_fh(con, -2); |
---|
[0] | 214 | |
---|
[807] | 215 | roar_errno = ROAR_ERROR_NONE; |
---|
| 216 | |
---|
[0] | 217 | return 0; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | int roar_identify (struct roar_connection * con, char * name) { |
---|
| 221 | struct roar_message mes; |
---|
| 222 | pid_t pid; |
---|
| 223 | int max_len; |
---|
| 224 | |
---|
[807] | 225 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 226 | |
---|
[0] | 227 | ROAR_DBG("roar_identify(*): try to identify myself..."); |
---|
| 228 | |
---|
| 229 | mes.cmd = ROAR_CMD_IDENTIFY; |
---|
| 230 | mes.stream = 0; |
---|
| 231 | mes.pos = 0; |
---|
| 232 | |
---|
| 233 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
| 234 | |
---|
| 235 | if ( name == NULL ) |
---|
| 236 | name = "libroar client"; |
---|
| 237 | |
---|
| 238 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
| 239 | |
---|
| 240 | max_len = strlen(name); |
---|
| 241 | ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len); |
---|
| 242 | |
---|
| 243 | if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) ) |
---|
| 244 | max_len = LIBROAR_BUFFER_MSGDATA - 5; |
---|
| 245 | |
---|
| 246 | mes.datalen = 5 + max_len; |
---|
| 247 | mes.data[0] = 1; |
---|
| 248 | |
---|
| 249 | pid = getpid(); |
---|
| 250 | *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid); |
---|
| 251 | ROAR_DBG("roar_identify(*): pid = %i", pid); |
---|
| 252 | |
---|
| 253 | strncpy(mes.data+5, name, max_len); |
---|
| 254 | |
---|
| 255 | return roar_req(con, &mes, NULL); |
---|
| 256 | } |
---|
| 257 | |
---|
[8] | 258 | #define _ROAR_MESS_BUF_LEN (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */) |
---|
[0] | 259 | int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) { |
---|
[1319] | 260 | struct roar_vio_calls vio; |
---|
| 261 | |
---|
[1660] | 262 | if ( roar_vio_open_fh_socket(&vio, roar_get_connection_fh(con)) == -1 ) |
---|
[1319] | 263 | return -1; |
---|
| 264 | |
---|
| 265 | return roar_vsend_message(&vio, mes, data); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | int roar_vsend_message(struct roar_vio_calls * vio, struct roar_message * mes, char * data) { |
---|
[0] | 269 | char buf[_ROAR_MESS_BUF_LEN]; |
---|
| 270 | |
---|
[807] | 271 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 272 | |
---|
[0] | 273 | ROAR_DBG("roar_send_message(*): try to send an request..."); |
---|
| 274 | |
---|
[8] | 275 | buf[0] = _ROAR_MESSAGE_VERSION; |
---|
| 276 | buf[1] = (unsigned char) mes->cmd; |
---|
| 277 | *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream); |
---|
| 278 | *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos); |
---|
| 279 | *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen); |
---|
[0] | 280 | |
---|
[1319] | 281 | if ( roar_vio_write(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) { |
---|
[807] | 282 | roar_errno = ROAR_ERROR_PIPE; |
---|
[0] | 283 | return -1; |
---|
[807] | 284 | } |
---|
[0] | 285 | |
---|
[807] | 286 | if ( mes->datalen != 0 ) { |
---|
[1319] | 287 | if ( roar_vio_write(vio, data == NULL ? mes->data : data, mes->datalen) != mes->datalen ) { |
---|
[807] | 288 | roar_errno = ROAR_ERROR_PIPE; |
---|
[0] | 289 | return -1; |
---|
[807] | 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
[1334] | 293 | roar_vio_sync(vio); // we need to do this becasue of ssl/compressed links |
---|
| 294 | |
---|
[807] | 295 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 296 | |
---|
| 297 | ROAR_DBG("roar_send_message(*) = 0"); |
---|
| 298 | return 0; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) { |
---|
[1319] | 302 | struct roar_vio_calls vio; |
---|
| 303 | |
---|
[1660] | 304 | if ( roar_vio_open_fh_socket(&vio, roar_get_connection_fh(con)) == -1 ) |
---|
[1319] | 305 | return -1; |
---|
| 306 | |
---|
| 307 | return roar_vrecv_message(&vio, mes, data); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | int roar_vrecv_message(struct roar_vio_calls * vio, struct roar_message * mes, char ** data) { |
---|
[0] | 311 | char buf[_ROAR_MESS_BUF_LEN]; |
---|
| 312 | |
---|
[807] | 313 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 314 | |
---|
[0] | 315 | ROAR_DBG("roar_recv_message(*): try to get a response form the server..."); |
---|
| 316 | |
---|
| 317 | if ( data ) |
---|
| 318 | *data = NULL; |
---|
| 319 | |
---|
[1319] | 320 | if ( roar_vio_read(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) { |
---|
[807] | 321 | roar_errno = ROAR_ERROR_PROTO; |
---|
[0] | 322 | return -1; |
---|
[807] | 323 | } |
---|
[0] | 324 | |
---|
| 325 | ROAR_DBG("roar_recv_message(*): Got a header"); |
---|
| 326 | |
---|
[807] | 327 | if ( buf[0] != _ROAR_MESSAGE_VERSION ) { |
---|
| 328 | roar_errno = ROAR_ERROR_PROTO; |
---|
[8] | 329 | return -1; |
---|
[807] | 330 | } |
---|
[8] | 331 | |
---|
| 332 | mes->cmd = (unsigned char)buf[1]; |
---|
| 333 | mes->stream = ROAR_NET2HOST16(*(uint16_t*)(buf+2)); |
---|
| 334 | mes->pos = ROAR_NET2HOST32(*(uint32_t*)(buf+4)); |
---|
| 335 | mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8)); |
---|
[0] | 336 | |
---|
| 337 | ROAR_DBG("roar_recv_message(*): command=%i(%s)", mes->cmd, |
---|
| 338 | mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN")); |
---|
| 339 | |
---|
[104] | 340 | if ( mes->datalen == 0 ) { |
---|
| 341 | ROAR_DBG("roar_recv_message(*): no data in this pkg"); |
---|
| 342 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
[807] | 343 | roar_errno = ROAR_ERROR_NONE; |
---|
[104] | 344 | return 0; |
---|
| 345 | } |
---|
| 346 | |
---|
[0] | 347 | if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) { |
---|
[1319] | 348 | if ( roar_vio_read(vio, mes->data, mes->datalen) == mes->datalen ) { |
---|
[0] | 349 | ROAR_DBG("roar_recv_message(*): Got data!"); |
---|
| 350 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
[807] | 351 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 352 | return 0; |
---|
| 353 | } |
---|
[807] | 354 | |
---|
| 355 | roar_errno = ROAR_ERROR_PIPE; |
---|
[0] | 356 | return -1; |
---|
| 357 | } else { |
---|
[807] | 358 | if ( data == NULL ) { |
---|
| 359 | roar_errno = ROAR_ERROR_MSGSIZE; |
---|
[0] | 360 | return -1; |
---|
[807] | 361 | } |
---|
[0] | 362 | |
---|
[807] | 363 | if ( (*data = malloc(mes->datalen)) == NULL ) { |
---|
| 364 | roar_errno = ROAR_ERROR_NOMEM; |
---|
[0] | 365 | return -1; |
---|
[807] | 366 | } |
---|
[0] | 367 | |
---|
[807] | 368 | if ( mes->datalen == 0 ) { |
---|
| 369 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 370 | return 0; |
---|
[807] | 371 | } |
---|
[0] | 372 | |
---|
[1319] | 373 | if ( roar_vio_read(vio, *data, mes->datalen) == mes->datalen ) { |
---|
[0] | 374 | ROAR_DBG("roar_recv_message(*): Got data!"); |
---|
| 375 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
[807] | 376 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 377 | return 0; |
---|
| 378 | } |
---|
[807] | 379 | |
---|
| 380 | roar_errno = ROAR_ERROR_PIPE; |
---|
[0] | 381 | return -1; |
---|
| 382 | } |
---|
| 383 | |
---|
[807] | 384 | // what happened here? |
---|
[0] | 385 | return -1; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) { |
---|
[1319] | 389 | struct roar_vio_calls vio; |
---|
| 390 | |
---|
[1660] | 391 | if ( roar_vio_open_fh_socket(&vio, roar_get_connection_fh(con)) == -1 ) |
---|
[1319] | 392 | return -1; |
---|
| 393 | |
---|
| 394 | return roar_vreq(&vio, mes, data); |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | int roar_vreq (struct roar_vio_calls * vio, struct roar_message * mes, char ** data) { |
---|
| 398 | if ( roar_vsend_message(vio, mes, data ? *data : NULL) != 0 ) |
---|
[0] | 399 | return -1; |
---|
| 400 | |
---|
| 401 | if ( data ) |
---|
| 402 | free(*data); |
---|
| 403 | |
---|
[1319] | 404 | return roar_vrecv_message(vio, mes, data); |
---|
[0] | 405 | } |
---|
| 406 | |
---|
| 407 | int roar_debug_message_print (struct roar_message * mes) { |
---|
| 408 | if ( mes == NULL ) |
---|
| 409 | return -1; |
---|
| 410 | |
---|
| 411 | ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd); |
---|
| 412 | ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream); |
---|
| 413 | ROAR_DBG("roar_debug_message_print(*): Pos : %u", mes->pos); |
---|
| 414 | ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen); |
---|
| 415 | |
---|
| 416 | ROAR_DBG("roar_debug_message_print(*) = 0"); |
---|
| 417 | return 0; |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | int roar_debug_audio_info_print (struct roar_audio_info * info) { |
---|
| 421 | if ( info == NULL ) |
---|
| 422 | return -1; |
---|
| 423 | |
---|
| 424 | ROAR_DBG("roar_debug_audio_info_print(*): Rate : %i", info->rate); |
---|
| 425 | ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels); |
---|
| 426 | ROAR_DBG("roar_debug_audio_info_print(*): Bits : %i", info->bits); |
---|
| 427 | ROAR_DBG("roar_debug_audio_info_print(*): Codec : %i", info->codec); |
---|
| 428 | |
---|
| 429 | ROAR_DBG("roar_debug_audio_info_print(*) = 0"); |
---|
| 430 | return 0; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | //ll |
---|