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