1 /*****************************************************************************
2 * telnet.c: VLM interface plugin
3 *****************************************************************************
4 * Copyright (C) 2000-2006 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 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_interface.h>
36 #include <vlc_input.h>
43 #ifdef HAVE_SYS_TIME_H
44 # include <sys/time.h>
54 #include <vlc_network.h>
58 #define READ_MODE_PWD 1
59 #define READ_MODE_CMD 2
60 #define WRITE_MODE_PWD 3 // when we write the word "Password:"
61 #define WRITE_MODE_CMD 4
71 /*****************************************************************************
73 *****************************************************************************/
74 static int Open ( vlc_object_t * );
75 static void Close( vlc_object_t * );
77 #define TELNETHOST_TEXT N_( "Host" )
78 #define TELNETHOST_LONGTEXT N_( "This is the host on which the " \
79 "interface will listen. It defaults to all network interfaces (0.0.0.0)." \
80 " If you want this interface to be available only on the local " \
81 "machine, enter \"127.0.0.1\"." )
82 #define TELNETPORT_TEXT N_( "Port" )
83 #define TELNETPORT_LONGTEXT N_( "This is the TCP port on which this " \
84 "interface will listen. It defaults to 4212." )
85 #define TELNETPORT_DEFAULT 4212
86 #define TELNETPWD_TEXT N_( "Password" )
87 #define TELNETPWD_LONGTEXT N_( "A single administration password is used " \
88 "to protect this interface. The default value is \"admin\"." )
89 #define TELNETPWD_DEFAULT "admin"
92 set_shortname( "Telnet" )
93 set_category( CAT_INTERFACE )
94 set_subcategory( SUBCAT_INTERFACE_CONTROL )
95 add_string( "telnet-host", "", NULL, TELNETHOST_TEXT,
96 TELNETHOST_LONGTEXT, true )
97 add_integer( "telnet-port", TELNETPORT_DEFAULT, NULL, TELNETPORT_TEXT,
98 TELNETPORT_LONGTEXT, true )
99 add_password( "telnet-password", TELNETPWD_DEFAULT, NULL, TELNETPWD_TEXT,
100 TELNETPWD_LONGTEXT, true )
101 set_description( N_("VLM remote control interface") )
102 add_category_hint( "VLM", NULL, false )
103 set_capability( "interface", 0 )
104 set_callbacks( Open , Close )
107 /*****************************************************************************
109 *****************************************************************************/
110 static void Run( intf_thread_t * );
114 int i_mode; /* read or write */
116 char buffer_read[1000]; // 1000 byte per command should be sufficient
119 char *p_buffer_write; // the position in the buffer
120 int i_buffer_write; // the number of byte we still have to send
121 int i_tel_cmd; // for specific telnet commands
125 static char *MessageToString( vlm_message_t *, int );
126 static void Write_message( telnet_client_t *, vlm_message_t *, const char *, int );
130 telnet_client_t **clients;
137 * getPort: Decide which port to use. There are two possibilities to
138 * specify a port: integrated in the --telnet-host option with :PORT
139 * or using the --telnet-port option. The --telnet-port option has
141 * This code relies upon the fact the url.i_port is 0 if the :PORT
142 * option is missing from --telnet-host.
144 static int getPort(intf_thread_t *p_intf, const vlc_url_t *url, int i_port)
146 if (i_port == TELNETPORT_DEFAULT && url->i_port != 0)
147 i_port = url->i_port;
148 if (url->i_port != 0 && url->i_port != i_port)
149 // Print error if two different ports have been specified
150 msg_Warn( p_intf, "ignoring port %d (using %d)", url->i_port, i_port );
154 /*****************************************************************************
155 * Open: initialize dummy interface
156 *****************************************************************************/
157 static int Open( vlc_object_t *p_this )
159 intf_thread_t *p_intf = (intf_thread_t*) p_this;
165 if( !(mediatheque = vlm_New( p_intf )) )
167 msg_Err( p_intf, "cannot start VLM" );
171 msg_Info( p_intf, "using the VLM interface plugin..." );
173 i_telnetport = var_InheritInteger( p_intf, "telnet-port" );
174 psz_address = var_InheritString( p_intf, "telnet-host" );
175 vlc_UrlParse(&url, psz_address, 0);
178 // There might be two ports given, resolve any potentially
180 url.i_port = getPort(p_intf, &url, i_telnetport);
182 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
185 vlm_Delete( mediatheque );
186 vlc_UrlClean( &url );
189 if( ( p_intf->p_sys->pi_fd = net_ListenTCP( p_intf, url.psz_host, url.i_port ) ) == NULL )
191 msg_Err( p_intf, "cannot listen for telnet" );
192 vlm_Delete( mediatheque );
193 vlc_UrlClean( &url );
194 free( p_intf->p_sys );
198 "telnet interface started on interface %s %d",
199 url.psz_host, url.i_port );
201 p_intf->p_sys->i_clients = 0;
202 p_intf->p_sys->clients = NULL;
203 p_intf->p_sys->mediatheque = mediatheque;
204 p_intf->pf_run = Run;
206 vlc_UrlClean( &url );
210 /*****************************************************************************
212 *****************************************************************************/
213 static void Close( vlc_object_t *p_this )
215 intf_thread_t *p_intf = (intf_thread_t*)p_this;
216 intf_sys_t *p_sys = p_intf->p_sys;
219 for( i = 0; i < p_sys->i_clients; i++ )
221 telnet_client_t *cl = p_sys->clients[i];
223 free( cl->buffer_write );
226 free( p_sys->clients );
228 net_ListenClose( p_sys->pi_fd );
230 vlm_Delete( p_sys->mediatheque );
235 /*****************************************************************************
237 *****************************************************************************/
238 static void Run( intf_thread_t *p_intf )
240 intf_sys_t *p_sys = p_intf->p_sys;
242 unsigned nlisten = 0;
244 for (const int *pfd = p_sys->pi_fd; *pfd != -1; pfd++)
245 nlisten++; /* How many listening sockets do we have? */
247 /* FIXME: make sure config_* is cancel-safe */
248 psz_password = var_InheritString( p_intf, "telnet-password" );
249 vlc_cleanup_push( free, psz_password );
253 unsigned ncli = p_sys->i_clients;
254 struct pollfd ufd[ncli + nlisten];
256 for (unsigned i = 0; i < ncli; i++)
258 telnet_client_t *cl = p_sys->clients[i];
261 if( (cl->i_mode == WRITE_MODE_PWD) || (cl->i_mode == WRITE_MODE_CMD) )
262 ufd[i].events = POLLOUT;
264 ufd[i].events = POLLIN;
268 for (unsigned i = 0; i < nlisten; i++)
270 ufd[ncli + i].fd = p_sys->pi_fd[i];
271 ufd[ncli + i].events = POLLIN;
272 ufd[ncli + i].revents = 0;
275 switch (poll (ufd, sizeof (ufd) / sizeof (ufd[0]), -1))
278 if (net_errno != EINTR)
280 msg_Err (p_intf, "network poll error");
282 pause (); /* We are screwed! */
284 abort (); /* We are even more screwed! (no pause() in win32) */
292 int canc = vlc_savecancel ();
293 /* check if there is something to do with the socket */
294 for (unsigned i = 0; i < ncli; i++)
296 telnet_client_t *cl = p_sys->clients[i];
298 if (ufd[i].revents & (POLLERR|POLLHUP))
302 TAB_REMOVE( p_intf->p_sys->i_clients ,
303 p_intf->p_sys->clients , cl );
304 free( cl->buffer_write );
309 if (ufd[i].revents & POLLOUT && (cl->i_buffer_write > 0))
313 i_len = send( cl->fd, cl->p_buffer_write ,
314 cl->i_buffer_write, 0 );
317 cl->p_buffer_write += i_len;
318 cl->i_buffer_write -= i_len;
321 if (ufd[i].revents & POLLIN)
326 while( ((i_recv=recv( cl->fd, cl->p_buffer_read, 1, 0 )) > 0) &&
327 ((cl->p_buffer_read - cl->buffer_read) < 999) )
329 switch( cl->i_tel_cmd )
332 switch( *(uint8_t *)cl->p_buffer_read )
337 *cl->p_buffer_read = '\n';
340 case TEL_IAC: // telnet specific command
350 switch( *(uint8_t *)cl->p_buffer_read )
352 case TEL_WILL: case TEL_WONT:
353 case TEL_DO: case TEL_DONT:
365 cl->p_buffer_read -= 2;
372 if( (cl->p_buffer_read - cl->buffer_read) == 999 )
374 Write_message( cl, NULL, "Line too long\r\n",
379 if( i_recv <= 0 && WSAGetLastError() == WSAEWOULDBLOCK )
384 if( i_recv == 0 || ( i_recv == -1 && ( end || errno != EAGAIN ) ) )
389 /* and now we should bidouille the data we received / send */
390 for(int i = 0 ; i < p_sys->i_clients ; i++ )
392 telnet_client_t *cl = p_sys->clients[i];
394 if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 )
396 // we have finished to send
397 cl->i_mode -= 2; // corresponding READ MODE
399 else if( cl->i_mode == READ_MODE_PWD &&
400 *cl->p_buffer_read == '\n' )
402 *cl->p_buffer_read = '\0';
403 if( !psz_password || !strcmp( psz_password, cl->buffer_read ) )
405 Write_message( cl, NULL, "\xff\xfc\x01\r\nWelcome, "
406 "Master\r\n> ", WRITE_MODE_CMD );
411 Write_message( cl, NULL,
412 "\r\nWrong password.\r\nPassword: ",
416 else if( cl->i_mode == READ_MODE_CMD &&
417 *cl->p_buffer_read == '\n' )
419 /* ok, here is a command line */
420 if( !strncmp( cl->buffer_read, "logout", 6 ) ||
421 !strncmp( cl->buffer_read, "quit", 4 ) ||
422 !strncmp( cl->buffer_read, "exit", 4 ) )
425 TAB_REMOVE( p_intf->p_sys->i_clients ,
426 p_intf->p_sys->clients , cl );
427 free( cl->buffer_write );
430 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
432 msg_Err( p_intf, "shutdown requested" );
433 libvlc_Quit( p_intf->p_libvlc );
435 else if( *cl->buffer_read == '@'
436 && strchr( cl->buffer_read, ' ' ) )
438 /* Module specific commands (use same syntax as in the
440 char *psz_name = cl->buffer_read + 1;
441 char *psz_cmd, *psz_arg, *psz_msg;
444 psz_cmd = strchr( cl->buffer_read, ' ' );
445 *psz_cmd = '\0'; psz_cmd++;
446 if( ( psz_arg = strchr( psz_cmd, '\n' ) ) ) *psz_arg = '\0';
447 if( ( psz_arg = strchr( psz_cmd, '\r' ) ) ) *psz_arg = '\0';
448 if( ( psz_arg = strchr( psz_cmd, ' ' ) )
455 i_ret = var_Command( p_intf, psz_name, psz_cmd, psz_arg,
460 vlm_message_t *message;
461 message = vlm_MessageNew( "Module command", "%s", psz_msg );
462 Write_message( cl, message, NULL, WRITE_MODE_CMD );
463 vlm_MessageDelete( message );
469 vlm_message_t *message;
471 /* create a standard string */
472 *cl->p_buffer_read = '\0';
474 vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
476 if( !strncmp( cl->buffer_read, "help", 4 ) )
478 vlm_message_t *p_my_help =
479 vlm_MessageSimpleNew( "Telnet Specific Commands:" );
480 vlm_MessageAdd( p_my_help,
481 vlm_MessageSimpleNew( "logout, quit, exit" ) );
482 vlm_MessageAdd( p_my_help,
483 vlm_MessageSimpleNew( "shutdown" ) );
484 vlm_MessageAdd( p_my_help,
485 vlm_MessageSimpleNew( "@moduleinstance command argument" ) );
486 vlm_MessageAdd( message, p_my_help );
488 Write_message( cl, message, NULL, WRITE_MODE_CMD );
489 vlm_MessageDelete( message );
494 /* handle new connections */
495 for (unsigned i = 0; i < nlisten; i++)
499 if (ufd[ncli + i].revents == 0)
502 fd = net_AcceptSingle (VLC_OBJECT(p_intf), ufd[ncli + i].fd);
506 telnet_client_t *cl = calloc( 1, sizeof( telnet_client_t ));
515 cl->buffer_write = NULL;
516 cl->p_buffer_write = cl->buffer_write;
517 Write_message( cl, NULL,
518 "Password: \xff\xfb\x01" , WRITE_MODE_PWD );
519 TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
521 vlc_restorecancel( canc );
526 static void Write_message( telnet_client_t *client, vlm_message_t *message,
527 const char *string_message, int i_mode )
531 client->p_buffer_read = client->buffer_read;
532 (client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
533 free( client->buffer_write );
535 /* generate the psz_message string */
538 /* ok, look for vlm_message_t */
539 psz_message = MessageToString( message, 0 );
543 /* it is a basic string_message */
544 psz_message = strdup( string_message );
547 client->buffer_write = client->p_buffer_write = psz_message;
548 client->i_buffer_write = strlen( psz_message );
549 client->i_mode = i_mode;
552 /* We need the level of the message to put a beautiful indentation.
553 * first level is 0 */
554 static char *MessageToString( vlm_message_t *message, int i_level )
556 #define STRING_CR "\r\n"
557 #define STRING_TAIL "> "
560 int i, i_message = sizeof( STRING_TAIL );
562 if( !message || !message->psz_name )
564 return strdup( STRING_CR STRING_TAIL );
566 else if( !i_level && !message->i_child && !message->psz_value )
568 /* A command is successful. Don't write anything */
569 return strdup( /*STRING_CR*/ STRING_TAIL );
572 i_message += strlen( message->psz_name ) + i_level * sizeof( " " ) + 1;
573 psz_message = xmalloc( i_message );
575 for( i = 0; i < i_level; i++ ) strcat( psz_message, " " );
576 strcat( psz_message, message->psz_name );
578 if( message->psz_value )
580 i_message += sizeof( " : " ) + strlen( message->psz_value ) +
582 psz_message = xrealloc( psz_message, i_message );
583 strcat( psz_message, " : " );
584 strcat( psz_message, message->psz_value );
585 strcat( psz_message, STRING_CR );
589 i_message += sizeof( STRING_CR );
590 psz_message = xrealloc( psz_message, i_message );
591 strcat( psz_message, STRING_CR );
594 for( i = 0; i < message->i_child; i++ )
596 char *child_message =
597 MessageToString( message->child[i], i_level + 1 );
599 i_message += strlen( child_message );
600 psz_message = xrealloc( psz_message, i_message );
601 strcat( psz_message, child_message );
602 free( child_message );
605 if( i_level == 0 ) strcat( psz_message, STRING_TAIL );