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>
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
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Open ( vlc_object_t * );
70 static void Close( vlc_object_t * );
72 #define TELNETHOST_TEXT N_( "Telnet Interface host" )
73 #define TELNETHOST_LONGTEXT N_( "Default to listen on all network interfaces" )
74 #define TELNETPORT_TEXT N_( "Telnet Interface port" )
75 #define TELNETPORT_LONGTEXT N_( "Default to 4212" )
76 #define TELNETPORT_DEFAULT 4212
77 #define TELNETPWD_TEXT N_( "Telnet Interface password" )
78 #define TELNETPWD_LONGTEXT N_( "Default to admin" )
79 #define TELNETPWD_DEFAULT "admin"
82 set_shortname( "Telnet" );
83 set_category( CAT_INTERFACE );
84 set_subcategory( SUBCAT_INTERFACE_GENERAL );
85 add_string( "telnet-host", "", NULL, TELNETHOST_TEXT,
86 TELNETHOST_LONGTEXT, VLC_TRUE );
87 add_integer( "telnet-port", TELNETPORT_DEFAULT, NULL, TELNETPORT_TEXT,
88 TELNETPORT_LONGTEXT, VLC_TRUE );
89 add_string( "telnet-password", TELNETPWD_DEFAULT, NULL, TELNETPWD_TEXT,
90 TELNETPWD_LONGTEXT, VLC_TRUE );
91 set_description( _("VLM remote control interface") );
92 add_category_hint( "VLM", NULL, VLC_FALSE );
93 set_capability( "interface", 0 );
94 set_callbacks( Open , Close );
97 /*****************************************************************************
99 *****************************************************************************/
100 static void Run( intf_thread_t * );
104 int i_mode; /* read or write */
106 char buffer_read[1000]; // 1000 byte per command should be sufficient
109 char *p_buffer_write; // the position in the buffer
110 int i_buffer_write; // the number of byte we still have to send
111 int i_tel_cmd; // for specific telnet commands
115 static char *MessageToString( vlm_message_t *, int );
116 static void Write_message( telnet_client_t *, vlm_message_t *, char *, int );
120 telnet_client_t **clients;
127 * getPort: Decide which port to use. There are two possibilities to
128 * specify a port: integrated in the --telnet-host option with :PORT
129 * or using the --telnet-port option. The --telnet-port option has
131 * This code relies upon the fact the url.i_port is 0 if the :PORT
132 * option is missing from --telnet-host.
134 static int getPort(intf_thread_t *p_intf, vlc_url_t url, int i_port)
136 // Print error if two different ports have been specified
137 if (url.i_port != 0 &&
138 i_port != TELNETPORT_DEFAULT &&
139 url.i_port != i_port )
141 msg_Err( p_intf, "ignoring port %d and using %d", url.i_port,
144 if (i_port != TELNETPORT_DEFAULT)
156 /*****************************************************************************
157 * Open: initialize dummy interface
158 *****************************************************************************/
159 static int Open( vlc_object_t *p_this )
161 intf_thread_t *p_intf = (intf_thread_t*) p_this;
167 if( !(mediatheque = vlm_New( p_intf )) )
169 msg_Err( p_intf, "cannot start VLM" );
173 msg_Info( p_intf, "Using the VLM interface plugin..." );
175 i_telnetport = config_GetInt( p_intf, "telnet-port" );
176 psz_address = config_GetPsz( p_intf, "telnet-host" );
178 vlc_UrlParse(&url, psz_address, 0);
180 // There might be two ports given, resolve any potentially
182 url.i_port = getPort(p_intf, url, i_telnetport);
184 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
185 if( ( p_intf->p_sys->pi_fd = net_ListenTCP( p_intf, url.psz_host, url.i_port ) )
188 msg_Err( p_intf, "cannot listen for telnet" );
191 free( p_intf->p_sys );
195 "Telnet interface started on interface %s %d",
196 url.psz_host, url.i_port );
198 p_intf->p_sys->i_clients = 0;
199 p_intf->p_sys->clients = NULL;
200 p_intf->p_sys->mediatheque = mediatheque;
201 p_intf->pf_run = Run;
208 /*****************************************************************************
210 *****************************************************************************/
211 static void Close( vlc_object_t *p_this )
213 intf_thread_t *p_intf = (intf_thread_t*)p_this;
214 intf_sys_t *p_sys = p_intf->p_sys;
217 for( i = 0; i < p_sys->i_clients; i++ )
219 telnet_client_t *cl = p_sys->clients[i];
224 if( p_sys->clients != NULL ) free( p_sys->clients );
226 net_ListenClose( p_sys->pi_fd );
228 vlm_Delete( p_sys->mediatheque );
233 /*****************************************************************************
235 *****************************************************************************/
236 static void Run( intf_thread_t *p_intf )
238 intf_sys_t *p_sys = p_intf->p_sys;
239 struct timeval timeout;
242 psz_password = config_GetPsz( p_intf, "telnet-password" );
244 while( !p_intf->b_die )
246 fd_set fds_read, fds_write;
247 int i_handle_max = 0;
248 int i_ret, i_len, fd, i;
250 /* if a new client wants to communicate */
251 fd = net_Accept( p_intf, p_sys->pi_fd, p_sys->i_clients > 0 ? 0 : -1 );
256 /* to be non blocking */
257 #if defined( WIN32 ) || defined( UNDER_CE )
259 unsigned long i_dummy = 1;
260 ioctlsocket( fd, FIONBIO, &i_dummy );
263 fcntl( fd, F_SETFL, O_NONBLOCK );
265 cl = malloc( sizeof( telnet_client_t ));
268 cl->buffer_write = NULL;
269 cl->p_buffer_write = cl->buffer_write;
270 Write_message( cl, NULL, "Password: \xff\xfb\x01", WRITE_MODE_PWD );
272 TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
275 /* to do a proper select */
276 FD_ZERO( &fds_read );
277 FD_ZERO( &fds_write );
279 for( i = 0 ; i < p_sys->i_clients ; i++ )
281 telnet_client_t *cl = p_sys->clients[i];
283 if( cl->i_mode == WRITE_MODE_PWD || cl->i_mode == WRITE_MODE_CMD )
285 FD_SET( cl->fd , &fds_write );
289 FD_SET( cl->fd , &fds_read );
291 i_handle_max = __MAX( i_handle_max, cl->fd );
295 timeout.tv_usec = 500*1000;
297 i_ret = select( i_handle_max + 1, &fds_read, &fds_write, 0, &timeout );
298 if( i_ret == -1 && errno != EINTR )
300 msg_Warn( p_intf, "cannot select sockets" );
304 else if( i_ret <= 0 )
309 /* check if there is something to do with the socket */
310 for( i = 0 ; i < p_sys->i_clients ; i++ )
312 telnet_client_t *cl = p_sys->clients[i];
314 if( FD_ISSET(cl->fd , &fds_write) && cl->i_buffer_write > 0 )
316 i_len = send( cl->fd , cl->p_buffer_write ,
317 cl->i_buffer_write , 0 );
320 cl->p_buffer_write += i_len;
321 cl->i_buffer_write -= i_len;
324 else if( FD_ISSET( cl->fd, &fds_read) )
329 while( (i_recv=recv( cl->fd, cl->p_buffer_read, 1, 0 )) > 0 &&
330 cl->p_buffer_read - cl->buffer_read < 999 )
332 switch( cl->i_tel_cmd )
335 switch( *(uint8_t *)cl->p_buffer_read )
340 *cl->p_buffer_read = '\n';
343 case TEL_IAC: // telnet specific command
353 switch( *(uint8_t *)cl->p_buffer_read )
355 case TEL_WILL: case TEL_WONT:
356 case TEL_DO: case TEL_DONT:
368 cl->p_buffer_read -= 2;
372 if( i_end != 0 ) break;
375 if( cl->p_buffer_read - cl->buffer_read == 999 )
377 Write_message( cl, NULL, "Line too long\r\n",
384 TAB_REMOVE( p_intf->p_sys->i_clients ,
385 p_intf->p_sys->clients , cl );
391 /* and now we should bidouille the data we received / send */
392 for( i = 0 ; i < p_sys->i_clients ; i++ )
394 telnet_client_t *cl = p_sys->clients[i];
396 if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 )
398 // we have finished to send
399 cl->i_mode -= 2; // corresponding READ MODE
401 else if( cl->i_mode == READ_MODE_PWD &&
402 *cl->p_buffer_read == '\n' )
404 *cl->p_buffer_read = '\0';
405 if( strcmp( psz_password, cl->buffer_read ) == 0 )
407 Write_message( cl, NULL, "\xff\xfc\x01\r\nWelcome, "
408 "Master\r\n> ", WRITE_MODE_CMD );
413 Write_message( cl, NULL,
414 "\r\nWrong password.\r\nPassword: ",
418 else if( cl->i_mode == READ_MODE_CMD &&
419 *cl->p_buffer_read == '\n' )
421 /* ok, here is a command line */
422 if( !strncmp( cl->buffer_read, "logout", 6 ) ||
423 !strncmp( cl->buffer_read, "quit", 4 ) ||
424 !strncmp( cl->buffer_read, "exit", 4 ) )
427 TAB_REMOVE( p_intf->p_sys->i_clients ,
428 p_intf->p_sys->clients , cl );
431 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
433 msg_Err( p_intf, "shutdown requested" );
434 p_intf->p_vlc->b_die = VLC_TRUE;
438 vlm_message_t *message;
440 /* create a standard string */
441 *cl->p_buffer_read = '\0';
443 vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
445 Write_message( cl, message, NULL, WRITE_MODE_CMD );
446 vlm_MessageDelete( message );
453 static void Write_message( telnet_client_t *client, vlm_message_t *message,
454 char *string_message, int i_mode )
458 client->p_buffer_read = client->buffer_read;
459 (client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
460 if( client->buffer_write ) free( client->buffer_write );
462 /* generate the psz_message string */
465 /* ok, look for vlm_message_t */
466 psz_message = MessageToString( message, 0 );
470 /* it is a basic string_message */
471 psz_message = strdup( string_message );
474 client->buffer_write = client->p_buffer_write = psz_message;
475 client->i_buffer_write = strlen( psz_message );
476 client->i_mode = i_mode;
479 /* We need the level of the message to put a beautiful indentation.
480 * first level is 0 */
481 static char *MessageToString( vlm_message_t *message, int i_level )
483 #define STRING_CR "\r\n"
484 #define STRING_TAIL "> "
487 int i, i_message = sizeof( STRING_TAIL );
489 if( !message || !message->psz_name )
491 return strdup( STRING_CR STRING_TAIL );
493 else if( !i_level && !message->i_child && !message->psz_value )
495 /* A command is successful. Don't write anything */
496 return strdup( STRING_CR STRING_TAIL );
499 i_message += strlen( message->psz_name ) + i_level * sizeof( " " ) + 1;
500 psz_message = malloc( i_message ); *psz_message = 0;
501 for( i = 0; i < i_level; i++ ) strcat( psz_message, " " );
502 strcat( psz_message, message->psz_name );
504 if( message->psz_value )
506 i_message += sizeof( " : " ) + strlen( message->psz_value ) +
508 psz_message = realloc( psz_message, i_message );
509 strcat( psz_message, " : " );
510 strcat( psz_message, message->psz_value );
511 strcat( psz_message, STRING_CR );
515 i_message += sizeof( STRING_CR );
516 psz_message = realloc( psz_message, i_message );
517 strcat( psz_message, STRING_CR );
520 for( i = 0; i < message->i_child; i++ )
522 char *child_message =
523 MessageToString( message->child[i], i_level + 1 );
525 i_message += strlen( child_message );
526 psz_message = realloc( psz_message, i_message );
527 strcat( psz_message, child_message );
528 free( child_message );
531 if( i_level == 0 ) strcat( psz_message, STRING_TAIL );