//basic.c: /* * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 * * This file is part of libroar a part of RoarAudio, * a cross-platform sound system for both, home and professional use. * See README for details. * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 * as published by the Free Software Foundation. * * libroar is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this software; see the file COPYING. If not, write to * the Free Software Foundation, 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * * NOTE for everyone want's to change something and send patches: * read README and HACKING! There a addition information on * the license of this document you need to read before you send * any patches. * * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc * or libpulse*: * The libs libroaresd, libroararts and libroarpulse link this lib * and are therefore GPL. Because of this it may be illigal to use * them with any software that uses libesd, libartsc or libpulse*. */ #include "libroar.h" int roar_connect_raw (const char * server, int flags, uint_least32_t timeout) { #ifdef ROAR_HAVE_LIBSLP struct roar_libroar_config * config = roar_libroar_get_config(); #endif char user_sock[128]; char * roar_server; int i = 0; int port = 0; int fh = -1; int is_decnet = 0; const char * obj = NULL; #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) struct passwd * pwd; #endif #ifdef ROAR_HAVE_LIBDNET struct stat decnet_stat; #endif #ifdef ROAR_HAVE_LIBX11 struct roar_x11_connection * x11con; #endif #ifdef ROAR_HAVE_LIBSLP struct roar_server * list; int workarounds_store; #endif #if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT) struct stat sockstat; #endif roar_err_set(ROAR_ERROR_UNKNOWN); if ( timeout != 0 ) { roar_err_set(ROAR_ERROR_INVAL); return -1; } if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK ) flags |= ROAR_ENUM_FLAG_NONBLOCK; if ( server == NULL ) server = roar_libroar_get_server(); if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL ) server = roar_server; #ifdef ROAR_HAVE_LIBX11 if ( server == NULL ) { if ( (x11con = roar_x11_connect(NULL)) != NULL ) { server = roar_x11_get_prop(x11con, "ROAR_SERVER"); roar_x11_disconnect(x11con); } } #endif #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, sizeof(user_sock)-1)) != -1 ) { user_sock[i] = 0; server = user_sock; } #endif if ( server != NULL && !strcasecmp(server, "+slp") ) { server = roar_slp_find_roard(0); if ( server == NULL ) { return -1; } } if ( server == NULL || *server == 0 ) { /* connect via defaults */ #ifdef ROAR_HAVE_UNIX #ifndef ROAR_TARGET_MICROCONTROLLER roar_server = getenv("HOME"); #else roar_server = NULL; #endif if ( roar_server == NULL ) { #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) if ( (pwd = getpwuid(getuid())) == NULL ) { roar_server = "/NX-HOME-DIR"; } else { roar_server = pwd->pw_dir; } #else roar_server = "/WIN32-SUCKS"; #endif } snprintf(user_sock, sizeof(user_sock)-1, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER); user_sock[sizeof(user_sock)-1] = 0; if ( (fh = roar_socket_connect(user_sock, 0)) != -1 ) return fh; if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 ) return fh; #endif if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 ) return fh; #ifdef ROAR_HAVE_LIBDNET if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) { if ( roar_socket_get_local_nodename() ) { snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT); if ( (fh = roar_socket_connect(user_sock, ROAR_DEFAULT_NUM)) != -1 ) return fh; } } #endif if ( (fh = roar_socket_connect("+abstract", 0)) != -1 ) return fh; #ifdef ROAR_HAVE_LIBSLP if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) && !(flags & ROAR_ENUM_FLAG_NONBLOCK) ) { if ( (server = roar_slp_find_roard(0)) != NULL ) { if ( (fh = roar_connect_raw(server, 0, 0)) != -1 ) return fh; /* in case we can not connect to the server given this may be a cache problem, we do a new lookup with the cache disabled in this case */ ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache"); if ( (server = roar_slp_find_roard(1)) != NULL ) if ( (fh = roar_connect_raw(server, 0, 0)) != -1 ) return fh; } } workarounds_store = config->workaround.workarounds; config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP; list = roar_enum_servers(flags, -1, -1); config->workaround.workarounds = workarounds_store; if ( list != NULL ) { for (i = 0; list[i].server != NULL; i++) { if ( (fh = roar_connect_raw((char*)list[i].server, 0, 0)) != -1 ) { roar_enum_servers_free(list); return fh; } } roar_enum_servers_free(list); } #endif return -1; } else { /* connect via (char*)server */ // find a port: if ( !strcmp(server, "+invalid") ) { roar_err_set(ROAR_ERROR_CANCELED); return -1; } strncpy(user_sock, server, sizeof(user_sock)-1); user_sock[sizeof(user_sock)-1] = 0; if ( *user_sock != '/' ) { // don't test AF_UNIX sockets for ports for (i = 0; user_sock[i] != 0; i++) { if ( user_sock[i] == ':' ) { if ( user_sock[i+1] == ':' ) { // DECnet, leave unchanged is_decnet = 1; obj = &user_sock[i+2]; break; } port = atoi(&(user_sock[i+1])); user_sock[i] = 0; break; } } } if ( is_decnet ) { if ( *user_sock == ':' ) { if ( roar_socket_get_local_nodename() != NULL ) { strncpy(user_sock, roar_socket_get_local_nodename(), sizeof(user_sock)-1); user_sock[sizeof(user_sock)-1] = 0; roar_mm_strlcat(user_sock, server, sizeof(user_sock)-1); user_sock[sizeof(user_sock)-1] = 0; obj = strstr(user_sock, "::"); obj += 2; } } if ( *obj == 0 ) { #ifdef DN_MAXOBJL roar_mm_strlcat(user_sock, ROAR_DEFAULT_OBJECT, sizeof(user_sock)-1); user_sock[sizeof(user_sock)-1] = 0; #else ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown."); #endif } ROAR_DBG("roar_connect_raw(*): user_sock='%s'", user_sock); } if ( port || is_decnet ) { fh = roar_socket_connect(user_sock, port); // restore the original string user_sock[i] = ':'; } else { #if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT) if ( user_sock[0] == '/' ) { if ( stat(user_sock, &sockstat) == 0 ) { if ( S_ISCHR(sockstat.st_mode) ) { return open(user_sock, O_RDWR|O_NOCTTY, 0666); } } } #endif fh = roar_socket_connect(user_sock, ROAR_DEFAULT_PORT); } } ROAR_DBG("roar_connect_raw(*) = %i", fh); return fh; } int roar_connect (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) { int fh; int err; if ( con == NULL ) { roar_err_set(ROAR_ERROR_FAULT); return -1; } roar_err_set(ROAR_ERROR_UNKNOWN); fh = roar_connect_raw(server, flags, timeout); if ( fh == -1 ) return -1; if ( roar_connect_fh(con, fh) == -1 ) { err = roar_error; #ifdef ROAR_TARGET_WIN32 closesocket(fh); #else close(fh); #endif roar_error = err; return -1; } if ( server != NULL ) { con->server_name = roar_mm_strdup(server); } return 0; } int roar_connect_fh (struct roar_connection * con, int fh) { if ( con == NULL || fh == -1 ) { roar_err_set(ROAR_ERROR_INVAL); return -1; } // specal hack to set an ilegal value used internaly in libroar: if ( fh == -2 ) fh = -1; memset(con, 0, sizeof(struct roar_connection)); con->refc = 1; con->flags = ROAR_CON_FLAGS_NONE; con->version = 0; con->cb_userdata = NULL; con->cb = NULL; con->server_stds = NULL; con->server_name = NULL; roar_err_init(&(con->errorframe)); con->viocon = &(con->viocon_store); if ( roar_vio_open_fh_socket(con->viocon, fh) != -1 ) { con->flags |= ROAR_CON_FLAGS_VIO; } roar_err_set(ROAR_ERROR_NONE); return 0; } int roar_get_connection_fh (struct roar_connection * con) { int fh; ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con); roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL); if ( con == NULL ) return -1; ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con); if ( roar_vio_ctl(con->viocon, ROAR_VIO_CTL_GET_FH, &fh) == -1 ) return -1; ROAR_DBG("roar_get_connection_fh(con=%p) = %i", con, fh); return fh; } struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) { if ( con == NULL ) return NULL; if ( con->flags & ROAR_CON_FLAGS_VIO ) return con->viocon; // TODO: try to open the VIO. return NULL; } const char * roar_get_connection_server(struct roar_connection * con) { if ( con == NULL ) { roar_err_set(ROAR_ERROR_FAULT); return NULL; } return con->server_name; } int roar_connectionref(struct roar_connection * con) { if ( con == NULL ) { roar_err_set(ROAR_ERROR_FAULT); return -1; } con->refc++; return 0; } int roar_connectionunref(struct roar_connection * con) { struct roar_vio_calls * vio; struct roar_message m; if ( con == NULL ) { roar_err_set(ROAR_ERROR_FAULT); return -1; } con->refc--; if ( con->refc ) return 0; memset(&m, 0, sizeof(m)); m.datalen = 0; m.stream = -1; m.pos = 0; m.cmd = ROAR_CMD_QUIT; roar_req(con, &m, NULL); if ( (vio = roar_get_connection_vio2(con)) != NULL ) { roar_vio_close(vio); } if ( con->server_stds != NULL ) { roar_stds_free(con->server_stds); con->server_stds = NULL; } if ( con->server_name != NULL ) { roar_mm_free(con->server_name); con->server_name = NULL; } if ( con->flags & ROAR_CON_FLAGS_FREESELF ) { roar_mm_free(con); } else { roar_connect_fh(con, -2); } return 0; } int roar_set_connection_callback(struct roar_connection * con, void (*cb)(struct roar_connection * con, struct roar_message * mes, void * data, void * userdata), void * userdata) { if ( con == NULL ) return -1; con->cb = cb; con->cb_userdata = userdata; return 0; } int roar_sync (struct roar_connection * con) { // wait for any non-client reqs return roar_wait_msg(con, 0x0000, 0x8000); } int roar_wait_msg (struct roar_connection * con, int16_t seq, int16_t seqmask) { roar_err_set(ROAR_ERROR_NOSYS); return -1; } int roar_noop (struct roar_connection * con) { struct roar_message mes; if ( con == NULL ) { roar_err_set(ROAR_ERROR_FAULT); return -1; } memset(&mes, 0, sizeof(mes)); mes.cmd = ROAR_CMD_NOOP; mes.stream = -1; return roar_req3(con, &mes, NULL); } int roar_identify (struct roar_connection * con, const char * name) { struct roar_message mes; uint32_t pid; int max_len; roar_err_set(ROAR_ERROR_UNKNOWN); ROAR_DBG("roar_identify(*): try to identify myself..."); memset(&mes, 0, sizeof(mes)); mes.cmd = ROAR_CMD_IDENTIFY; mes.stream = -1; mes.pos = 0; ROAR_DBG("roar_identify(*): name=%p", name); if ( name == NULL ) name = "libroar client"; ROAR_DBG("roar_identify(*): name=%p", name); max_len = roar_mm_strlen(name); ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len); if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) ) max_len = LIBROAR_BUFFER_MSGDATA - 5; mes.datalen = 5 + max_len; mes.data[0] = 1; pid = getpid(); mes.data[1] = (pid & 0xFF000000UL) >> 24; mes.data[2] = (pid & 0x00FF0000UL) >> 16; mes.data[3] = (pid & 0x0000FF00UL) >> 8; mes.data[4] = (pid & 0x000000FFUL) >> 0; ROAR_DBG("roar_identify(*): pid = %i", (int)pid); strncpy(mes.data+5, name, max_len); return roar_req3(con, &mes, NULL); } //ll