source: roaraudio/libroar/slp.c @ 3437:4289f31e687f

Last change on this file since 3437:4289f31e687f was 2027:3d40f205da04, checked in by phi, 15 years ago

check for presense of OpenSLP lib ;)

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