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