1 | //slp.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
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 | SLPBoolean roar_slp_url_callback(SLPHandle hslp, |
---|
39 | const char * srvurl, |
---|
40 | unsigned short lifetime, |
---|
41 | SLPError errcode, |
---|
42 | void * cookie) { |
---|
43 | #ifdef ROAR_HAVE_LIBSLP |
---|
44 | struct roar_slp_cookie * self = cookie; |
---|
45 | |
---|
46 | ROAR_DBG("roar_slp_url_callback(*) = ?"); |
---|
47 | |
---|
48 | if (errcode == SLP_OK || errcode == SLP_LAST_CALL) { |
---|
49 | self->callbackerr = SLP_OK; |
---|
50 | |
---|
51 | if ( srvurl == NULL ) /* hu? */ |
---|
52 | return SLP_TRUE; |
---|
53 | |
---|
54 | if ( self->matchcount == ROAR_SLP_MAX_MATCHES ) |
---|
55 | return SLP_FALSE; |
---|
56 | |
---|
57 | strncpy(self->match[self->matchcount].url, srvurl, ROAR_SLP_MAX_URL_LEN); |
---|
58 | |
---|
59 | self->match[self->matchcount].url[ROAR_SLP_MAX_URL_LEN-1] = 0; |
---|
60 | |
---|
61 | self->match[self->matchcount].tod = time(NULL); |
---|
62 | self->match[self->matchcount].tod += lifetime; |
---|
63 | |
---|
64 | self->matchcount++; |
---|
65 | } else { |
---|
66 | self->callbackerr = errcode; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | /* return SLP_TRUE because we want to be called again */ |
---|
71 | /* if more services were found */ |
---|
72 | |
---|
73 | return SLP_TRUE; |
---|
74 | #else |
---|
75 | return SLP_FALSE; |
---|
76 | #endif |
---|
77 | } |
---|
78 | |
---|
79 | int roar_slp_search (struct roar_slp_cookie * cookie, char * type) { |
---|
80 | #ifdef ROAR_HAVE_LIBSLP |
---|
81 | SLPError err; |
---|
82 | SLPHandle hslp; |
---|
83 | |
---|
84 | ROAR_DBG("roar_slp_search(cookie=%p, type='%s') = ?", cookie, type); |
---|
85 | |
---|
86 | if ( cookie->search != NULL ) /* currently only non-search filter mode supported */ |
---|
87 | return -1; |
---|
88 | |
---|
89 | err = SLPOpen("en", SLP_FALSE, &hslp); |
---|
90 | if (err != SLP_OK) { |
---|
91 | return -1; |
---|
92 | } |
---|
93 | |
---|
94 | ROAR_DBG("roar_slp_search(*) = ?"); |
---|
95 | |
---|
96 | err = SLPFindSrvs(hslp, |
---|
97 | type, |
---|
98 | 0, /* use configured scopes */ |
---|
99 | 0, /* no attr filter */ |
---|
100 | roar_slp_url_callback, |
---|
101 | cookie); |
---|
102 | |
---|
103 | ROAR_DBG("roar_slp_search(*) = ?"); |
---|
104 | |
---|
105 | /* err may contain an error code that occurred as the slp library */ |
---|
106 | /* _prepared_ to make the call. */ |
---|
107 | if (err != SLP_OK) { |
---|
108 | return -1; |
---|
109 | } |
---|
110 | |
---|
111 | /* callbackerr may contain an error code (that was assigned through */ |
---|
112 | /* the callback cookie) that occurred as slp packets were sent on */ |
---|
113 | /* the wire */ |
---|
114 | if (cookie->callbackerr != SLP_OK) { |
---|
115 | return -1; |
---|
116 | } |
---|
117 | |
---|
118 | ROAR_DBG("roar_slp_search(*) = ?"); |
---|
119 | |
---|
120 | /* Now that we're done using slp, close the slp handle */ |
---|
121 | //SLPClose(hslp); |
---|
122 | |
---|
123 | ROAR_DBG("roar_slp_search(*) = ?"); |
---|
124 | |
---|
125 | return 0; |
---|
126 | #else |
---|
127 | return -1; |
---|
128 | #endif |
---|
129 | } |
---|
130 | |
---|
131 | int roar_slp_cookie_init (struct roar_slp_cookie * cookie, struct roar_slp_search * search) { |
---|
132 | if ( cookie == NULL ) |
---|
133 | return -1; |
---|
134 | |
---|
135 | memset(cookie, 0, sizeof(struct roar_slp_cookie)); |
---|
136 | |
---|
137 | cookie->search = search; |
---|
138 | |
---|
139 | return 0; |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | char * roar_slp_find_roard (int nocache) { |
---|
144 | static char addr[80]; |
---|
145 | |
---|
146 | if ( roar_slp_find_roard_r(addr, 80, nocache) == -1 ) |
---|
147 | return NULL; |
---|
148 | |
---|
149 | return addr; |
---|
150 | } |
---|
151 | |
---|
152 | int roar_slp_find_roard_r (char * addr, size_t len, int nocache) { |
---|
153 | static struct roar_slp_match cache = {"", 0}; |
---|
154 | struct roar_slp_cookie cookie; |
---|
155 | int offset = 0; |
---|
156 | char * url; |
---|
157 | |
---|
158 | ROAR_DBG("roar_slp_find_roard_r(addr=%p, len=%i) = ?", addr, len); |
---|
159 | |
---|
160 | if ( addr == NULL || len == 0 ) |
---|
161 | return -1; |
---|
162 | |
---|
163 | *addr = 0; // just in case... |
---|
164 | |
---|
165 | if ( nocache || cache.tod < time(NULL) ) { |
---|
166 | #ifdef DEBUG |
---|
167 | if ( nocache ) { |
---|
168 | ROAR_DBG("roar_slp_find_roard_r(*): forced ignoring of cache, doing a new lookup."); |
---|
169 | } |
---|
170 | #endif |
---|
171 | ROAR_DBG("roar_slp_find_roard_r(*): cache too old, searching for a new server..."); |
---|
172 | ROAR_DBG("roar_slp_find_roard_r(*) = ?"); |
---|
173 | |
---|
174 | if ( roar_slp_cookie_init(&cookie, NULL) == -1 ) |
---|
175 | return -1; |
---|
176 | |
---|
177 | ROAR_DBG("roar_slp_find_roard_r(*) = ?"); |
---|
178 | |
---|
179 | if ( roar_slp_search(&cookie, ROAR_SLP_URL_TYPE) == -1 ) |
---|
180 | return -1; |
---|
181 | |
---|
182 | ROAR_DBG("roar_slp_find_roard_r(*) = ?"); |
---|
183 | |
---|
184 | if ( cookie.matchcount == 0 ) |
---|
185 | return -1; |
---|
186 | |
---|
187 | ROAR_DBG("roar_slp_find_roard_r(*) = ?"); |
---|
188 | |
---|
189 | url = cookie.match[0].url; |
---|
190 | |
---|
191 | ROAR_DBG("roar_slp_find_roard_r(*): found new server, caching it"); |
---|
192 | memcpy(&cache, &(cookie.match[0]), sizeof(cache)); |
---|
193 | } else { |
---|
194 | ROAR_DBG("roar_slp_find_roard_r(*): cache within TTL, no need to search for server, using cache."); |
---|
195 | url = cache.url; |
---|
196 | } |
---|
197 | |
---|
198 | if ( !strncmp(url, ROAR_SLP_URL_TYPE "://", ROAR_SLP_URL_TYPE_LEN + 3) ) |
---|
199 | offset = 28; |
---|
200 | |
---|
201 | ROAR_DBG("roar_slp_find_roard_r(*): url='%s'", cookie.match[0].url); |
---|
202 | |
---|
203 | strncpy(addr, &(url[offset]), len); |
---|
204 | addr[len-1] = 0; // also just in case. |
---|
205 | |
---|
206 | ROAR_DBG("roar_slp_find_roard_r(*): addr='%s'", addr); |
---|
207 | |
---|
208 | return 0; |
---|
209 | } |
---|
210 | |
---|
211 | //ll |
---|