source: roaraudio/libroar/enumdev.c @ 4775:9f4ca59b800b

Last change on this file since 4775:9f4ca59b800b was 4775:9f4ca59b800b, checked in by phi, 13 years ago

ignore OpenSLP for enumdev if disabled by config (WAS_NO_SLP)

File size: 6.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
44 if ( c->server == NULL )
45  return -1;
46
47 if ( flags & ROAR_ENUM_FLAG_NONBLOCK )
48  return 0;
49
50 if ( roar_connect(&con, (char*)c->server) == -1 )
51  return -1;
52
53 if ( (flags & ROAR_ENUM_FLAG_DESC) || (flags & ROAR_ENUM_FLAG_LOCATION) ) {
54  info = roar_server_info(&con);
55  if ( info != NULL ) {
56   if ( info->location != NULL )
57    c->location = roar_mm_strdup(info->location);
58
59   if ( info->description != NULL )
60    c->description = roar_mm_strdup(info->description);
61
62   roar_server_info_free(info);
63  }
64 }
65
66 roar_disconnect(&con);
67
68 return 0;
69}
70
71#define _add(x) if ( (x) != NULL ) servers[ret++] = roar_mm_strdup((x))
72static ssize_t _esl_defaults(int flags, int dir, int socktype, char ** servers, size_t maxlen) {
73#ifdef ROAR_HAVE_LIBX11
74 struct roar_x11_connection * x11con;
75#endif
76 ssize_t ret = 0;
77 const char * new;
78 char buf[1024];
79 int i;
80
81 if ( maxlen < 10 )
82  return -1;
83
84 new = roar_libroar_get_server();
85 _add(new);
86
87 new = getenv("ROAR_SERVER");
88 _add(new);
89
90#ifdef ROAR_HAVE_LIBX11
91 if ( (x11con = roar_x11_connect(NULL)) != NULL ) {
92  new = roar_x11_get_prop(x11con, "ROAR_SERVER");
93  _add(new);
94  roar_x11_disconnect(x11con);
95 }
96#endif
97
98#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
99 if ( (i = readlink("/etc/roarserver", buf, sizeof(buf)-1)) != -1 ) {
100   buf[i] = 0;
101   _add(buf);
102 }
103#endif
104
105 if ( (new = roar_env_get_home(0)) != NULL ) {
106  snprintf(buf, sizeof(buf)-1, "%s/%s", new, ROAR_DEFAULT_SOCK_USER);
107  buf[sizeof(buf)-1] = 0;
108  _add(buf);
109 }
110
111 servers[ret++] = roar_mm_strdup(ROAR_DEFAULT_SOCK_GLOBAL);
112 servers[ret++] = roar_mm_strdup(ROAR_DEFAULT_HOST);
113 servers[ret++] = roar_mm_strdup("::" ROAR_DEFAULT_OBJECT);
114 servers[ret++] = roar_mm_strdup("+abstract");
115 servers[ret++] = roar_mm_strdup("/tmp/muroard");
116
117 return ret;
118}
119
120static ssize_t _esl_slp(int flags, int dir, int socktype, char ** servers, size_t maxlen) {
121 struct roar_libroar_config * config = roar_libroar_get_config();
122 struct roar_slp_cookie cookie;
123 int offset;
124 char * url;
125 size_t i;
126 ssize_t ret = 0;
127
128 if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP )
129  return 0;
130
131 if ( roar_slp_cookie_init(&cookie, NULL) == -1 )
132  return -1;
133
134 if ( roar_slp_search(&cookie, ROAR_SLP_URL_TYPE) == -1 )
135  return -1;
136
137 if ( cookie.matchcount == 0 )
138  return -1;
139
140 ROAR_DBG("_esl_slp(*): cookie.matchcount=%i", (int)cookie.matchcount);
141
142 for (i = 0; i < (size_t)cookie.matchcount && (ssize_t)maxlen > ret; i++) {
143  url = cookie.match[i].url;
144  ROAR_DBG("_esl_slp(*): cookie.match[%i].url='%s'", (int)i, url);
145
146  offset = 0;
147
148  if ( !strncmp(url, ROAR_SLP_URL_TYPE "://", ROAR_SLP_URL_TYPE_LEN + 3) )
149   offset = ROAR_SLP_URL_TYPE_LEN + 3;
150
151  ROAR_DBG("_esl_slp(*): url=%p, offset=%i", url, offset);
152  url = &(url[offset]);
153  ROAR_DBG("_esl_slp(*): url='%s'", url);
154
155  if ( *url == 0 )
156   continue;
157
158  _add(url);
159 }
160
161 return ret;
162}
163
164struct locmed {
165 int supflags;
166 ssize_t (*func)(int flags, int dir, int socktype, char ** servers, size_t maxlen);
167};
168
169static struct locmed _libroar_locmod[] = {
170 {ROAR_ENUM_FLAG_NONBLOCK|ROAR_ENUM_FLAG_HARDNONBLOCK, _esl_defaults},
171 {ROAR_ENUM_FLAG_NONE,                                 _esl_slp}
172};
173
174struct roar_server * roar_enum_servers(int flags, int dir, int socktype) {
175 struct roar_server * ret = NULL;
176 struct roar_server * c;
177 char * servers[64];
178 size_t have = 1;
179 size_t i, cp, unic;
180 ssize_t r;
181 int testflags = flags;
182 int is_uniq;
183
184 // load config:
185 roar_libroar_get_config();
186
187 if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK )
188  flags |= ROAR_ENUM_FLAG_NONBLOCK;
189
190 if ( testflags & ROAR_ENUM_FLAG_DESC )
191  testflags -= ROAR_ENUM_FLAG_DESC;
192 if ( testflags & ROAR_ENUM_FLAG_LOCATION )
193  testflags -= ROAR_ENUM_FLAG_LOCATION;
194
195 for (i = 0; i < sizeof(_libroar_locmod)/sizeof(*_libroar_locmod); i++) {
196  if ( (_libroar_locmod[i].supflags & testflags) == testflags ) {
197   r = _libroar_locmod[i].func(flags, dir, socktype, &(servers[have-1]), (sizeof(servers)/sizeof(*servers)) - have);
198   if ( r > 0 )
199    have += r;
200  }
201 }
202
203 ret = roar_mm_malloc(have*sizeof(struct roar_server));
204
205 if (ret == NULL)
206  return NULL;
207
208 have--;
209
210 for (i = cp = 0; i < have; i++) {
211  c = &(ret[cp]);
212  c->server = servers[i];
213  c->description = NULL;
214  c->location = NULL;
215
216  // uniq test:
217  is_uniq = 1;
218  for (unic = 0; unic < cp; unic++)
219   if ( !strcmp(ret[unic].server, servers[i]) )
220    is_uniq = 0;
221
222  if ( is_uniq && _test_server(c, flags) == 0 ) {
223   cp++;
224  } else {
225   roar_mm_free(servers[i]);
226  }
227 }
228
229 ret[cp].server = NULL;
230 ret[cp].description = roar_mm_strdup("Default server");
231 ret[cp].location = NULL;
232
233 return ret;
234}
235
236int roar_enum_servers_free(struct roar_server * servs) {
237 struct roar_server * c;
238 int i;
239
240 if ( servs == NULL )
241  return -1;
242
243 for (i = 0; (c = &(servs[i]))->server != NULL; i++) {
244  roar_mm_free((void*)c->server);
245  if ( c->description != NULL )
246   roar_mm_free((void*)c->description);
247  if ( c->location != NULL )
248   roar_mm_free((void*)c->location);
249 }
250
251 if ( c->description != NULL )
252  roar_mm_free((void*)c->description);
253 if ( c->location != NULL )
254  roar_mm_free((void*)c->location);
255
256 roar_mm_free(servs);
257
258 return 0;
259}
260
261ssize_t roar_enum_servers_num(struct roar_server * servs) {
262 size_t ret;
263
264 if ( servs == NULL )
265  return -1;
266
267 for (ret = 0; servs[ret].server != NULL; ret++);
268
269 return ret;
270}
271
272//ll
Note: See TracBrowser for help on using the repository browser.