]> git.sesse.net Git - vlc/blob - modules/control/telnet.c
* vlm.*: move vlm to the core (now, vlm_New create only one instance)
[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 #   include <ws2tcpip.h>
53 #   ifndef IN_MULTICAST
54 #       define IN_MULTICAST(a) IN_CLASSD(a)
55 #   endif
56 #else
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() */
62 #   endif
63 #endif
64
65 #include "network.h"
66
67 #include "vlc_vlm.h"
68
69 #if defined( WIN32 ) || defined( UNDER_CE )
70 #define SOCKET_CLOSE(a)    closesocket(a)
71 #else
72 #define SOCKET_CLOSE(a)    close(a)
73 #endif
74
75 #define LISTEN_BACKLOG 100
76
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
81
82 /* telnet commands */
83 #define TEL_WILL    251
84 #define TEL_WONT    252
85 #define TEL_DO      253
86 #define TEL_DONT    254
87 #define TEL_IAC     255
88 #define TEL_ECHO    1
89
90 /*****************************************************************************
91  * Module descriptor
92  *****************************************************************************/
93 static int  Open ( vlc_object_t * );
94 static void Close( vlc_object_t * );
95
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" )
100
101 vlc_module_begin();
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 );
109 vlc_module_end();
110
111 /*****************************************************************************
112  * Local prototypes.
113  *****************************************************************************/
114 static void Run( intf_thread_t * );
115
116 typedef struct
117 {
118     int        i_mode; /* read or write */
119     int        fd;
120     uint8_t    buffer_read[1000]; // 1000 byte per command should be sufficient
121     char      *buffer_write;
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
126 } telnet_client_t;
127
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 );
131
132 struct intf_sys_t
133 {
134    telnet_client_t **clients;
135    int             i_clients;
136    int             fd;
137    vlm_t          *mediatheque;
138 };
139
140 /*****************************************************************************
141  * Open: initialize dummy interface
142  *****************************************************************************/
143 static int Open( vlc_object_t *p_this )
144 {
145     intf_thread_t *p_intf = (intf_thread_t*) p_this;
146     int i_telnetport;
147
148     i_telnetport = config_GetInt( p_intf, "telnet-port" );
149
150 #ifdef WIN32
151     vlc_bool_t b_quiet;
152     b_quiet = config_GetInt( p_intf, "dummy-quiet" );
153     if( !b_quiet )
154         CONSOLE_INTRO_MSG;
155 #endif
156
157     msg_Info( p_intf, _("Using the VLM interface plugin...") );
158
159     p_intf->pf_run = Run;
160
161     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
162     if( ( p_intf->p_sys->fd = SocketListen( p_intf , i_telnetport ) ) < 0 )
163     {
164         msg_Err( p_intf, "cannot listen for telnet" );
165         free( p_intf->p_sys );
166         return VLC_EGENERIC;
167     }
168     msg_Info( p_intf, _("Telnet interface started on port: %d"), i_telnetport );
169
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 );
173
174     return VLC_SUCCESS;
175 }
176
177 /*****************************************************************************
178  * Close:
179  *****************************************************************************/
180 static void Close( vlc_object_t *p_this )
181 {
182     intf_thread_t *p_intf = (intf_thread_t*)p_this;
183     intf_sys_t    *p_sys  = p_intf->p_sys;
184     int i;
185
186     for( i = 0; i < p_sys->i_clients; i++ )
187     {
188         telnet_client_t *cl = p_sys->clients[i];
189
190         net_Close( cl->fd );
191         free( cl );
192     }
193     if( p_sys->clients != NULL ) free( p_sys->clients );
194
195     net_Close( p_sys->fd );
196
197     vlm_Delete( p_sys->mediatheque );
198
199     free( p_sys );
200 }
201
202
203 /*****************************************************************************
204  * Run: main loop
205  *****************************************************************************/
206 static void Run( intf_thread_t *p_intf )
207 {
208     intf_sys_t     *p_sys = p_intf->p_sys;
209     struct timeval  timeout;
210     int             i_sock_size = sizeof( struct sockaddr_in );
211     char           *s_password;
212
213     s_password = config_GetPsz( p_intf, "telnet-password" );
214
215     while( !p_intf->b_die )
216     {
217         fd_set          fds_read;
218         fd_set          fds_write;
219         int             i_handle_max = 0;
220         int             i_ret;
221         struct          sockaddr_in sock2;
222         int             i_len;
223         int             fd;
224         int             i;
225
226         /* if a new client wants to communicate */
227         fd = accept( p_sys->fd, (struct sockaddr *)&sock2, &i_sock_size );
228         if( fd > 0 )
229         {
230             telnet_client_t *cl;
231
232             /* to be non blockant */
233 #if defined( WIN32 ) || defined( UNDER_CE )
234             {
235                 unsigned long i_dummy = 1;
236                 ioctlsocket( fd, FIONBIO, &i_dummy );
237             }
238 #else
239             fcntl( fd, F_SETFL, O_NONBLOCK );
240 #endif
241             cl = malloc( sizeof( telnet_client_t ));
242             cl->i_tel_cmd = 0;
243             cl->fd = fd;
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 );
247
248             TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
249         }
250
251         /* to do a proper select */
252         FD_ZERO( &fds_read );
253         FD_ZERO( &fds_write );
254
255         for( i = 0 ; i < p_sys->i_clients ; i++ )
256         {
257             telnet_client_t *cl = p_sys->clients[i];
258
259             if( cl->i_mode == WRITE_MODE_PWD || cl->i_mode == WRITE_MODE_CMD )
260             {
261                 FD_SET( cl->fd , &fds_write );
262             }
263             else
264             {
265                 FD_SET( cl->fd , &fds_read );
266             }
267             i_handle_max = __MAX( i_handle_max, cl->fd );
268         }
269
270         timeout.tv_sec = 0;
271         timeout.tv_usec = 500*1000;
272
273         i_ret = select( i_handle_max + 1, &fds_read, &fds_write, NULL, &timeout );
274         if( i_ret == -1 && errno != EINTR )
275         {
276             msg_Warn( p_intf, "cannot select sockets" );
277             msleep( 1000 );
278             continue;
279         }
280         else if( i_ret <= 0 )
281         {
282             continue;
283         }
284
285         /* check if there is something to do with the socket */
286         for( i = 0 ; i < p_sys->i_clients ; i++ )
287         {
288             telnet_client_t *cl = p_sys->clients[i];
289
290             if( FD_ISSET(cl->fd , &fds_write) && cl->i_buffer_write > 0 )
291             {
292                 i_len = send( cl->fd , cl->p_buffer_write ,
293                               cl->i_buffer_write , 0 );
294                 if( i_len > 0 )
295                 {
296                     cl->p_buffer_write += i_len;
297                     cl->i_buffer_write -= i_len;
298                 }
299             }
300             else if( FD_ISSET( cl->fd, &fds_read) )
301             {
302                 int i_end = 0;
303
304                 while( recv( cl->fd, cl->p_buffer_read, 1, 0 ) > 0 &&
305                        cl->p_buffer_read - cl->buffer_read < 999 )
306                 {
307                     switch( cl->i_tel_cmd )
308                     {
309                         case 0:
310                             switch( *cl->p_buffer_read )
311                             {
312                                 case '\r':
313                                     break;
314                                 case '\n':
315                                     *cl->p_buffer_read = '\n';
316                                     i_end = 1;
317                                     break;
318                                 case TEL_IAC: // telnet specific command
319                                     cl->i_tel_cmd = 1;
320                                     cl->p_buffer_read++;
321                                     break;
322                                 default:
323                                     cl->p_buffer_read++;
324                                     break;
325                             }
326                             break;
327                         case 1:
328                             switch( *cl->p_buffer_read )
329                             {
330                                 case TEL_WILL: case TEL_WONT: case TEL_DO: case TEL_DONT:
331                                     cl->i_tel_cmd++;
332                                     cl->p_buffer_read++;
333                                     break;
334                                 default:
335                                     cl->i_tel_cmd = 0;
336                                     cl->p_buffer_read--;
337                                     break;
338                             }
339                             break;
340                         case 2:
341                             cl->i_tel_cmd = 0;
342                             cl->p_buffer_read -= 2;
343                             break;
344                     }
345
346                     if( i_end != 0 )
347                     {
348                         break;
349                     }
350                 }
351
352                 if( cl->p_buffer_read - cl->buffer_read == 999 ) // too long !
353                 {
354                     Write_message( cl , NULL, "Line too long\n" , cl->i_mode + 2 );
355                 }
356             }
357         }
358
359         /* and now we should bidouille the data we received / send */
360         for( i = 0 ; i < p_sys->i_clients ; i++ )
361         {
362             telnet_client_t *cl = p_sys->clients[i];
363
364             if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 ) // we have finished to send
365             {
366                cl->i_mode -= 2; // corresponding READ MODE
367             }
368             else if( cl->i_mode == READ_MODE_PWD && *cl->p_buffer_read == '\n' )
369             {
370                 *cl->p_buffer_read = '\0';
371                 if( strcmp( s_password, cl->buffer_read ) == 0 )
372                 {
373                     Write_message( cl , NULL, "\xff\xfc\x01\nWelcome, Master\n> ", WRITE_MODE_CMD );
374                 }
375                 else
376                 {
377                     /* wrong password */
378                     Write_message( cl , NULL, "\n\rTry again, you polio:\n" , WRITE_MODE_PWD );
379                 }
380             }
381             else if( cl->i_mode == READ_MODE_CMD && *cl->p_buffer_read == '\n' )
382             {
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 ) )
387                 {
388                     close( cl->fd );
389                     TAB_REMOVE( p_intf->p_sys->i_clients , p_intf->p_sys->clients , cl );
390                     free( cl );
391                 }
392                 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
393                 {
394                     msg_Err( p_intf, "shutdown requested" );
395                     p_intf->p_vlc->b_die = VLC_TRUE;
396                 }
397                 else
398                 {
399                     vlm_message_t *message;
400
401                     /* create a standard string */
402                     *cl->p_buffer_read = '\0';
403
404                     vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read , &message);
405
406                     Write_message( cl , message, NULL , WRITE_MODE_CMD );
407
408                     vlm_MessageDelete( message );
409                 }
410             }
411         }
412     }
413 }
414
415 static void Write_message( telnet_client_t * client, vlm_message_t * message, char * string_message, int i_mode )
416 {
417     char *psz_message;
418
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 );
422
423     /* generate the psz_message string */
424     if( message != NULL ) /* ok, look for vlm_message_t */
425     {
426         psz_message = MessageToString( message , 0 );
427         psz_message = realloc( psz_message , strlen( psz_message ) + strlen( "\n> " ) + 1 );
428         strcat( psz_message , "\n> " );
429     }
430     else /* it is a basic string_message */
431     {
432         psz_message = strdup( string_message );
433     }
434
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;
440     free( psz_message );
441 }
442
443 /* Does what we want except select and accept */
444 static int SocketListen( intf_thread_t *p_intf , int i_port )
445 {
446     struct sockaddr_in sock;
447     int fd;
448     int i_opt;
449     int i_flags;
450
451     /* open socket */
452     fd = socket( AF_INET, SOCK_STREAM, 0 );
453     if( fd < 0 )
454     {
455         msg_Err( p_intf, "cannot open socket" );
456         goto socket_failed;
457     }
458     /* reuse socket */
459     i_opt = 1;
460     if( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
461                     (void *) &i_opt, sizeof( i_opt ) ) < 0 )
462     {
463         msg_Warn( p_intf, "cannot configure socket (SO_REUSEADDR)" );
464     }
465
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;
471
472     /* bind it */
473     if( bind( fd, (struct sockaddr *)&sock, sizeof( struct sockaddr_in ) ) < 0 )
474     {
475         msg_Err( p_intf, "cannot bind socket" );
476         goto socket_failed;
477     }
478
479    /* set to non-blocking */
480 #if defined( WIN32 ) || defined( UNDER_CE )
481     {
482         unsigned long i_dummy = 1;
483         if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
484         {
485             msg_Err( p_intf, "cannot set socket to non-blocking mode" );
486             goto socket_failed;
487         }
488     }
489 #else
490     if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 )
491     {
492         msg_Err( p_intf, "cannot F_GETFL socket" );
493         goto socket_failed;
494     }
495     if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
496     {
497         msg_Err( p_intf, "cannot F_SETFL O_NONBLOCK" );
498         goto socket_failed;
499     }
500 #endif
501     /* listen */
502     if( listen( fd, LISTEN_BACKLOG ) < 0 )
503     {
504         msg_Err( p_intf, "cannot listen socket" );
505         goto socket_failed;
506     }
507
508     return fd;
509
510 socket_failed:
511     if( fd >= 0 )
512     {
513         SOCKET_CLOSE( fd );
514     }
515     return -1;
516 }
517
518 /* we need the level of the message to put a beautiful indentation.
519    first level is 0 */
520 static char* MessageToString( vlm_message_t* message , int i_level )
521 {
522     int i;
523     char *psz_message;
524
525     if( message == NULL )
526     {
527         return strdup( "" );
528     }
529     else if( i_level == 0 && message->i_child == 0 && message->psz_value == NULL  ) /* a command is successful */
530     {
531         /* don't write anything */
532         return strdup( "" );
533     }
534     else
535     {
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++ )
539         {
540             strcat( psz_message , " " );
541         }
542         strcat( psz_message , message->psz_name );
543         if( message->psz_value )
544         {
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 );
548         }
549
550         for( i = 0 ; i < message->i_child ; i++ )
551         {
552             char *child_message = MessageToString( message->child[i] , i_level + 1 );
553
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 );
558         }
559
560         return psz_message;
561     }
562 }