source: roaraudio/libroar/slp.c @ 3517:1a3218a3fc5b

Last change on this file since 3517:1a3218a3fc5b was 3517:1a3218a3fc5b, checked in by phi, 14 years ago

updated license headers, FSF moved office

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