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 add_integer( "telnet-port", 4212, NULL, TELNETPORT_TEXT,
86 TELNETPORT_LONGTEXT, VLC_TRUE );
87 add_string( "telnet-password", "admin", NULL, TELNETPWD_TEXT,
88 TELNETPWD_LONGTEXT, VLC_TRUE );
89 set_description( _("Telnet remote control interface") );
90 add_category_hint( "VLM", NULL, VLC_FALSE );
91 set_capability( "interface", 0 );
92 set_callbacks( Open , Close );
95 /*****************************************************************************
97 *****************************************************************************/
98 static void Run( intf_thread_t * );
102 int i_mode; /* read or write */
104 uint8_t buffer_read[1000]; // 1000 byte per command should be sufficient
106 uint8_t *p_buffer_read;
107 uint8_t *p_buffer_write; // the position in the buffer
108 int i_buffer_write; // the number of byte we still have to send
109 int i_tel_cmd; // for specific telnet commands
113 static char *MessageToString( vlm_message_t *, int );
114 static void Write_message( telnet_client_t *, vlm_message_t *, char *, int );
118 telnet_client_t **clients;
124 /*****************************************************************************
125 * Open: initialize dummy interface
126 *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
129 intf_thread_t *p_intf = (intf_thread_t*) p_this;
133 if( !(mediatheque = vlm_New( p_intf )) )
135 msg_Err( p_intf, "cannot start VLM" );
139 msg_Info( p_intf, "Using the VLM interface plugin..." );
141 i_telnetport = config_GetInt( p_intf, "telnet-port" );
143 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
144 if( ( p_intf->p_sys->fd = net_ListenTCP( p_intf , "", i_telnetport ) ) < 0 )
146 msg_Err( p_intf, "cannot listen for telnet" );
147 free( p_intf->p_sys );
150 msg_Info( p_intf, "Telnet interface started on port: %d", i_telnetport );
152 p_intf->p_sys->i_clients = 0;
153 p_intf->p_sys->clients = NULL;
154 p_intf->p_sys->mediatheque = mediatheque;
155 p_intf->pf_run = Run;
160 /*****************************************************************************
162 *****************************************************************************/
163 static void Close( vlc_object_t *p_this )
165 intf_thread_t *p_intf = (intf_thread_t*)p_this;
166 intf_sys_t *p_sys = p_intf->p_sys;
169 for( i = 0; i < p_sys->i_clients; i++ )
171 telnet_client_t *cl = p_sys->clients[i];
176 if( p_sys->clients != NULL ) free( p_sys->clients );
178 net_Close( p_sys->fd );
180 vlm_Delete( p_sys->mediatheque );
185 /*****************************************************************************
187 *****************************************************************************/
188 static void Run( intf_thread_t *p_intf )
190 intf_sys_t *p_sys = p_intf->p_sys;
191 struct timeval timeout;
194 psz_password = config_GetPsz( p_intf, "telnet-password" );
196 while( !p_intf->b_die )
198 fd_set fds_read, fds_write;
199 int i_handle_max = 0;
200 int i_ret, i_len, fd, i;
202 /* if a new client wants to communicate */
203 fd = net_Accept( p_intf, p_sys->fd, p_sys->i_clients > 0 ? 0 : -1 );
208 /* to be non blocking */
209 #if defined( WIN32 ) || defined( UNDER_CE )
211 unsigned long i_dummy = 1;
212 ioctlsocket( fd, FIONBIO, &i_dummy );
215 fcntl( fd, F_SETFL, O_NONBLOCK );
217 cl = malloc( sizeof( telnet_client_t ));
220 cl->buffer_write = NULL;
221 cl->p_buffer_write = cl->buffer_write;
222 Write_message( cl, NULL, "Password:\xff\xfb\x01", WRITE_MODE_PWD );
224 TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
227 /* to do a proper select */
228 FD_ZERO( &fds_read );
229 FD_ZERO( &fds_write );
231 for( i = 0 ; i < p_sys->i_clients ; i++ )
233 telnet_client_t *cl = p_sys->clients[i];
235 if( cl->i_mode == WRITE_MODE_PWD || cl->i_mode == WRITE_MODE_CMD )
237 FD_SET( cl->fd , &fds_write );
241 FD_SET( cl->fd , &fds_read );
243 i_handle_max = __MAX( i_handle_max, cl->fd );
247 timeout.tv_usec = 500*1000;
249 i_ret = select( i_handle_max + 1, &fds_read, &fds_write, 0, &timeout );
250 if( i_ret == -1 && errno != EINTR )
252 msg_Warn( p_intf, "cannot select sockets" );
256 else if( i_ret <= 0 )
261 /* check if there is something to do with the socket */
262 for( i = 0 ; i < p_sys->i_clients ; i++ )
264 telnet_client_t *cl = p_sys->clients[i];
266 if( FD_ISSET(cl->fd , &fds_write) && cl->i_buffer_write > 0 )
268 i_len = send( cl->fd , cl->p_buffer_write ,
269 cl->i_buffer_write , 0 );
272 cl->p_buffer_write += i_len;
273 cl->i_buffer_write -= i_len;
276 else if( FD_ISSET( cl->fd, &fds_read) )
280 while( recv( cl->fd, cl->p_buffer_read, 1, 0 ) > 0 &&
281 cl->p_buffer_read - cl->buffer_read < 999 )
283 switch( cl->i_tel_cmd )
286 switch( *cl->p_buffer_read )
291 *cl->p_buffer_read = '\n';
294 case TEL_IAC: // telnet specific command
304 switch( *cl->p_buffer_read )
306 case TEL_WILL: case TEL_WONT:
307 case TEL_DO: case TEL_DONT:
319 cl->p_buffer_read -= 2;
323 if( i_end != 0 ) break;
326 if( cl->p_buffer_read - cl->buffer_read == 999 )
328 Write_message( cl, NULL, "Line too long\r\n",
334 /* and now we should bidouille the data we received / send */
335 for( i = 0 ; i < p_sys->i_clients ; i++ )
337 telnet_client_t *cl = p_sys->clients[i];
339 if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 )
341 // we have finished to send
342 cl->i_mode -= 2; // corresponding READ MODE
344 else if( cl->i_mode == READ_MODE_PWD &&
345 *cl->p_buffer_read == '\n' )
347 *cl->p_buffer_read = '\0';
348 if( strcmp( psz_password, cl->buffer_read ) == 0 )
350 Write_message( cl, NULL, "\xff\xfc\x01\r\nWelcome, "
351 "Master\r\n> ", WRITE_MODE_CMD );
356 Write_message( cl, NULL, "\r\nTry again, you polio:",
360 else if( cl->i_mode == READ_MODE_CMD &&
361 *cl->p_buffer_read == '\n' )
363 /* ok, here is a command line */
364 if( !strncmp( cl->buffer_read, "logout", 6 ) ||
365 !strncmp( cl->buffer_read, "quit", 4 ) ||
366 !strncmp( cl->buffer_read, "exit", 4 ) )
369 TAB_REMOVE( p_intf->p_sys->i_clients ,
370 p_intf->p_sys->clients , cl );
373 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
375 msg_Err( p_intf, "shutdown requested" );
376 p_intf->p_vlc->b_die = VLC_TRUE;
380 vlm_message_t *message;
382 /* create a standard string */
383 *cl->p_buffer_read = '\0';
385 vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
387 Write_message( cl, message, NULL, WRITE_MODE_CMD );
388 vlm_MessageDelete( message );
395 static void Write_message( telnet_client_t *client, vlm_message_t *message,
396 char *string_message, int i_mode )
400 client->p_buffer_read = client->buffer_read;
401 (client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
402 if( client->buffer_write ) free( client->buffer_write );
404 /* generate the psz_message string */
407 /* ok, look for vlm_message_t */
408 psz_message = MessageToString( message, 0 );
412 /* it is a basic string_message */
413 psz_message = strdup( string_message );
416 client->buffer_write = client->p_buffer_write = psz_message;
417 client->i_buffer_write = strlen( psz_message );
418 client->i_mode = i_mode;
421 /* We need the level of the message to put a beautiful indentation.
422 * first level is 0 */
423 static char *MessageToString( vlm_message_t *message, int i_level )
425 #define STRING_CR "\r\n"
426 #define STRING_TAIL "> "
429 int i, i_message = sizeof( STRING_TAIL );
431 if( !message || !message->psz_name )
433 return strdup( STRING_CR STRING_TAIL );
435 else if( !i_level && !message->i_child && !message->psz_value )
437 /* A command is successful. Don't write anything */
438 return strdup( STRING_CR STRING_TAIL );
441 i_message += strlen( message->psz_name ) + i_level * sizeof( " " ) + 1;
442 psz_message = malloc( i_message ); *psz_message = 0;
443 for( i = 0; i < i_level; i++ ) strcat( psz_message, " " );
444 strcat( psz_message, message->psz_name );
446 if( message->psz_value )
448 i_message += sizeof( " : " ) + strlen( message->psz_value ) +
450 psz_message = realloc( psz_message, i_message );
451 strcat( psz_message, " : " );
452 strcat( psz_message, message->psz_value );
453 strcat( psz_message, STRING_CR );
457 i_message += sizeof( STRING_CR );
458 psz_message = realloc( psz_message, i_message );
459 strcat( psz_message, STRING_CR );
462 for( i = 0; i < message->i_child; i++ )
464 char *child_message =
465 MessageToString( message->child[i], i_level + 1 );
467 i_message += strlen( child_message );
468 psz_message = realloc( psz_message, i_message );
469 strcat( psz_message, child_message );
470 free( child_message );
473 if( i_level == 0 ) strcat( psz_message, STRING_TAIL );