]> git.sesse.net Git - vlc/blob - modules/access/slp.c
2be2436c2d66ea54853ffd7c17f5e47123ad6ad7
[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.16 2003/09/14 16:41:48 zorglub 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     set_capability( "access", 0 );
88     set_callbacks( Open, Close );
89
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     msg_Dbg(p_input,"URL: %s",psz_srvurl);
150
151     /* or there was a problem with getting the data we requested */
152     if( (slpe_errcode != SLP_OK) )
153     {
154         msg_Err( p_input,
155                  "SrvUrlCallback got an error %i with URL %s",
156                  slpe_errcode,
157                  psz_srvurl );
158         return SLP_TRUE;
159     }
160
161     /* search the returned address after a double-slash */
162     psz_s = strstr( psz_srvurl, "//" );
163     if( psz_s == NULL )
164     {
165         msg_Err( (input_thread_t *)p_input,
166                  "SrvUrlCallback got a strange string of your libslp" );
167         return SLP_TRUE;
168     }
169     /* skip the slashes */
170     psz_s = &psz_s[2];
171     /* add udp:@ in front of the address */
172     psz_s = strncat( psz_item,
173                      psz_s,
174                      sizeof(psz_item) - strlen(psz_item) - 1 );
175
176     /* create a playlist  item */
177     p_playlist_item = malloc( sizeof( playlist_item_t ) );
178     if( p_playlist_item == NULL )
179     {
180         msg_Err( p_input, "out of memory" );
181         return SLP_TRUE;
182     }
183
184     p_playlist_item->psz_name = NULL;
185     p_playlist_item->psz_uri  = strdup( psz_s );
186     p_playlist_item->i_type = 0;
187     p_playlist_item->i_status = 0;
188     p_playlist_item->b_autodeletion = VLC_FALSE;
189     p_playlist_item->i_options = 0;
190     p_playlist_item->ppsz_options = 0;
191
192     /* search the description of the stream */
193     if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
194                  SLP_FALSE,                              /* synchronous ops */
195                  &slph_slp3 ) == SLP_OK )
196     {
197         /* search all attributes */
198         slpe_result = SLPFindAttrs( slph_slp3,
199                                     psz_srvurl,
200                                     config_GetPsz( p_input, "slp-scopelist" ),
201                                     config_GetPsz( p_input, "slp-attrids" ),
202                                     AttrCallback,
203                                     p_playlist_item
204                                   );
205
206         /* we're done, clean up */
207         SLPClose( slph_slp3 );
208     }
209
210     /* add a default name if we found no attribute */
211     if( p_playlist_item->psz_name == NULL )
212     {
213         p_playlist_item->psz_name = strdup( psz_s );
214     }
215
216     /* search the main playlist object */
217     p_playlist = vlc_object_find( (input_thread_t *)p_input,
218                                   VLC_OBJECT_PLAYLIST,
219                                   FIND_ANYWHERE );
220     if( p_playlist == NULL )
221     {
222         msg_Warn( (input_thread_t *)p_input,
223                   "could not find playlist, not adding entries" );
224         return SLP_TRUE;
225     }
226
227     playlist_AddItem( p_playlist,
228                       p_playlist_item,
229                       PLAYLIST_APPEND,
230                       PLAYLIST_END );
231     vlc_object_release( (vlc_object_t *)p_playlist );
232
233     msg_Info( (input_thread_t *)p_input,
234              "added « %s » (lifetime %i) to playlist",
235              psz_srvurl,
236              i_lifetime );
237
238     return SLP_TRUE;
239 }
240
241 /*****************************************************************************
242  * SrvTypeCallback: searchs all servers of a certain type
243  *****************************************************************************/
244 static SLPBoolean SrvTypeCallback( SLPHandle slph_slp,
245                            const char * psz_srvurl,
246                            SLPError slpe_errcode,
247                            void * p_cookie )
248 {
249     input_thread_t * p_input = (input_thread_t  *)p_cookie;
250     SLPError slpe_result;
251     SLPHandle slph_slp2;
252     char *psz_eos;
253     char *psz_service;
254
255     msg_Dbg(p_input,"Services: %s",psz_srvurl);
256     /* our callback was only called to tell us there's nothing more to read */
257     if( slpe_errcode == SLP_LAST_CALL )
258     {
259         return SLP_TRUE;
260     }
261     
262     msg_Dbg(p_input,"Services: %s",psz_srvurl);
263
264     /* or there was a problem with getting the data we requested */
265     if( slpe_errcode != SLP_OK )
266     {
267         msg_Err( p_input,
268                  "SrvTypeCallback got an error %i with URL %s",
269                  slpe_errcode,
270                  psz_srvurl );
271         return SLP_TRUE;
272     }
273
274     /* get a new handle to the library */
275     if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
276                  SLP_FALSE,                              /* synchronous ops */
277                  &slph_slp2 ) == SLP_OK )
278     {
279         /* search for services */
280         while(1)
281         {
282             if( *psz_srvurl == '\0')  break;
283
284             if( !strncasecmp( psz_srvurl, "service:", 8 ) )
285             {
286                 while(1)
287                 {
288                     psz_eos = strchr( psz_srvurl, ',');
289                     if(!psz_eos) break;
290                     if(!strncasecmp(psz_eos+1,"service:",8)) break;
291                 }
292         
293                 if(psz_eos) 
294                     *psz_eos = '\0';
295
296                 psz_service = strdup( psz_srvurl);
297                
298                 msg_Dbg(p_input,"Getting details for %s",psz_service);
299                 
300                 slpe_result = SLPFindSrvs( slph_slp2,
301                                    psz_service,
302                                    config_GetPsz( p_input, "slp-scopelist" ),
303                                    config_GetPsz( p_input, "slp-filter" ),
304                                    SrvUrlCallback,
305                                    p_input );
306
307                 if(psz_eos)
308                     psz_srvurl = psz_eos;
309                 
310 //                SLPClose( slph_slp2 );
311
312                 if( slpe_result != SLP_OK )
313                 {
314                    msg_Err( p_input,
315                            "SLPFindSrvs error %i finding servers of type %s",
316                            slpe_result,
317                            psz_service );
318                 }
319             }
320             psz_srvurl++;
321         }
322     }
323                 SLPClose( slph_slp2 );
324
325     return SLP_TRUE;
326 }
327
328 /*****************************************************************************
329  * Open: initialize library for the access module
330  *****************************************************************************/
331 static int Open( vlc_object_t * p_this )
332 {
333     input_thread_t * p_input = (input_thread_t *)p_this;
334     SLPError         slpe_result;
335     SLPHandle        slph_slp;
336     playlist_t *     p_playlist;
337
338     /* remove the "slp:" entry of the playlist */
339     p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
340                                                  FIND_ANYWHERE );
341     if( !p_playlist )
342     {
343         msg_Warn( p_input, "hey I can't find the main playlist, I need it" );
344         return VLC_FALSE;
345     }
346
347     p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
348     vlc_object_release( (vlc_object_t *)p_playlist );
349
350     /* get a new handle to the library */
351     if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
352                  SLP_FALSE,                              /* synchronous ops */
353                  &slph_slp ) == SLP_OK )
354     {
355         /* search all service types */
356         slpe_result =
357             SLPFindSrvTypes( slph_slp,
358                              config_GetPsz( p_input, "slp-namingauthority" ),
359                              config_GetPsz( p_input, "slp-scopelist" ),
360                              SrvTypeCallback,
361                              p_input );
362         /* we're done, clean up */
363         SLPClose( slph_slp );
364     }
365
366     if( !p_input->psz_demux || !*p_input->psz_demux )
367     {
368         p_input->psz_demux = "demux_slp";
369     }
370
371     p_input->pf_read = Read;
372     p_input->pf_set_program = NULL;
373     p_input->pf_set_area = NULL;
374     p_input->pf_seek = NULL;
375
376     vlc_mutex_lock( &p_input->stream.stream_lock );
377     p_input->stream.b_pace_control = VLC_FALSE;
378     p_input->stream.b_seekable = VLC_FALSE;
379     p_input->stream.p_selected_area->i_tell = 0;
380     p_input->stream.p_selected_area->i_size = 0;
381     p_input->stream.i_method = INPUT_METHOD_SLP;
382     vlc_mutex_unlock( &p_input->stream.stream_lock );
383     p_input->i_mtu = 0;
384
385     return VLC_SUCCESS;
386 }
387
388 /*****************************************************************************
389  * Close: close access
390  *****************************************************************************/
391 static void Close( vlc_object_t * p_this )
392 {
393     return;
394 }
395
396 /*****************************************************************************
397  * Read: should fill but zeroes the buffer
398  *****************************************************************************/
399 static ssize_t Read  ( input_thread_t *p_input, byte_t *p_buffer, size_t s )
400 {
401     memset( p_buffer, 0, s );
402     return s;
403 }
404
405 /*****************************************************************************
406  * Init: initialize demux
407  *****************************************************************************/
408 static int Init ( vlc_object_t *p_this )
409 {
410     input_thread_t *p_input = (input_thread_t *)p_this;
411
412     if( p_input->stream.i_method != INPUT_METHOD_SLP )
413     {
414         return VLC_FALSE;
415     }
416
417     p_input->pf_demux  = Demux;
418     p_input->pf_demux_control = demux_vaControlDefault;
419     p_input->pf_rewind = NULL;
420
421     return VLC_SUCCESS;
422 }
423
424 /*****************************************************************************
425  * Demux: should demux but does nothing
426  *****************************************************************************/
427 static int Demux ( input_thread_t * p_input )
428 {
429     return 0;
430 }
431
432 /*****************************************************************************
433  * End: end demux
434  *****************************************************************************/
435 static void End ( vlc_object_t *p_this )
436 {
437     return;
438 }
439