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