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