1 /*****************************************************************************
2 * telnet.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 TELNETPORT_TEXT N_( "Telnet Interface port" )
80 #define TELNETPORT_LONGTEXT N_( "Default to 4212" )
81 #define TELNETPWD_TEXT N_( "Telnet Interface password" )
82 #define TELNETPWD_LONGTEXT N_( "Default to admin" )
85 set_shortname( "Telnet" );
86 set_category( CAT_INTERFACE );
87 set_subcategory( SUBCAT_INTERFACE_GENERAL );
88 add_integer( "telnet-port", 4212, NULL, TELNETPORT_TEXT,
89 TELNETPORT_LONGTEXT, VLC_TRUE );
90 add_string( "telnet-password", "admin", NULL, TELNETPWD_TEXT,
91 TELNETPWD_LONGTEXT, VLC_TRUE );
92 set_description( _("VLM remote control interface") );
93 add_category_hint( "VLM", NULL, VLC_FALSE );
94 set_capability( "interface", 0 );
95 set_callbacks( Open , Close );
98 /*****************************************************************************
100 *****************************************************************************/
101 static void Run( intf_thread_t * );
105 int i_mode; /* read or write */
107 uint8_t buffer_read[1000]; // 1000 byte per command should be sufficient
109 uint8_t *p_buffer_read;
110 uint8_t *p_buffer_write; // the position in the buffer
111 int i_buffer_write; // the number of byte we still have to send
112 int i_tel_cmd; // for specific telnet commands
116 static char *MessageToString( vlm_message_t *, int );
117 static void Write_message( telnet_client_t *, vlm_message_t *, char *, int );
121 telnet_client_t **clients;
127 /*****************************************************************************
128 * Open: initialize dummy interface
129 *****************************************************************************/
130 static int Open( vlc_object_t *p_this )
132 intf_thread_t *p_intf = (intf_thread_t*) p_this;
136 if( !(mediatheque = vlm_New( p_intf )) )
138 msg_Err( p_intf, "cannot start VLM" );
142 msg_Info( p_intf, "Using the VLM interface plugin..." );
144 i_telnetport = config_GetInt( p_intf, "telnet-port" );
146 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
147 if( ( p_intf->p_sys->fd = net_ListenTCP( p_intf , "", i_telnetport ) ) < 0 )
149 msg_Err( p_intf, "cannot listen for telnet" );
150 free( p_intf->p_sys );
153 msg_Info( p_intf, "Telnet interface started on port: %d", i_telnetport );
155 p_intf->p_sys->i_clients = 0;
156 p_intf->p_sys->clients = NULL;
157 p_intf->p_sys->mediatheque = mediatheque;
158 p_intf->pf_run = Run;
163 /*****************************************************************************
165 *****************************************************************************/
166 static void Close( vlc_object_t *p_this )
168 intf_thread_t *p_intf = (intf_thread_t*)p_this;
169 intf_sys_t *p_sys = p_intf->p_sys;
172 for( i = 0; i < p_sys->i_clients; i++ )
174 telnet_client_t *cl = p_sys->clients[i];
179 if( p_sys->clients != NULL ) free( p_sys->clients );
181 net_Close( p_sys->fd );
183 vlm_Delete( p_sys->mediatheque );
188 /*****************************************************************************
190 *****************************************************************************/
191 static void Run( intf_thread_t *p_intf )
193 intf_sys_t *p_sys = p_intf->p_sys;
194 struct timeval timeout;
197 psz_password = config_GetPsz( p_intf, "telnet-password" );
199 while( !p_intf->b_die )
201 fd_set fds_read, fds_write;
202 int i_handle_max = 0;
203 int i_ret, i_len, fd, i;
205 /* if a new client wants to communicate */
206 fd = net_Accept( p_intf, p_sys->fd, p_sys->i_clients > 0 ? 0 : -1 );
211 /* to be non blocking */
212 #if defined( WIN32 ) || defined( UNDER_CE )
214 unsigned long i_dummy = 1;
215 ioctlsocket( fd, FIONBIO, &i_dummy );
218 fcntl( fd, F_SETFL, O_NONBLOCK );
220 cl = malloc( sizeof( telnet_client_t ));
223 cl->buffer_write = NULL;
224 cl->p_buffer_write = cl->buffer_write;
225 Write_message( cl, NULL, "Password:\xff\xfb\x01", WRITE_MODE_PWD );
227 TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
230 /* to do a proper select */
231 FD_ZERO( &fds_read );
232 FD_ZERO( &fds_write );
234 for( i = 0 ; i < p_sys->i_clients ; i++ )
236 telnet_client_t *cl = p_sys->clients[i];
238 if( cl->i_mode == WRITE_MODE_PWD || cl->i_mode == WRITE_MODE_CMD )
240 FD_SET( cl->fd , &fds_write );
244 FD_SET( cl->fd , &fds_read );
246 i_handle_max = __MAX( i_handle_max, cl->fd );
250 timeout.tv_usec = 500*1000;
252 i_ret = select( i_handle_max + 1, &fds_read, &fds_write, 0, &timeout );
253 if( i_ret == -1 && errno != EINTR )
255 msg_Warn( p_intf, "cannot select sockets" );
259 else if( i_ret <= 0 )
264 /* check if there is something to do with the socket */
265 for( i = 0 ; i < p_sys->i_clients ; i++ )
267 telnet_client_t *cl = p_sys->clients[i];
269 if( FD_ISSET(cl->fd , &fds_write) && cl->i_buffer_write > 0 )
271 i_len = send( cl->fd , cl->p_buffer_write ,
272 cl->i_buffer_write , 0 );
275 cl->p_buffer_write += i_len;
276 cl->i_buffer_write -= i_len;
279 else if( FD_ISSET( cl->fd, &fds_read) )
284 while( (i_recv=recv( cl->fd, cl->p_buffer_read, 1, 0 )) > 0 &&
285 cl->p_buffer_read - cl->buffer_read < 999 )
287 switch( cl->i_tel_cmd )
290 switch( *cl->p_buffer_read )
295 *cl->p_buffer_read = '\n';
298 case TEL_IAC: // telnet specific command
308 switch( *cl->p_buffer_read )
310 case TEL_WILL: case TEL_WONT:
311 case TEL_DO: case TEL_DONT:
323 cl->p_buffer_read -= 2;
327 if( i_end != 0 ) break;
330 if( cl->p_buffer_read - cl->buffer_read == 999 )
332 Write_message( cl, NULL, "Line too long\r\n",
339 TAB_REMOVE( p_intf->p_sys->i_clients ,
340 p_intf->p_sys->clients , cl );
346 /* and now we should bidouille the data we received / send */
347 for( i = 0 ; i < p_sys->i_clients ; i++ )
349 telnet_client_t *cl = p_sys->clients[i];
351 if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 )
353 // we have finished to send
354 cl->i_mode -= 2; // corresponding READ MODE
356 else if( cl->i_mode == READ_MODE_PWD &&
357 *cl->p_buffer_read == '\n' )
359 *cl->p_buffer_read = '\0';
360 if( strcmp( psz_password, cl->buffer_read ) == 0 )
362 Write_message( cl, NULL, "\xff\xfc\x01\r\nWelcome, "
363 "Master\r\n> ", WRITE_MODE_CMD );
368 Write_message( cl, NULL, "\r\nTry again, you polio:",
372 else if( cl->i_mode == READ_MODE_CMD &&
373 *cl->p_buffer_read == '\n' )
375 /* ok, here is a command line */
376 if( !strncmp( cl->buffer_read, "logout", 6 ) ||
377 !strncmp( cl->buffer_read, "quit", 4 ) ||
378 !strncmp( cl->buffer_read, "exit", 4 ) )
381 TAB_REMOVE( p_intf->p_sys->i_clients ,
382 p_intf->p_sys->clients , cl );
385 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
387 msg_Err( p_intf, "shutdown requested" );
388 p_intf->p_vlc->b_die = VLC_TRUE;
392 vlm_message_t *message;
394 /* create a standard string */
395 *cl->p_buffer_read = '\0';
397 vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
399 Write_message( cl, message, NULL, WRITE_MODE_CMD );
400 vlm_MessageDelete( message );
407 static void Write_message( telnet_client_t *client, vlm_message_t *message,
408 char *string_message, int i_mode )
412 client->p_buffer_read = client->buffer_read;
413 (client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
414 if( client->buffer_write ) free( client->buffer_write );
416 /* generate the psz_message string */
419 /* ok, look for vlm_message_t */
420 psz_message = MessageToString( message, 0 );
424 /* it is a basic string_message */
425 psz_message = strdup( string_message );
428 client->buffer_write = client->p_buffer_write = psz_message;
429 client->i_buffer_write = strlen( psz_message );
430 client->i_mode = i_mode;
433 /* We need the level of the message to put a beautiful indentation.
434 * first level is 0 */
435 static char *MessageToString( vlm_message_t *message, int i_level )
437 #define STRING_CR "\r\n"
438 #define STRING_TAIL "> "
441 int i, i_message = sizeof( STRING_TAIL );
443 if( !message || !message->psz_name )
445 return strdup( STRING_CR STRING_TAIL );
447 else if( !i_level && !message->i_child && !message->psz_value )
449 /* A command is successful. Don't write anything */
450 return strdup( STRING_CR STRING_TAIL );
453 i_message += strlen( message->psz_name ) + i_level * sizeof( " " ) + 1;
454 psz_message = malloc( i_message ); *psz_message = 0;
455 for( i = 0; i < i_level; i++ ) strcat( psz_message, " " );
456 strcat( psz_message, message->psz_name );
458 if( message->psz_value )
460 i_message += sizeof( " : " ) + strlen( message->psz_value ) +
462 psz_message = realloc( psz_message, i_message );
463 strcat( psz_message, " : " );
464 strcat( psz_message, message->psz_value );
465 strcat( psz_message, STRING_CR );
469 i_message += sizeof( STRING_CR );
470 psz_message = realloc( psz_message, i_message );
471 strcat( psz_message, STRING_CR );
474 for( i = 0; i < message->i_child; i++ )
476 char *child_message =
477 MessageToString( message->child[i], i_level + 1 );
479 i_message += strlen( child_message );
480 psz_message = realloc( psz_message, i_message );
481 strcat( psz_message, child_message );
482 free( child_message );
485 if( i_level == 0 ) strcat( psz_message, STRING_TAIL );