source: roaraudio/libroar/slp.c @ 5430:70a234a359df

Last change on this file since 5430:70a234a359df was 5381:430b1d26e12d, checked in by phi, 12 years ago

updated copyright years

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