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