]> git.sesse.net Git - vlc/blob - modules/access/slp.c
* configure.ac.in:
[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.3 2003/01/16 23:25:55 lool 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 <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include <vlc_playlist.h>
31
32 #include <slp.h>
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 static int  Open  ( vlc_object_t * );
38 static void Close ( vlc_object_t * );
39 static ssize_t Read  ( input_thread_t *, byte_t *, size_t );
40
41 static int  Init  ( vlc_object_t * );
42 static void End   ( vlc_object_t * );
43 static int  Demux ( input_thread_t * );
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48
49 #define SRVTYPE_TEXT "SLP service type"
50 #define SRVTYPE_LONGTEXT "The service type string for SLP queries, " \
51                          "including the authority string (if any) for the " \
52                          "request. May not be empty"
53 #define SCOPELIST_TEXT "SLP scopes list"
54 #define SCOPELIST_LONGTEXT "This string is a comma separated list of scope " \
55                            "names or empty if you want to use the default "  \
56                            "scopes"
57 #define FILTER_TEXT "SLP LDAP filter"
58 #define FILTER_LONGTEXT "This is a query formulated of attribute pattern " \
59                         "matching expressions in the form of an LDAPv3 "   \
60                         "search filter or empty for all answers"
61 #define LANG_TEXT "language asked in SLP requests"
62 #define LANG_LONGTEXT "RFC 1766 Language Tag for the natural language " \
63                       "locale of requests, leave empty to use the "     \
64                       "default locale"
65
66 vlc_module_begin();
67     set_description( _("SLP access module") );
68     add_category_hint( N_("slp"), NULL );
69     add_string( "slp-srvtype", "service:vls.services.videolan.org:udpm",
70                 NULL, SRVTYPE_TEXT, SRVTYPE_LONGTEXT );
71     add_string( "slp-scopelist", "", NULL, SCOPELIST_TEXT,
72                 SCOPELIST_LONGTEXT );
73     add_string( "slp-filter", "", NULL, FILTER_TEXT, FILTER_LONGTEXT );
74     add_string( "slp-lang", "", NULL, LANG_TEXT, LANG_LONGTEXT );
75     add_submodule();
76         set_capability( "access", 0 );
77         set_callbacks( Open, Close );
78     add_submodule();
79         add_shortcut( "demux_slp" );
80         set_capability( "demux", 0 );
81         set_callbacks( Init, End );
82 vlc_module_end();
83
84 /*****************************************************************************
85  * SrvUrlCallback: adds an entry to the playlist
86  *****************************************************************************/
87 static SLPBoolean SrvUrlCallback( SLPHandle hslp,
88                            const char * psz_srvurl,
89                            uint16_t i_lifetime,
90                            SLPError slpe_errcode,
91                            void * p_input )
92 {
93     playlist_t * p_playlist;
94     char psz_item[42] = "udp:@";
95     char * psz_s;
96
97     if( slpe_errcode == SLP_OK )
98     {
99         p_playlist = vlc_object_find( (input_thread_t *)p_input,
100                                       VLC_OBJECT_PLAYLIST,
101                                       FIND_ANYWHERE );
102         if( p_playlist == NULL )
103         {
104             msg_Dbg( (input_thread_t *)p_input, "could not find playlist" );
105             return SLP_FALSE;
106         }
107
108         /* search the returned address after a double-slash */
109         psz_s = strstr( psz_srvurl, "//" );
110         /* skip the slashes */
111         psz_s = &psz_s[2];
112         if( psz_s == NULL )
113         {
114             msg_Dbg( (input_thread_t *)p_input,
115                      "something went wrong with your libslp" );
116             return SLP_FALSE;
117         }
118         /* add udp:@ in front of the address */
119         psz_s = strncat( psz_item,
120                          psz_s,
121                          sizeof(psz_item) - strlen(psz_item) - 1 );
122         playlist_Add( p_playlist, psz_s,
123                       PLAYLIST_APPEND,
124                       PLAYLIST_END );
125         vlc_object_release( (vlc_object_t *)p_playlist );
126
127         msg_Dbg( (input_thread_t *)p_input,
128                  "added « %s » (lifetime %i) to playlist",
129                  psz_srvurl,
130                  i_lifetime );
131     }
132
133     return SLP_TRUE;
134 }
135
136 /*****************************************************************************
137  * Open: initialize library for the access module
138  *****************************************************************************/
139 static int Open( vlc_object_t * p_this )
140 {
141     input_thread_t * p_input = (input_thread_t *)p_this;
142     char *           psz_name = strdup(p_input->psz_name);
143     SLPError         slpe_result;
144     SLPHandle        slph_slp;
145     playlist_t *     p_playlist;
146
147     /* remove the "slp:" entry of the playlist */
148     p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
149                                                  FIND_ANYWHERE );
150     if( !p_playlist )
151     {
152         msg_Err( p_input, "can't find playlist" );
153         return -1;
154     }
155
156     p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
157     vlc_object_release( (vlc_object_t *)p_playlist );
158
159     /* get a new handle to the library */
160     if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
161                  SLP_FALSE,                              /* synchronous ops */
162                  &slph_slp ) == SLP_OK )
163     {
164         /* search for services */
165         slpe_result = SLPFindSrvs( slph_slp,
166                                    config_GetPsz( p_input, "slp-srvtype" ),
167                                    config_GetPsz( p_input, "slp-scopelist" ),
168                                    config_GetPsz( p_input, "slp-filter" ),
169                                    SrvUrlCallback,
170                                    p_input );
171         if( slpe_result != SLP_OK )
172         {
173             msg_Dbg( p_input,
174                      "slp error opening %s: %i",
175                      psz_name,
176                      slpe_result );
177         }
178         /* we're done, clean up */
179         SLPClose( slph_slp );
180     }
181
182     if( !p_input->psz_demux || !*p_input->psz_demux )
183     {
184         p_input->psz_demux = "demux_slp";
185     }
186
187     p_input->pf_read = Read;
188     p_input->pf_set_program = NULL;
189     p_input->pf_set_area = NULL;
190     p_input->pf_seek = NULL;
191
192     vlc_mutex_lock( &p_input->stream.stream_lock );
193     p_input->stream.b_pace_control = VLC_FALSE;
194     p_input->stream.b_seekable = VLC_FALSE;
195     p_input->stream.b_connected = VLC_TRUE;
196     p_input->stream.p_selected_area->i_tell = 0;
197     p_input->stream.p_selected_area->i_size = 0;
198     p_input->stream.i_method = INPUT_METHOD_SLP;
199     vlc_mutex_unlock( &p_input->stream.stream_lock );
200     p_input->i_mtu = 0;
201
202     return 0;
203 }
204
205 /*****************************************************************************
206  * Close: close access
207  *****************************************************************************/
208 static void Close( vlc_object_t * p_this )
209 {
210     return;
211 }
212
213 /*****************************************************************************
214  * Read: should fill but zeroes the buffer
215  *****************************************************************************/
216 static ssize_t Read  ( input_thread_t *p_input, byte_t *p_buffer, size_t s )
217 {
218     memset( p_buffer, 0, s );
219     return s;
220 }
221
222 /*****************************************************************************
223  * Init: initialize demux
224  *****************************************************************************/
225 int Init ( vlc_object_t *p_this )
226 {
227     input_thread_t *p_input = (input_thread_t *)p_this;
228
229     if( p_input->stream.i_method != INPUT_METHOD_SLP )
230     {
231         return -1;
232     }
233
234     p_input->pf_demux  = Demux;
235     p_input->pf_rewind = NULL;
236
237     return 0;
238 }
239
240 /*****************************************************************************
241  * Demux: should demux but does nothing
242  *****************************************************************************/
243 static int Demux ( input_thread_t * p_input )
244 {
245     return 0;
246 }
247
248 /*****************************************************************************
249  * End: end demux
250  *****************************************************************************/
251 static void End ( vlc_object_t *p_this )
252 {
253     return;
254 }
255