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>
52 # include <ws2tcpip.h>
54 # define IN_MULTICAST(a) IN_CLASSD(a)
57 # include <netdb.h> /* hostent ... */
58 # include <sys/socket.h>
59 # include <netinet/in.h>
60 # ifdef HAVE_ARPA_INET_H
61 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
69 #if defined( WIN32 ) || defined( UNDER_CE )
70 #define SOCKET_CLOSE(a) closesocket(a)
72 #define SOCKET_CLOSE(a) close(a)
75 #define LISTEN_BACKLOG 100
77 #define READ_MODE_PWD 1
78 #define READ_MODE_CMD 2
79 #define WRITE_MODE_PWD 3 // when we write the word "Password:"
80 #define WRITE_MODE_CMD 4
90 /*****************************************************************************
92 *****************************************************************************/
93 static int Open ( vlc_object_t * );
94 static void Close( vlc_object_t * );
96 #define TELNETPORT_TEXT N_( "Telnet Interface port" )
97 #define TELNETPORT_LONGTEXT N_( "Default to 4212" )
98 #define TELNETPWD_TEXT N_( "Telnet Interface password" )
99 #define TELNETPWD_LONGTEXT N_( "Default to admin" )
102 add_integer( "telnet-port", 4212, NULL, TELNETPORT_TEXT, TELNETPORT_LONGTEXT, VLC_TRUE );
103 add_string( "telnet-password", "admin", NULL,
104 TELNETPWD_TEXT, TELNETPWD_LONGTEXT, VLC_TRUE );
105 set_description( _("telnet remote control interface") );
106 add_category_hint( N_( "VLM" ), NULL, VLC_FALSE );
107 set_capability( "interface", 0 );
108 set_callbacks( Open , Close );
111 /*****************************************************************************
113 *****************************************************************************/
114 static void Run( intf_thread_t * );
118 int i_mode; /* read or write */
120 uint8_t buffer_read[1000]; // 1000 byte per command should be sufficient
122 uint8_t *p_buffer_read;
123 uint8_t *p_buffer_write; // the position in the buffer
124 int i_buffer_write; // the number of byte we still have to send
125 int i_tel_cmd; // for specific telnet commands
128 static char* MessageToString( vlm_message_t* , int );
129 static void Write_message( telnet_client_t * , vlm_message_t* , char * , int );
130 static int SocketListen( intf_thread_t * , int );
134 telnet_client_t **clients;
140 /*****************************************************************************
141 * Open: initialize dummy interface
142 *****************************************************************************/
143 static int Open( vlc_object_t *p_this )
145 intf_thread_t *p_intf = (intf_thread_t*) p_this;
148 i_telnetport = config_GetInt( p_intf, "telnet-port" );
152 b_quiet = config_GetInt( p_intf, "dummy-quiet" );
157 msg_Info( p_intf, _("Using the VLM interface plugin...") );
159 p_intf->pf_run = Run;
161 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
162 if( ( p_intf->p_sys->fd = SocketListen( p_intf , i_telnetport ) ) < 0 )
164 msg_Err( p_intf, "cannot listen for telnet" );
165 free( p_intf->p_sys );
168 msg_Info( p_intf, _("Telnet interface started on port: %d"), i_telnetport );
170 p_intf->p_sys->i_clients = 0;
171 p_intf->p_sys->clients = NULL;
172 p_intf->p_sys->mediatheque = vlm_New( p_intf );
177 /*****************************************************************************
179 *****************************************************************************/
180 static void Close( vlc_object_t *p_this )
182 intf_thread_t *p_intf = (intf_thread_t*)p_this;
183 intf_sys_t *p_sys = p_intf->p_sys;
186 for( i = 0; i < p_sys->i_clients; i++ )
188 telnet_client_t *cl = p_sys->clients[i];
193 if( p_sys->clients != NULL ) free( p_sys->clients );
195 net_Close( p_sys->fd );
197 vlm_Delete( p_sys->mediatheque );
203 /*****************************************************************************
205 *****************************************************************************/
206 static void Run( intf_thread_t *p_intf )
208 intf_sys_t *p_sys = p_intf->p_sys;
209 struct timeval timeout;
210 int i_sock_size = sizeof( struct sockaddr_in );
213 s_password = config_GetPsz( p_intf, "telnet-password" );
215 while( !p_intf->b_die )
219 int i_handle_max = 0;
221 struct sockaddr_in sock2;
226 /* if a new client wants to communicate */
227 fd = accept( p_sys->fd, (struct sockaddr *)&sock2, &i_sock_size );
232 /* to be non blockant */
233 #if defined( WIN32 ) || defined( UNDER_CE )
235 unsigned long i_dummy = 1;
236 ioctlsocket( fd, FIONBIO, &i_dummy );
239 fcntl( fd, F_SETFL, O_NONBLOCK );
241 cl = malloc( sizeof( telnet_client_t ));
244 cl->buffer_write = NULL;
245 cl->p_buffer_write = cl->buffer_write;
246 Write_message( cl, NULL, "Password:\xff\xfb\x01", WRITE_MODE_PWD );
248 TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
251 /* to do a proper select */
252 FD_ZERO( &fds_read );
253 FD_ZERO( &fds_write );
255 for( i = 0 ; i < p_sys->i_clients ; i++ )
257 telnet_client_t *cl = p_sys->clients[i];
259 if( cl->i_mode == WRITE_MODE_PWD || cl->i_mode == WRITE_MODE_CMD )
261 FD_SET( cl->fd , &fds_write );
265 FD_SET( cl->fd , &fds_read );
267 i_handle_max = __MAX( i_handle_max, cl->fd );
271 timeout.tv_usec = 500*1000;
273 i_ret = select( i_handle_max + 1, &fds_read, &fds_write, NULL, &timeout );
274 if( i_ret == -1 && errno != EINTR )
276 msg_Warn( p_intf, "cannot select sockets" );
280 else if( i_ret <= 0 )
285 /* check if there is something to do with the socket */
286 for( i = 0 ; i < p_sys->i_clients ; i++ )
288 telnet_client_t *cl = p_sys->clients[i];
290 if( FD_ISSET(cl->fd , &fds_write) && cl->i_buffer_write > 0 )
292 i_len = send( cl->fd , cl->p_buffer_write ,
293 cl->i_buffer_write , 0 );
296 cl->p_buffer_write += i_len;
297 cl->i_buffer_write -= i_len;
300 else if( FD_ISSET( cl->fd, &fds_read) )
304 while( recv( cl->fd, cl->p_buffer_read, 1, 0 ) > 0 &&
305 cl->p_buffer_read - cl->buffer_read < 999 )
307 switch( cl->i_tel_cmd )
310 switch( *cl->p_buffer_read )
315 *cl->p_buffer_read = '\n';
318 case TEL_IAC: // telnet specific command
328 switch( *cl->p_buffer_read )
330 case TEL_WILL: case TEL_WONT: case TEL_DO: case TEL_DONT:
342 cl->p_buffer_read -= 2;
352 if( cl->p_buffer_read - cl->buffer_read == 999 ) // too long !
354 Write_message( cl , NULL, "Line too long\n" , cl->i_mode + 2 );
359 /* and now we should bidouille the data we received / send */
360 for( i = 0 ; i < p_sys->i_clients ; i++ )
362 telnet_client_t *cl = p_sys->clients[i];
364 if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 ) // we have finished to send
366 cl->i_mode -= 2; // corresponding READ MODE
368 else if( cl->i_mode == READ_MODE_PWD && *cl->p_buffer_read == '\n' )
370 *cl->p_buffer_read = '\0';
371 if( strcmp( s_password, cl->buffer_read ) == 0 )
373 Write_message( cl , NULL, "\xff\xfc\x01\nWelcome, Master\n> ", WRITE_MODE_CMD );
378 Write_message( cl , NULL, "\n\rTry again, you polio:\n" , WRITE_MODE_PWD );
381 else if( cl->i_mode == READ_MODE_CMD && *cl->p_buffer_read == '\n' )
383 /* ok, here is a command line */
384 if( !strncmp( cl->buffer_read, "logout", 6 ) ||
385 !strncmp( cl->buffer_read, "quit", 4 ) ||
386 !strncmp( cl->buffer_read, "exit", 4 ) )
389 TAB_REMOVE( p_intf->p_sys->i_clients , p_intf->p_sys->clients , cl );
392 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
394 msg_Err( p_intf, "shutdown requested" );
395 p_intf->p_vlc->b_die = VLC_TRUE;
399 vlm_message_t *message;
401 /* create a standard string */
402 *cl->p_buffer_read = '\0';
404 vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read , &message);
406 Write_message( cl , message, NULL , WRITE_MODE_CMD );
408 vlm_MessageDelete( message );
415 static void Write_message( telnet_client_t * client, vlm_message_t * message, char * string_message, int i_mode )
419 client->p_buffer_read = client->buffer_read;
420 (client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
421 if( client->buffer_write ) free( client->buffer_write );
423 /* generate the psz_message string */
424 if( message != NULL ) /* ok, look for vlm_message_t */
426 psz_message = MessageToString( message , 0 );
427 psz_message = realloc( psz_message , strlen( psz_message ) + strlen( "\n> " ) + 1 );
428 strcat( psz_message , "\n> " );
430 else /* it is a basic string_message */
432 psz_message = strdup( string_message );
435 client->buffer_write = malloc( strlen( psz_message ) + 1 );
436 strcpy( client->buffer_write , psz_message );
437 client->p_buffer_write = client->buffer_write;
438 client->i_buffer_write = strlen( psz_message );
439 client->i_mode = i_mode;
443 /* Does what we want except select and accept */
444 static int SocketListen( intf_thread_t *p_intf , int i_port )
446 struct sockaddr_in sock;
452 fd = socket( AF_INET, SOCK_STREAM, 0 );
455 msg_Err( p_intf, "cannot open socket" );
460 if( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
461 (void *) &i_opt, sizeof( i_opt ) ) < 0 )
463 msg_Warn( p_intf, "cannot configure socket (SO_REUSEADDR)" );
466 /* fill p_socket structure */
467 memset( &sock, 0, sizeof( struct sockaddr_in ) );
468 sock.sin_family = AF_INET; /* family */
469 sock.sin_port = htons( (uint16_t)i_port );
470 sock.sin_addr.s_addr = INADDR_ANY;
473 if( bind( fd, (struct sockaddr *)&sock, sizeof( struct sockaddr_in ) ) < 0 )
475 msg_Err( p_intf, "cannot bind socket" );
479 /* set to non-blocking */
480 #if defined( WIN32 ) || defined( UNDER_CE )
482 unsigned long i_dummy = 1;
483 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
485 msg_Err( p_intf, "cannot set socket to non-blocking mode" );
490 if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 )
492 msg_Err( p_intf, "cannot F_GETFL socket" );
495 if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
497 msg_Err( p_intf, "cannot F_SETFL O_NONBLOCK" );
502 if( listen( fd, LISTEN_BACKLOG ) < 0 )
504 msg_Err( p_intf, "cannot listen socket" );
518 /* we need the level of the message to put a beautiful indentation.
520 static char* MessageToString( vlm_message_t* message , int i_level )
525 if( message == NULL )
529 else if( i_level == 0 && message->i_child == 0 && message->psz_value == NULL ) /* a command is successful */
531 /* don't write anything */
536 psz_message = strdup( "" );
537 psz_message = realloc( psz_message , strlen( psz_message ) + strlen( message->psz_name ) + i_level + 1 );
538 for( i = 0 ; i < i_level ; i++ )
540 strcat( psz_message , " " );
542 strcat( psz_message , message->psz_name );
543 if( message->psz_value )
545 psz_message = realloc( psz_message , strlen( psz_message ) + strlen( message->psz_value ) + 3 + 1 );
546 strcat( psz_message , " : " );
547 strcat( psz_message , message->psz_value );
550 for( i = 0 ; i < message->i_child ; i++ )
552 char *child_message = MessageToString( message->child[i] , i_level + 1 );
554 psz_message = realloc( psz_message , strlen( psz_message ) + strlen( child_message ) + 1 + 1 );
555 strcat( psz_message, "\n" );
556 strcat( psz_message, child_message );
557 free( child_message );