[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) { |
---|
[807] | 155 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
[0] | 156 | con->fh = roar_connect_raw(server); |
---|
| 157 | |
---|
| 158 | if ( con->fh == -1 ) |
---|
| 159 | return -1; |
---|
| 160 | |
---|
[807] | 161 | roar_errno = ROAR_ERROR_NONE; |
---|
| 162 | |
---|
[0] | 163 | return 0; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | int roar_disconnect (struct roar_connection * con) { |
---|
| 167 | struct roar_message m; |
---|
| 168 | |
---|
| 169 | m.datalen = 0; |
---|
| 170 | m.stream = 0; |
---|
[133] | 171 | m.pos = 0; |
---|
[0] | 172 | m.cmd = ROAR_CMD_QUIT; |
---|
| 173 | |
---|
| 174 | roar_req(con, &m, NULL); |
---|
| 175 | |
---|
| 176 | close(con->fh); |
---|
| 177 | |
---|
| 178 | con->fh = -1; |
---|
| 179 | |
---|
[807] | 180 | roar_errno = ROAR_ERROR_NONE; |
---|
| 181 | |
---|
[0] | 182 | return 0; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | int roar_identify (struct roar_connection * con, char * name) { |
---|
| 186 | struct roar_message mes; |
---|
| 187 | pid_t pid; |
---|
| 188 | int max_len; |
---|
| 189 | |
---|
[807] | 190 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 191 | |
---|
[0] | 192 | ROAR_DBG("roar_identify(*): try to identify myself..."); |
---|
| 193 | |
---|
| 194 | mes.cmd = ROAR_CMD_IDENTIFY; |
---|
| 195 | mes.stream = 0; |
---|
| 196 | mes.pos = 0; |
---|
| 197 | |
---|
| 198 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
| 199 | |
---|
| 200 | if ( name == NULL ) |
---|
| 201 | name = "libroar client"; |
---|
| 202 | |
---|
| 203 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
| 204 | |
---|
| 205 | max_len = strlen(name); |
---|
| 206 | ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len); |
---|
| 207 | |
---|
| 208 | if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) ) |
---|
| 209 | max_len = LIBROAR_BUFFER_MSGDATA - 5; |
---|
| 210 | |
---|
| 211 | mes.datalen = 5 + max_len; |
---|
| 212 | mes.data[0] = 1; |
---|
| 213 | |
---|
| 214 | pid = getpid(); |
---|
| 215 | *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid); |
---|
| 216 | ROAR_DBG("roar_identify(*): pid = %i", pid); |
---|
| 217 | |
---|
| 218 | strncpy(mes.data+5, name, max_len); |
---|
| 219 | |
---|
| 220 | return roar_req(con, &mes, NULL); |
---|
| 221 | } |
---|
| 222 | |
---|
[8] | 223 | #define _ROAR_MESS_BUF_LEN (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */) |
---|
[0] | 224 | int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) { |
---|
| 225 | char buf[_ROAR_MESS_BUF_LEN]; |
---|
| 226 | |
---|
[807] | 227 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 228 | |
---|
[0] | 229 | ROAR_DBG("roar_send_message(*): try to send an request..."); |
---|
| 230 | |
---|
[8] | 231 | buf[0] = _ROAR_MESSAGE_VERSION; |
---|
| 232 | buf[1] = (unsigned char) mes->cmd; |
---|
| 233 | *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream); |
---|
| 234 | *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos); |
---|
| 235 | *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen); |
---|
[0] | 236 | |
---|
[1099] | 237 | if ( ROAR_NETWORK_WRITE(con->fh, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) { |
---|
[807] | 238 | roar_errno = ROAR_ERROR_PIPE; |
---|
[0] | 239 | return -1; |
---|
[807] | 240 | } |
---|
[0] | 241 | |
---|
[807] | 242 | if ( mes->datalen != 0 ) { |
---|
[1099] | 243 | if ( ROAR_NETWORK_WRITE(con->fh, data == NULL ? mes->data : data, mes->datalen) != mes->datalen ) { |
---|
[807] | 244 | roar_errno = ROAR_ERROR_PIPE; |
---|
[0] | 245 | return -1; |
---|
[807] | 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 250 | |
---|
| 251 | ROAR_DBG("roar_send_message(*) = 0"); |
---|
| 252 | return 0; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) { |
---|
| 256 | char buf[_ROAR_MESS_BUF_LEN]; |
---|
| 257 | |
---|
[807] | 258 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 259 | |
---|
[0] | 260 | ROAR_DBG("roar_recv_message(*): try to get a response form the server..."); |
---|
| 261 | |
---|
| 262 | if ( data ) |
---|
| 263 | *data = NULL; |
---|
| 264 | |
---|
[1099] | 265 | if ( ROAR_NETWORK_READ(con->fh, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) { |
---|
[807] | 266 | roar_errno = ROAR_ERROR_PROTO; |
---|
[0] | 267 | return -1; |
---|
[807] | 268 | } |
---|
[0] | 269 | |
---|
| 270 | ROAR_DBG("roar_recv_message(*): Got a header"); |
---|
| 271 | |
---|
[807] | 272 | if ( buf[0] != _ROAR_MESSAGE_VERSION ) { |
---|
| 273 | roar_errno = ROAR_ERROR_PROTO; |
---|
[8] | 274 | return -1; |
---|
[807] | 275 | } |
---|
[8] | 276 | |
---|
| 277 | mes->cmd = (unsigned char)buf[1]; |
---|
| 278 | mes->stream = ROAR_NET2HOST16(*(uint16_t*)(buf+2)); |
---|
| 279 | mes->pos = ROAR_NET2HOST32(*(uint32_t*)(buf+4)); |
---|
| 280 | mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8)); |
---|
[0] | 281 | |
---|
| 282 | ROAR_DBG("roar_recv_message(*): command=%i(%s)", mes->cmd, |
---|
| 283 | mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN")); |
---|
| 284 | |
---|
[104] | 285 | if ( mes->datalen == 0 ) { |
---|
| 286 | ROAR_DBG("roar_recv_message(*): no data in this pkg"); |
---|
| 287 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
[807] | 288 | roar_errno = ROAR_ERROR_NONE; |
---|
[104] | 289 | return 0; |
---|
| 290 | } |
---|
| 291 | |
---|
[0] | 292 | if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) { |
---|
[1099] | 293 | if ( ROAR_NETWORK_READ(con->fh, mes->data, mes->datalen) == mes->datalen ) { |
---|
[0] | 294 | ROAR_DBG("roar_recv_message(*): Got data!"); |
---|
| 295 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
[807] | 296 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 297 | return 0; |
---|
| 298 | } |
---|
[807] | 299 | |
---|
| 300 | roar_errno = ROAR_ERROR_PIPE; |
---|
[0] | 301 | return -1; |
---|
| 302 | } else { |
---|
[807] | 303 | if ( data == NULL ) { |
---|
| 304 | roar_errno = ROAR_ERROR_MSGSIZE; |
---|
[0] | 305 | return -1; |
---|
[807] | 306 | } |
---|
[0] | 307 | |
---|
[807] | 308 | if ( (*data = malloc(mes->datalen)) == NULL ) { |
---|
| 309 | roar_errno = ROAR_ERROR_NOMEM; |
---|
[0] | 310 | return -1; |
---|
[807] | 311 | } |
---|
[0] | 312 | |
---|
[807] | 313 | if ( mes->datalen == 0 ) { |
---|
| 314 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 315 | return 0; |
---|
[807] | 316 | } |
---|
[0] | 317 | |
---|
[1099] | 318 | if ( ROAR_NETWORK_READ(con->fh, *data, mes->datalen) == mes->datalen ) { |
---|
[0] | 319 | ROAR_DBG("roar_recv_message(*): Got data!"); |
---|
| 320 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
[807] | 321 | roar_errno = ROAR_ERROR_NONE; |
---|
[0] | 322 | return 0; |
---|
| 323 | } |
---|
[807] | 324 | |
---|
| 325 | roar_errno = ROAR_ERROR_PIPE; |
---|
[0] | 326 | return -1; |
---|
| 327 | } |
---|
| 328 | |
---|
[807] | 329 | // what happened here? |
---|
[0] | 330 | return -1; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) { |
---|
| 334 | if ( roar_send_message(con, mes, data ? *data : NULL) != 0 ) |
---|
| 335 | return -1; |
---|
| 336 | |
---|
| 337 | if ( data ) |
---|
| 338 | free(*data); |
---|
| 339 | |
---|
| 340 | return roar_recv_message(con, mes, data); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | int roar_debug_message_print (struct roar_message * mes) { |
---|
| 344 | if ( mes == NULL ) |
---|
| 345 | return -1; |
---|
| 346 | |
---|
| 347 | ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd); |
---|
| 348 | ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream); |
---|
| 349 | ROAR_DBG("roar_debug_message_print(*): Pos : %u", mes->pos); |
---|
| 350 | ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen); |
---|
| 351 | |
---|
| 352 | ROAR_DBG("roar_debug_message_print(*) = 0"); |
---|
| 353 | return 0; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | int roar_debug_audio_info_print (struct roar_audio_info * info) { |
---|
| 357 | if ( info == NULL ) |
---|
| 358 | return -1; |
---|
| 359 | |
---|
| 360 | ROAR_DBG("roar_debug_audio_info_print(*): Rate : %i", info->rate); |
---|
| 361 | ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels); |
---|
| 362 | ROAR_DBG("roar_debug_audio_info_print(*): Bits : %i", info->bits); |
---|
| 363 | ROAR_DBG("roar_debug_audio_info_print(*): Codec : %i", info->codec); |
---|
| 364 | |
---|
| 365 | ROAR_DBG("roar_debug_audio_info_print(*) = 0"); |
---|
| 366 | return 0; |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | //ll |
---|