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