[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) ) { |
---|
[5406] | 236 | #ifdef O_NOCTTY |
---|
[5428] | 237 | fh = open(user_sock, O_RDWR|O_NOCTTY, 0666); |
---|
[5406] | 238 | #else |
---|
[5428] | 239 | fh = open(user_sock, O_RDWR, 0666); |
---|
[5406] | 240 | #endif |
---|
[5368] | 241 | } |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | #endif |
---|
[5428] | 245 | if ( fh == -1 ) |
---|
| 246 | fh = roar_socket_connect(type, user_sock, ROAR_DEFAULT_PORT); |
---|
[5368] | 247 | } |
---|
| 248 | |
---|
| 249 | if ( fh == -1 ) |
---|
| 250 | return -1; |
---|
| 251 | |
---|
| 252 | if ( roar_vio_open_fh_socket(con->viocon, fh) == -1 ) { |
---|
| 253 | err = roar_error; |
---|
| 254 | #ifdef ROAR_TARGET_WIN32 |
---|
| 255 | closesocket(fh); |
---|
| 256 | #else |
---|
| 257 | close(fh); |
---|
| 258 | #endif |
---|
| 259 | roar_error = err; |
---|
| 260 | return -1; |
---|
| 261 | } else { |
---|
| 262 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | roar_err_set(ROAR_ERROR_NONE); |
---|
| 266 | return 0; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | static int roar_connect_raw (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) { |
---|
[4780] | 270 | #ifdef ROAR_HAVE_LIBSLP |
---|
[4653] | 271 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
[4780] | 272 | #endif |
---|
[5393] | 273 | #if defined(ROAR_HAVE_UNIX) || !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[5114] | 274 | char user_sock[128]; |
---|
[0] | 275 | char * roar_server; |
---|
[5393] | 276 | #endif |
---|
| 277 | #if defined(ROAR_HAVE_LIBSLP) || !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[701] | 278 | int i = 0; |
---|
[5393] | 279 | #endif |
---|
[1475] | 280 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[1026] | 281 | struct passwd * pwd; |
---|
[1393] | 282 | #endif |
---|
[828] | 283 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 284 | struct stat decnet_stat; |
---|
| 285 | #endif |
---|
[3372] | 286 | #ifdef ROAR_HAVE_LIBX11 |
---|
| 287 | struct roar_x11_connection * x11con; |
---|
| 288 | #endif |
---|
[4945] | 289 | #ifdef ROAR_HAVE_LIBSLP |
---|
[4806] | 290 | struct roar_server * list; |
---|
| 291 | int workarounds_store; |
---|
[4945] | 292 | #endif |
---|
[0] | 293 | |
---|
[4873] | 294 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
[807] | 295 | |
---|
[4806] | 296 | if ( timeout != 0 ) { |
---|
[4873] | 297 | roar_err_set(ROAR_ERROR_INVAL); |
---|
[4806] | 298 | return -1; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK ) |
---|
| 302 | flags |= ROAR_ENUM_FLAG_NONBLOCK; |
---|
| 303 | |
---|
| 304 | |
---|
[5368] | 305 | if ( server == NULL || *server == 0 ) |
---|
[2567] | 306 | server = roar_libroar_get_server(); |
---|
| 307 | |
---|
[5368] | 308 | if ( server == NULL || *server == 0 ) |
---|
| 309 | server = getenv("ROAR_SERVER"); |
---|
[0] | 310 | |
---|
[3372] | 311 | #ifdef ROAR_HAVE_LIBX11 |
---|
[5368] | 312 | if ( server == NULL || *server == 0 ) { |
---|
[3372] | 313 | if ( (x11con = roar_x11_connect(NULL)) != NULL ) { |
---|
| 314 | server = roar_x11_get_prop(x11con, "ROAR_SERVER"); |
---|
| 315 | roar_x11_disconnect(x11con); |
---|
| 316 | } |
---|
| 317 | } |
---|
| 318 | #endif |
---|
| 319 | |
---|
[1436] | 320 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[5368] | 321 | if ( (server == NULL || *server == 0) && (i = readlink("/etc/roarserver", user_sock, sizeof(user_sock)-1)) != -1 ) { |
---|
[448] | 322 | user_sock[i] = 0; |
---|
| 323 | server = user_sock; |
---|
| 324 | } |
---|
[1093] | 325 | #endif |
---|
[448] | 326 | |
---|
[3077] | 327 | if ( server != NULL && !strcasecmp(server, "+slp") ) { |
---|
[3076] | 328 | server = roar_slp_find_roard(0); |
---|
| 329 | if ( server == NULL ) { |
---|
| 330 | return -1; |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | |
---|
[5471] | 334 | // "+default" is an alias for NULL. |
---|
| 335 | if ( server != NULL && !strcasecmp(server, "+default") ) { |
---|
| 336 | server = NULL; |
---|
| 337 | } |
---|
| 338 | |
---|
[61] | 339 | if ( server == NULL || *server == 0 ) { |
---|
[0] | 340 | /* connect via defaults */ |
---|
| 341 | |
---|
[1764] | 342 | #ifdef ROAR_HAVE_UNIX |
---|
[1436] | 343 | #ifndef ROAR_TARGET_MICROCONTROLLER |
---|
[1026] | 344 | roar_server = getenv("HOME"); |
---|
[1435] | 345 | #else |
---|
| 346 | roar_server = NULL; |
---|
| 347 | #endif |
---|
[1026] | 348 | |
---|
| 349 | if ( roar_server == NULL ) { |
---|
[1436] | 350 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[1026] | 351 | if ( (pwd = getpwuid(getuid())) == NULL ) { |
---|
| 352 | roar_server = "/NX-HOME-DIR"; |
---|
| 353 | } else { |
---|
| 354 | roar_server = pwd->pw_dir; |
---|
| 355 | } |
---|
[1078] | 356 | #else |
---|
| 357 | roar_server = "/WIN32-SUCKS"; |
---|
| 358 | #endif |
---|
[1026] | 359 | } |
---|
| 360 | |
---|
[5114] | 361 | snprintf(user_sock, sizeof(user_sock)-1, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER); |
---|
| 362 | user_sock[sizeof(user_sock)-1] = 0; |
---|
[0] | 363 | |
---|
[5368] | 364 | if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 ) |
---|
| 365 | return 0; |
---|
[0] | 366 | |
---|
[5368] | 367 | if ( _connect_server(con, ROAR_DEFAULT_SOCK_GLOBAL, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 ) |
---|
| 368 | return 0; |
---|
[1764] | 369 | #endif |
---|
[237] | 370 | |
---|
[5368] | 371 | if ( _connect_server(con, ROAR_DEFAULT_HOSTPORT, ROAR_SOCKET_TYPE_TCP, flags, timeout) == 0 ) |
---|
| 372 | return 0; |
---|
[0] | 373 | |
---|
[828] | 374 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 375 | if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) { |
---|
| 376 | if ( roar_socket_get_local_nodename() ) { |
---|
| 377 | snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT); |
---|
[5368] | 378 | if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_DECNET, flags, timeout) == 0 ) |
---|
| 379 | return 0; |
---|
[828] | 380 | } |
---|
[522] | 381 | } |
---|
[828] | 382 | #endif |
---|
[2007] | 383 | |
---|
[5375] | 384 | if ( _connect_server(con, "+abstract", ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) == 0 ) |
---|
[5368] | 385 | return 0; |
---|
[4109] | 386 | |
---|
[2007] | 387 | #ifdef ROAR_HAVE_LIBSLP |
---|
[4806] | 388 | if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) && |
---|
[5373] | 389 | !(flags & ROAR_ENUM_FLAG_NONBLOCK) && |
---|
| 390 | !(flags & ROAR_ENUM_FLAG_LOCALONLY) |
---|
[4806] | 391 | ) { |
---|
| 392 | if ( (server = roar_slp_find_roard(0)) != NULL ) { |
---|
[5375] | 393 | if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) |
---|
[5368] | 394 | return 0; |
---|
[2014] | 395 | |
---|
[4806] | 396 | /* in case we can not connect to the server given this may be a cache problem, |
---|
| 397 | we do a new lookup with the cache disabled in this case */ |
---|
[5237] | 398 | ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache"); |
---|
[4806] | 399 | if ( (server = roar_slp_find_roard(1)) != NULL ) |
---|
[5375] | 400 | if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) |
---|
[5368] | 401 | return 0; |
---|
[4806] | 402 | } |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | workarounds_store = config->workaround.workarounds; |
---|
| 406 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP; |
---|
| 407 | list = roar_enum_servers(flags, -1, -1); |
---|
| 408 | config->workaround.workarounds = workarounds_store; |
---|
| 409 | if ( list != NULL ) { |
---|
| 410 | for (i = 0; list[i].server != NULL; i++) { |
---|
[5375] | 411 | if ( _connect_server(con, list[i].server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) { |
---|
[4806] | 412 | roar_enum_servers_free(list); |
---|
[5368] | 413 | return 0; |
---|
[4806] | 414 | } |
---|
| 415 | } |
---|
| 416 | roar_enum_servers_free(list); |
---|
[4653] | 417 | } |
---|
[2007] | 418 | #endif |
---|
[522] | 419 | |
---|
[2014] | 420 | return -1; |
---|
| 421 | |
---|
[0] | 422 | } else { |
---|
| 423 | /* connect via (char*)server */ |
---|
[5375] | 424 | if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) != 0 ) |
---|
[5369] | 425 | return -1; |
---|
| 426 | return 0; |
---|
[0] | 427 | } |
---|
| 428 | |
---|
[5368] | 429 | roar_err_set(ROAR_ERROR_NODEV); |
---|
| 430 | ROAR_DBG("roar_connect_raw(*) = -1 // error=NODEV"); |
---|
| 431 | return -1; |
---|
[0] | 432 | } |
---|
| 433 | |
---|
[5237] | 434 | int roar_connect (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) { |
---|
[5373] | 435 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
| 436 | |
---|
[1315] | 437 | if ( con == NULL ) { |
---|
[4873] | 438 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[1315] | 439 | return -1; |
---|
| 440 | } |
---|
| 441 | |
---|
[5368] | 442 | if ( roar_connect_none(con) == -1 ) |
---|
[0] | 443 | return -1; |
---|
| 444 | |
---|
[5368] | 445 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
[5373] | 446 | if ( roar_connect_raw(con, server, flags | config->serverflags, timeout) == -1 ) |
---|
[5296] | 447 | return -1; |
---|
| 448 | |
---|
[5373] | 449 | // TODO: this should be moved to be within _connect_server() so it also stores server names of auto located servers. |
---|
[5296] | 450 | if ( server != NULL ) { |
---|
| 451 | con->server_name = roar_mm_strdup(server); |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | return 0; |
---|
[1315] | 455 | } |
---|
| 456 | |
---|
[5365] | 457 | int roar_connect_none (struct roar_connection * con) { |
---|
[5465] | 458 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
| 459 | |
---|
[5365] | 460 | if ( con == NULL ) { |
---|
[4873] | 461 | roar_err_set(ROAR_ERROR_INVAL); |
---|
[1315] | 462 | return -1; |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | memset(con, 0, sizeof(struct roar_connection)); |
---|
[5296] | 466 | con->refc = 1; |
---|
[5467] | 467 | con->flags = ROAR_CON_FLAGS_ISCLIENT; |
---|
[5465] | 468 | con->version = _ROAR_MESSAGE_VERSION; |
---|
[5232] | 469 | con->cb_userdata = NULL; |
---|
| 470 | con->cb = NULL; |
---|
[5296] | 471 | con->server_stds = NULL; |
---|
| 472 | con->server_name = NULL; |
---|
[5232] | 473 | |
---|
| 474 | roar_err_init(&(con->errorframe)); |
---|
[1315] | 475 | |
---|
[5296] | 476 | con->viocon = &(con->viocon_store); |
---|
| 477 | |
---|
[5365] | 478 | // con->flags |= ROAR_CON_FLAGS_VIO; |
---|
| 479 | |
---|
[5465] | 480 | if ( config->protocolversion != -1 ) |
---|
| 481 | roar_set_connection_version(con, config->protocolversion); |
---|
| 482 | |
---|
[5365] | 483 | roar_err_set(ROAR_ERROR_NONE); |
---|
| 484 | return 0; |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | int roar_connect_vio (struct roar_connection * con, struct roar_vio_calls * vio) { |
---|
| 488 | if ( con == NULL || vio == NULL ) { |
---|
| 489 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 490 | return -1; |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | if ( roar_connect_none(con) == -1 ) |
---|
| 494 | return -1; |
---|
| 495 | |
---|
| 496 | con->viocon = vio; |
---|
| 497 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
| 498 | |
---|
| 499 | return -1; |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | int roar_connect_fh (struct roar_connection * con, int fh) { |
---|
| 503 | |
---|
| 504 | if ( con == NULL || fh == -1 ) { |
---|
| 505 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 506 | return -1; |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | if ( roar_connect_none(con) == -1 ) |
---|
| 510 | return -1; |
---|
| 511 | |
---|
| 512 | // specal hack to set an ilegal value used internaly in libroar: |
---|
| 513 | if ( fh == -2 ) |
---|
| 514 | fh = -1; |
---|
| 515 | |
---|
[5296] | 516 | if ( roar_vio_open_fh_socket(con->viocon, fh) != -1 ) { |
---|
[3869] | 517 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
[5231] | 518 | } |
---|
[1315] | 519 | |
---|
[4873] | 520 | roar_err_set(ROAR_ERROR_NONE); |
---|
[0] | 521 | return 0; |
---|
| 522 | } |
---|
| 523 | |
---|
[1660] | 524 | int roar_get_connection_fh (struct roar_connection * con) { |
---|
[5231] | 525 | int fh; |
---|
| 526 | |
---|
| 527 | ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con); |
---|
| 528 | |
---|
[3869] | 529 | roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL); |
---|
[2809] | 530 | |
---|
[2043] | 531 | if ( con == NULL ) |
---|
| 532 | return -1; |
---|
| 533 | |
---|
[5231] | 534 | ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con); |
---|
| 535 | |
---|
[5296] | 536 | if ( roar_vio_ctl(con->viocon, ROAR_VIO_CTL_GET_FH, &fh) == -1 ) |
---|
[3869] | 537 | return -1; |
---|
| 538 | |
---|
[5231] | 539 | ROAR_DBG("roar_get_connection_fh(con=%p) = %i", con, fh); |
---|
[3869] | 540 | |
---|
[5231] | 541 | return fh; |
---|
[2043] | 542 | } |
---|
| 543 | |
---|
[3869] | 544 | struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) { |
---|
| 545 | if ( con == NULL ) |
---|
| 546 | return NULL; |
---|
| 547 | |
---|
| 548 | if ( con->flags & ROAR_CON_FLAGS_VIO ) |
---|
[5296] | 549 | return con->viocon; |
---|
[3869] | 550 | |
---|
| 551 | // TODO: try to open the VIO. |
---|
| 552 | |
---|
| 553 | return NULL; |
---|
| 554 | } |
---|
| 555 | |
---|
[5296] | 556 | const char * roar_get_connection_server(struct roar_connection * con) { |
---|
| 557 | if ( con == NULL ) { |
---|
| 558 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 559 | return NULL; |
---|
| 560 | } |
---|
| 561 | |
---|
| 562 | return con->server_name; |
---|
| 563 | } |
---|
| 564 | |
---|
| 565 | int roar_connectionref(struct roar_connection * con) { |
---|
| 566 | if ( con == NULL ) { |
---|
| 567 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 568 | return -1; |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | con->refc++; |
---|
| 572 | |
---|
| 573 | return 0; |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | int roar_connectionunref(struct roar_connection * con) { |
---|
[3869] | 577 | struct roar_vio_calls * vio; |
---|
[0] | 578 | struct roar_message m; |
---|
| 579 | |
---|
[5296] | 580 | if ( con == NULL ) { |
---|
| 581 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 582 | return -1; |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | con->refc--; |
---|
| 586 | |
---|
| 587 | if ( con->refc ) |
---|
| 588 | return 0; |
---|
| 589 | |
---|
[3875] | 590 | memset(&m, 0, sizeof(m)); |
---|
| 591 | |
---|
[5144] | 592 | m.datalen = 0; |
---|
| 593 | m.stream = -1; |
---|
| 594 | m.pos = 0; |
---|
[0] | 595 | m.cmd = ROAR_CMD_QUIT; |
---|
| 596 | |
---|
| 597 | roar_req(con, &m, NULL); |
---|
| 598 | |
---|
[3869] | 599 | if ( (vio = roar_get_connection_vio2(con)) != NULL ) { |
---|
| 600 | roar_vio_close(vio); |
---|
[2809] | 601 | } |
---|
[0] | 602 | |
---|
[5296] | 603 | if ( con->server_stds != NULL ) { |
---|
| 604 | roar_stds_free(con->server_stds); |
---|
| 605 | con->server_stds = NULL; |
---|
| 606 | } |
---|
[0] | 607 | |
---|
[5296] | 608 | if ( con->server_name != NULL ) { |
---|
| 609 | roar_mm_free(con->server_name); |
---|
| 610 | con->server_name = NULL; |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | if ( con->flags & ROAR_CON_FLAGS_FREESELF ) { |
---|
| 614 | roar_mm_free(con); |
---|
| 615 | } else { |
---|
| 616 | roar_connect_fh(con, -2); |
---|
| 617 | } |
---|
[807] | 618 | |
---|
[0] | 619 | return 0; |
---|
| 620 | } |
---|
| 621 | |
---|
[3913] | 622 | int roar_set_connection_callback(struct roar_connection * con, |
---|
| 623 | void (*cb)(struct roar_connection * con, |
---|
| 624 | struct roar_message * mes, |
---|
[5231] | 625 | void * data, |
---|
[3913] | 626 | void * userdata), |
---|
| 627 | void * userdata) { |
---|
[5465] | 628 | if ( con == NULL ) { |
---|
| 629 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[3913] | 630 | return -1; |
---|
[5465] | 631 | } |
---|
[3913] | 632 | |
---|
| 633 | con->cb = cb; |
---|
[5231] | 634 | con->cb_userdata = userdata; |
---|
[3913] | 635 | |
---|
| 636 | return 0; |
---|
| 637 | } |
---|
| 638 | |
---|
[5465] | 639 | int roar_set_connection_version(struct roar_connection * con, int version) { |
---|
| 640 | if ( con == NULL ) { |
---|
| 641 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 642 | return -1; |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | if ( version == -1 ) |
---|
| 646 | version = _ROAR_MESSAGE_VERSION; |
---|
| 647 | |
---|
| 648 | if ( version < 0 ) { |
---|
| 649 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 650 | return -1; |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | con->version = version; |
---|
| 654 | |
---|
| 655 | if ( version == 2 ) { |
---|
| 656 | con->flags |= ROAR_CON_FLAGS_SUPPORT_V2; |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | return 0; |
---|
| 660 | } |
---|
| 661 | |
---|
[5467] | 662 | int16_t roar_message_genseq(struct roar_connection * con, int is_client) { |
---|
| 663 | static int16_t store = 0; |
---|
| 664 | int16_t ret = 0; |
---|
| 665 | |
---|
| 666 | ret = store++; |
---|
| 667 | |
---|
[5468] | 668 | if ( ret == 0 ) |
---|
| 669 | ret = store++; |
---|
| 670 | |
---|
[5467] | 671 | if ( is_client == -1 ) { |
---|
| 672 | if ( con != NULL ) { |
---|
| 673 | is_client = con->flags & ROAR_CON_FLAGS_ISCLIENT ? 1 : 0; |
---|
| 674 | } |
---|
| 675 | } |
---|
| 676 | |
---|
| 677 | if ( is_client == -1 ) |
---|
| 678 | is_client = 1; |
---|
| 679 | |
---|
| 680 | ret |= 0x8000U; |
---|
| 681 | if ( !is_client ) |
---|
| 682 | ret -= 0x8000U; |
---|
| 683 | |
---|
| 684 | return ret; |
---|
| 685 | } |
---|
| 686 | |
---|
[3914] | 687 | int roar_sync (struct roar_connection * con) { |
---|
| 688 | // wait for any non-client reqs |
---|
| 689 | return roar_wait_msg(con, 0x0000, 0x8000); |
---|
| 690 | } |
---|
| 691 | |
---|
| 692 | int roar_wait_msg (struct roar_connection * con, int16_t seq, int16_t seqmask) { |
---|
[5148] | 693 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
[3914] | 694 | return -1; |
---|
| 695 | } |
---|
[3913] | 696 | |
---|
[3836] | 697 | int roar_noop (struct roar_connection * con) { |
---|
| 698 | struct roar_message mes; |
---|
| 699 | |
---|
| 700 | if ( con == NULL ) { |
---|
[5148] | 701 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[3836] | 702 | return -1; |
---|
| 703 | } |
---|
| 704 | |
---|
| 705 | memset(&mes, 0, sizeof(mes)); |
---|
| 706 | |
---|
| 707 | mes.cmd = ROAR_CMD_NOOP; |
---|
[5144] | 708 | mes.stream = -1; |
---|
[3836] | 709 | |
---|
[5146] | 710 | return roar_req3(con, &mes, NULL); |
---|
[3836] | 711 | } |
---|
| 712 | |
---|
[5114] | 713 | int roar_identify (struct roar_connection * con, const char * name) { |
---|
[0] | 714 | struct roar_message mes; |
---|
[5132] | 715 | uint32_t pid; |
---|
[0] | 716 | int max_len; |
---|
| 717 | |
---|
[4873] | 718 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
[807] | 719 | |
---|
[0] | 720 | ROAR_DBG("roar_identify(*): try to identify myself..."); |
---|
| 721 | |
---|
[3875] | 722 | memset(&mes, 0, sizeof(mes)); |
---|
| 723 | |
---|
[0] | 724 | mes.cmd = ROAR_CMD_IDENTIFY; |
---|
[5144] | 725 | mes.stream = -1; |
---|
| 726 | mes.pos = 0; |
---|
[0] | 727 | |
---|
| 728 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
| 729 | |
---|
| 730 | if ( name == NULL ) |
---|
| 731 | name = "libroar client"; |
---|
| 732 | |
---|
| 733 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
| 734 | |
---|
[5114] | 735 | max_len = roar_mm_strlen(name); |
---|
[0] | 736 | ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len); |
---|
| 737 | |
---|
| 738 | if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) ) |
---|
| 739 | max_len = LIBROAR_BUFFER_MSGDATA - 5; |
---|
| 740 | |
---|
| 741 | mes.datalen = 5 + max_len; |
---|
| 742 | mes.data[0] = 1; |
---|
| 743 | |
---|
| 744 | pid = getpid(); |
---|
[5132] | 745 | mes.data[1] = (pid & 0xFF000000UL) >> 24; |
---|
| 746 | mes.data[2] = (pid & 0x00FF0000UL) >> 16; |
---|
| 747 | mes.data[3] = (pid & 0x0000FF00UL) >> 8; |
---|
| 748 | mes.data[4] = (pid & 0x000000FFUL) >> 0; |
---|
| 749 | ROAR_DBG("roar_identify(*): pid = %i", (int)pid); |
---|
[0] | 750 | |
---|
| 751 | strncpy(mes.data+5, name, max_len); |
---|
| 752 | |
---|
[5146] | 753 | return roar_req3(con, &mes, NULL); |
---|
[0] | 754 | } |
---|
| 755 | |
---|
| 756 | //ll |
---|