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