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