]> git.sesse.net Git - vlc/blob - modules/control/telnet.c
* ALL: removed l10n of various untranslatable strings such as 'ffmpeg' or 'Vorbis'
[vlc] / modules / control / telnet.c
1 /*****************************************************************************
2  * telnet.c: VLM interface plugin
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id$
6  *
7  * Authors: Simon Latapie <garf@videolan.org>
8  *          Laurent Aimar <fenrir@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/intf.h>
32
33 #include <vlc/input.h>
34
35 #include <sys/stat.h>
36
37 #include <errno.h>
38 #include <fcntl.h>
39
40 #ifdef HAVE_SYS_TIME_H
41 #    include <sys/time.h>
42 #endif
43
44 #ifdef HAVE_UNISTD_H
45 #   include <unistd.h>
46 #endif
47
48 #if defined( UNDER_CE )
49 #   include <winsock.h>
50 #elif defined( WIN32 )
51 #   include <winsock2.h>
52 #else
53 #   include <sys/socket.h>
54 #endif
55
56 #include "network.h"
57
58 #include "vlc_vlm.h"
59
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
64
65 /* telnet commands */
66 #define TEL_WILL    251
67 #define TEL_WONT    252
68 #define TEL_DO      253
69 #define TEL_DONT    254
70 #define TEL_IAC     255
71 #define TEL_ECHO    1
72
73 /*****************************************************************************
74  * Module descriptor
75  *****************************************************************************/
76 static int  Open ( vlc_object_t * );
77 static void Close( vlc_object_t * );
78
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" )
83
84 vlc_module_begin();
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 );
96 vlc_module_end();
97
98 /*****************************************************************************
99  * Local prototypes.
100  *****************************************************************************/
101 static void Run( intf_thread_t * );
102
103 typedef struct
104 {
105     int        i_mode; /* read or write */
106     int        fd;
107     uint8_t    buffer_read[1000]; // 1000 byte per command should be sufficient
108     char      *buffer_write;
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
113
114 } telnet_client_t;
115
116 static char *MessageToString( vlm_message_t *, int );
117 static void Write_message( telnet_client_t *, vlm_message_t *, char *, int );
118
119 struct intf_sys_t
120 {
121    telnet_client_t **clients;
122    int             i_clients;
123    int             fd;
124    vlm_t           *mediatheque;
125 };
126
127 /*****************************************************************************
128  * Open: initialize dummy interface
129  *****************************************************************************/
130 static int Open( vlc_object_t *p_this )
131 {
132     intf_thread_t *p_intf = (intf_thread_t*) p_this;
133     vlm_t *mediatheque;
134     int i_telnetport;
135
136     if( !(mediatheque = vlm_New( p_intf )) )
137     {
138         msg_Err( p_intf, "cannot start VLM" );
139         return VLC_EGENERIC;
140     }
141
142     msg_Info( p_intf, "Using the VLM interface plugin..." );
143
144     i_telnetport = config_GetInt( p_intf, "telnet-port" );
145
146     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
147     if( ( p_intf->p_sys->fd = net_ListenTCP( p_intf , "", i_telnetport ) ) < 0 )
148     {
149         msg_Err( p_intf, "cannot listen for telnet" );
150         free( p_intf->p_sys );
151         return VLC_EGENERIC;
152     }
153     msg_Info( p_intf, "Telnet interface started on port: %d", i_telnetport );
154
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;
159
160     return VLC_SUCCESS;
161 }
162
163 /*****************************************************************************
164  * Close:
165  *****************************************************************************/
166 static void Close( vlc_object_t *p_this )
167 {
168     intf_thread_t *p_intf = (intf_thread_t*)p_this;
169     intf_sys_t    *p_sys  = p_intf->p_sys;
170     int i;
171
172     for( i = 0; i < p_sys->i_clients; i++ )
173     {
174         telnet_client_t *cl = p_sys->clients[i];
175
176         net_Close( cl->fd );
177         free( cl );
178     }
179     if( p_sys->clients != NULL ) free( p_sys->clients );
180
181     net_Close( p_sys->fd );
182
183     vlm_Delete( p_sys->mediatheque );
184
185     free( p_sys );
186 }
187
188 /*****************************************************************************
189  * Run: main loop
190  *****************************************************************************/
191 static void Run( intf_thread_t *p_intf )
192 {
193     intf_sys_t     *p_sys = p_intf->p_sys;
194     struct timeval  timeout;
195     char           *psz_password;
196
197     psz_password = config_GetPsz( p_intf, "telnet-password" );
198
199     while( !p_intf->b_die )
200     {
201         fd_set fds_read, fds_write;
202         int    i_handle_max = 0;
203         int    i_ret, i_len, fd, i;
204
205         /* if a new client wants to communicate */
206         fd = net_Accept( p_intf, p_sys->fd, p_sys->i_clients > 0 ? 0 : -1 );
207         if( fd > 0 )
208         {
209             telnet_client_t *cl;
210
211             /* to be non blocking */
212 #if defined( WIN32 ) || defined( UNDER_CE )
213             {
214                 unsigned long i_dummy = 1;
215                 ioctlsocket( fd, FIONBIO, &i_dummy );
216             }
217 #else
218             fcntl( fd, F_SETFL, O_NONBLOCK );
219 #endif
220             cl = malloc( sizeof( telnet_client_t ));
221             cl->i_tel_cmd = 0;
222             cl->fd = fd;
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 );
226
227             TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
228         }
229
230         /* to do a proper select */
231         FD_ZERO( &fds_read );
232         FD_ZERO( &fds_write );
233
234         for( i = 0 ; i < p_sys->i_clients ; i++ )
235         {
236             telnet_client_t *cl = p_sys->clients[i];
237
238             if( cl->i_mode == WRITE_MODE_PWD || cl->i_mode == WRITE_MODE_CMD )
239             {
240                 FD_SET( cl->fd , &fds_write );
241             }
242             else
243             {
244                 FD_SET( cl->fd , &fds_read );
245             }
246             i_handle_max = __MAX( i_handle_max, cl->fd );
247         }
248
249         timeout.tv_sec = 0;
250         timeout.tv_usec = 500*1000;
251
252         i_ret = select( i_handle_max + 1, &fds_read, &fds_write, 0, &timeout );
253         if( i_ret == -1 && errno != EINTR )
254         {
255             msg_Warn( p_intf, "cannot select sockets" );
256             msleep( 1000 );
257             continue;
258         }
259         else if( i_ret <= 0 )
260         {
261             continue;
262         }
263
264         /* check if there is something to do with the socket */
265         for( i = 0 ; i < p_sys->i_clients ; i++ )
266         {
267             telnet_client_t *cl = p_sys->clients[i];
268
269             if( FD_ISSET(cl->fd , &fds_write) && cl->i_buffer_write > 0 )
270             {
271                 i_len = send( cl->fd , cl->p_buffer_write ,
272                               cl->i_buffer_write , 0 );
273                 if( i_len > 0 )
274                 {
275                     cl->p_buffer_write += i_len;
276                     cl->i_buffer_write -= i_len;
277                 }
278             }
279             else if( FD_ISSET( cl->fd, &fds_read) )
280             {
281                 int i_end = 0;
282                 int i_recv;
283
284                 while( (i_recv=recv( cl->fd, cl->p_buffer_read, 1, 0 )) > 0 &&
285                        cl->p_buffer_read - cl->buffer_read < 999 )
286                 {
287                     switch( cl->i_tel_cmd )
288                     {
289                     case 0:
290                         switch( *cl->p_buffer_read )
291                         {
292                         case '\r':
293                             break;
294                         case '\n':
295                             *cl->p_buffer_read = '\n';
296                             i_end = 1;
297                             break;
298                         case TEL_IAC: // telnet specific command
299                             cl->i_tel_cmd = 1;
300                             cl->p_buffer_read++;
301                             break;
302                         default:
303                             cl->p_buffer_read++;
304                             break;
305                         }
306                         break;
307                     case 1:
308                         switch( *cl->p_buffer_read )
309                         {
310                         case TEL_WILL: case TEL_WONT:
311                         case TEL_DO: case TEL_DONT:
312                             cl->i_tel_cmd++;
313                             cl->p_buffer_read++;
314                             break;
315                         default:
316                             cl->i_tel_cmd = 0;
317                             cl->p_buffer_read--;
318                             break;
319                         }
320                         break;
321                     case 2:
322                         cl->i_tel_cmd = 0;
323                         cl->p_buffer_read -= 2;
324                         break;
325                     }
326
327                     if( i_end != 0 ) break;
328                 }
329
330                 if( cl->p_buffer_read - cl->buffer_read == 999 )
331                 {
332                     Write_message( cl, NULL, "Line too long\r\n",
333                                    cl->i_mode + 2 );
334                 }
335
336                 if (i_recv == 0)
337                 {
338                     net_Close( cl->fd );
339                     TAB_REMOVE( p_intf->p_sys->i_clients ,
340                                 p_intf->p_sys->clients , cl );
341                     free( cl );
342                 }
343             }
344         }
345
346         /* and now we should bidouille the data we received / send */
347         for( i = 0 ; i < p_sys->i_clients ; i++ )
348         {
349             telnet_client_t *cl = p_sys->clients[i];
350
351             if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 )
352             {
353                // we have finished to send
354                cl->i_mode -= 2; // corresponding READ MODE
355             }
356             else if( cl->i_mode == READ_MODE_PWD &&
357                      *cl->p_buffer_read == '\n' )
358             {
359                 *cl->p_buffer_read = '\0';
360                 if( strcmp( psz_password, cl->buffer_read ) == 0 )
361                 {
362                     Write_message( cl, NULL, "\xff\xfc\x01\r\nWelcome, "
363                                    "Master\r\n> ", WRITE_MODE_CMD );
364                 }
365                 else
366                 {
367                     /* wrong password */
368                     Write_message( cl, NULL, "\r\nTry again, you polio:",
369                                    WRITE_MODE_PWD );
370                 }
371             }
372             else if( cl->i_mode == READ_MODE_CMD &&
373                      *cl->p_buffer_read == '\n' )
374             {
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 ) )
379                 {
380                     net_Close( cl->fd );
381                     TAB_REMOVE( p_intf->p_sys->i_clients ,
382                                 p_intf->p_sys->clients , cl );
383                     free( cl );
384                 }
385                 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
386                 {
387                     msg_Err( p_intf, "shutdown requested" );
388                     p_intf->p_vlc->b_die = VLC_TRUE;
389                 }
390                 else
391                 {
392                     vlm_message_t *message;
393
394                     /* create a standard string */
395                     *cl->p_buffer_read = '\0';
396
397                     vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
398                                         &message );
399                     Write_message( cl, message, NULL, WRITE_MODE_CMD );
400                     vlm_MessageDelete( message );
401                 }
402             }
403         }
404     }
405 }
406
407 static void Write_message( telnet_client_t *client, vlm_message_t *message,
408                            char *string_message, int i_mode )
409 {
410     char *psz_message;
411
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 );
415
416     /* generate the psz_message string */
417     if( message )
418     {
419         /* ok, look for vlm_message_t */
420         psz_message = MessageToString( message, 0 );
421     }
422     else
423     {
424         /* it is a basic string_message */
425         psz_message = strdup( string_message );
426     }
427
428     client->buffer_write = client->p_buffer_write = psz_message;
429     client->i_buffer_write = strlen( psz_message );
430     client->i_mode = i_mode;
431 }
432
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 )
436 {
437 #define STRING_CR "\r\n"
438 #define STRING_TAIL "> "
439
440     char *psz_message;
441     int i, i_message = sizeof( STRING_TAIL );
442
443     if( !message || !message->psz_name )
444     {
445         return strdup( STRING_CR STRING_TAIL );
446     }
447     else if( !i_level && !message->i_child && !message->psz_value  )
448     {
449         /* A command is successful. Don't write anything */
450         return strdup( STRING_CR STRING_TAIL );
451     }
452
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 );
457
458     if( message->psz_value )
459     {
460         i_message += sizeof( " : " ) + strlen( message->psz_value ) +
461             sizeof( STRING_CR );
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 );
466     }
467     else
468     {
469         i_message += sizeof( STRING_CR );
470         psz_message = realloc( psz_message, i_message );
471         strcat( psz_message, STRING_CR );
472     }
473
474     for( i = 0; i < message->i_child; i++ )
475     {
476         char *child_message =
477             MessageToString( message->child[i], i_level + 1 );
478
479         i_message += strlen( child_message );
480         psz_message = realloc( psz_message, i_message );
481         strcat( psz_message, child_message );
482         free( child_message );
483     }
484
485     if( i_level == 0 ) strcat( psz_message, STRING_TAIL );
486
487     return psz_message;
488 }