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