[0] | 1 | //basic.c: |
---|
| 2 | |
---|
[690] | 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
[690] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[690] | 23 | * |
---|
| 24 | * NOTE for everyone want's to change something and send patches: |
---|
| 25 | * read README and HACKING! There a addition information on |
---|
| 26 | * the license of this document you need to read before you send |
---|
| 27 | * any patches. |
---|
| 28 | * |
---|
| 29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 30 | * or libpulse*: |
---|
| 31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 34 | */ |
---|
| 35 | |
---|
[0] | 36 | #include "libroar.h" |
---|
| 37 | |
---|
[5369] | 38 | static int _start_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { |
---|
| 39 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
| 40 | enum { |
---|
| 41 | NORMAL = 0, |
---|
| 42 | SYSTEM = 1 |
---|
| 43 | } mode = NORMAL; |
---|
| 44 | const char * daemonimage = NULL; |
---|
| 45 | int socks[2]; |
---|
| 46 | int r; |
---|
| 47 | char fhstr[12]; |
---|
| 48 | |
---|
| 49 | if ( !strncmp(server, "+fork=", 6) ) { |
---|
| 50 | server += 6; |
---|
| 51 | if ( server[0] == 0 ) { |
---|
| 52 | // no special case, we just ignore it. |
---|
| 53 | } else if ( server[0] == 'd' && server[1] == ':' ) { |
---|
| 54 | server += 2; |
---|
| 55 | daemonimage = server; |
---|
| 56 | } else if ( server[0] == '!' ) { |
---|
| 57 | server += 1; |
---|
| 58 | daemonimage = server; |
---|
| 59 | mode = SYSTEM; |
---|
| 60 | } else { |
---|
| 61 | roar_err_set(ROAR_ERROR_ILLSEQ); |
---|
| 62 | return -1; |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | if ( daemonimage == NULL ) |
---|
[5373] | 67 | daemonimage = roar_libroar_get_config()->daemonimage; |
---|
| 68 | |
---|
| 69 | // we keep this step to be compatible with older versions. |
---|
| 70 | if ( daemonimage == NULL || *daemonimage == 0 ) { |
---|
[5369] | 71 | daemonimage = getenv("ROAR_DAEMONIMAGE"); |
---|
[5373] | 72 | if ( daemonimage != NULL ) { |
---|
| 73 | ROAR_WARN("_start_server(*): Usage of $ROAR_DAEMONIMAGE is obsolete. Use ROAR_OPTIONS=daemonimage:..."); |
---|
| 74 | } |
---|
| 75 | } |
---|
[5369] | 76 | |
---|
| 77 | if ( daemonimage == NULL || *daemonimage == 0 ) |
---|
| 78 | daemonimage = "roard"; |
---|
| 79 | |
---|
| 80 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
| 81 | roar_err_from_errno(); |
---|
| 82 | return -1; |
---|
| 83 | } |
---|
| 84 | |
---|
[5373] | 85 | r = roar_fork(NULL); |
---|
[5369] | 86 | |
---|
| 87 | if ( r == -1 ) { // error! |
---|
[5373] | 88 | ROAR_ERR("_start_server(*): Can not fork: %s", roar_error2str(roar_error)); |
---|
[5369] | 89 | close(socks[0]); |
---|
| 90 | close(socks[1]); |
---|
| 91 | return -1; |
---|
| 92 | } else if ( r == 0 ) { // we are the child |
---|
| 93 | close(socks[0]); |
---|
| 94 | |
---|
| 95 | close(ROAR_STDIN ); // we do not want roard to have any standard input |
---|
| 96 | close(ROAR_STDOUT); // STDOUT is also not needed, so we close it, |
---|
| 97 | // but STDERR we keep open for error messages. |
---|
| 98 | |
---|
| 99 | snprintf(fhstr, sizeof(fhstr), "%i", socks[1]); |
---|
| 100 | |
---|
| 101 | switch (mode) { |
---|
| 102 | case NORMAL: |
---|
| 103 | execlp(daemonimage, daemonimage, "--no-listen", "--client-fh", fhstr, (_LIBROAR_GOOD_CAST char*)NULL); |
---|
| 104 | break; |
---|
| 105 | case SYSTEM: |
---|
| 106 | dup2(socks[1], ROAR_STDIN ); |
---|
| 107 | dup2(socks[1], ROAR_STDOUT); |
---|
| 108 | execl("/bin/sh", "/bin/sh", "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL); |
---|
| 109 | execlp("sh", "sh", "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL); |
---|
| 110 | break; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | // we are still alive? |
---|
[5373] | 114 | ROAR_ERR("_start_server(*): alive after exec(), that's bad!"); |
---|
[5369] | 115 | _exit(1); |
---|
| 116 | } else { // we are the parent |
---|
| 117 | close(socks[1]); |
---|
| 118 | if ( roar_vio_open_fh_socket(con->viocon, socks[0]) == -1 ) { |
---|
| 119 | close(socks[0]); |
---|
| 120 | return -1; |
---|
| 121 | } else { |
---|
| 122 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
| 123 | } |
---|
| 124 | return 0; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | return -1; |
---|
| 128 | #else |
---|
[5373] | 129 | ROAR_ERR("_start_server(*): There is no UNIX Domain Socket support in win32, download a real OS."); |
---|
[5369] | 130 | return -1; |
---|
| 131 | #endif |
---|
| 132 | } |
---|
| 133 | |
---|
[5373] | 134 | static int _connect_internal(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { |
---|
| 135 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
| 136 | struct roar_vio_calls * vio; |
---|
| 137 | |
---|
| 138 | if ( config->connect_internal != NULL ) { |
---|
| 139 | vio = config->connect_internal(con, server, type, flags, timeout); |
---|
| 140 | if ( vio == NULL ) |
---|
| 141 | return -1; |
---|
| 142 | |
---|
| 143 | con->viocon = vio; |
---|
| 144 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
| 145 | |
---|
| 146 | return 0; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | roar_err_set(ROAR_ERROR_BADHOST); |
---|
| 150 | return -1; |
---|
| 151 | } |
---|
| 152 | |
---|
[5368] | 153 | static int _connect_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { |
---|
| 154 | #if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT) |
---|
| 155 | struct stat sockstat; |
---|
| 156 | #endif |
---|
| 157 | const char * obj = NULL; |
---|
| 158 | char user_sock[128]; |
---|
| 159 | int is_decnet = 0; |
---|
| 160 | int port = 0; |
---|
| 161 | int i = 0; |
---|
| 162 | int fh = -1; |
---|
| 163 | int err; |
---|
| 164 | |
---|
| 165 | if ( con == NULL || server == NULL ) { |
---|
| 166 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 167 | return -1; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | if ( !strcmp(server, "+invalid") ) { |
---|
| 171 | roar_err_set(ROAR_ERROR_CANCELED); |
---|
| 172 | return -1; |
---|
| 173 | } else if ( !strncmp(server, "+dstr=", 6) ) { |
---|
| 174 | if ( roar_vio_open_dstr_simple(con->viocon, server+6, ROAR_VIOF_READWRITE) == -1 ) |
---|
| 175 | return -1; |
---|
| 176 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
| 177 | return 0; |
---|
[5369] | 178 | } else if ( !strcmp(server, "+fork") || !strncmp(server, "+fork=", 6) ) { |
---|
| 179 | return _start_server(con, server, type, flags, timeout); |
---|
[5373] | 180 | } else if ( !strcmp(server, "+internal") || !strncmp(server, "+internal=", 10) ) { |
---|
| 181 | return _connect_internal(con, server, type, flags, timeout); |
---|
[5368] | 182 | } |
---|
| 183 | |
---|
| 184 | strncpy(user_sock, server, sizeof(user_sock)-1); |
---|
| 185 | user_sock[sizeof(user_sock)-1] = 0; |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | if ( *user_sock != '/' ) { // don't test AF_UNIX sockets for ports |
---|
| 189 | for (i = 0; user_sock[i] != 0; i++) { |
---|
| 190 | if ( user_sock[i] == ':' ) { |
---|
| 191 | if ( user_sock[i+1] == ':' ) { // DECnet, leave unchanged |
---|
| 192 | is_decnet = 1; |
---|
| 193 | obj = &user_sock[i+2]; |
---|
| 194 | break; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | port = atoi(&(user_sock[i+1])); |
---|
| 198 | user_sock[i] = 0; |
---|
| 199 | break; |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | if ( is_decnet ) { |
---|
| 205 | if ( *user_sock == ':' ) { |
---|
| 206 | if ( roar_socket_get_local_nodename() != NULL ) { |
---|
| 207 | strncpy(user_sock, roar_socket_get_local_nodename(), sizeof(user_sock)-1); |
---|
| 208 | user_sock[sizeof(user_sock)-1] = 0; |
---|
| 209 | roar_mm_strlcat(user_sock, server, sizeof(user_sock)-1); |
---|
| 210 | user_sock[sizeof(user_sock)-1] = 0; |
---|
| 211 | obj = strstr(user_sock, "::"); |
---|
| 212 | obj += 2; |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | if ( *obj == 0 ) { |
---|
| 217 | #ifdef DN_MAXOBJL |
---|
| 218 | roar_mm_strlcat(user_sock, ROAR_DEFAULT_OBJECT, sizeof(user_sock)-1); |
---|
| 219 | user_sock[sizeof(user_sock)-1] = 0; |
---|
| 220 | #else |
---|
| 221 | ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown."); |
---|
| 222 | #endif |
---|
| 223 | } |
---|
| 224 | ROAR_DBG("roar_connect_raw(*): user_sock='%s'", user_sock); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | if ( port || is_decnet ) { |
---|
[5375] | 228 | fh = roar_socket_connect(type, user_sock, port); |
---|
[5368] | 229 | // restore the original string |
---|
| 230 | user_sock[i] = ':'; |
---|
| 231 | } else { |
---|
| 232 | #if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT) |
---|
| 233 | if ( user_sock[0] == '/' ) { |
---|
| 234 | if ( stat(user_sock, &sockstat) == 0 ) { |
---|
| 235 | if ( S_ISCHR(sockstat.st_mode) ) { |
---|
| 236 | return open(user_sock, O_RDWR|O_NOCTTY, 0666); |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | #endif |
---|
[5375] | 241 | fh = roar_socket_connect(type, user_sock, ROAR_DEFAULT_PORT); |
---|
[5368] | 242 | } |
---|
| 243 | |
---|
| 244 | if ( fh == -1 ) |
---|
| 245 | return -1; |
---|
| 246 | |
---|
| 247 | if ( roar_vio_open_fh_socket(con->viocon, fh) == -1 ) { |
---|
| 248 | err = roar_error; |
---|
| 249 | #ifdef ROAR_TARGET_WIN32 |
---|
| 250 | closesocket(fh); |
---|
| 251 | #else |
---|
| 252 | close(fh); |
---|
| 253 | #endif |
---|
| 254 | roar_error = err; |
---|
| 255 | return -1; |
---|
| 256 | } else { |
---|
| 257 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | roar_err_set(ROAR_ERROR_NONE); |
---|
| 261 | return 0; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | static int roar_connect_raw (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) { |
---|
[4780] | 265 | #ifdef ROAR_HAVE_LIBSLP |
---|
[4653] | 266 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
[4780] | 267 | #endif |
---|
[5114] | 268 | char user_sock[128]; |
---|
[0] | 269 | char * roar_server; |
---|
[701] | 270 | int i = 0; |
---|
[1475] | 271 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[1026] | 272 | struct passwd * pwd; |
---|
[1393] | 273 | #endif |
---|
[828] | 274 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 275 | struct stat decnet_stat; |
---|
| 276 | #endif |
---|
[3372] | 277 | #ifdef ROAR_HAVE_LIBX11 |
---|
| 278 | struct roar_x11_connection * x11con; |
---|
| 279 | #endif |
---|
[4945] | 280 | #ifdef ROAR_HAVE_LIBSLP |
---|
[4806] | 281 | struct roar_server * list; |
---|
| 282 | int workarounds_store; |
---|
[4945] | 283 | #endif |
---|
[0] | 284 | |
---|
[4873] | 285 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
[807] | 286 | |
---|
[4806] | 287 | if ( timeout != 0 ) { |
---|
[4873] | 288 | roar_err_set(ROAR_ERROR_INVAL); |
---|
[4806] | 289 | return -1; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK ) |
---|
| 293 | flags |= ROAR_ENUM_FLAG_NONBLOCK; |
---|
| 294 | |
---|
| 295 | |
---|
[5368] | 296 | if ( server == NULL || *server == 0 ) |
---|
[2567] | 297 | server = roar_libroar_get_server(); |
---|
| 298 | |
---|
[5368] | 299 | if ( server == NULL || *server == 0 ) |
---|
| 300 | server = getenv("ROAR_SERVER"); |
---|
[0] | 301 | |
---|
[3372] | 302 | #ifdef ROAR_HAVE_LIBX11 |
---|
[5368] | 303 | if ( server == NULL || *server == 0 ) { |
---|
[3372] | 304 | if ( (x11con = roar_x11_connect(NULL)) != NULL ) { |
---|
| 305 | server = roar_x11_get_prop(x11con, "ROAR_SERVER"); |
---|
| 306 | roar_x11_disconnect(x11con); |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | #endif |
---|
| 310 | |
---|
[1436] | 311 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[5368] | 312 | if ( (server == NULL || *server == 0) && (i = readlink("/etc/roarserver", user_sock, sizeof(user_sock)-1)) != -1 ) { |
---|
[448] | 313 | user_sock[i] = 0; |
---|
| 314 | server = user_sock; |
---|
| 315 | } |
---|
[1093] | 316 | #endif |
---|
[448] | 317 | |
---|
[3077] | 318 | if ( server != NULL && !strcasecmp(server, "+slp") ) { |
---|
[3076] | 319 | server = roar_slp_find_roard(0); |
---|
| 320 | if ( server == NULL ) { |
---|
| 321 | return -1; |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | |
---|
[61] | 325 | if ( server == NULL || *server == 0 ) { |
---|
[0] | 326 | /* connect via defaults */ |
---|
| 327 | |
---|
[1764] | 328 | #ifdef ROAR_HAVE_UNIX |
---|
[1436] | 329 | #ifndef ROAR_TARGET_MICROCONTROLLER |
---|
[1026] | 330 | roar_server = getenv("HOME"); |
---|
[1435] | 331 | #else |
---|
| 332 | roar_server = NULL; |
---|
| 333 | #endif |
---|
[1026] | 334 | |
---|
| 335 | if ( roar_server == NULL ) { |
---|
[1436] | 336 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[1026] | 337 | if ( (pwd = getpwuid(getuid())) == NULL ) { |
---|
| 338 | roar_server = "/NX-HOME-DIR"; |
---|
| 339 | } else { |
---|
| 340 | roar_server = pwd->pw_dir; |
---|
| 341 | } |
---|
[1078] | 342 | #else |
---|
| 343 | roar_server = "/WIN32-SUCKS"; |
---|
| 344 | #endif |
---|
[1026] | 345 | } |
---|
| 346 | |
---|
[5114] | 347 | snprintf(user_sock, sizeof(user_sock)-1, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER); |
---|
| 348 | user_sock[sizeof(user_sock)-1] = 0; |
---|
[0] | 349 | |
---|
[5368] | 350 | if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 ) |
---|
| 351 | return 0; |
---|
[0] | 352 | |
---|
[5368] | 353 | if ( _connect_server(con, ROAR_DEFAULT_SOCK_GLOBAL, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 ) |
---|
| 354 | return 0; |
---|
[1764] | 355 | #endif |
---|
[237] | 356 | |
---|
[5368] | 357 | if ( _connect_server(con, ROAR_DEFAULT_HOSTPORT, ROAR_SOCKET_TYPE_TCP, flags, timeout) == 0 ) |
---|
| 358 | return 0; |
---|
[0] | 359 | |
---|
[828] | 360 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 361 | if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) { |
---|
| 362 | if ( roar_socket_get_local_nodename() ) { |
---|
| 363 | snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT); |
---|
[5368] | 364 | if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_DECNET, flags, timeout) == 0 ) |
---|
| 365 | return 0; |
---|
[828] | 366 | } |
---|
[522] | 367 | } |
---|
[828] | 368 | #endif |
---|
[2007] | 369 | |
---|
[5375] | 370 | if ( _connect_server(con, "+abstract", ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) == 0 ) |
---|
[5368] | 371 | return 0; |
---|
[4109] | 372 | |
---|
[2007] | 373 | #ifdef ROAR_HAVE_LIBSLP |
---|
[4806] | 374 | if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) && |
---|
[5373] | 375 | !(flags & ROAR_ENUM_FLAG_NONBLOCK) && |
---|
| 376 | !(flags & ROAR_ENUM_FLAG_LOCALONLY) |
---|
[4806] | 377 | ) { |
---|
| 378 | if ( (server = roar_slp_find_roard(0)) != NULL ) { |
---|
[5375] | 379 | if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) |
---|
[5368] | 380 | return 0; |
---|
[2014] | 381 | |
---|
[4806] | 382 | /* in case we can not connect to the server given this may be a cache problem, |
---|
| 383 | we do a new lookup with the cache disabled in this case */ |
---|
[5237] | 384 | ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache"); |
---|
[4806] | 385 | if ( (server = roar_slp_find_roard(1)) != NULL ) |
---|
[5375] | 386 | if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) |
---|
[5368] | 387 | return 0; |
---|
[4806] | 388 | } |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | workarounds_store = config->workaround.workarounds; |
---|
| 392 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP; |
---|
| 393 | list = roar_enum_servers(flags, -1, -1); |
---|
| 394 | config->workaround.workarounds = workarounds_store; |
---|
| 395 | if ( list != NULL ) { |
---|
| 396 | for (i = 0; list[i].server != NULL; i++) { |
---|
[5375] | 397 | if ( _connect_server(con, list[i].server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) { |
---|
[4806] | 398 | roar_enum_servers_free(list); |
---|
[5368] | 399 | return 0; |
---|
[4806] | 400 | } |
---|
| 401 | } |
---|
| 402 | roar_enum_servers_free(list); |
---|
[4653] | 403 | } |
---|
[2007] | 404 | #endif |
---|
[522] | 405 | |
---|
[2014] | 406 | return -1; |
---|
| 407 | |
---|
[0] | 408 | } else { |
---|
| 409 | /* connect via (char*)server */ |
---|
[5375] | 410 | if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) != 0 ) |
---|
[5369] | 411 | return -1; |
---|
| 412 | return 0; |
---|
[0] | 413 | } |
---|
| 414 | |
---|
[5368] | 415 | roar_err_set(ROAR_ERROR_NODEV); |
---|
| 416 | ROAR_DBG("roar_connect_raw(*) = -1 // error=NODEV"); |
---|
| 417 | return -1; |
---|
[0] | 418 | } |
---|
| 419 | |
---|
[5237] | 420 | int roar_connect (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) { |
---|
[5373] | 421 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
| 422 | |
---|
[1315] | 423 | if ( con == NULL ) { |
---|
[4873] | 424 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[1315] | 425 | return -1; |
---|
| 426 | } |
---|
| 427 | |
---|
[5368] | 428 | if ( roar_connect_none(con) == -1 ) |
---|
[0] | 429 | return -1; |
---|
| 430 | |
---|
[5368] | 431 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
[5373] | 432 | if ( roar_connect_raw(con, server, flags | config->serverflags, timeout) == -1 ) |
---|
[5296] | 433 | return -1; |
---|
| 434 | |
---|
[5373] | 435 | // TODO: this should be moved to be within _connect_server() so it also stores server names of auto located servers. |
---|
[5296] | 436 | if ( server != NULL ) { |
---|
| 437 | con->server_name = roar_mm_strdup(server); |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | return 0; |
---|
[1315] | 441 | } |
---|
| 442 | |
---|
[5365] | 443 | int roar_connect_none (struct roar_connection * con) { |
---|
| 444 | if ( con == NULL ) { |
---|
[4873] | 445 | roar_err_set(ROAR_ERROR_INVAL); |
---|
[1315] | 446 | return -1; |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | memset(con, 0, sizeof(struct roar_connection)); |
---|
[5296] | 450 | con->refc = 1; |
---|
[5232] | 451 | con->flags = ROAR_CON_FLAGS_NONE; |
---|
| 452 | con->version = 0; |
---|
| 453 | con->cb_userdata = NULL; |
---|
| 454 | con->cb = NULL; |
---|
[5296] | 455 | con->server_stds = NULL; |
---|
| 456 | con->server_name = NULL; |
---|
[5232] | 457 | |
---|
| 458 | roar_err_init(&(con->errorframe)); |
---|
[1315] | 459 | |
---|
[5296] | 460 | con->viocon = &(con->viocon_store); |
---|
| 461 | |
---|
[5365] | 462 | // con->flags |= ROAR_CON_FLAGS_VIO; |
---|
| 463 | |
---|
| 464 | roar_err_set(ROAR_ERROR_NONE); |
---|
| 465 | return 0; |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | int roar_connect_vio (struct roar_connection * con, struct roar_vio_calls * vio) { |
---|
| 469 | if ( con == NULL || vio == NULL ) { |
---|
| 470 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 471 | return -1; |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | if ( roar_connect_none(con) == -1 ) |
---|
| 475 | return -1; |
---|
| 476 | |
---|
| 477 | con->viocon = vio; |
---|
| 478 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
| 479 | |
---|
| 480 | return -1; |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | int roar_connect_fh (struct roar_connection * con, int fh) { |
---|
| 484 | |
---|
| 485 | if ( con == NULL || fh == -1 ) { |
---|
| 486 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 487 | return -1; |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | if ( roar_connect_none(con) == -1 ) |
---|
| 491 | return -1; |
---|
| 492 | |
---|
| 493 | // specal hack to set an ilegal value used internaly in libroar: |
---|
| 494 | if ( fh == -2 ) |
---|
| 495 | fh = -1; |
---|
| 496 | |
---|
[5296] | 497 | if ( roar_vio_open_fh_socket(con->viocon, fh) != -1 ) { |
---|
[3869] | 498 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
[5231] | 499 | } |
---|
[1315] | 500 | |
---|
[4873] | 501 | roar_err_set(ROAR_ERROR_NONE); |
---|
[0] | 502 | return 0; |
---|
| 503 | } |
---|
| 504 | |
---|
[1660] | 505 | int roar_get_connection_fh (struct roar_connection * con) { |
---|
[5231] | 506 | int fh; |
---|
| 507 | |
---|
| 508 | ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con); |
---|
| 509 | |
---|
[3869] | 510 | roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL); |
---|
[2809] | 511 | |
---|
[2043] | 512 | if ( con == NULL ) |
---|
| 513 | return -1; |
---|
| 514 | |
---|
[5231] | 515 | ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con); |
---|
| 516 | |
---|
[5296] | 517 | if ( roar_vio_ctl(con->viocon, ROAR_VIO_CTL_GET_FH, &fh) == -1 ) |
---|
[3869] | 518 | return -1; |
---|
| 519 | |
---|
[5231] | 520 | ROAR_DBG("roar_get_connection_fh(con=%p) = %i", con, fh); |
---|
[3869] | 521 | |
---|
[5231] | 522 | return fh; |
---|
[2043] | 523 | } |
---|
| 524 | |
---|
[3869] | 525 | struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) { |
---|
| 526 | if ( con == NULL ) |
---|
| 527 | return NULL; |
---|
| 528 | |
---|
| 529 | if ( con->flags & ROAR_CON_FLAGS_VIO ) |
---|
[5296] | 530 | return con->viocon; |
---|
[3869] | 531 | |
---|
| 532 | // TODO: try to open the VIO. |
---|
| 533 | |
---|
| 534 | return NULL; |
---|
| 535 | } |
---|
| 536 | |
---|
[5296] | 537 | const char * roar_get_connection_server(struct roar_connection * con) { |
---|
| 538 | if ( con == NULL ) { |
---|
| 539 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 540 | return NULL; |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | return con->server_name; |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | int roar_connectionref(struct roar_connection * con) { |
---|
| 547 | if ( con == NULL ) { |
---|
| 548 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 549 | return -1; |
---|
| 550 | } |
---|
| 551 | |
---|
| 552 | con->refc++; |
---|
| 553 | |
---|
| 554 | return 0; |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | int roar_connectionunref(struct roar_connection * con) { |
---|
[3869] | 558 | struct roar_vio_calls * vio; |
---|
[0] | 559 | struct roar_message m; |
---|
| 560 | |
---|
[5296] | 561 | if ( con == NULL ) { |
---|
| 562 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 563 | return -1; |
---|
| 564 | } |
---|
| 565 | |
---|
| 566 | con->refc--; |
---|
| 567 | |
---|
| 568 | if ( con->refc ) |
---|
| 569 | return 0; |
---|
| 570 | |
---|
[3875] | 571 | memset(&m, 0, sizeof(m)); |
---|
| 572 | |
---|
[5144] | 573 | m.datalen = 0; |
---|
| 574 | m.stream = -1; |
---|
| 575 | m.pos = 0; |
---|
[0] | 576 | m.cmd = ROAR_CMD_QUIT; |
---|
| 577 | |
---|
| 578 | roar_req(con, &m, NULL); |
---|
| 579 | |
---|
[3869] | 580 | if ( (vio = roar_get_connection_vio2(con)) != NULL ) { |
---|
| 581 | roar_vio_close(vio); |
---|
[2809] | 582 | } |
---|
[0] | 583 | |
---|
[5296] | 584 | if ( con->server_stds != NULL ) { |
---|
| 585 | roar_stds_free(con->server_stds); |
---|
| 586 | con->server_stds = NULL; |
---|
| 587 | } |
---|
[0] | 588 | |
---|
[5296] | 589 | if ( con->server_name != NULL ) { |
---|
| 590 | roar_mm_free(con->server_name); |
---|
| 591 | con->server_name = NULL; |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | if ( con->flags & ROAR_CON_FLAGS_FREESELF ) { |
---|
| 595 | roar_mm_free(con); |
---|
| 596 | } else { |
---|
| 597 | roar_connect_fh(con, -2); |
---|
| 598 | } |
---|
[807] | 599 | |
---|
[0] | 600 | return 0; |
---|
| 601 | } |
---|
| 602 | |
---|
[3913] | 603 | int roar_set_connection_callback(struct roar_connection * con, |
---|
| 604 | void (*cb)(struct roar_connection * con, |
---|
| 605 | struct roar_message * mes, |
---|
[5231] | 606 | void * data, |
---|
[3913] | 607 | void * userdata), |
---|
| 608 | void * userdata) { |
---|
| 609 | if ( con == NULL ) |
---|
| 610 | return -1; |
---|
| 611 | |
---|
| 612 | con->cb = cb; |
---|
[5231] | 613 | con->cb_userdata = userdata; |
---|
[3913] | 614 | |
---|
| 615 | return 0; |
---|
| 616 | } |
---|
| 617 | |
---|
[3914] | 618 | int roar_sync (struct roar_connection * con) { |
---|
| 619 | // wait for any non-client reqs |
---|
| 620 | return roar_wait_msg(con, 0x0000, 0x8000); |
---|
| 621 | } |
---|
| 622 | |
---|
| 623 | int roar_wait_msg (struct roar_connection * con, int16_t seq, int16_t seqmask) { |
---|
[5148] | 624 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
[3914] | 625 | return -1; |
---|
| 626 | } |
---|
[3913] | 627 | |
---|
[3836] | 628 | int roar_noop (struct roar_connection * con) { |
---|
| 629 | struct roar_message mes; |
---|
| 630 | |
---|
| 631 | if ( con == NULL ) { |
---|
[5148] | 632 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[3836] | 633 | return -1; |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | memset(&mes, 0, sizeof(mes)); |
---|
| 637 | |
---|
| 638 | mes.cmd = ROAR_CMD_NOOP; |
---|
[5144] | 639 | mes.stream = -1; |
---|
[3836] | 640 | |
---|
[5146] | 641 | return roar_req3(con, &mes, NULL); |
---|
[3836] | 642 | } |
---|
| 643 | |
---|
[5114] | 644 | int roar_identify (struct roar_connection * con, const char * name) { |
---|
[0] | 645 | struct roar_message mes; |
---|
[5132] | 646 | uint32_t pid; |
---|
[0] | 647 | int max_len; |
---|
| 648 | |
---|
[4873] | 649 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
[807] | 650 | |
---|
[0] | 651 | ROAR_DBG("roar_identify(*): try to identify myself..."); |
---|
| 652 | |
---|
[3875] | 653 | memset(&mes, 0, sizeof(mes)); |
---|
| 654 | |
---|
[0] | 655 | mes.cmd = ROAR_CMD_IDENTIFY; |
---|
[5144] | 656 | mes.stream = -1; |
---|
| 657 | mes.pos = 0; |
---|
[0] | 658 | |
---|
| 659 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
| 660 | |
---|
| 661 | if ( name == NULL ) |
---|
| 662 | name = "libroar client"; |
---|
| 663 | |
---|
| 664 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
| 665 | |
---|
[5114] | 666 | max_len = roar_mm_strlen(name); |
---|
[0] | 667 | ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len); |
---|
| 668 | |
---|
| 669 | if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) ) |
---|
| 670 | max_len = LIBROAR_BUFFER_MSGDATA - 5; |
---|
| 671 | |
---|
| 672 | mes.datalen = 5 + max_len; |
---|
| 673 | mes.data[0] = 1; |
---|
| 674 | |
---|
| 675 | pid = getpid(); |
---|
[5132] | 676 | mes.data[1] = (pid & 0xFF000000UL) >> 24; |
---|
| 677 | mes.data[2] = (pid & 0x00FF0000UL) >> 16; |
---|
| 678 | mes.data[3] = (pid & 0x0000FF00UL) >> 8; |
---|
| 679 | mes.data[4] = (pid & 0x000000FFUL) >> 0; |
---|
| 680 | ROAR_DBG("roar_identify(*): pid = %i", (int)pid); |
---|
[0] | 681 | |
---|
| 682 | strncpy(mes.data+5, name, max_len); |
---|
| 683 | |
---|
[5146] | 684 | return roar_req3(con, &mes, NULL); |
---|
[0] | 685 | } |
---|
| 686 | |
---|
| 687 | //ll |
---|