1 /*****************************************************************************
2 * telnet.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2005 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() */
33 #include <vlc/input.h>
40 #ifdef HAVE_SYS_TIME_H
41 # include <sys/time.h>
48 #if defined( UNDER_CE )
50 #elif defined( WIN32 )
51 # include <winsock2.h>
53 # include <sys/socket.h>
60 #define READ_MODE_PWD 1
61 #define READ_MODE_CMD 2
62 #define WRITE_MODE_PWD 3 // when we write the word "Password:"
63 #define WRITE_MODE_CMD 4
73 /*****************************************************************************
75 *****************************************************************************/
76 static int Open ( vlc_object_t * );
77 static void Close( vlc_object_t * );
79 #define TELNETHOST_TEXT N_( "Telnet Interface host" )
80 #define TELNETHOST_LONGTEXT N_( "Default to listen on all network interfaces" )
81 #define TELNETPORT_TEXT N_( "Telnet Interface port" )
82 #define TELNETPORT_LONGTEXT N_( "Default to 4212" )
83 #define TELNETPORT_DEFAULT 4212
84 #define TELNETPWD_TEXT N_( "Telnet Interface password" )
85 #define TELNETPWD_LONGTEXT N_( "Default to admin" )
86 #define TELNETPWD_DEFAULT "admin"
89 set_shortname( "Telnet" );
90 set_category( CAT_INTERFACE );
91 set_subcategory( SUBCAT_INTERFACE_GENERAL );
92 add_string( "telnet-host", "", NULL, TELNETHOST_TEXT,
93 TELNETHOST_LONGTEXT, VLC_TRUE );
94 add_integer( "telnet-port", TELNETPORT_DEFAULT, NULL, TELNETPORT_TEXT,
95 TELNETPORT_LONGTEXT, VLC_TRUE );
96 add_string( "telnet-password", TELNETPWD_DEFAULT, NULL, TELNETPWD_TEXT,
97 TELNETPWD_LONGTEXT, VLC_TRUE );
98 set_description( _("VLM remote control interface") );
99 add_category_hint( "VLM", NULL, VLC_FALSE );
100 set_capability( "interface", 0 );
101 set_callbacks( Open , Close );
104 /*****************************************************************************
106 *****************************************************************************/
107 static void Run( intf_thread_t * );
111 int i_mode; /* read or write */
113 char buffer_read[1000]; // 1000 byte per command should be sufficient
116 char *p_buffer_write; // the position in the buffer
117 int i_buffer_write; // the number of byte we still have to send
118 int i_tel_cmd; // for specific telnet commands
122 static char *MessageToString( vlm_message_t *, int );
123 static void Write_message( telnet_client_t *, vlm_message_t *, char *, int );
127 telnet_client_t **clients;
134 * getPort: Decide which port to use. There are two possibilities to
135 * specify a port: integrated in the --telnet-host option with :PORT
136 * or using the --telnet-port option. The --telnet-port option has
138 * This code relies upon the fact the url.i_port is 0 if the :PORT
139 * option is missing from --telnet-host.
141 static int getPort(intf_thread_t *p_intf, vlc_url_t url, int i_port)
143 // Print error if two different ports have been specified
144 if (url.i_port != 0 &&
145 i_port != TELNETPORT_DEFAULT &&
146 url.i_port != i_port )
148 msg_Err( p_intf, "ignoring port %d and using %d", url.i_port,
151 if (i_port != TELNETPORT_DEFAULT)
163 /*****************************************************************************
164 * Open: initialize dummy interface
165 *****************************************************************************/
166 static int Open( vlc_object_t *p_this )
168 intf_thread_t *p_intf = (intf_thread_t*) p_this;
174 if( !(mediatheque = vlm_New( p_intf )) )
176 msg_Err( p_intf, "cannot start VLM" );
180 msg_Info( p_intf, "Using the VLM interface plugin..." );
182 i_telnetport = config_GetInt( p_intf, "telnet-port" );
183 psz_address = config_GetPsz( p_intf, "telnet-host" );
185 vlc_UrlParse(&url, psz_address, 0);
187 // There might be two ports given, resolve any potentially
189 url.i_port = getPort(p_intf, url, i_telnetport);
191 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
192 if( ( p_intf->p_sys->pi_fd = net_ListenTCP( p_intf, url.psz_host, url.i_port ) )
195 msg_Err( p_intf, "cannot listen for telnet" );
198 free( p_intf->p_sys );
202 "Telnet interface started on interface %s %d",
203 url.psz_host, url.i_port );
205 p_intf->p_sys->i_clients = 0;
206 p_intf->p_sys->clients = NULL;
207 p_intf->p_sys->mediatheque = mediatheque;
208 p_intf->pf_run = Run;
215 /*****************************************************************************
217 *****************************************************************************/
218 static void Close( vlc_object_t *p_this )
220 intf_thread_t *p_intf = (intf_thread_t*)p_this;
221 intf_sys_t *p_sys = p_intf->p_sys;
224 for( i = 0; i < p_sys->i_clients; i++ )
226 telnet_client_t *cl = p_sys->clients[i];
231 if( p_sys->clients != NULL ) free( p_sys->clients );
233 net_ListenClose( p_sys->pi_fd );
235 vlm_Delete( p_sys->mediatheque );
240 /*****************************************************************************
242 *****************************************************************************/
243 static void Run( intf_thread_t *p_intf )
245 intf_sys_t *p_sys = p_intf->p_sys;
246 struct timeval timeout;
249 psz_password = config_GetPsz( p_intf, "telnet-password" );
251 while( !p_intf->b_die )
253 fd_set fds_read, fds_write;
254 int i_handle_max = 0;
255 int i_ret, i_len, fd, i;
257 /* if a new client wants to communicate */
258 fd = net_Accept( p_intf, p_sys->pi_fd, p_sys->i_clients > 0 ? 0 : -1 );
263 /* to be non blocking */
264 #if defined( WIN32 ) || defined( UNDER_CE )
266 unsigned long i_dummy = 1;
267 ioctlsocket( fd, FIONBIO, &i_dummy );
270 fcntl( fd, F_SETFL, O_NONBLOCK );
272 cl = malloc( sizeof( telnet_client_t ));
275 cl->buffer_write = NULL;
276 cl->p_buffer_write = cl->buffer_write;
277 Write_message( cl, NULL, "Password: \xff\xfb\x01", WRITE_MODE_PWD );
279 TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
282 /* to do a proper select */
283 FD_ZERO( &fds_read );
284 FD_ZERO( &fds_write );
286 for( i = 0 ; i < p_sys->i_clients ; i++ )
288 telnet_client_t *cl = p_sys->clients[i];
290 if( cl->i_mode == WRITE_MODE_PWD || cl->i_mode == WRITE_MODE_CMD )
292 FD_SET( cl->fd , &fds_write );
296 FD_SET( cl->fd , &fds_read );
298 i_handle_max = __MAX( i_handle_max, cl->fd );
302 timeout.tv_usec = 500*1000;
304 i_ret = select( i_handle_max + 1, &fds_read, &fds_write, 0, &timeout );
305 if( i_ret == -1 && errno != EINTR )
307 msg_Warn( p_intf, "cannot select sockets" );
311 else if( i_ret <= 0 )
316 /* check if there is something to do with the socket */
317 for( i = 0 ; i < p_sys->i_clients ; i++ )
319 telnet_client_t *cl = p_sys->clients[i];
321 if( FD_ISSET(cl->fd , &fds_write) && cl->i_buffer_write > 0 )
323 i_len = send( cl->fd , cl->p_buffer_write ,
324 cl->i_buffer_write , 0 );
327 cl->p_buffer_write += i_len;
328 cl->i_buffer_write -= i_len;
331 else if( FD_ISSET( cl->fd, &fds_read) )
336 while( (i_recv=recv( cl->fd, cl->p_buffer_read, 1, 0 )) > 0 &&
337 cl->p_buffer_read - cl->buffer_read < 999 )
339 switch( cl->i_tel_cmd )
342 switch( *(uint8_t *)cl->p_buffer_read )
347 *cl->p_buffer_read = '\n';
350 case TEL_IAC: // telnet specific command
360 switch( *(uint8_t *)cl->p_buffer_read )
362 case TEL_WILL: case TEL_WONT:
363 case TEL_DO: case TEL_DONT:
375 cl->p_buffer_read -= 2;
379 if( i_end != 0 ) break;
382 if( cl->p_buffer_read - cl->buffer_read == 999 )
384 Write_message( cl, NULL, "Line too long\r\n",
391 TAB_REMOVE( p_intf->p_sys->i_clients ,
392 p_intf->p_sys->clients , cl );
398 /* and now we should bidouille the data we received / send */
399 for( i = 0 ; i < p_sys->i_clients ; i++ )
401 telnet_client_t *cl = p_sys->clients[i];
403 if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 )
405 // we have finished to send
406 cl->i_mode -= 2; // corresponding READ MODE
408 else if( cl->i_mode == READ_MODE_PWD &&
409 *cl->p_buffer_read == '\n' )
411 *cl->p_buffer_read = '\0';
412 if( strcmp( psz_password, cl->buffer_read ) == 0 )
414 Write_message( cl, NULL, "\xff\xfc\x01\r\nWelcome, "
415 "Master\r\n> ", WRITE_MODE_CMD );
420 Write_message( cl, NULL,
421 "\r\nWrong password.\r\nPassword: ",
425 else if( cl->i_mode == READ_MODE_CMD &&
426 *cl->p_buffer_read == '\n' )
428 /* ok, here is a command line */
429 if( !strncmp( cl->buffer_read, "logout", 6 ) ||
430 !strncmp( cl->buffer_read, "quit", 4 ) ||
431 !strncmp( cl->buffer_read, "exit", 4 ) )
434 TAB_REMOVE( p_intf->p_sys->i_clients ,
435 p_intf->p_sys->clients , cl );
438 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
440 msg_Err( p_intf, "shutdown requested" );
441 p_intf->p_vlc->b_die = VLC_TRUE;
445 vlm_message_t *message;
447 /* create a standard string */
448 *cl->p_buffer_read = '\0';
450 vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
452 Write_message( cl, message, NULL, WRITE_MODE_CMD );
453 vlm_MessageDelete( message );
460 static void Write_message( telnet_client_t *client, vlm_message_t *message,
461 char *string_message, int i_mode )
465 client->p_buffer_read = client->buffer_read;
466 (client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
467 if( client->buffer_write ) free( client->buffer_write );
469 /* generate the psz_message string */
472 /* ok, look for vlm_message_t */
473 psz_message = MessageToString( message, 0 );
477 /* it is a basic string_message */
478 psz_message = strdup( string_message );
481 client->buffer_write = client->p_buffer_write = psz_message;
482 client->i_buffer_write = strlen( psz_message );
483 client->i_mode = i_mode;
486 /* We need the level of the message to put a beautiful indentation.
487 * first level is 0 */
488 static char *MessageToString( vlm_message_t *message, int i_level )
490 #define STRING_CR "\r\n"
491 #define STRING_TAIL "> "
494 int i, i_message = sizeof( STRING_TAIL );
496 if( !message || !message->psz_name )
498 return strdup( STRING_CR STRING_TAIL );
500 else if( !i_level && !message->i_child && !message->psz_value )
502 /* A command is successful. Don't write anything */
503 return strdup( STRING_CR STRING_TAIL );
506 i_message += strlen( message->psz_name ) + i_level * sizeof( " " ) + 1;
507 psz_message = malloc( i_message ); *psz_message = 0;
508 for( i = 0; i < i_level; i++ ) strcat( psz_message, " " );
509 strcat( psz_message, message->psz_name );
511 if( message->psz_value )
513 i_message += sizeof( " : " ) + strlen( message->psz_value ) +
515 psz_message = realloc( psz_message, i_message );
516 strcat( psz_message, " : " );
517 strcat( psz_message, message->psz_value );
518 strcat( psz_message, STRING_CR );
522 i_message += sizeof( STRING_CR );
523 psz_message = realloc( psz_message, i_message );
524 strcat( psz_message, STRING_CR );
527 for( i = 0; i < message->i_child; i++ )
529 char *child_message =
530 MessageToString( message->child[i], i_level + 1 );
532 i_message += strlen( child_message );
533 psz_message = realloc( psz_message, i_message );
534 strcat( psz_message, child_message );
535 free( child_message );
538 if( i_level == 0 ) strcat( psz_message, STRING_TAIL );