]> git.sesse.net Git - vlc/blob - modules/control/telnet.c
beos/VideoOutput.cpp: fixed a crash in window destruction
[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     add_integer( "telnet-port", 4212, NULL, TELNETPORT_TEXT,
86                  TELNETPORT_LONGTEXT, VLC_TRUE );
87     add_string( "telnet-password", "admin", NULL, TELNETPWD_TEXT,
88                 TELNETPWD_LONGTEXT, VLC_TRUE );
89     set_description( _("Telnet remote control interface") );
90     add_category_hint( "VLM", NULL, VLC_FALSE );
91     set_capability( "interface", 0 );
92     set_callbacks( Open , Close );
93 vlc_module_end();
94
95 /*****************************************************************************
96  * Local prototypes.
97  *****************************************************************************/
98 static void Run( intf_thread_t * );
99
100 typedef struct
101 {
102     int        i_mode; /* read or write */
103     int        fd;
104     uint8_t    buffer_read[1000]; // 1000 byte per command should be sufficient
105     char      *buffer_write;
106     uint8_t   *p_buffer_read;
107     uint8_t   *p_buffer_write; // the position in the buffer
108     int        i_buffer_write; // the number of byte we still have to send
109     int        i_tel_cmd; // for specific telnet commands
110
111 } telnet_client_t;
112
113 static char *MessageToString( vlm_message_t *, int );
114 static void Write_message( telnet_client_t *, vlm_message_t *, char *, int );
115
116 struct intf_sys_t
117 {
118    telnet_client_t **clients;
119    int             i_clients;
120    int             fd;
121    vlm_t           *mediatheque;
122 };
123
124 /*****************************************************************************
125  * Open: initialize dummy interface
126  *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
128 {
129     intf_thread_t *p_intf = (intf_thread_t*) p_this;
130     vlm_t *mediatheque;
131     int i_telnetport;
132
133     if( !(mediatheque = vlm_New( p_intf )) )
134     {
135         msg_Err( p_intf, "cannot start VLM" );
136         return VLC_EGENERIC;
137     }
138
139     msg_Info( p_intf, "Using the VLM interface plugin..." );
140
141     i_telnetport = config_GetInt( p_intf, "telnet-port" );
142
143     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
144     if( ( p_intf->p_sys->fd = net_ListenTCP( p_intf , "", i_telnetport ) ) < 0 )
145     {
146         msg_Err( p_intf, "cannot listen for telnet" );
147         free( p_intf->p_sys );
148         return VLC_EGENERIC;
149     }
150     msg_Info( p_intf, "Telnet interface started on port: %d", i_telnetport );
151
152     p_intf->p_sys->i_clients   = 0;
153     p_intf->p_sys->clients     = NULL;
154     p_intf->p_sys->mediatheque = mediatheque;
155     p_intf->pf_run = Run;
156
157     return VLC_SUCCESS;
158 }
159
160 /*****************************************************************************
161  * Close:
162  *****************************************************************************/
163 static void Close( vlc_object_t *p_this )
164 {
165     intf_thread_t *p_intf = (intf_thread_t*)p_this;
166     intf_sys_t    *p_sys  = p_intf->p_sys;
167     int i;
168
169     for( i = 0; i < p_sys->i_clients; i++ )
170     {
171         telnet_client_t *cl = p_sys->clients[i];
172
173         net_Close( cl->fd );
174         free( cl );
175     }
176     if( p_sys->clients != NULL ) free( p_sys->clients );
177
178     net_Close( p_sys->fd );
179
180     vlm_Delete( p_sys->mediatheque );
181
182     free( p_sys );
183 }
184
185 /*****************************************************************************
186  * Run: main loop
187  *****************************************************************************/
188 static void Run( intf_thread_t *p_intf )
189 {
190     intf_sys_t     *p_sys = p_intf->p_sys;
191     struct timeval  timeout;
192     char           *psz_password;
193
194     psz_password = config_GetPsz( p_intf, "telnet-password" );
195
196     while( !p_intf->b_die )
197     {
198         fd_set fds_read, fds_write;
199         int    i_handle_max = 0;
200         int    i_ret, i_len, fd, i;
201
202         /* if a new client wants to communicate */
203         fd = net_Accept( p_intf, p_sys->fd, p_sys->i_clients > 0 ? 0 : -1 );
204         if( fd > 0 )
205         {
206             telnet_client_t *cl;
207
208             /* to be non blocking */
209 #if defined( WIN32 ) || defined( UNDER_CE )
210             {
211                 unsigned long i_dummy = 1;
212                 ioctlsocket( fd, FIONBIO, &i_dummy );
213             }
214 #else
215             fcntl( fd, F_SETFL, O_NONBLOCK );
216 #endif
217             cl = malloc( sizeof( telnet_client_t ));
218             cl->i_tel_cmd = 0;
219             cl->fd = fd;
220             cl->buffer_write = NULL;
221             cl->p_buffer_write = cl->buffer_write;
222             Write_message( cl, NULL, "Password:\xff\xfb\x01", WRITE_MODE_PWD );
223
224             TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
225         }
226
227         /* to do a proper select */
228         FD_ZERO( &fds_read );
229         FD_ZERO( &fds_write );
230
231         for( i = 0 ; i < p_sys->i_clients ; i++ )
232         {
233             telnet_client_t *cl = p_sys->clients[i];
234
235             if( cl->i_mode == WRITE_MODE_PWD || cl->i_mode == WRITE_MODE_CMD )
236             {
237                 FD_SET( cl->fd , &fds_write );
238             }
239             else
240             {
241                 FD_SET( cl->fd , &fds_read );
242             }
243             i_handle_max = __MAX( i_handle_max, cl->fd );
244         }
245
246         timeout.tv_sec = 0;
247         timeout.tv_usec = 500*1000;
248
249         i_ret = select( i_handle_max + 1, &fds_read, &fds_write, 0, &timeout );
250         if( i_ret == -1 && errno != EINTR )
251         {
252             msg_Warn( p_intf, "cannot select sockets" );
253             msleep( 1000 );
254             continue;
255         }
256         else if( i_ret <= 0 )
257         {
258             continue;
259         }
260
261         /* check if there is something to do with the socket */
262         for( i = 0 ; i < p_sys->i_clients ; i++ )
263         {
264             telnet_client_t *cl = p_sys->clients[i];
265
266             if( FD_ISSET(cl->fd , &fds_write) && cl->i_buffer_write > 0 )
267             {
268                 i_len = send( cl->fd , cl->p_buffer_write ,
269                               cl->i_buffer_write , 0 );
270                 if( i_len > 0 )
271                 {
272                     cl->p_buffer_write += i_len;
273                     cl->i_buffer_write -= i_len;
274                 }
275             }
276             else if( FD_ISSET( cl->fd, &fds_read) )
277             {
278                 int i_end = 0;
279
280                 while( recv( cl->fd, cl->p_buffer_read, 1, 0 ) > 0 &&
281                        cl->p_buffer_read - cl->buffer_read < 999 )
282                 {
283                     switch( cl->i_tel_cmd )
284                     {
285                     case 0:
286                         switch( *cl->p_buffer_read )
287                         {
288                         case '\r':
289                             break;
290                         case '\n':
291                             *cl->p_buffer_read = '\n';
292                             i_end = 1;
293                             break;
294                         case TEL_IAC: // telnet specific command
295                             cl->i_tel_cmd = 1;
296                             cl->p_buffer_read++;
297                             break;
298                         default:
299                             cl->p_buffer_read++;
300                             break;
301                         }
302                         break;
303                     case 1:
304                         switch( *cl->p_buffer_read )
305                         {
306                         case TEL_WILL: case TEL_WONT:
307                         case TEL_DO: case TEL_DONT:
308                             cl->i_tel_cmd++;
309                             cl->p_buffer_read++;
310                             break;
311                         default:
312                             cl->i_tel_cmd = 0;
313                             cl->p_buffer_read--;
314                             break;
315                         }
316                         break;
317                     case 2:
318                         cl->i_tel_cmd = 0;
319                         cl->p_buffer_read -= 2;
320                         break;
321                     }
322
323                     if( i_end != 0 ) break;
324                 }
325
326                 if( cl->p_buffer_read - cl->buffer_read == 999 )
327                 {
328                     Write_message( cl, NULL, "Line too long\r\n",
329                                    cl->i_mode + 2 );
330                 }
331             }
332         }
333
334         /* and now we should bidouille the data we received / send */
335         for( i = 0 ; i < p_sys->i_clients ; i++ )
336         {
337             telnet_client_t *cl = p_sys->clients[i];
338
339             if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 )
340             {
341                // we have finished to send
342                cl->i_mode -= 2; // corresponding READ MODE
343             }
344             else if( cl->i_mode == READ_MODE_PWD &&
345                      *cl->p_buffer_read == '\n' )
346             {
347                 *cl->p_buffer_read = '\0';
348                 if( strcmp( psz_password, cl->buffer_read ) == 0 )
349                 {
350                     Write_message( cl, NULL, "\xff\xfc\x01\r\nWelcome, "
351                                    "Master\r\n> ", WRITE_MODE_CMD );
352                 }
353                 else
354                 {
355                     /* wrong password */
356                     Write_message( cl, NULL, "\r\nTry again, you polio:",
357                                    WRITE_MODE_PWD );
358                 }
359             }
360             else if( cl->i_mode == READ_MODE_CMD &&
361                      *cl->p_buffer_read == '\n' )
362             {
363                 /* ok, here is a command line */
364                 if( !strncmp( cl->buffer_read, "logout", 6 ) ||
365                     !strncmp( cl->buffer_read, "quit", 4 )  ||
366                     !strncmp( cl->buffer_read, "exit", 4 ) )
367                 {
368                     net_Close( cl->fd );
369                     TAB_REMOVE( p_intf->p_sys->i_clients ,
370                                 p_intf->p_sys->clients , cl );
371                     free( cl );
372                 }
373                 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
374                 {
375                     msg_Err( p_intf, "shutdown requested" );
376                     p_intf->p_vlc->b_die = VLC_TRUE;
377                 }
378                 else
379                 {
380                     vlm_message_t *message;
381
382                     /* create a standard string */
383                     *cl->p_buffer_read = '\0';
384
385                     vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
386                                         &message );
387                     Write_message( cl, message, NULL, WRITE_MODE_CMD );
388                     vlm_MessageDelete( message );
389                 }
390             }
391         }
392     }
393 }
394
395 static void Write_message( telnet_client_t *client, vlm_message_t *message,
396                            char *string_message, int i_mode )
397 {
398     char *psz_message;
399
400     client->p_buffer_read = client->buffer_read;
401     (client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
402     if( client->buffer_write ) free( client->buffer_write );
403
404     /* generate the psz_message string */
405     if( message )
406     {
407         /* ok, look for vlm_message_t */
408         psz_message = MessageToString( message, 0 );
409     }
410     else
411     {
412         /* it is a basic string_message */
413         psz_message = strdup( string_message );
414     }
415
416     client->buffer_write = client->p_buffer_write = psz_message;
417     client->i_buffer_write = strlen( psz_message );
418     client->i_mode = i_mode;
419 }
420
421 /* We need the level of the message to put a beautiful indentation.
422  * first level is 0 */
423 static char *MessageToString( vlm_message_t *message, int i_level )
424 {
425 #define STRING_CR "\r\n"
426 #define STRING_TAIL "> "
427
428     char *psz_message;
429     int i, i_message = sizeof( STRING_TAIL );
430
431     if( !message || !message->psz_name )
432     {
433         return strdup( STRING_CR STRING_TAIL );
434     }
435     else if( !i_level && !message->i_child && !message->psz_value  )
436     {
437         /* A command is successful. Don't write anything */
438         return strdup( STRING_CR STRING_TAIL );
439     }
440
441     i_message += strlen( message->psz_name ) + i_level * sizeof( "    " ) + 1;
442     psz_message = malloc( i_message ); *psz_message = 0;
443     for( i = 0; i < i_level; i++ ) strcat( psz_message, "    " );
444     strcat( psz_message, message->psz_name );
445
446     if( message->psz_value )
447     {
448         i_message += sizeof( " : " ) + strlen( message->psz_value ) +
449             sizeof( STRING_CR );
450         psz_message = realloc( psz_message, i_message );
451         strcat( psz_message, " : " );
452         strcat( psz_message, message->psz_value );
453         strcat( psz_message, STRING_CR );
454     }
455     else
456     {
457         i_message += sizeof( STRING_CR );
458         psz_message = realloc( psz_message, i_message );
459         strcat( psz_message, STRING_CR );
460     }
461
462     for( i = 0; i < message->i_child; i++ )
463     {
464         char *child_message =
465             MessageToString( message->child[i], i_level + 1 );
466
467         i_message += strlen( child_message );
468         psz_message = realloc( psz_message, i_message );
469         strcat( psz_message, child_message );
470         free( child_message );
471     }
472
473     if( i_level == 0 ) strcat( psz_message, STRING_TAIL );
474
475     return psz_message;
476 }