]> git.sesse.net Git - vlc/blob - modules/control/telnet.c
For consistency, remove references to vlc from libvlc
[vlc] / modules / control / telnet.c
1 /*****************************************************************************
2  * telnet.c: VLM interface plugin
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Simon Latapie <garf@videolan.org>
8  *          Laurent Aimar <fenrir@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/intf.h>
32
33 #include <vlc/input.h>
34
35 #include <sys/stat.h>
36
37 #include <errno.h>
38 #include <fcntl.h>
39
40 #ifdef HAVE_SYS_TIME_H
41 #    include <sys/time.h>
42 #endif
43
44 #ifdef HAVE_UNISTD_H
45 #   include <unistd.h>
46 #endif
47
48 #include "network.h"
49 #include "vlc_url.h"
50
51 #include "vlc_vlm.h"
52
53 #define READ_MODE_PWD 1
54 #define READ_MODE_CMD 2
55 #define WRITE_MODE_PWD 3 // when we write the word "Password:"
56 #define WRITE_MODE_CMD 4
57
58 /* telnet commands */
59 #define TEL_WILL    251
60 #define TEL_WONT    252
61 #define TEL_DO      253
62 #define TEL_DONT    254
63 #define TEL_IAC     255
64 #define TEL_ECHO    1
65
66 /*****************************************************************************
67  * Module descriptor
68  *****************************************************************************/
69 static int  Open ( vlc_object_t * );
70 static void Close( vlc_object_t * );
71
72 #define TELNETHOST_TEXT N_( "Host" )
73 #define TELNETHOST_LONGTEXT N_( "This is the host on which the " \
74     "interface will listen. It defaults to all network interfaces (0.0.0.0)." \
75     " If you want this interface to be available only on the local " \
76     "machine, enter \"127.0.0.1\"." )
77 #define TELNETPORT_TEXT N_( "Port" )
78 #define TELNETPORT_LONGTEXT N_( "This is the TCP port on which this " \
79     "interface will listen. It defaults to 4212." )
80 #define TELNETPORT_DEFAULT 4212
81 #define TELNETPWD_TEXT N_( "Password" )
82 #define TELNETPWD_LONGTEXT N_( "A single administration password is used " \
83     "to protect this interface. The default value is \"admin\"." )
84 #define TELNETPWD_DEFAULT "admin"
85
86 vlc_module_begin();
87     set_shortname( "Telnet" );
88     set_category( CAT_INTERFACE );
89     set_subcategory( SUBCAT_INTERFACE_CONTROL );
90     add_string( "telnet-host", "", NULL, TELNETHOST_TEXT,
91                  TELNETHOST_LONGTEXT, VLC_TRUE );
92     add_integer( "telnet-port", TELNETPORT_DEFAULT, NULL, TELNETPORT_TEXT,
93                  TELNETPORT_LONGTEXT, VLC_TRUE );
94     add_string( "telnet-password", TELNETPWD_DEFAULT, NULL, TELNETPWD_TEXT,
95                 TELNETPWD_LONGTEXT, VLC_TRUE );
96     set_description( _("VLM remote control interface") );
97     add_category_hint( "VLM", NULL, VLC_FALSE );
98     set_capability( "interface", 0 );
99     set_callbacks( Open , Close );
100 vlc_module_end();
101
102 /*****************************************************************************
103  * Local prototypes.
104  *****************************************************************************/
105 static void Run( intf_thread_t * );
106
107 typedef struct
108 {
109     int        i_mode; /* read or write */
110     int        fd;
111     char       buffer_read[1000]; // 1000 byte per command should be sufficient
112     char      *buffer_write;
113     char      *p_buffer_read;
114     char      *p_buffer_write; // the position in the buffer
115     int        i_buffer_write; // the number of byte we still have to send
116     int        i_tel_cmd; // for specific telnet commands
117
118 } telnet_client_t;
119
120 static char *MessageToString( vlm_message_t *, int );
121 static void Write_message( telnet_client_t *, vlm_message_t *, char *, int );
122
123 struct intf_sys_t
124 {
125    telnet_client_t **clients;
126    int             i_clients;
127    int             *pi_fd;
128    vlm_t           *mediatheque;
129 };
130
131 /* 
132  * getPort: Decide which port to use. There are two possibilities to
133  * specify a port: integrated in the --telnet-host option with :PORT
134  * or using the --telnet-port option. The --telnet-port option has
135  * precedence. 
136  * This code relies upon the fact the url.i_port is 0 if the :PORT 
137  * option is missing from --telnet-host.
138  */
139 static int getPort(intf_thread_t *p_intf, vlc_url_t url, int i_port)
140 {
141     // Print error if two different ports have been specified
142     if (url.i_port != 0  &&
143         i_port != TELNETPORT_DEFAULT && 
144         url.i_port != i_port )
145     {
146             msg_Err( p_intf, "ignoring port %d and using %d", url.i_port,
147                  i_port);
148     }
149     if (i_port != TELNETPORT_DEFAULT)
150     {
151             return i_port;
152     }
153     if (url.i_port != 0)
154     {
155             return url.i_port;
156     }
157     return i_port;
158 }
159
160 /*****************************************************************************
161  * Open: initialize dummy interface
162  *****************************************************************************/
163 static int Open( vlc_object_t *p_this )
164 {
165     intf_thread_t *p_intf = (intf_thread_t*) p_this;
166     vlm_t *mediatheque;
167     char *psz_address;
168     vlc_url_t url;
169     int i_telnetport;
170
171     if( !(mediatheque = vlm_New( p_intf )) )
172     {
173         msg_Err( p_intf, "cannot start VLM" );
174         return VLC_EGENERIC;
175     }
176
177     msg_Info( p_intf, "using the VLM interface plugin..." );
178
179     i_telnetport = config_GetInt( p_intf, "telnet-port" );
180     psz_address  = config_GetPsz( p_intf, "telnet-host" );
181
182     vlc_UrlParse(&url, psz_address, 0);
183
184     // There might be two ports given, resolve any potentially
185     // conflict
186     url.i_port = getPort(p_intf, url, i_telnetport);
187
188     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
189     if( ( p_intf->p_sys->pi_fd = net_ListenTCP( p_intf, url.psz_host, url.i_port ) )
190                 == NULL )
191     {
192         msg_Err( p_intf, "cannot listen for telnet" );
193         vlc_UrlClean(&url);
194         free( psz_address );
195         free( p_intf->p_sys );
196         return VLC_EGENERIC;
197     }
198     msg_Info( p_intf, 
199               "telnet interface started on interface %s %d",
200               url.psz_host, url.i_port );
201
202     p_intf->p_sys->i_clients   = 0;
203     p_intf->p_sys->clients     = NULL;
204     p_intf->p_sys->mediatheque = mediatheque;
205     p_intf->pf_run = Run;
206
207     vlc_UrlClean(&url);
208     free( psz_address );
209     return VLC_SUCCESS;
210 }
211
212 /*****************************************************************************
213  * Close:
214  *****************************************************************************/
215 static void Close( vlc_object_t *p_this )
216 {
217     intf_thread_t *p_intf = (intf_thread_t*)p_this;
218     intf_sys_t    *p_sys  = p_intf->p_sys;
219     int i;
220
221     for( i = 0; i < p_sys->i_clients; i++ )
222     {
223         telnet_client_t *cl = p_sys->clients[i];
224
225         net_Close( cl->fd );
226         free( cl );
227     }
228     if( p_sys->clients != NULL ) free( p_sys->clients );
229
230     net_ListenClose( p_sys->pi_fd );
231
232     vlm_Delete( p_sys->mediatheque );
233
234     free( p_sys );
235 }
236
237 /*****************************************************************************
238  * Run: main loop
239  *****************************************************************************/
240 static void Run( intf_thread_t *p_intf )
241 {
242     intf_sys_t     *p_sys = p_intf->p_sys;
243     struct timeval  timeout;
244     char           *psz_password;
245
246     psz_password = config_GetPsz( p_intf, "telnet-password" );
247
248     while( !p_intf->b_die )
249     {
250         fd_set fds_read, fds_write;
251         int    i_handle_max = 0;
252         int    i_ret, i_len, fd, i;
253
254         /* if a new client wants to communicate */
255         fd = net_Accept( p_intf, p_sys->pi_fd, p_sys->i_clients > 0 ? 0 : -1 );
256         if( fd > 0 )
257         {
258             telnet_client_t *cl;
259
260             /* to be non blocking */
261 #if defined( WIN32 ) || defined( UNDER_CE )
262             {
263                 unsigned long i_dummy = 1;
264                 ioctlsocket( fd, FIONBIO, &i_dummy );
265             }
266 #else
267             fcntl( fd, F_SETFL, O_NONBLOCK );
268 #endif
269             cl = malloc( sizeof( telnet_client_t ));
270             cl->i_tel_cmd = 0;
271             cl->fd = fd;
272             cl->buffer_write = NULL;
273             cl->p_buffer_write = cl->buffer_write;
274             Write_message( cl, NULL, "Password: \xff\xfb\x01", WRITE_MODE_PWD );
275
276             TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
277         }
278
279         /* to do a proper select */
280         FD_ZERO( &fds_read );
281         FD_ZERO( &fds_write );
282
283         for( i = 0 ; i < p_sys->i_clients ; i++ )
284         {
285             telnet_client_t *cl = p_sys->clients[i];
286
287             if( cl->i_mode == WRITE_MODE_PWD || cl->i_mode == WRITE_MODE_CMD )
288             {
289                 FD_SET( cl->fd , &fds_write );
290             }
291             else
292             {
293                 FD_SET( cl->fd , &fds_read );
294             }
295             i_handle_max = __MAX( i_handle_max, cl->fd );
296         }
297
298         timeout.tv_sec = 0;
299         timeout.tv_usec = 500*1000;
300
301         i_ret = select( i_handle_max + 1, &fds_read, &fds_write, 0, &timeout );
302         if( i_ret == -1 && errno != EINTR )
303         {
304             msg_Warn( p_intf, "cannot select sockets" );
305             msleep( 1000 );
306             continue;
307         }
308         else if( i_ret <= 0 )
309         {
310             continue;
311         }
312
313         /* check if there is something to do with the socket */
314         for( i = 0 ; i < p_sys->i_clients ; i++ )
315         {
316             telnet_client_t *cl = p_sys->clients[i];
317
318             if( FD_ISSET(cl->fd , &fds_write) && cl->i_buffer_write > 0 )
319             {
320                 i_len = send( cl->fd , cl->p_buffer_write ,
321                               cl->i_buffer_write , 0 );
322                 if( i_len > 0 )
323                 {
324                     cl->p_buffer_write += i_len;
325                     cl->i_buffer_write -= i_len;
326                 }
327             }
328             else if( FD_ISSET( cl->fd, &fds_read) )
329             {
330                 int i_end = 0;
331                 int i_recv;
332
333                 while( (i_recv=recv( cl->fd, cl->p_buffer_read, 1, 0 )) > 0 &&
334                        cl->p_buffer_read - cl->buffer_read < 999 )
335                 {
336                     switch( cl->i_tel_cmd )
337                     {
338                     case 0:
339                         switch( *(uint8_t *)cl->p_buffer_read )
340                         {
341                         case '\r':
342                             break;
343                         case '\n':
344                             *cl->p_buffer_read = '\n';
345                             i_end = 1;
346                             break;
347                         case TEL_IAC: // telnet specific command
348                             cl->i_tel_cmd = 1;
349                             cl->p_buffer_read++;
350                             break;
351                         default:
352                             cl->p_buffer_read++;
353                             break;
354                         }
355                         break;
356                     case 1:
357                         switch( *(uint8_t *)cl->p_buffer_read )
358                         {
359                         case TEL_WILL: case TEL_WONT:
360                         case TEL_DO: case TEL_DONT:
361                             cl->i_tel_cmd++;
362                             cl->p_buffer_read++;
363                             break;
364                         default:
365                             cl->i_tel_cmd = 0;
366                             cl->p_buffer_read--;
367                             break;
368                         }
369                         break;
370                     case 2:
371                         cl->i_tel_cmd = 0;
372                         cl->p_buffer_read -= 2;
373                         break;
374                     }
375
376                     if( i_end != 0 ) break;
377                 }
378
379                 if( cl->p_buffer_read - cl->buffer_read == 999 )
380                 {
381                     Write_message( cl, NULL, "Line too long\r\n",
382                                    cl->i_mode + 2 );
383                 }
384
385                 if (i_recv == 0)
386                 {
387                     net_Close( cl->fd );
388                     TAB_REMOVE( p_intf->p_sys->i_clients ,
389                                 p_intf->p_sys->clients , cl );
390                     free( cl );
391                 }
392             }
393         }
394
395         /* and now we should bidouille the data we received / send */
396         for( i = 0 ; i < p_sys->i_clients ; i++ )
397         {
398             telnet_client_t *cl = p_sys->clients[i];
399
400             if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 )
401             {
402                // we have finished to send
403                cl->i_mode -= 2; // corresponding READ MODE
404             }
405             else if( cl->i_mode == READ_MODE_PWD &&
406                      *cl->p_buffer_read == '\n' )
407             {
408                 *cl->p_buffer_read = '\0';
409                 if( strcmp( psz_password, cl->buffer_read ) == 0 )
410                 {
411                     Write_message( cl, NULL, "\xff\xfc\x01\r\nWelcome, "
412                                    "Master\r\n> ", WRITE_MODE_CMD );
413                 }
414                 else
415                 {
416                     /* wrong password */
417                     Write_message( cl, NULL,
418                                    "\r\nWrong password.\r\nPassword: ",
419                                    WRITE_MODE_PWD );
420                 }
421             }
422             else if( cl->i_mode == READ_MODE_CMD &&
423                      *cl->p_buffer_read == '\n' )
424             {
425                 /* ok, here is a command line */
426                 if( !strncmp( cl->buffer_read, "logout", 6 ) ||
427                     !strncmp( cl->buffer_read, "quit", 4 )  ||
428                     !strncmp( cl->buffer_read, "exit", 4 ) )
429                 {
430                     net_Close( cl->fd );
431                     TAB_REMOVE( p_intf->p_sys->i_clients ,
432                                 p_intf->p_sys->clients , cl );
433                     free( cl );
434                 }
435                 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
436                 {
437                     msg_Err( p_intf, "shutdown requested" );
438                     p_intf->p_libvlc->b_die = VLC_TRUE;
439                 }
440                 else
441                 {
442                     vlm_message_t *message;
443
444                     /* create a standard string */
445                     *cl->p_buffer_read = '\0';
446
447                     vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
448                                         &message );
449                     Write_message( cl, message, NULL, WRITE_MODE_CMD );
450                     vlm_MessageDelete( message );
451                 }
452             }
453         }
454     }
455 }
456
457 static void Write_message( telnet_client_t *client, vlm_message_t *message,
458                            char *string_message, int i_mode )
459 {
460     char *psz_message;
461
462     client->p_buffer_read = client->buffer_read;
463     (client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
464     if( client->buffer_write ) free( client->buffer_write );
465
466     /* generate the psz_message string */
467     if( message )
468     {
469         /* ok, look for vlm_message_t */
470         psz_message = MessageToString( message, 0 );
471     }
472     else
473     {
474         /* it is a basic string_message */
475         psz_message = strdup( string_message );
476     }
477
478     client->buffer_write = client->p_buffer_write = psz_message;
479     client->i_buffer_write = strlen( psz_message );
480     client->i_mode = i_mode;
481 }
482
483 /* We need the level of the message to put a beautiful indentation.
484  * first level is 0 */
485 static char *MessageToString( vlm_message_t *message, int i_level )
486 {
487 #define STRING_CR "\r\n"
488 #define STRING_TAIL "> "
489
490     char *psz_message;
491     int i, i_message = sizeof( STRING_TAIL );
492
493     if( !message || !message->psz_name )
494     {
495         return strdup( STRING_CR STRING_TAIL );
496     }
497     else if( !i_level && !message->i_child && !message->psz_value  )
498     {
499         /* A command is successful. Don't write anything */
500         return strdup( /*STRING_CR*/ STRING_TAIL );
501     }
502
503     i_message += strlen( message->psz_name ) + i_level * sizeof( "    " ) + 1;
504     psz_message = malloc( i_message );
505     *psz_message = 0;
506     for( i = 0; i < i_level; i++ ) strcat( psz_message, "    " );
507     strcat( psz_message, message->psz_name );
508
509     if( message->psz_value )
510     {
511         i_message += sizeof( " : " ) + strlen( message->psz_value ) +
512             sizeof( STRING_CR );
513         psz_message = realloc( psz_message, i_message );
514         strcat( psz_message, " : " );
515         strcat( psz_message, message->psz_value );
516         strcat( psz_message, STRING_CR );
517     }
518     else
519     {
520         i_message += sizeof( STRING_CR );
521         psz_message = realloc( psz_message, i_message );
522         strcat( psz_message, STRING_CR );
523     }
524
525     for( i = 0; i < message->i_child; i++ )
526     {
527         char *child_message =
528             MessageToString( message->child[i], i_level + 1 );
529
530         i_message += strlen( child_message );
531         psz_message = realloc( psz_message, i_message );
532         strcat( psz_message, child_message );
533         free( child_message );
534     }
535
536     if( i_level == 0 ) strcat( psz_message, STRING_TAIL );
537
538     return psz_message;
539 }