]> git.sesse.net Git - vlc/blob - modules/access/slp.c
0e5dd6dd1f7e0dd4f572d2b58392b9ef5c84cdf2
[vlc] / modules / access / slp.c
1 /*****************************************************************************
2  * slp.c: SLP access plugin
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
5  * $Id: slp.c,v 1.20 2004/01/25 17:31:22 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 #if 0
54 #define SRVTYPE_TEXT /*N_*/("SLP service type")
55 #define SRVTYPE_LONGTEXT /*N_*/( \
56     "The service type string for SLP queries, including the authority " \
57     "string (if any) for the request. May not be empty." )
58 #endif
59
60 #define ATTRIDS_TEXT N_("SLP attribute identifiers")
61 #define ATTRIDS_LONGTEXT N_( \
62     "This string is a comma separated list of attribute identifiers to " \
63     "search for a playlist title or empty to use all attributes." )
64
65 #define SCOPELIST_TEXT N_("SLP scopes list")
66 #define SCOPELIST_LONGTEXT N_( \
67     "This string is a comma separated list of scope names or empty if you " \
68     "want to use the default scopes. It is used in all SLP queries." )
69
70 #define NAMINGAUTHORITY_TEXT N_("SLP naming authority")
71 #define NAMINGAUTHORITY_LONGTEXT N_( \
72     "This string is a list of naming authorities to search. " \
73     "Use \"*\" for all and the empty string for the default of IANA." )
74
75 #define FILTER_TEXT N_("SLP LDAP filter")
76 #define FILTER_LONGTEXT N_( \
77     "This is a query formulated of attribute pattern matching expressions " \
78     "in the form of an LDAPv3 search filter or empty for all answers." )
79
80 #define LANG_TEXT N_("Language requested in SLP requests")
81 #define LANG_LONGTEXT N_( \
82     "RFC 1766 Language tag for the natural language locale of requests, " \
83     "leave empty to use the default locale. It is used in all SLP queries." )
84
85 vlc_module_begin();
86     set_description( _("SLP input") );
87
88     add_string( "slp-attrids", "", NULL, ATTRIDS_TEXT, ATTRIDS_LONGTEXT,
89                 VLC_TRUE );
90     add_string( "slp-scopelist", "", NULL, SCOPELIST_TEXT,
91                 SCOPELIST_LONGTEXT, VLC_TRUE );
92     add_string( "slp-namingauthority", "*", NULL, NAMINGAUTHORITY_TEXT,
93                 NAMINGAUTHORITY_LONGTEXT, VLC_TRUE );
94     add_string( "slp-filter", "", NULL, FILTER_TEXT, FILTER_LONGTEXT,
95                 VLC_TRUE );
96     add_string( "slp-lang", "", NULL, LANG_TEXT, LANG_LONGTEXT, VLC_TRUE );
97
98     set_capability( "access", 0 );
99     set_callbacks( Open, Close );
100
101     add_submodule();
102         add_shortcut( "demux_slp" );
103         set_capability( "demux", 0 );
104         set_callbacks( Init, End );
105 vlc_module_end();
106
107 /*****************************************************************************
108  * AttrCallback: updates the description of a playlist item
109  *****************************************************************************/
110 static SLPBoolean AttrCallback( SLPHandle slph_slp,
111                            const char * psz_attrlist,
112                            SLPError slpe_errcode,
113                            void * p_cookie )
114 {
115     playlist_item_t * p_playlist_item = (playlist_item_t *)p_cookie;
116
117     /* our callback was only called to tell us there's nothing more to read */
118     if( slpe_errcode == SLP_LAST_CALL )
119     {
120         return SLP_TRUE;
121     }
122
123     /* or there was a problem with getting the data we requested */
124     if( (slpe_errcode != SLP_OK) )
125     {
126 #if 0
127         msg_Err( (vlc_object_t*)NULL,
128                  "AttrCallback got an error %i with attribute %s",
129                  slpe_errcode,
130                  psz_attrlist );
131 #endif
132         return SLP_TRUE;
133     }
134
135     p_playlist_item->psz_name = strdup(psz_attrlist);     /* NULL is checked */
136     return SLP_TRUE;
137 }
138
139 /*****************************************************************************
140  * SrvUrlCallback: adds an entry to the playlist
141  *****************************************************************************/
142 static SLPBoolean SrvUrlCallback( SLPHandle slph_slp,
143                            const char * psz_srvurl,
144                            uint16_t i_lifetime,
145                            SLPError slpe_errcode,
146                            void * p_cookie )
147 {
148     input_thread_t * p_input = (input_thread_t  *)p_cookie;
149     playlist_t * p_playlist;
150     char psz_item[42] = ""; //"udp:@";
151     char * psz_s;                           /* to hold the uri of the stream */
152     SLPHandle slph_slp3;
153     SLPError slpe_result;
154     playlist_item_t * p_playlist_item;
155
156     /* our callback was only called to tell us there's nothing more to read */
157     if( slpe_errcode == SLP_LAST_CALL )
158     {
159         return SLP_TRUE;
160     }
161
162     msg_Dbg(p_input,"URL: %s",psz_srvurl);
163
164     /* or there was a problem with getting the data we requested */
165     if( (slpe_errcode != SLP_OK) )
166     {
167         msg_Err( p_input, "SrvUrlCallback got an error %i with URL %s",
168                  slpe_errcode, psz_srvurl );
169         return SLP_TRUE;
170     }
171
172     /* search the returned address after a double-slash */
173     psz_s = strstr( psz_srvurl, "//" );
174     if( psz_s == NULL )
175     {
176         msg_Err( (input_thread_t *)p_input,
177                  "SrvUrlCallback got a strange string of your libslp" );
178         return SLP_TRUE;
179     }
180     /* skip the slashes */
181     psz_s = &psz_s[2];
182     /* add udp:@ in front of the address */
183     psz_s = strncat( psz_item,
184                      psz_s,
185                      sizeof(psz_item) - strlen(psz_item) - 1 );
186
187     /* create a playlist  item */
188     p_playlist_item = malloc( sizeof( playlist_item_t ) );
189     memset( p_playlist_item, 0, sizeof( playlist_item_t ) );
190     if( p_playlist_item == NULL )
191     {
192         msg_Err( p_input, "out of memory" );
193         return SLP_TRUE;
194     }
195
196     p_playlist_item->psz_name = NULL;
197     p_playlist_item->psz_uri  = strdup( psz_s );
198     p_playlist_item->i_duration = -1;
199     p_playlist_item->i_group = i_group;
200     p_playlist_item->b_enabled = VLC_TRUE;
201
202     /* search the description of the stream */
203     if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
204                  SLP_FALSE,                              /* synchronous ops */
205                  &slph_slp3 ) == SLP_OK )
206     {
207         /* search all attributes */
208         slpe_result = SLPFindAttrs( slph_slp3,
209                                     psz_srvurl,
210                                     config_GetPsz( p_input, "slp-scopelist" ),
211                                     config_GetPsz( p_input, "slp-attrids" ),
212                                     AttrCallback,
213                                     p_playlist_item
214                                   );
215
216         /* we're done, clean up */
217         SLPClose( slph_slp3 );
218     }
219
220     /* add a default name if we found no attribute */
221     if( p_playlist_item->psz_name == NULL )
222     {
223         p_playlist_item->psz_name = strdup( psz_s );
224     }
225
226     /* search the main playlist object */
227     p_playlist = vlc_object_find( (input_thread_t *)p_input,
228                                   VLC_OBJECT_PLAYLIST,
229                                   FIND_ANYWHERE );
230     if( p_playlist == NULL )
231     {
232         msg_Warn( (input_thread_t *)p_input,
233                   "could not find playlist, not adding entries" );
234         return SLP_TRUE;
235     }
236
237     playlist_AddItem( p_playlist,
238                       p_playlist_item,
239                       PLAYLIST_APPEND,
240                       PLAYLIST_END );
241     vlc_object_release( (vlc_object_t *)p_playlist );
242
243     msg_Info( (input_thread_t *)p_input,
244               "added « %s » (lifetime %i) to playlist",
245                psz_srvurl, i_lifetime );
246
247     return SLP_TRUE;
248 }
249
250 /*****************************************************************************
251  * SrvTypeCallback: searchs all servers of a certain type
252  *****************************************************************************/
253 static SLPBoolean SrvTypeCallback( SLPHandle slph_slp,
254                            const char * psz_srvurl,
255                            SLPError slpe_errcode,
256                            void * p_cookie )
257 {
258     input_thread_t * p_input = (input_thread_t  *)p_cookie;
259     SLPError slpe_result;
260     SLPHandle slph_slp2;
261     char *psz_eos;
262     char *psz_service;
263
264     msg_Dbg( p_input, "services: %s", psz_srvurl );
265     /* our callback was only called to tell us there's nothing more to read */
266     if( slpe_errcode == SLP_LAST_CALL )
267     {
268         return SLP_TRUE;
269     }
270
271     msg_Dbg( p_input, "services: %s", psz_srvurl );
272
273     /* or there was a problem with getting the data we requested */
274     if( slpe_errcode != SLP_OK )
275     {
276         msg_Err( p_input, "SrvTypeCallback got an error %i with URL %s",
277                  slpe_errcode, 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, 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 }