source: roaraudio/libroar/enumdev.c @ 5026:eb2aee394b16

Last change on this file since 5026:eb2aee394b16 was 5026:eb2aee394b16, checked in by phi, 11 years ago

use roar_mm_str*()

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