source: roaraudio/libroar/enumdev.c @ 5326:dd73c777e8dd

Last change on this file since 5326:dd73c777e8dd was 5324:d8a11b9bfafb, checked in by phi, 12 years ago

honor $SSH_CLIENT and $SSH_CONNECTION

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