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