[4350] | 1 | //enumdev.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5961] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2014 |
---|
[4350] | 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 |
---|
| 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
| 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 | |
---|
| 36 | #include "libroar.h" |
---|
| 37 | |
---|
[4352] | 38 | // TODO: we should put all the data in one big alloced block. |
---|
| 39 | |
---|
[4353] | 40 | static int _test_server(struct roar_server * c, int flags) { |
---|
| 41 | struct roar_connection con; |
---|
[4435] | 42 | struct roar_server_info * info; |
---|
[4777] | 43 | int checked = 0; |
---|
[4435] | 44 | |
---|
[4353] | 45 | if ( c->server == NULL ) |
---|
| 46 | return -1; |
---|
| 47 | |
---|
| 48 | if ( flags & ROAR_ENUM_FLAG_NONBLOCK ) |
---|
| 49 | return 0; |
---|
| 50 | |
---|
[4777] | 51 | ROAR_DBG("_test_server(c=%p{.server='%s', ...}, flags=0x%.4X) = ?", c, c->server, flags); |
---|
| 52 | |
---|
[5138] | 53 | roar_libroar_nowarn(); |
---|
[5238] | 54 | if ( roar_connect(&con, c->server, 0, 0) == -1 ) { |
---|
[5138] | 55 | roar_libroar_warn(); |
---|
[4353] | 56 | return -1; |
---|
[5138] | 57 | } |
---|
| 58 | roar_libroar_warn(); |
---|
[4353] | 59 | |
---|
[5027] | 60 | ROAR_DBG("_test_server(c=%p{.server='%s', ...}, flags=0x%.4X) = ?", c, c->server, flags); |
---|
| 61 | |
---|
[4444] | 62 | if ( (flags & ROAR_ENUM_FLAG_DESC) || (flags & ROAR_ENUM_FLAG_LOCATION) ) { |
---|
| 63 | info = roar_server_info(&con); |
---|
| 64 | if ( info != NULL ) { |
---|
[4777] | 65 | checked = 1; |
---|
[4444] | 66 | if ( info->location != NULL ) |
---|
| 67 | c->location = roar_mm_strdup(info->location); |
---|
[4435] | 68 | |
---|
[4444] | 69 | if ( info->description != NULL ) |
---|
| 70 | c->description = roar_mm_strdup(info->description); |
---|
[4435] | 71 | |
---|
[4444] | 72 | roar_server_info_free(info); |
---|
| 73 | } |
---|
[4435] | 74 | } |
---|
| 75 | |
---|
[5027] | 76 | ROAR_DBG("_test_server(c=%p{.server='%s', ...}, flags=0x%.4X) = ?", c, c->server, flags); |
---|
| 77 | |
---|
[4777] | 78 | // make sure we get at least a NOOP from the server to detect protocol mismatch. |
---|
| 79 | if ( !checked ) { |
---|
| 80 | if ( roar_noop(&con) != 0 ) { |
---|
| 81 | roar_disconnect(&con); |
---|
| 82 | return -1; |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
[4353] | 86 | roar_disconnect(&con); |
---|
| 87 | |
---|
[5027] | 88 | ROAR_DBG("_test_server(c=%p{.server='%s', ...}, flags=0x%.4X) = 0", c, c->server, flags); |
---|
[4353] | 89 | return 0; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | #define _add(x) if ( (x) != NULL ) servers[ret++] = roar_mm_strdup((x)) |
---|
| 93 | static ssize_t _esl_defaults(int flags, int dir, int socktype, char ** servers, size_t maxlen) { |
---|
| 94 | #ifdef ROAR_HAVE_LIBX11 |
---|
| 95 | struct roar_x11_connection * x11con; |
---|
| 96 | #endif |
---|
| 97 | ssize_t ret = 0; |
---|
| 98 | const char * new; |
---|
| 99 | char buf[1024]; |
---|
[4780] | 100 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[4353] | 101 | int i; |
---|
[4780] | 102 | #endif |
---|
[5324] | 103 | char * ssh_buf, * tmp; |
---|
[4353] | 104 | |
---|
[5270] | 105 | (void)flags, (void)dir, (void)socktype; |
---|
| 106 | |
---|
[5373] | 107 | if ( maxlen < 12 ) |
---|
[4353] | 108 | return -1; |
---|
| 109 | |
---|
[5373] | 110 | // We can not yet test if those servers are local or reachable via network so |
---|
| 111 | // we skip them for now. |
---|
| 112 | // TODO: test those servers. |
---|
| 113 | if ( !(flags & ROAR_ENUM_FLAG_LOCALONLY) ) { |
---|
| 114 | new = roar_libroar_get_server(); |
---|
| 115 | _add(new); |
---|
[4353] | 116 | |
---|
[5754] | 117 | new = roar_env_get("ROAR_SERVER"); |
---|
[5373] | 118 | _add(new); |
---|
[4353] | 119 | |
---|
| 120 | #ifdef ROAR_HAVE_LIBX11 |
---|
[5373] | 121 | if ( (x11con = roar_x11_connect(NULL)) != NULL ) { |
---|
| 122 | new = roar_x11_get_prop(x11con, "ROAR_SERVER"); |
---|
| 123 | _add(new); |
---|
| 124 | roar_x11_disconnect(x11con); |
---|
| 125 | } |
---|
[4353] | 126 | #endif |
---|
| 127 | |
---|
[5754] | 128 | new = roar_env_get("SSH_CLIENT"); |
---|
[5373] | 129 | if ( new == NULL ) |
---|
[5754] | 130 | new = roar_env_get("SSH_CONNECTION"); |
---|
[5324] | 131 | |
---|
[5373] | 132 | if ( new != NULL ) { |
---|
| 133 | ssh_buf = roar_mm_strdup(new); |
---|
| 134 | if ( ssh_buf != NULL ) { |
---|
| 135 | tmp = strstr(ssh_buf, " "); |
---|
| 136 | if ( tmp == NULL ) { |
---|
| 137 | // invalid format. |
---|
| 138 | roar_mm_free(ssh_buf); |
---|
| 139 | } else { |
---|
| 140 | *tmp = 0; |
---|
| 141 | servers[ret++] = ssh_buf; |
---|
| 142 | } |
---|
[5324] | 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
[4353] | 146 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[5895] | 147 | tmp = roar_libroar_get_path("sysconf-roarserver", 0, NULL, NULL); |
---|
| 148 | if ( tmp != NULL ) { |
---|
| 149 | if ( (i = readlink(tmp, buf, sizeof(buf)-1)) != -1 ) { |
---|
| 150 | buf[i] = 0; |
---|
| 151 | _add(buf); |
---|
| 152 | } |
---|
| 153 | roar_mm_free(tmp); |
---|
[5373] | 154 | } |
---|
| 155 | #endif |
---|
[4353] | 156 | } |
---|
| 157 | |
---|
| 158 | if ( (new = roar_env_get_home(0)) != NULL ) { |
---|
| 159 | snprintf(buf, sizeof(buf)-1, "%s/%s", new, ROAR_DEFAULT_SOCK_USER); |
---|
| 160 | buf[sizeof(buf)-1] = 0; |
---|
| 161 | _add(buf); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | servers[ret++] = roar_mm_strdup(ROAR_DEFAULT_SOCK_GLOBAL); |
---|
| 165 | servers[ret++] = roar_mm_strdup(ROAR_DEFAULT_HOST); |
---|
| 166 | servers[ret++] = roar_mm_strdup("::" ROAR_DEFAULT_OBJECT); |
---|
| 167 | servers[ret++] = roar_mm_strdup("+abstract"); |
---|
| 168 | servers[ret++] = roar_mm_strdup("/tmp/muroard"); |
---|
[5373] | 169 | servers[ret++] = roar_mm_strdup("/dev/roar"); |
---|
[4353] | 170 | |
---|
| 171 | return ret; |
---|
| 172 | } |
---|
| 173 | |
---|
[4414] | 174 | static ssize_t _esl_slp(int flags, int dir, int socktype, char ** servers, size_t maxlen) { |
---|
[4775] | 175 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
[4414] | 176 | struct roar_slp_cookie cookie; |
---|
| 177 | int offset; |
---|
| 178 | char * url; |
---|
| 179 | size_t i; |
---|
| 180 | ssize_t ret = 0; |
---|
| 181 | |
---|
[4775] | 182 | if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP ) |
---|
| 183 | return 0; |
---|
| 184 | |
---|
[4414] | 185 | if ( roar_slp_cookie_init(&cookie, NULL) == -1 ) |
---|
| 186 | return -1; |
---|
| 187 | |
---|
| 188 | if ( roar_slp_search(&cookie, ROAR_SLP_URL_TYPE) == -1 ) |
---|
| 189 | return -1; |
---|
| 190 | |
---|
| 191 | if ( cookie.matchcount == 0 ) |
---|
| 192 | return -1; |
---|
| 193 | |
---|
| 194 | ROAR_DBG("_esl_slp(*): cookie.matchcount=%i", (int)cookie.matchcount); |
---|
| 195 | |
---|
| 196 | for (i = 0; i < (size_t)cookie.matchcount && (ssize_t)maxlen > ret; i++) { |
---|
| 197 | url = cookie.match[i].url; |
---|
| 198 | ROAR_DBG("_esl_slp(*): cookie.match[%i].url='%s'", (int)i, url); |
---|
| 199 | |
---|
| 200 | offset = 0; |
---|
| 201 | |
---|
| 202 | if ( !strncmp(url, ROAR_SLP_URL_TYPE "://", ROAR_SLP_URL_TYPE_LEN + 3) ) |
---|
| 203 | offset = ROAR_SLP_URL_TYPE_LEN + 3; |
---|
| 204 | |
---|
| 205 | ROAR_DBG("_esl_slp(*): url=%p, offset=%i", url, offset); |
---|
| 206 | url = &(url[offset]); |
---|
| 207 | ROAR_DBG("_esl_slp(*): url='%s'", url); |
---|
| 208 | |
---|
| 209 | if ( *url == 0 ) |
---|
| 210 | continue; |
---|
| 211 | |
---|
| 212 | _add(url); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | return ret; |
---|
| 216 | } |
---|
| 217 | |
---|
[5016] | 218 | #ifdef ROAR_HAVE_FOPEN |
---|
[4777] | 219 | static ssize_t _esl_neighbours(int flags, int dir, int socktype, char ** servers, size_t maxlen) { |
---|
| 220 | ssize_t ret = 0; |
---|
| 221 | char buf[1024]; |
---|
| 222 | FILE * inp; |
---|
| 223 | size_t i; |
---|
| 224 | char * delm; |
---|
| 225 | const struct { |
---|
| 226 | const char * file; |
---|
| 227 | const char * suffix; |
---|
| 228 | const size_t skip; |
---|
| 229 | } *f, files[] = { |
---|
| 230 | #ifdef ROAR_PROC_NET_DECNET_NEIGH |
---|
| 231 | {ROAR_PROC_NET_DECNET_NEIGH, "::", 1}, |
---|
| 232 | #endif |
---|
| 233 | #ifdef ROAR_PROC_NET_ARP |
---|
| 234 | {ROAR_PROC_NET_ARP, NULL, 1}, |
---|
| 235 | #endif |
---|
[5832] | 236 | // {roar_libroar_get_path_static("sysconf-hosts"), NULL, 0}, |
---|
[4777] | 237 | {NULL, NULL, 0} |
---|
| 238 | }; |
---|
| 239 | |
---|
[5270] | 240 | (void)flags, (void)dir, (void)socktype; |
---|
| 241 | |
---|
[4777] | 242 | for (f = &(files[0]); f->file != NULL; f++) { |
---|
| 243 | if ( (inp = fopen(f->file, "r")) == NULL ) |
---|
| 244 | continue; |
---|
| 245 | |
---|
| 246 | for (i = 0; i < f->skip; i++) { |
---|
| 247 | if ( fgets(buf, sizeof(buf), inp) == NULL ) { |
---|
| 248 | // bad error... |
---|
| 249 | fclose(inp); |
---|
| 250 | return ret; |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | while (ret < (ssize_t)maxlen && fgets(buf, sizeof(buf), inp) != NULL) { |
---|
| 255 | // skip comments and empty lions |
---|
| 256 | if ( buf[0] == '#' || buf[0] == '\0' ) |
---|
| 257 | continue; |
---|
| 258 | |
---|
| 259 | // ensure we have a tailing \0. |
---|
| 260 | buf[sizeof(buf)-1] = 0; |
---|
| 261 | |
---|
| 262 | delm = strstr(buf, " "); |
---|
| 263 | if ( delm == NULL ) |
---|
| 264 | delm = strstr(buf, "\t"); |
---|
| 265 | |
---|
| 266 | // does it look like a correctly formated lion? |
---|
| 267 | // we may remove this check or relax it later. |
---|
| 268 | if ( delm == NULL ) |
---|
| 269 | continue; |
---|
| 270 | |
---|
| 271 | *delm = 0; |
---|
| 272 | |
---|
| 273 | if ( f->suffix != NULL ) { |
---|
[5026] | 274 | if ( (roar_mm_strlen(buf) + roar_mm_strlen(f->suffix) + 1) > sizeof(buf) ) |
---|
[4777] | 275 | continue; |
---|
| 276 | |
---|
[5026] | 277 | roar_mm_strscat(buf, f->suffix); |
---|
[4777] | 278 | } |
---|
| 279 | |
---|
| 280 | // work around a bug in Linux kernel which always reports node 0.2 to be up. |
---|
| 281 | if ( !strcmp(buf, "0.2::") ) |
---|
| 282 | continue; |
---|
| 283 | |
---|
| 284 | _add(buf); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | fclose(inp); |
---|
| 288 | if ( ret == (ssize_t)maxlen ) |
---|
| 289 | break; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | return ret; |
---|
| 293 | } |
---|
[5016] | 294 | #endif |
---|
[4777] | 295 | |
---|
[4352] | 296 | struct locmed { |
---|
| 297 | int supflags; |
---|
| 298 | ssize_t (*func)(int flags, int dir, int socktype, char ** servers, size_t maxlen); |
---|
| 299 | }; |
---|
| 300 | |
---|
| 301 | static struct locmed _libroar_locmod[] = { |
---|
[5373] | 302 | {ROAR_ENUM_FLAG_NONBLOCK|ROAR_ENUM_FLAG_HARDNONBLOCK|ROAR_ENUM_FLAG_LOCALONLY, _esl_defaults}, |
---|
| 303 | {ROAR_ENUM_FLAG_NONE, _esl_slp}, |
---|
[5016] | 304 | #ifdef ROAR_HAVE_FOPEN |
---|
[5373] | 305 | {ROAR_ENUM_FLAG_NONBLOCK, _esl_neighbours} |
---|
[5016] | 306 | #endif |
---|
[4352] | 307 | }; |
---|
| 308 | |
---|
| 309 | struct roar_server * roar_enum_servers(int flags, int dir, int socktype) { |
---|
[5373] | 310 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
[4352] | 311 | struct roar_server * ret = NULL; |
---|
| 312 | struct roar_server * c; |
---|
| 313 | char * servers[64]; |
---|
| 314 | size_t have = 1; |
---|
[4354] | 315 | size_t i, cp, unic; |
---|
[4352] | 316 | ssize_t r; |
---|
[5373] | 317 | int testflags; |
---|
[4354] | 318 | int is_uniq; |
---|
[4352] | 319 | |
---|
[5373] | 320 | flags |= config->serverflags; |
---|
| 321 | testflags = flags; |
---|
| 322 | |
---|
[4356] | 323 | if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK ) |
---|
| 324 | flags |= ROAR_ENUM_FLAG_NONBLOCK; |
---|
| 325 | |
---|
[4352] | 326 | if ( testflags & ROAR_ENUM_FLAG_DESC ) |
---|
| 327 | testflags -= ROAR_ENUM_FLAG_DESC; |
---|
| 328 | if ( testflags & ROAR_ENUM_FLAG_LOCATION ) |
---|
| 329 | testflags -= ROAR_ENUM_FLAG_LOCATION; |
---|
| 330 | |
---|
| 331 | for (i = 0; i < sizeof(_libroar_locmod)/sizeof(*_libroar_locmod); i++) { |
---|
| 332 | if ( (_libroar_locmod[i].supflags & testflags) == testflags ) { |
---|
| 333 | r = _libroar_locmod[i].func(flags, dir, socktype, &(servers[have-1]), (sizeof(servers)/sizeof(*servers)) - have); |
---|
| 334 | if ( r > 0 ) |
---|
| 335 | have += r; |
---|
| 336 | } |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | ret = roar_mm_malloc(have*sizeof(struct roar_server)); |
---|
| 340 | |
---|
| 341 | if (ret == NULL) |
---|
| 342 | return NULL; |
---|
| 343 | |
---|
| 344 | have--; |
---|
| 345 | |
---|
[4353] | 346 | for (i = cp = 0; i < have; i++) { |
---|
| 347 | c = &(ret[cp]); |
---|
[4352] | 348 | c->server = servers[i]; |
---|
| 349 | c->description = NULL; |
---|
| 350 | c->location = NULL; |
---|
[4354] | 351 | |
---|
| 352 | // uniq test: |
---|
| 353 | is_uniq = 1; |
---|
| 354 | for (unic = 0; unic < cp; unic++) |
---|
| 355 | if ( !strcmp(ret[unic].server, servers[i]) ) |
---|
| 356 | is_uniq = 0; |
---|
| 357 | |
---|
| 358 | if ( is_uniq && _test_server(c, flags) == 0 ) { |
---|
[4353] | 359 | cp++; |
---|
| 360 | } else { |
---|
| 361 | roar_mm_free(servers[i]); |
---|
| 362 | } |
---|
[4352] | 363 | } |
---|
| 364 | |
---|
[4353] | 365 | ret[cp].server = NULL; |
---|
| 366 | ret[cp].description = roar_mm_strdup("Default server"); |
---|
| 367 | ret[cp].location = NULL; |
---|
[4352] | 368 | |
---|
| 369 | return ret; |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | int roar_enum_servers_free(struct roar_server * servs) { |
---|
| 373 | struct roar_server * c; |
---|
| 374 | int i; |
---|
| 375 | |
---|
[5145] | 376 | if ( servs == NULL ) { |
---|
| 377 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[4352] | 378 | return -1; |
---|
[5145] | 379 | } |
---|
[4352] | 380 | |
---|
| 381 | for (i = 0; (c = &(servs[i]))->server != NULL; i++) { |
---|
| 382 | roar_mm_free((void*)c->server); |
---|
| 383 | if ( c->description != NULL ) |
---|
| 384 | roar_mm_free((void*)c->description); |
---|
| 385 | if ( c->location != NULL ) |
---|
| 386 | roar_mm_free((void*)c->location); |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | if ( c->description != NULL ) |
---|
| 390 | roar_mm_free((void*)c->description); |
---|
| 391 | if ( c->location != NULL ) |
---|
| 392 | roar_mm_free((void*)c->location); |
---|
| 393 | |
---|
| 394 | roar_mm_free(servs); |
---|
| 395 | |
---|
| 396 | return 0; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | ssize_t roar_enum_servers_num(struct roar_server * servs) { |
---|
| 400 | size_t ret; |
---|
| 401 | |
---|
[5145] | 402 | if ( servs == NULL ) { |
---|
| 403 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[4352] | 404 | return -1; |
---|
[5145] | 405 | } |
---|
[4352] | 406 | |
---|
| 407 | for (ret = 0; servs[ret].server != NULL; ret++); |
---|
| 408 | |
---|
| 409 | return ret; |
---|
| 410 | } |
---|
| 411 | |
---|
[4350] | 412 | //ll |
---|