[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; |
---|
[1026] | 45 | struct passwd * pwd; |
---|
[828] | 46 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 47 | struct stat decnet_stat; |
---|
| 48 | #endif |
---|
[0] | 49 | |
---|
[807] | 50 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 51 | |
---|
[61] | 52 | if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL ) |
---|
[0] | 53 | server = roar_server; |
---|
| 54 | |
---|
[1093] | 55 | #ifndef ROAR_TARGET_WIN32 |
---|
[448] | 56 | if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) { |
---|
| 57 | user_sock[i] = 0; |
---|
| 58 | server = user_sock; |
---|
| 59 | } |
---|
[1093] | 60 | #endif |
---|
[448] | 61 | |
---|
[61] | 62 | if ( server == NULL || *server == 0 ) { |
---|
[0] | 63 | /* connect via defaults */ |
---|
| 64 | |
---|
| 65 | |
---|
[1026] | 66 | roar_server = getenv("HOME"); |
---|
| 67 | |
---|
| 68 | if ( roar_server == NULL ) { |
---|
[1078] | 69 | #ifndef ROAR_TARGET_WIN32 |
---|
[1026] | 70 | if ( (pwd = getpwuid(getuid())) == NULL ) { |
---|
| 71 | roar_server = "/NX-HOME-DIR"; |
---|
| 72 | } else { |
---|
| 73 | roar_server = pwd->pw_dir; |
---|
| 74 | } |
---|
[1078] | 75 | #else |
---|
| 76 | roar_server = "/WIN32-SUCKS"; |
---|
| 77 | #endif |
---|
[1026] | 78 | } |
---|
| 79 | |
---|
| 80 | snprintf(user_sock, 79, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER); |
---|
[0] | 81 | |
---|
| 82 | if ( (fh = roar_socket_connect(user_sock, 0)) != -1 ) |
---|
| 83 | return fh; |
---|
| 84 | |
---|
[237] | 85 | if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 ) |
---|
| 86 | return fh; |
---|
| 87 | |
---|
[0] | 88 | if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 ) |
---|
| 89 | return fh; |
---|
| 90 | |
---|
[828] | 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 | } |
---|
[522] | 97 | } |
---|
[828] | 98 | #endif |
---|
[522] | 99 | |
---|
[0] | 100 | } else { |
---|
| 101 | /* connect via (char*)server */ |
---|
[501] | 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; |
---|
[516] | 108 | obj = &server[i+2]; |
---|
[501] | 109 | break; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | port = atoi(server+i+1); |
---|
| 113 | server[i] = 0; |
---|
| 114 | break; |
---|
| 115 | } |
---|
[0] | 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
[521] | 119 | if ( is_decnet ) { |
---|
| 120 | *user_sock = 0; |
---|
| 121 | if ( *server == ':' ) { |
---|
| 122 | if ( roar_socket_get_local_nodename() ) |
---|
[1066] | 123 | strncat(user_sock, roar_socket_get_local_nodename(), 6); |
---|
[521] | 124 | } |
---|
| 125 | |
---|
[1066] | 126 | strncat(user_sock, server, 79); |
---|
[516] | 127 | server = user_sock; |
---|
[521] | 128 | if ( *obj == 0 ) { |
---|
[1066] | 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 |
---|
[521] | 134 | } |
---|
[516] | 135 | } |
---|
| 136 | |
---|
[501] | 137 | if ( port || is_decnet ) { |
---|
[0] | 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 | |
---|
[807] | 146 | if ( fh == -1 ) |
---|
| 147 | roar_errno = ROAR_ERROR_CONNREFUSED; |
---|
| 148 | |
---|
[0] | 149 | ROAR_DBG("roar_connect_raw(*) = %i", fh); |
---|
| 150 | |
---|
| 151 | return fh; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | int roar_connect (struct roar_connection * con, char * server) { |
---|
[1315] | 155 | int fh; |
---|
| 156 | |
---|
| 157 | if ( con == NULL ) { |
---|
| 158 | roar_errno = ROAR_ERROR_INVAL; |
---|
| 159 | return -1; |
---|
| 160 | } |
---|
| 161 | |
---|
[807] | 162 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
[1315] | 163 | fh = roar_connect_raw(server); |
---|
[0] | 164 | |
---|
[1315] | 165 | if ( fh == -1 ) |
---|
[0] | 166 | return -1; |
---|
| 167 | |
---|
[1315] | 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 | |
---|
[807] | 186 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 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; |
---|
[133] | 195 | m.pos = 0; |
---|
[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 | |
---|
[807] | 204 | roar_errno = ROAR_ERROR_NONE; |
---|
| 205 | |
---|
[0] | 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 | |
---|
[807] | 214 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 215 | |
---|
[0] | 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 | |
---|
[8] | 247 | #define _ROAR_MESS_BUF_LEN (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */) |
---|
[0] | 248 | int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) { |
---|
[1319] | 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) { |
---|
[0] | 258 | char buf[_ROAR_MESS_BUF_LEN]; |
---|
| 259 | |
---|
[807] | 260 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 261 | |
---|
[0] | 262 | ROAR_DBG("roar_send_message(*): try to send an request..."); |
---|
| 263 | |
---|
[8] | 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); |
---|
[0] | 269 | |
---|
[1319] | 270 | if ( roar_vio_write(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) { |
---|
[807] | 271 | roar_errno = ROAR_ERROR_PIPE; |
---|
[0] | 272 | return -1; |
---|
[807] | 273 | } |
---|
[0] | 274 | |
---|
[807] | 275 | if ( mes->datalen != 0 ) { |
---|
[1319] | 276 | if ( roar_vio_write(vio, data == NULL ? mes->data : data, mes->datalen) != mes->datalen ) { |
---|
[807] | 277 | roar_errno = ROAR_ERROR_PIPE; |
---|
[0] | 278 | return -1; |
---|
[807] | 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
[1334] | 282 | roar_vio_sync(vio); // we need to do this becasue of ssl/compressed links |
---|
| 283 | |
---|
[807] | 284 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 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) { |
---|
[1319] | 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) { |
---|
[0] | 300 | char buf[_ROAR_MESS_BUF_LEN]; |
---|
| 301 | |
---|
[807] | 302 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 303 | |
---|
[0] | 304 | ROAR_DBG("roar_recv_message(*): try to get a response form the server..."); |
---|
| 305 | |
---|
| 306 | if ( data ) |
---|
| 307 | *data = NULL; |
---|
| 308 | |
---|
[1319] | 309 | if ( roar_vio_read(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) { |
---|
[807] | 310 | roar_errno = ROAR_ERROR_PROTO; |
---|
[0] | 311 | return -1; |
---|
[807] | 312 | } |
---|
[0] | 313 | |
---|
| 314 | ROAR_DBG("roar_recv_message(*): Got a header"); |
---|
| 315 | |
---|
[807] | 316 | if ( buf[0] != _ROAR_MESSAGE_VERSION ) { |
---|
| 317 | roar_errno = ROAR_ERROR_PROTO; |
---|
[8] | 318 | return -1; |
---|
[807] | 319 | } |
---|
[8] | 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)); |
---|
[0] | 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 | |
---|
[104] | 329 | if ( mes->datalen == 0 ) { |
---|
| 330 | ROAR_DBG("roar_recv_message(*): no data in this pkg"); |
---|
| 331 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
[807] | 332 | roar_errno = ROAR_ERROR_NONE; |
---|
[104] | 333 | return 0; |
---|
| 334 | } |
---|
| 335 | |
---|
[0] | 336 | if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) { |
---|
[1319] | 337 | if ( roar_vio_read(vio, mes->data, mes->datalen) == mes->datalen ) { |
---|
[0] | 338 | ROAR_DBG("roar_recv_message(*): Got data!"); |
---|
| 339 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
[807] | 340 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 341 | return 0; |
---|
| 342 | } |
---|
[807] | 343 | |
---|
| 344 | roar_errno = ROAR_ERROR_PIPE; |
---|
[0] | 345 | return -1; |
---|
| 346 | } else { |
---|
[807] | 347 | if ( data == NULL ) { |
---|
| 348 | roar_errno = ROAR_ERROR_MSGSIZE; |
---|
[0] | 349 | return -1; |
---|
[807] | 350 | } |
---|
[0] | 351 | |
---|
[807] | 352 | if ( (*data = malloc(mes->datalen)) == NULL ) { |
---|
| 353 | roar_errno = ROAR_ERROR_NOMEM; |
---|
[0] | 354 | return -1; |
---|
[807] | 355 | } |
---|
[0] | 356 | |
---|
[807] | 357 | if ( mes->datalen == 0 ) { |
---|
| 358 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 359 | return 0; |
---|
[807] | 360 | } |
---|
[0] | 361 | |
---|
[1319] | 362 | if ( roar_vio_read(vio, *data, mes->datalen) == mes->datalen ) { |
---|
[0] | 363 | ROAR_DBG("roar_recv_message(*): Got data!"); |
---|
| 364 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
[807] | 365 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 366 | return 0; |
---|
| 367 | } |
---|
[807] | 368 | |
---|
| 369 | roar_errno = ROAR_ERROR_PIPE; |
---|
[0] | 370 | return -1; |
---|
| 371 | } |
---|
| 372 | |
---|
[807] | 373 | // what happened here? |
---|
[0] | 374 | return -1; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) { |
---|
[1319] | 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 ) |
---|
[0] | 388 | return -1; |
---|
| 389 | |
---|
| 390 | if ( data ) |
---|
| 391 | free(*data); |
---|
| 392 | |
---|
[1319] | 393 | return roar_vrecv_message(vio, mes, data); |
---|
[0] | 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 |
---|