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