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