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