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