]> git.sesse.net Git - vlc/blob - modules/access/slp.c
* now 0.6.0-cvs
[vlc] / modules / access / slp.c
1 /*****************************************************************************
2  * slp.c: SLP access plugin
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: slp.c,v 1.11 2003/05/15 22:27:36 massiot Exp $
6  *
7  * Authors: Loïc Minier <lool@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program 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 program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                                /* malloc */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include <vlc_playlist.h>
33
34 #include <slp.h>
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Open  ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
41 static ssize_t Read  ( input_thread_t *, byte_t *, size_t );
42
43 static int  Init  ( vlc_object_t * );
44 static void End   ( vlc_object_t * );
45 static int  Demux ( input_thread_t * );
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50
51 #define SRVTYPE_TEXT     "SLP service type"
52 #define SRVTYPE_LONGTEXT "The service type string for SLP queries, " \
53                          "including the authority string (if any) for the " \
54                          "request. May not be empty"
55 #define ATTRIDS_TEXT     "SLP attribute identifiers"
56 #define ATTRIDS_LONGTEXT "This string is a comma separated list of " \
57                          "attribute identifiers to search for a playlist "\
58                          "title or empty to use all attributes"
59 #define SCOPELIST_TEXT     "SLP scopes list"
60 #define SCOPELIST_LONGTEXT "This string is a comma separated list of scope " \
61                            "names or empty if you want to use the default "  \
62                            "scopes; it is used in all SLP queries"
63 #define NAMINGAUTHORITY_TEXT     "SLP naming authority"
64 #define NAMINGAUTHORITY_LONGTEXT "This string is a list of naming " \
65                                  "authorities to search. Use \"*\" for all " \
66                                  "and the empty string for the default of " \
67                                  "IANA"
68 #define FILTER_TEXT     "SLP LDAP filter"
69 #define FILTER_LONGTEXT "This is a query formulated of attribute pattern " \
70                         "matching expressions in the form of an LDAPv3 "   \
71                         "search filter or empty for all answers"
72 #define LANG_TEXT     "Language requested in SLP requests"
73 #define LANG_LONGTEXT "RFC 1766 Language Tag for the natural language " \
74                       "locale of requests, leave empty to use the "     \
75                       "default locale; it is used in all SLP queries"
76
77 vlc_module_begin();
78     set_description( _("SLP input") );
79     add_category_hint( N_("slp"), NULL, VLC_TRUE );
80     add_string( "slp-attrids", "", NULL, ATTRIDS_TEXT, ATTRIDS_LONGTEXT, VLC_TRUE );
81     add_string( "slp-scopelist", "", NULL, SCOPELIST_TEXT,
82                 SCOPELIST_LONGTEXT, VLC_TRUE );
83     add_string( "slp-namingauthority", "*", NULL, NAMINGAUTHORITY_TEXT,
84                 NAMINGAUTHORITY_LONGTEXT, VLC_TRUE );
85     add_string( "slp-filter", "", NULL, FILTER_TEXT, FILTER_LONGTEXT, VLC_TRUE );
86     add_string( "slp-lang", "", NULL, LANG_TEXT, LANG_LONGTEXT, VLC_TRUE );
87     add_submodule();
88         set_capability( "access", 0 );
89         set_callbacks( Open, Close );
90     add_submodule();
91         add_shortcut( "demux_slp" );
92         set_capability( "demux", 0 );
93         set_callbacks( Init, End );
94 vlc_module_end();
95
96 /*****************************************************************************
97  * AttrCallback: updates the description of a playlist item
98  *****************************************************************************/
99 static SLPBoolean AttrCallback( SLPHandle slph_slp,
100                            const char * psz_attrlist,
101                            SLPError slpe_errcode,
102                            void * p_cookie )
103 {
104     playlist_item_t * p_playlist_item = (playlist_item_t *)p_cookie;
105
106     /* our callback was only called to tell us there's nothing more to read */
107     if( slpe_errcode == SLP_LAST_CALL )
108     {
109         return SLP_TRUE;
110     }
111
112     /* or there was a problem with getting the data we requested */
113     if( (slpe_errcode != SLP_OK) )
114     {
115 /*        msg_Err( (vlc_object_t*)NULL,
116                  "AttrCallback got an error %i with attribute %s",
117                  slpe_errcode,
118                  psz_attrlist ); */
119         return SLP_TRUE;
120     }
121
122     p_playlist_item->psz_name = strdup(psz_attrlist);     /* NULL is checked */
123     return SLP_TRUE;
124 }
125
126 /*****************************************************************************
127  * SrvUrlCallback: adds an entry to the playlist
128  *****************************************************************************/
129 static SLPBoolean SrvUrlCallback( SLPHandle slph_slp,
130                            const char * psz_srvurl,
131                            uint16_t i_lifetime,
132                            SLPError slpe_errcode,
133                            void * p_cookie )
134 {
135     input_thread_t * p_input = (input_thread_t  *)p_cookie;
136     playlist_t * p_playlist;
137     char psz_item[42] = "udp:@";
138     char * psz_s;                           /* to hold the uri of the stream */
139     SLPHandle slph_slp3;
140     SLPError slpe_result;
141     playlist_item_t * p_playlist_item;
142
143     /* our callback was only called to tell us there's nothing more to read */
144     if( slpe_errcode == SLP_LAST_CALL )
145     {
146         return SLP_TRUE;
147     }
148
149     /* or there was a problem with getting the data we requested */
150     if( (slpe_errcode != SLP_OK) )
151     {
152         msg_Err( p_input,
153                  "SrvUrlCallback got an error %i with URL %s",
154                  slpe_errcode,
155                  psz_srvurl );
156         return SLP_TRUE;
157     }
158
159     /* search the returned address after a double-slash */
160     psz_s = strstr( psz_srvurl, "//" );
161     if( psz_s == NULL )
162     {
163         msg_Err( (input_thread_t *)p_input,
164                  "SrvUrlCallback got a strange string of your libslp" );
165         return SLP_TRUE;
166     }
167     /* skip the slashes */
168     psz_s = &psz_s[2];
169     /* add udp:@ in front of the address */
170     psz_s = strncat( psz_item,
171                      psz_s,
172                      sizeof(psz_item) - strlen(psz_item) - 1 );
173
174     /* create a playlist  item */
175     p_playlist_item = malloc( sizeof( playlist_item_t ) );
176     if( p_playlist_item == NULL )
177     {
178         msg_Err( p_input, "out of memory" );
179         return SLP_TRUE;
180     }
181
182     p_playlist_item->psz_name = NULL;
183     p_playlist_item->psz_uri  = strdup( psz_s );
184     p_playlist_item->i_type = 0;
185     p_playlist_item->i_status = 0;
186     p_playlist_item->b_autodeletion = VLC_FALSE;
187
188     /* search the description of the stream */
189     if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
190                  SLP_FALSE,                              /* synchronous ops */
191                  &slph_slp3 ) == SLP_OK )
192     {
193         /* search all attributes */
194         slpe_result = SLPFindAttrs( slph_slp3,
195                                     psz_srvurl,
196                                     config_GetPsz( p_input, "slp-scopelist" ),
197                                     config_GetPsz( p_input, "slp-attrids" ),
198                                     AttrCallback,
199                                     p_playlist_item
200                                   );
201
202         /* we're done, clean up */
203         SLPClose( slph_slp3 );
204     }
205
206     /* add a default name if we found no attribute */
207     if( p_playlist_item->psz_name == NULL )
208     {
209         p_playlist_item->psz_name = strdup( psz_s );
210     }
211
212     /* search the main playlist object */
213     p_playlist = vlc_object_find( (input_thread_t *)p_input,
214                                   VLC_OBJECT_PLAYLIST,
215                                   FIND_ANYWHERE );
216     if( p_playlist == NULL )
217     {
218         msg_Warn( (input_thread_t *)p_input,
219                   "could not find playlist, not adding entries" );
220         return SLP_TRUE;
221     }
222
223     playlist_AddItem( p_playlist,
224                       p_playlist_item,
225                       PLAYLIST_APPEND,
226                       PLAYLIST_END );
227     vlc_object_release( (vlc_object_t *)p_playlist );
228
229     msg_Info( (input_thread_t *)p_input,
230              "added « %s » (lifetime %i) to playlist",
231              psz_srvurl,
232              i_lifetime );
233
234     return SLP_TRUE;
235 }
236
237 /*****************************************************************************
238  * SrvTypeCallback: searchs all servers of a certain type
239  *****************************************************************************/
240 static SLPBoolean SrvTypeCallback( SLPHandle slph_slp,
241                            const char * psz_srvurl,
242                            SLPError slpe_errcode,
243                            void * p_cookie )
244 {
245     input_thread_t * p_input = (input_thread_t  *)p_cookie;
246     SLPError slpe_result;
247     SLPHandle slph_slp2;
248
249     /* our callback was only called to tell us there's nothing more to read */
250     if( slpe_errcode == SLP_LAST_CALL )
251     {
252         return SLP_TRUE;
253     }
254
255     /* or there was a problem with getting the data we requested */
256     if( slpe_errcode != SLP_OK )
257     {
258         msg_Err( p_input,
259                  "SrvTypeCallback got an error %i with URL %s",
260                  slpe_errcode,
261                  psz_srvurl );
262         return SLP_TRUE;
263     }
264
265     /* get a new handle to the library */
266     if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
267                  SLP_FALSE,                              /* synchronous ops */
268                  &slph_slp2 ) == SLP_OK )
269     {
270         /* search for services */
271         slpe_result = SLPFindSrvs( slph_slp2,
272                                    psz_srvurl,
273                                    config_GetPsz( p_input, "slp-scopelist" ),
274                                    config_GetPsz( p_input, "slp-filter" ),
275                                    SrvUrlCallback,
276                                    p_input );
277
278         SLPClose( slph_slp2 );
279
280         if( slpe_result != SLP_OK )
281         {
282             msg_Err( p_input,
283                      "SLPFindSrvs error %i finding servers of type %s",
284                      slpe_result,
285                      psz_srvurl );
286         }
287     }
288
289     return SLP_TRUE;
290 }
291
292 /*****************************************************************************
293  * Open: initialize library for the access module
294  *****************************************************************************/
295 static int Open( vlc_object_t * p_this )
296 {
297     input_thread_t * p_input = (input_thread_t *)p_this;
298     SLPError         slpe_result;
299     SLPHandle        slph_slp;
300     playlist_t *     p_playlist;
301
302     /* remove the "slp:" entry of the playlist */
303     p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
304                                                  FIND_ANYWHERE );
305     if( !p_playlist )
306     {
307         msg_Warn( p_input, "hey I can't find the main playlist, I need it" );
308         return VLC_FALSE;
309     }
310
311     p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
312     vlc_object_release( (vlc_object_t *)p_playlist );
313
314     /* get a new handle to the library */
315     if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
316                  SLP_FALSE,                              /* synchronous ops */
317                  &slph_slp ) == SLP_OK )
318     {
319         /* search all service types */
320         slpe_result =
321             SLPFindSrvTypes( slph_slp,
322                              config_GetPsz( p_input, "slp-namingauthority" ),
323                              config_GetPsz( p_input, "slp-scopelist" ),
324                              SrvTypeCallback,
325                              p_input );
326         /* we're done, clean up */
327         SLPClose( slph_slp );
328     }
329
330     if( !p_input->psz_demux || !*p_input->psz_demux )
331     {
332         p_input->psz_demux = "demux_slp";
333     }
334
335     p_input->pf_read = Read;
336     p_input->pf_set_program = NULL;
337     p_input->pf_set_area = NULL;
338     p_input->pf_seek = NULL;
339
340     vlc_mutex_lock( &p_input->stream.stream_lock );
341     p_input->stream.b_pace_control = VLC_FALSE;
342     p_input->stream.b_seekable = VLC_FALSE;
343     p_input->stream.p_selected_area->i_tell = 0;
344     p_input->stream.p_selected_area->i_size = 0;
345     p_input->stream.i_method = INPUT_METHOD_SLP;
346     vlc_mutex_unlock( &p_input->stream.stream_lock );
347     p_input->i_mtu = 0;
348
349     return VLC_SUCCESS;
350 }
351
352 /*****************************************************************************
353  * Close: close access
354  *****************************************************************************/
355 static void Close( vlc_object_t * p_this )
356 {
357     return;
358 }
359
360 /*****************************************************************************
361  * Read: should fill but zeroes the buffer
362  *****************************************************************************/
363 static ssize_t Read  ( input_thread_t *p_input, byte_t *p_buffer, size_t s )
364 {
365     memset( p_buffer, 0, s );
366     return s;
367 }
368
369 /*****************************************************************************
370  * Init: initialize demux
371  *****************************************************************************/
372 static int Init ( vlc_object_t *p_this )
373 {
374     input_thread_t *p_input = (input_thread_t *)p_this;
375
376     if( p_input->stream.i_method != INPUT_METHOD_SLP )
377     {
378         return VLC_FALSE;
379     }
380
381     p_input->pf_demux  = Demux;
382     p_input->pf_rewind = NULL;
383
384     return VLC_SUCCESS;
385 }
386
387 /*****************************************************************************
388  * Demux: should demux but does nothing
389  *****************************************************************************/
390 static int Demux ( input_thread_t * p_input )
391 {
392     return 0;
393 }
394
395 /*****************************************************************************
396  * End: end demux
397  *****************************************************************************/
398 static void End ( vlc_object_t *p_this )
399 {
400     return;
401 }
402