]> git.sesse.net Git - vlc/blob - modules/control/telnet.c
Don't explicitly flag the block. Clement could you check if fixes the break of local...
[vlc] / modules / control / telnet.c
1 /*****************************************************************************
2  * telnet.c: VLM interface plugin
3  *****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
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 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"
87
88 vlc_module_begin();
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 );
102 vlc_module_end();
103
104 /*****************************************************************************
105  * Local prototypes.
106  *****************************************************************************/
107 static void Run( intf_thread_t * );
108
109 typedef struct
110 {
111     int        i_mode; /* read or write */
112     int        fd;
113     char       buffer_read[1000]; // 1000 byte per command should be sufficient
114     char      *buffer_write;
115     char      *p_buffer_read;
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
119
120 } telnet_client_t;
121
122 static char *MessageToString( vlm_message_t *, int );
123 static void Write_message( telnet_client_t *, vlm_message_t *, char *, int );
124
125 struct intf_sys_t
126 {
127    telnet_client_t **clients;
128    int             i_clients;
129    int             *pi_fd;
130    vlm_t           *mediatheque;
131 };
132
133 /* 
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
137  * precedence. 
138  * This code relies upon the fact the url.i_port is 0 if the :PORT 
139  * option is missing from --telnet-host.
140  */
141 static int getPort(intf_thread_t *p_intf, vlc_url_t url, int i_port)
142     {
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 )
147     {
148             msg_Err( p_intf, "ignoring port %d and using %d", url.i_port,
149                  i_port);
150     }
151     if (i_port != TELNETPORT_DEFAULT)
152     {
153             return i_port;
154     }
155     if (url.i_port != 0)
156     {
157             return url.i_port;
158     }
159     // return default
160     return i_port;
161 }
162
163 /*****************************************************************************
164  * Open: initialize dummy interface
165  *****************************************************************************/
166 static int Open( vlc_object_t *p_this )
167 {
168     intf_thread_t *p_intf = (intf_thread_t*) p_this;
169     vlm_t *mediatheque;
170     char *psz_address;
171     vlc_url_t url;
172     int i_telnetport;
173
174     if( !(mediatheque = vlm_New( p_intf )) )
175     {
176         msg_Err( p_intf, "cannot start VLM" );
177         return VLC_EGENERIC;
178     }
179
180     msg_Info( p_intf, "Using the VLM interface plugin..." );
181
182     i_telnetport = config_GetInt( p_intf, "telnet-port" );
183     psz_address  = config_GetPsz( p_intf, "telnet-host" );
184
185     vlc_UrlParse(&url, psz_address, 0);
186
187     // There might be two ports given, resolve any potentially
188     // conflict
189     url.i_port = getPort(p_intf, url, i_telnetport);
190
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 ) )
193                 == NULL )
194     {
195         msg_Err( p_intf, "cannot listen for telnet" );
196         free( p_intf->p_sys );
197         return VLC_EGENERIC;
198     }
199     msg_Info( p_intf, 
200               "Telnet interface started on interface %s %d",
201               url.psz_host, url.i_port );
202
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;
207
208     return VLC_SUCCESS;
209 }
210
211 /*****************************************************************************
212  * Close:
213  *****************************************************************************/
214 static void Close( vlc_object_t *p_this )
215 {
216     intf_thread_t *p_intf = (intf_thread_t*)p_this;
217     intf_sys_t    *p_sys  = p_intf->p_sys;
218     int i;
219
220     for( i = 0; i < p_sys->i_clients; i++ )
221     {
222         telnet_client_t *cl = p_sys->clients[i];
223
224         net_Close( cl->fd );
225         free( cl );
226     }
227     if( p_sys->clients != NULL ) free( p_sys->clients );
228
229     net_ListenClose( p_sys->pi_fd );
230
231     vlm_Delete( p_sys->mediatheque );
232
233     free( p_sys );
234 }
235
236 /*****************************************************************************
237  * Run: main loop
238  *****************************************************************************/
239 static void Run( intf_thread_t *p_intf )
240 {
241     intf_sys_t     *p_sys = p_intf->p_sys;
242     struct timeval  timeout;
243     char           *psz_password;
244
245     psz_password = config_GetPsz( p_intf, "telnet-password" );
246
247     while( !p_intf->b_die )
248     {
249         fd_set fds_read, fds_write;
250         int    i_handle_max = 0;
251         int    i_ret, i_len, fd, i;
252
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 );
255         if( fd > 0 )
256         {
257             telnet_client_t *cl;
258
259             /* to be non blocking */
260 #if defined( WIN32 ) || defined( UNDER_CE )
261             {
262                 unsigned long i_dummy = 1;
263                 ioctlsocket( fd, FIONBIO, &i_dummy );
264             }
265 #else
266             fcntl( fd, F_SETFL, O_NONBLOCK );
267 #endif
268             cl = malloc( sizeof( telnet_client_t ));
269             cl->i_tel_cmd = 0;
270             cl->fd = fd;
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 );
274
275             TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
276         }
277
278         /* to do a proper select */
279         FD_ZERO( &fds_read );
280         FD_ZERO( &fds_write );
281
282         for( i = 0 ; i < p_sys->i_clients ; i++ )
283         {
284             telnet_client_t *cl = p_sys->clients[i];
285
286             if( cl->i_mode == WRITE_MODE_PWD || cl->i_mode == WRITE_MODE_CMD )
287             {
288                 FD_SET( cl->fd , &fds_write );
289             }
290             else
291             {
292                 FD_SET( cl->fd , &fds_read );
293             }
294             i_handle_max = __MAX( i_handle_max, cl->fd );
295         }
296
297         timeout.tv_sec = 0;
298         timeout.tv_usec = 500*1000;
299
300         i_ret = select( i_handle_max + 1, &fds_read, &fds_write, 0, &timeout );
301         if( i_ret == -1 && errno != EINTR )
302         {
303             msg_Warn( p_intf, "cannot select sockets" );
304             msleep( 1000 );
305             continue;
306         }
307         else if( i_ret <= 0 )
308         {
309             continue;
310         }
311
312         /* check if there is something to do with the socket */
313         for( i = 0 ; i < p_sys->i_clients ; i++ )
314         {
315             telnet_client_t *cl = p_sys->clients[i];
316
317             if( FD_ISSET(cl->fd , &fds_write) && cl->i_buffer_write > 0 )
318             {
319                 i_len = send( cl->fd , cl->p_buffer_write ,
320                               cl->i_buffer_write , 0 );
321                 if( i_len > 0 )
322                 {
323                     cl->p_buffer_write += i_len;
324                     cl->i_buffer_write -= i_len;
325                 }
326             }
327             else if( FD_ISSET( cl->fd, &fds_read) )
328             {
329                 int i_end = 0;
330                 int i_recv;
331
332                 while( (i_recv=recv( cl->fd, cl->p_buffer_read, 1, 0 )) > 0 &&
333                        cl->p_buffer_read - cl->buffer_read < 999 )
334                 {
335                     switch( cl->i_tel_cmd )
336                     {
337                     case 0:
338                         switch( *(uint8_t *)cl->p_buffer_read )
339                         {
340                         case '\r':
341                             break;
342                         case '\n':
343                             *cl->p_buffer_read = '\n';
344                             i_end = 1;
345                             break;
346                         case TEL_IAC: // telnet specific command
347                             cl->i_tel_cmd = 1;
348                             cl->p_buffer_read++;
349                             break;
350                         default:
351                             cl->p_buffer_read++;
352                             break;
353                         }
354                         break;
355                     case 1:
356                         switch( *(uint8_t *)cl->p_buffer_read )
357                         {
358                         case TEL_WILL: case TEL_WONT:
359                         case TEL_DO: case TEL_DONT:
360                             cl->i_tel_cmd++;
361                             cl->p_buffer_read++;
362                             break;
363                         default:
364                             cl->i_tel_cmd = 0;
365                             cl->p_buffer_read--;
366                             break;
367                         }
368                         break;
369                     case 2:
370                         cl->i_tel_cmd = 0;
371                         cl->p_buffer_read -= 2;
372                         break;
373                     }
374
375                     if( i_end != 0 ) break;
376                 }
377
378                 if( cl->p_buffer_read - cl->buffer_read == 999 )
379                 {
380                     Write_message( cl, NULL, "Line too long\r\n",
381                                    cl->i_mode + 2 );
382                 }
383
384                 if (i_recv == 0)
385                 {
386                     net_Close( cl->fd );
387                     TAB_REMOVE( p_intf->p_sys->i_clients ,
388                                 p_intf->p_sys->clients , cl );
389                     free( cl );
390                 }
391             }
392         }
393
394         /* and now we should bidouille the data we received / send */
395         for( i = 0 ; i < p_sys->i_clients ; i++ )
396         {
397             telnet_client_t *cl = p_sys->clients[i];
398
399             if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 )
400             {
401                // we have finished to send
402                cl->i_mode -= 2; // corresponding READ MODE
403             }
404             else if( cl->i_mode == READ_MODE_PWD &&
405                      *cl->p_buffer_read == '\n' )
406             {
407                 *cl->p_buffer_read = '\0';
408                 if( strcmp( psz_password, cl->buffer_read ) == 0 )
409                 {
410                     Write_message( cl, NULL, "\xff\xfc\x01\r\nWelcome, "
411                                    "Master\r\n> ", WRITE_MODE_CMD );
412                 }
413                 else
414                 {
415                     /* wrong password */
416                     Write_message( cl, NULL, "\r\nWrong password. ",
417                                    WRITE_MODE_PWD );
418                 }
419             }
420             else if( cl->i_mode == READ_MODE_CMD &&
421                      *cl->p_buffer_read == '\n' )
422             {
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 ) )
427                 {
428                     net_Close( cl->fd );
429                     TAB_REMOVE( p_intf->p_sys->i_clients ,
430                                 p_intf->p_sys->clients , cl );
431                     free( cl );
432                 }
433                 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
434                 {
435                     msg_Err( p_intf, "shutdown requested" );
436                     p_intf->p_vlc->b_die = VLC_TRUE;
437                 }
438                 else
439                 {
440                     vlm_message_t *message;
441
442                     /* create a standard string */
443                     *cl->p_buffer_read = '\0';
444
445                     vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
446                                         &message );
447                     Write_message( cl, message, NULL, WRITE_MODE_CMD );
448                     vlm_MessageDelete( message );
449                 }
450             }
451         }
452     }
453 }
454
455 static void Write_message( telnet_client_t *client, vlm_message_t *message,
456                            char *string_message, int i_mode )
457 {
458     char *psz_message;
459
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 );
463
464     /* generate the psz_message string */
465     if( message )
466     {
467         /* ok, look for vlm_message_t */
468         psz_message = MessageToString( message, 0 );
469     }
470     else
471     {
472         /* it is a basic string_message */
473         psz_message = strdup( string_message );
474     }
475
476     client->buffer_write = client->p_buffer_write = psz_message;
477     client->i_buffer_write = strlen( psz_message );
478     client->i_mode = i_mode;
479 }
480
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 )
484 {
485 #define STRING_CR "\r\n"
486 #define STRING_TAIL "> "
487
488     char *psz_message;
489     int i, i_message = sizeof( STRING_TAIL );
490
491     if( !message || !message->psz_name )
492     {
493         return strdup( STRING_CR STRING_TAIL );
494     }
495     else if( !i_level && !message->i_child && !message->psz_value  )
496     {
497         /* A command is successful. Don't write anything */
498         return strdup( STRING_CR STRING_TAIL );
499     }
500
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 );
505
506     if( message->psz_value )
507     {
508         i_message += sizeof( " : " ) + strlen( message->psz_value ) +
509             sizeof( STRING_CR );
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 );
514     }
515     else
516     {
517         i_message += sizeof( STRING_CR );
518         psz_message = realloc( psz_message, i_message );
519         strcat( psz_message, STRING_CR );
520     }
521
522     for( i = 0; i < message->i_child; i++ )
523     {
524         char *child_message =
525             MessageToString( message->child[i], i_level + 1 );
526
527         i_message += strlen( child_message );
528         psz_message = realloc( psz_message, i_message );
529         strcat( psz_message, child_message );
530         free( child_message );
531     }
532
533     if( i_level == 0 ) strcat( psz_message, STRING_TAIL );
534
535     return psz_message;
536 }