source: roaraudio/libroar/enumdev.c @ 5298:a0d70da17a74

Last change on this file since 5298:a0d70da17a74 was 5270:e25346c13638, checked in by phi, 12 years ago

fixed some gcc -Wextra warnings

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