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., 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 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" );
196 free( p_intf->p_sys );
200 "Telnet interface started on interface %s %d",
201 url.psz_host, url.i_port );
203 p_intf->p_sys->i_clients = 0;
204 p_intf->p_sys->clients = NULL;
205 p_intf->p_sys->mediatheque = mediatheque;
206 p_intf->pf_run = Run;
211 /*****************************************************************************
213 *****************************************************************************/
214 static void Close( vlc_object_t *p_this )
216 intf_thread_t *p_intf = (intf_thread_t*)p_this;
217 intf_sys_t *p_sys = p_intf->p_sys;
220 for( i = 0; i < p_sys->i_clients; i++ )
222 telnet_client_t *cl = p_sys->clients[i];
227 if( p_sys->clients != NULL ) free( p_sys->clients );
229 net_ListenClose( p_sys->pi_fd );
231 vlm_Delete( p_sys->mediatheque );
236 /*****************************************************************************
238 *****************************************************************************/
239 static void Run( intf_thread_t *p_intf )
241 intf_sys_t *p_sys = p_intf->p_sys;
242 struct timeval timeout;
245 psz_password = config_GetPsz( p_intf, "telnet-password" );
247 while( !p_intf->b_die )
249 fd_set fds_read, fds_write;
250 int i_handle_max = 0;
251 int i_ret, i_len, fd, i;
253 /* if a new client wants to communicate */
254 fd = net_Accept( p_intf, p_sys->pi_fd, p_sys->i_clients > 0 ? 0 : -1 );
259 /* to be non blocking */
260 #if defined( WIN32 ) || defined( UNDER_CE )
262 unsigned long i_dummy = 1;
263 ioctlsocket( fd, FIONBIO, &i_dummy );
266 fcntl( fd, F_SETFL, O_NONBLOCK );
268 cl = malloc( sizeof( telnet_client_t ));
271 cl->buffer_write = NULL;
272 cl->p_buffer_write = cl->buffer_write;
273 Write_message( cl, NULL, "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, "\r\nWrong password. ",
420 else if( cl->i_mode == READ_MODE_CMD &&
421 *cl->p_buffer_read == '\n' )
423 /* ok, here is a command line */
424 if( !strncmp( cl->buffer_read, "logout", 6 ) ||
425 !strncmp( cl->buffer_read, "quit", 4 ) ||
426 !strncmp( cl->buffer_read, "exit", 4 ) )
429 TAB_REMOVE( p_intf->p_sys->i_clients ,
430 p_intf->p_sys->clients , cl );
433 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
435 msg_Err( p_intf, "shutdown requested" );
436 p_intf->p_vlc->b_die = VLC_TRUE;
440 vlm_message_t *message;
442 /* create a standard string */
443 *cl->p_buffer_read = '\0';
445 vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
447 Write_message( cl, message, NULL, WRITE_MODE_CMD );
448 vlm_MessageDelete( message );
455 static void Write_message( telnet_client_t *client, vlm_message_t *message,
456 char *string_message, int i_mode )
460 client->p_buffer_read = client->buffer_read;
461 (client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
462 if( client->buffer_write ) free( client->buffer_write );
464 /* generate the psz_message string */
467 /* ok, look for vlm_message_t */
468 psz_message = MessageToString( message, 0 );
472 /* it is a basic string_message */
473 psz_message = strdup( string_message );
476 client->buffer_write = client->p_buffer_write = psz_message;
477 client->i_buffer_write = strlen( psz_message );
478 client->i_mode = i_mode;
481 /* We need the level of the message to put a beautiful indentation.
482 * first level is 0 */
483 static char *MessageToString( vlm_message_t *message, int i_level )
485 #define STRING_CR "\r\n"
486 #define STRING_TAIL "> "
489 int i, i_message = sizeof( STRING_TAIL );
491 if( !message || !message->psz_name )
493 return strdup( STRING_CR STRING_TAIL );
495 else if( !i_level && !message->i_child && !message->psz_value )
497 /* A command is successful. Don't write anything */
498 return strdup( STRING_CR STRING_TAIL );
501 i_message += strlen( message->psz_name ) + i_level * sizeof( " " ) + 1;
502 psz_message = malloc( i_message ); *psz_message = 0;
503 for( i = 0; i < i_level; i++ ) strcat( psz_message, " " );
504 strcat( psz_message, message->psz_name );
506 if( message->psz_value )
508 i_message += sizeof( " : " ) + strlen( message->psz_value ) +
510 psz_message = realloc( psz_message, i_message );
511 strcat( psz_message, " : " );
512 strcat( psz_message, message->psz_value );
513 strcat( psz_message, STRING_CR );
517 i_message += sizeof( STRING_CR );
518 psz_message = realloc( psz_message, i_message );
519 strcat( psz_message, STRING_CR );
522 for( i = 0; i < message->i_child; i++ )
524 char *child_message =
525 MessageToString( message->child[i], i_level + 1 );
527 i_message += strlen( child_message );
528 psz_message = realloc( psz_message, i_message );
529 strcat( psz_message, child_message );
530 free( child_message );
533 if( i_level == 0 ) strcat( psz_message, STRING_TAIL );