source: roaraudio/libroar/slp.c @ 5231:8b30ddb689b8

Last change on this file since 5231:8b30ddb689b8 was 4805:f4f72e985020, checked in by phi, 13 years ago

use sizeof()

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