1 /*****************************************************************************
2 * telnet.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
7 * Authors: Simon Latapie <garf@videolan.org>
8 * Laurent Aimar <fenrir@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc_interface.h>
32 #include <vlc_input.h>
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
47 #include <vlc_network.h>
51 #define READ_MODE_PWD 1
52 #define READ_MODE_CMD 2
53 #define WRITE_MODE_PWD 3 // when we write the word "Password:"
54 #define WRITE_MODE_CMD 4
64 /*****************************************************************************
66 *****************************************************************************/
67 static int Open ( vlc_object_t * );
68 static void Close( vlc_object_t * );
70 #define TELNETHOST_TEXT N_( "Host" )
71 #define TELNETHOST_LONGTEXT N_( "This is the host on which the " \
72 "interface will listen. It defaults to all network interfaces (0.0.0.0)." \
73 " If you want this interface to be available only on the local " \
74 "machine, enter \"127.0.0.1\"." )
75 #define TELNETPORT_TEXT N_( "Port" )
76 #define TELNETPORT_LONGTEXT N_( "This is the TCP port on which this " \
77 "interface will listen. It defaults to 4212." )
78 #define TELNETPORT_DEFAULT 4212
79 #define TELNETPWD_TEXT N_( "Password" )
80 #define TELNETPWD_LONGTEXT N_( "A single administration password is used " \
81 "to protect this interface. The default value is \"admin\"." )
82 #define TELNETPWD_DEFAULT "admin"
85 set_shortname( "Telnet" );
86 set_category( CAT_INTERFACE );
87 set_subcategory( SUBCAT_INTERFACE_CONTROL );
88 add_string( "telnet-host", "", NULL, TELNETHOST_TEXT,
89 TELNETHOST_LONGTEXT, VLC_TRUE );
90 add_integer( "telnet-port", TELNETPORT_DEFAULT, NULL, TELNETPORT_TEXT,
91 TELNETPORT_LONGTEXT, VLC_TRUE );
92 add_string( "telnet-password", TELNETPWD_DEFAULT, NULL, TELNETPWD_TEXT,
93 TELNETPWD_LONGTEXT, VLC_TRUE );
94 set_description( _("VLM remote control interface") );
95 add_category_hint( "VLM", NULL, VLC_FALSE );
96 set_capability( "interface", 0 );
97 set_callbacks( Open , Close );
100 /*****************************************************************************
102 *****************************************************************************/
103 static void Run( intf_thread_t * );
107 int i_mode; /* read or write */
109 char buffer_read[1000]; // 1000 byte per command should be sufficient
112 char *p_buffer_write; // the position in the buffer
113 int i_buffer_write; // the number of byte we still have to send
114 int i_tel_cmd; // for specific telnet commands
118 static char *MessageToString( vlm_message_t *, int );
119 static void Write_message( telnet_client_t *, vlm_message_t *, char *, int );
123 telnet_client_t **clients;
130 * getPort: Decide which port to use. There are two possibilities to
131 * specify a port: integrated in the --telnet-host option with :PORT
132 * or using the --telnet-port option. The --telnet-port option has
134 * This code relies upon the fact the url.i_port is 0 if the :PORT
135 * option is missing from --telnet-host.
137 static int getPort(intf_thread_t *p_intf, vlc_url_t url, int i_port)
139 // Print error if two different ports have been specified
140 if (url.i_port != 0 &&
141 i_port != TELNETPORT_DEFAULT &&
142 url.i_port != i_port )
144 msg_Err( p_intf, "ignoring port %d and using %d", url.i_port,
147 if (i_port != TELNETPORT_DEFAULT)
158 /*****************************************************************************
159 * Open: initialize dummy interface
160 *****************************************************************************/
161 static int Open( vlc_object_t *p_this )
163 intf_thread_t *p_intf = (intf_thread_t*) p_this;
169 if( !(mediatheque = vlm_New( p_intf )) )
171 msg_Err( p_intf, "cannot start VLM" );
175 msg_Info( p_intf, "using the VLM interface plugin..." );
177 i_telnetport = config_GetInt( p_intf, "telnet-port" );
178 psz_address = config_GetPsz( p_intf, "telnet-host" );
180 vlc_UrlParse(&url, psz_address, 0);
182 // There might be two ports given, resolve any potentially
184 url.i_port = getPort(p_intf, url, i_telnetport);
186 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
187 if( ( p_intf->p_sys->pi_fd = net_ListenTCP( p_intf, url.psz_host, url.i_port ) )
190 msg_Err( p_intf, "cannot listen for telnet" );
193 free( p_intf->p_sys );
197 "telnet interface started on interface %s %d",
198 url.psz_host, url.i_port );
200 p_intf->p_sys->i_clients = 0;
201 p_intf->p_sys->clients = NULL;
202 p_intf->p_sys->mediatheque = mediatheque;
203 p_intf->pf_run = Run;
210 /*****************************************************************************
212 *****************************************************************************/
213 static void Close( vlc_object_t *p_this )
215 intf_thread_t *p_intf = (intf_thread_t*)p_this;
216 intf_sys_t *p_sys = p_intf->p_sys;
219 for( i = 0; i < p_sys->i_clients; i++ )
221 telnet_client_t *cl = p_sys->clients[i];
226 if( p_sys->clients != NULL ) free( p_sys->clients );
228 net_ListenClose( p_sys->pi_fd );
230 vlm_Delete( p_sys->mediatheque );
235 /*****************************************************************************
237 *****************************************************************************/
238 static void Run( intf_thread_t *p_intf )
240 intf_sys_t *p_sys = p_intf->p_sys;
241 struct timeval timeout;
244 psz_password = config_GetPsz( p_intf, "telnet-password" );
246 while( !intf_ShouldDie( p_intf ) )
248 fd_set fds_read, fds_write;
249 int i_handle_max = 0;
250 int i_ret, i_len, fd, i;
252 /* if a new client wants to communicate */
253 fd = net_Accept( p_intf, p_sys->pi_fd, p_sys->i_clients > 0 ? 0 : -1 );
258 /* to be non blocking */
259 #if defined( WIN32 ) || defined( UNDER_CE )
261 unsigned long i_dummy = 1;
262 ioctlsocket( fd, FIONBIO, &i_dummy );
265 fcntl( fd, F_SETFL, O_NONBLOCK );
267 cl = malloc( sizeof( telnet_client_t ));
270 cl->buffer_write = NULL;
271 cl->p_buffer_write = cl->buffer_write;
272 Write_message( cl, NULL,
273 _( "Password: \xff\xfb\x01" ), WRITE_MODE_PWD );
275 TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
278 /* to do a proper select */
279 FD_ZERO( &fds_read );
280 FD_ZERO( &fds_write );
282 for( i = 0 ; i < p_sys->i_clients ; i++ )
284 telnet_client_t *cl = p_sys->clients[i];
286 if( cl->i_mode == WRITE_MODE_PWD || cl->i_mode == WRITE_MODE_CMD )
288 FD_SET( cl->fd , &fds_write );
292 FD_SET( cl->fd , &fds_read );
294 i_handle_max = __MAX( i_handle_max, cl->fd );
298 timeout.tv_usec = 500*1000;
300 i_ret = select( i_handle_max + 1, &fds_read, &fds_write, 0, &timeout );
301 if( i_ret == -1 && errno != EINTR )
303 msg_Warn( p_intf, "cannot select sockets" );
307 else if( i_ret <= 0 )
312 /* check if there is something to do with the socket */
313 for( i = 0 ; i < p_sys->i_clients ; i++ )
315 telnet_client_t *cl = p_sys->clients[i];
317 if( FD_ISSET(cl->fd , &fds_write) && cl->i_buffer_write > 0 )
319 i_len = send( cl->fd , cl->p_buffer_write ,
320 cl->i_buffer_write , 0 );
323 cl->p_buffer_write += i_len;
324 cl->i_buffer_write -= i_len;
327 else if( FD_ISSET( cl->fd, &fds_read) )
332 while( (i_recv=recv( cl->fd, cl->p_buffer_read, 1, 0 )) > 0 &&
333 cl->p_buffer_read - cl->buffer_read < 999 )
335 switch( cl->i_tel_cmd )
338 switch( *(uint8_t *)cl->p_buffer_read )
343 *cl->p_buffer_read = '\n';
346 case TEL_IAC: // telnet specific command
356 switch( *(uint8_t *)cl->p_buffer_read )
358 case TEL_WILL: case TEL_WONT:
359 case TEL_DO: case TEL_DONT:
371 cl->p_buffer_read -= 2;
375 if( i_end != 0 ) break;
378 if( cl->p_buffer_read - cl->buffer_read == 999 )
380 Write_message( cl, NULL, _( "Line too long\r\n" ),
387 TAB_REMOVE( p_intf->p_sys->i_clients ,
388 p_intf->p_sys->clients , cl );
394 /* and now we should bidouille the data we received / send */
395 for( i = 0 ; i < p_sys->i_clients ; i++ )
397 telnet_client_t *cl = p_sys->clients[i];
399 if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 )
401 // we have finished to send
402 cl->i_mode -= 2; // corresponding READ MODE
404 else if( cl->i_mode == READ_MODE_PWD &&
405 *cl->p_buffer_read == '\n' )
407 *cl->p_buffer_read = '\0';
408 if( strcmp( psz_password, cl->buffer_read ) == 0 )
410 Write_message( cl, NULL, _( "\xff\xfc\x01\r\nWelcome, "
411 "Master\r\n> " ), WRITE_MODE_CMD );
416 Write_message( cl, NULL,
417 _( "\r\nWrong password.\r\nPassword: " ),
421 else if( cl->i_mode == READ_MODE_CMD &&
422 *cl->p_buffer_read == '\n' )
424 /* ok, here is a command line */
425 if( !strncmp( cl->buffer_read, "logout", 6 ) ||
426 !strncmp( cl->buffer_read, "quit", 4 ) ||
427 !strncmp( cl->buffer_read, "exit", 4 ) )
430 TAB_REMOVE( p_intf->p_sys->i_clients ,
431 p_intf->p_sys->clients , cl );
434 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
436 msg_Err( p_intf, "shutdown requested" );
437 p_intf->p_libvlc->b_die = VLC_TRUE;
441 vlm_message_t *message;
443 /* create a standard string */
444 *cl->p_buffer_read = '\0';
446 vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
448 if( !strncmp( cl->buffer_read, "help", 4 ) )
450 vlm_message_t *p_my_help =
451 vlm_MessageNew( "Telnet Specific Commands:", NULL );
452 vlm_MessageAdd( p_my_help,
453 vlm_MessageNew( "logout, quit, exit" , NULL ) );
454 vlm_MessageAdd( p_my_help,
455 vlm_MessageNew( "shutdown" , NULL ) );
456 vlm_MessageAdd( message, p_my_help );
458 Write_message( cl, message, NULL, WRITE_MODE_CMD );
459 vlm_MessageDelete( message );
467 static void Write_message( telnet_client_t *client, vlm_message_t *message,
468 char *string_message, int i_mode )
472 client->p_buffer_read = client->buffer_read;
473 (client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
474 if( client->buffer_write ) free( client->buffer_write );
476 /* generate the psz_message string */
479 /* ok, look for vlm_message_t */
480 psz_message = MessageToString( message, 0 );
484 /* it is a basic string_message */
485 psz_message = strdup( string_message );
488 client->buffer_write = client->p_buffer_write = psz_message;
489 client->i_buffer_write = strlen( psz_message );
490 client->i_mode = i_mode;
493 /* We need the level of the message to put a beautiful indentation.
494 * first level is 0 */
495 static char *MessageToString( vlm_message_t *message, int i_level )
497 #define STRING_CR "\r\n"
498 #define STRING_TAIL "> "
501 int i, i_message = sizeof( STRING_TAIL );
503 if( !message || !message->psz_name )
505 return strdup( STRING_CR STRING_TAIL );
507 else if( !i_level && !message->i_child && !message->psz_value )
509 /* A command is successful. Don't write anything */
510 return strdup( /*STRING_CR*/ STRING_TAIL );
513 i_message += strlen( message->psz_name ) + i_level * sizeof( " " ) + 1;
514 psz_message = malloc( i_message );
516 for( i = 0; i < i_level; i++ ) strcat( psz_message, " " );
517 strcat( psz_message, message->psz_name );
519 if( message->psz_value )
521 i_message += sizeof( " : " ) + strlen( message->psz_value ) +
523 psz_message = realloc( psz_message, i_message );
524 strcat( psz_message, " : " );
525 strcat( psz_message, message->psz_value );
526 strcat( psz_message, STRING_CR );
530 i_message += sizeof( STRING_CR );
531 psz_message = realloc( psz_message, i_message );
532 strcat( psz_message, STRING_CR );
535 for( i = 0; i < message->i_child; i++ )
537 char *child_message =
538 MessageToString( message->child[i], i_level + 1 );
540 i_message += strlen( child_message );
541 psz_message = realloc( psz_message, i_message );
542 strcat( psz_message, child_message );
543 free( child_message );
546 if( i_level == 0 ) strcat( psz_message, STRING_TAIL );