source: roaraudio/libroar/enumdev.c @ 5878:3b92b0d6ef9b

Last change on this file since 5878:3b92b0d6ef9b was 5832:f0b38d5ea016, checked in by phi, 11 years ago

added roar_libroar_get_path_static() and make use of it

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