-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandroid_console.c
2134 lines (1780 loc) · 63.6 KB
/
android_console.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
*/
/*
* Android emulator control console
*
* this console is enabled automatically at emulator startup, on port 5554 by default,
* unless some other emulator is already running. See (android_emulation_start in android_sdl.c
* for details)
*
* you can telnet to the console, then use commands like 'help' or others to dynamically
* change emulator settings.
*
*/
#include "sockets.h"
#include "vl.h"
#include "android.h"
#include "sockets.h"
#include "hw/goldfish_device.h"
#include "hw/power_supply.h"
#include "shaper.h"
#include "modem_driver.h"
#include "android_gps.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "android_events.h"
#include "skin_keyboard.h"
#if defined(CONFIG_SLIRP)
#include "libslirp.h"
#endif
/* set to 1 to use the i/o and event functions
* defined in "telephony/sysdeps.h"
*/
#define USE_SYSDEPS 0
#include "sysdeps.h"
#define DEBUG 1
#if 1
# define D_ACTIVE VERBOSE_CHECK(console)
#else
# define D_ACTIVE DEBUG
#endif
#if DEBUG
# define D(x) do { if (D_ACTIVE) ( printf x , fflush(stdout) ); } while (0)
#else
# define D(x) do{}while(0)
#endif
extern int slirp_inited; /* in vl.c */
typedef struct ControlGlobalRec_* ControlGlobal;
typedef struct ControlClientRec_* ControlClient;
typedef struct {
int host_port;
int host_udp;
unsigned int guest_addr;
int guest_port;
} RedirRec, *Redir;
#if USE_SYSDEPS
typedef SysChannel Socket;
#else /* !USE_SYSDEPS */
typedef int Socket;
#endif /* !USE_SYSDEPS */
typedef struct ControlClientRec_
{
struct ControlClientRec_* next; /* next client in list */
Socket sock; /* socket used for communication */
ControlGlobal global;
char finished;
char buff[ 4096 ];
int buff_len;
} ControlClientRec;
typedef struct ControlGlobalRec_
{
/* listening socket */
Socket listen_fd;
/* the list of current clients */
ControlClient clients;
/* the list of redirections currently active */
Redir redirs;
int num_redirs;
int max_redirs;
} ControlGlobalRec;
static int
control_global_add_redir( ControlGlobal global,
int host_port,
int host_udp,
unsigned int guest_addr,
int guest_port )
{
Redir redir;
if (global->num_redirs >= global->max_redirs)
{
int old_max = global->max_redirs;
int new_max = old_max + (old_max >> 1) + 4;
Redir new_redirs = realloc( global->redirs, new_max*sizeof(global->redirs[0]) );
if (new_redirs == NULL)
return -1;
global->redirs = new_redirs;
global->max_redirs = new_max;
}
redir = &global->redirs[ global->num_redirs++ ];
redir->host_port = host_port;
redir->host_udp = host_udp;
redir->guest_addr = guest_addr;
redir->guest_port = guest_port;
return 0;
}
static int
control_global_del_redir( ControlGlobal global,
int host_port,
int host_udp )
{
int nn;
for (nn = 0; nn < global->num_redirs; nn++)
{
Redir redir = &global->redirs[nn];
if ( redir->host_port == host_port &&
redir->host_udp == host_udp )
{
memmove( redir, redir + 1, ((global->num_redirs - nn)-1)*sizeof(*redir) );
global->num_redirs -= 1;
return 0;
}
}
/* we didn't find it */
return -1;
}
static void
control_client_destroy( ControlClient client )
{
ControlGlobal global = client->global;
ControlClient *pnode = &global->clients;
D(( "destroying control client %p\n", client ));
#if USE_SYSDEPS
sys_channel_on( client->sock, 0, NULL, NULL );
#else
qemu_set_fd_handler( client->sock, NULL, NULL, NULL );
#endif
for ( ;; ) {
ControlClient node = *pnode;
if ( node == NULL )
break;
if ( node == client ) {
*pnode = node->next;
node->next = NULL;
break;
}
pnode = &node->next;
}
#if USE_SYSDEPS
sys_channel_close( client->sock );
client->sock = NULL;
#else
socket_close( client->sock );
client->sock = -1;
#endif
free( client );
}
static void control_client_read( void* _client ); /* forward */
static void control_control_write( ControlClient client, const char* buff, int len )
{
int ret;
if (len < 0)
len = strlen(buff);
while (len > 0) {
#if USE_SYSDEPS
ret = sys_channel_write( client->sock, buff, len );
#else
ret = send( client->sock, buff, len, 0);
#endif
if (ret < 0) {
if (socket_errno != EINTR && socket_errno != EWOULDBLOCK)
return;
} else {
buff += ret;
len -= ret;
}
}
}
static void control_write( ControlClient client, const char* format, ... )
{
static char temp[1024];
va_list args;
va_start(args, format);
vsnprintf( temp, sizeof(temp), format, args );
va_end(args);
temp[ sizeof(temp)-1 ] = 0;
control_control_write( client, temp, -1 );
}
static ControlClient
control_client_create( Socket socket,
ControlGlobal global )
{
ControlClient client = calloc( sizeof(*client), 1 );
if (client) {
#if !USE_SYSDEPS
socket_set_lowlatency( socket );
socket_set_nonblock( socket );
#endif
client->finished = 0;
client->global = global;
client->sock = socket;
client->next = global->clients;
global->clients = client;
#if USE_SYSDEPS
sys_channel_on( socket, SYS_EVENT_READ,
(SysChannelCallback) control_client_read,
client );
#else
qemu_set_fd_handler( socket, control_client_read, NULL, client );
#endif
}
return client;
}
typedef const struct CommandDefRec_ *CommandDef;
typedef struct CommandDefRec_ {
const char* names;
const char* abstract;
const char* description;
void (*descriptor)( ControlClient client );
int (*handler)( ControlClient client, char* args );
CommandDef subcommands; /* if handler is NULL */
} CommandDefRec;
static const CommandDefRec main_commands[]; /* forward */
static CommandDef
find_command( char* input, CommandDef commands, char* *pend, char* *pargs )
{
int nn;
char* args = strchr(input, ' ');
if (args != NULL) {
while (*args == ' ')
args++;
if (args[0] == 0)
args = NULL;
}
for (nn = 0; commands[nn].names != NULL; nn++)
{
const char* name = commands[nn].names;
const char* sep;
do {
int len, c;
sep = strchr( name, '|' );
if (sep)
len = sep - name;
else
len = strlen(name);
c = input[len];
if ( !memcmp( name, input, len ) && (c == ' ' || c == 0) ) {
*pend = input + len;
*pargs = args;
return &commands[nn];
}
if (sep)
name = sep + 1;
} while (sep != NULL && *name);
}
/* NOTE: don't touch *pend and *pargs if no command is found */
return NULL;
}
static void
dump_help( ControlClient client,
CommandDef cmd,
const char* prefix )
{
if (cmd->description) {
control_write( client, "%s", cmd->description );
} else if (cmd->descriptor) {
cmd->descriptor( client );
} else
control_write( client, "%s\r\n", cmd->abstract );
if (cmd->subcommands) {
cmd = cmd->subcommands;
control_write( client, "\r\navailable sub-commands:\r\n" );
for ( ; cmd->names != NULL; cmd++ ) {
control_write( client, " %s %-15s %s\r\n", prefix, cmd->names, cmd->abstract );
}
control_write( client, "\r\n" );
}
}
static void
control_client_do_command( ControlClient client )
{
char* line = client->buff;
char* args = NULL;
CommandDef commands = main_commands;
char* cmdend = client->buff;
CommandDef cmd = find_command( line, commands, &cmdend, &args );
if (cmd == NULL) {
control_write( client, "KO: unknown command, try 'help'\r\n" );
return;
}
for (;;) {
CommandDef subcmd;
if (cmd->handler) {
if ( !cmd->handler( client, args ) )
control_write( client, "OK\r\n" );
break;
}
/* no handler means we should have sub-commands */
if (cmd->subcommands == NULL) {
control_write( client, "KO: internal error: buggy command table for '%.*s'\r\n",
cmdend - client->buff, client->buff );
break;
}
/* we need a sub-command here */
if ( !args ) {
dump_help( client, cmd, "" );
control_write( client, "KO: missing sub-command\r\n" );
break;
}
line = args;
commands = cmd->subcommands;
subcmd = find_command( line, commands, &cmdend, &args );
if (subcmd == NULL) {
dump_help( client, cmd, "" );
control_write( client, "KO: bad sub-command\r\n" );
break;
}
cmd = subcmd;
}
}
/* implement the 'help' command */
static int
do_help( ControlClient client, char* args )
{
char* line;
char* start = args;
char* end = start;
CommandDef cmd = main_commands;
/* without arguments, simply dump all commands */
if (args == NULL) {
control_write( client, "Android console command help:\r\n\r\n" );
for ( ; cmd->names != NULL; cmd++ ) {
control_write( client, " %-15s %s\r\n", cmd->names, cmd->abstract );
}
control_write( client, "\r\ntry 'help <command>' for command-specific help\r\n" );
return 0;
}
/* with an argument, find the corresponding command */
for (;;) {
CommandDef subcmd;
line = args;
subcmd = find_command( line, cmd, &end, &args );
if (subcmd == NULL) {
control_write( client, "try one of these instead:\r\n\r\n" );
for ( ; cmd->names != NULL; cmd++ ) {
control_write( client, " %.*s %s\r\n",
end - start, start, cmd->names );
}
control_write( client, "\r\nKO: unknown command\r\n" );
return -1;
}
if ( !args || !subcmd->subcommands ) {
dump_help( client, subcmd, start );
return 0;
}
cmd = subcmd->subcommands;
}
}
static void
control_client_read_byte( ControlClient client, unsigned char ch )
{
if (ch == '\r')
{
/* filter them out */
}
else if (ch == '\n')
{
client->buff[ client->buff_len ] = 0;
control_client_do_command( client );
if (client->finished)
return;
client->buff_len = 0;
}
else
{
if (client->buff_len >= sizeof(client->buff)-1)
client->buff_len = 0;
client->buff[ client->buff_len++ ] = ch;
}
}
static void
control_client_read( void* _client )
{
ControlClient client = _client;
unsigned char buf[4096];
int size;
D(( "in control_client read: " ));
#if USE_SYSDEPS
size = sys_channel_read( client->sock, buf, sizeof(buf) );
#else
size = recv( client->sock, buf, sizeof(buf), 0 );
#endif
if (size < 0) {
D(( "size < 0, exiting with %d: %s\n", socket_errno, socket_errstr() ));
if (socket_errno != EWOULDBLOCK && socket_errno != EINTR)
control_client_destroy( client );
return;
}
if (size == 0) {
/* end of connection */
D(( "end of connection detected !!\n" ));
control_client_destroy( client );
}
else {
int nn;
#ifdef _WIN32
# if DEBUG
char temp[16];
int count = size > sizeof(temp)-1 ? sizeof(temp)-1 : size;
for (nn = 0; nn < count; nn++) {
int c = buf[nn];
if (c == '\n')
temp[nn] = '!';
else if (c < 32)
temp[nn] = '.';
else
temp[nn] = (char)c;
}
temp[nn] = 0;
D(( "received %d bytes: %s\n", size, temp ));
# endif
#else
D(( "received %.*s\n", size, buf ));
#endif
for (nn = 0; nn < size; nn++) {
control_client_read_byte( client, buf[nn] );
if (client->finished) {
control_client_destroy(client);
return;
}
}
}
}
/* this function is called on each new client connection */
static void
control_global_accept( void* _global )
{
ControlGlobal global = _global;
ControlClient client;
Socket fd;
D(( "control_global_accept: just in (fd=%p)\n", (void*)global->listen_fd ));
#if USE_SYSDEPS
fd = sys_channel_create_tcp_handler( global->listen_fd );
if (fd == NULL) {
perror("accept");
return;
}
#else
for(;;) {
struct sockaddr_in sockaddr;
socklen_t len = sizeof(sockaddr);
fd = accept( global->listen_fd, (struct sockaddr *)&sockaddr, &len);
if (fd < 0 && socket_errno != EINTR) {
D(( "problem in accept: %d: %s\n", socket_errno, socket_errstr() ));
perror("accept");
return;
} else if (fd >= 0) {
break;
}
D(( "relooping in accept()\n" ));
}
socket_set_xreuseaddr( fd );
#endif
D(( "control_global_accept: creating new client\n" ));
client = control_client_create( fd, global );
if (client) {
D(( "control_global_accept: new client %p\n", client ));
control_write( client, "Android Console: type 'help' for a list of commands\r\n" );
control_write( client, "OK\r\n" );
}
}
static int
control_global_init( ControlGlobal global,
int control_port )
{
Socket fd;
#if !USE_SYSDEPS
int ret;
struct sockaddr_in sockaddr;
#endif
memset( global, 0, sizeof(*global) );
sys_main_init();
#if USE_SYSDEPS
fd = sys_channel_create_tcp_server( control_port );
if (fd == NULL) {
return -1;
}
D(("global fd=%p\n", fd));
global->listen_fd = fd;
sys_channel_on( fd, SYS_EVENT_READ,
(SysChannelCallback) control_global_accept,
global );
#else
fd = socket(PF_INET, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
return -1;
}
socket_set_xreuseaddr( fd );
memset( &sockaddr, 0, sizeof(sockaddr) );
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(control_port);
sockaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
if (ret < 0) {
perror("bind");
close( fd );
return -1;
}
ret = listen(fd, 0);
if (ret < 0) {
perror("listen");
close( fd );
return -1;
}
socket_set_nonblock(fd);
global->listen_fd = fd;
qemu_set_fd_handler( fd, control_global_accept, NULL, global );
#endif
return 0;
}
static int
do_quit( ControlClient client, char* args )
{
client->finished = 1;
return -1;
}
/********************************************************************************************/
/********************************************************************************************/
/***** ******/
/***** N E T W O R K S E T T I N G S ******/
/***** ******/
/********************************************************************************************/
/********************************************************************************************/
static int
do_network_status( ControlClient client, char* args )
{
control_write( client, "Current network status:\r\n" );
control_write( client, " download speed: %8d bits/s (%.1f KB/s)\r\n",
(long)qemu_net_download_speed, qemu_net_download_speed/8192. );
control_write( client, " upload speed: %8d bits/s (%.1f KB/s)\r\n",
(long)qemu_net_upload_speed, qemu_net_upload_speed/8192. );
control_write( client, " minimum latency: %ld ms\r\n", qemu_net_min_latency );
control_write( client, " maximum latency: %ld ms\r\n", qemu_net_max_latency );
return 0;
}
static void
dump_network_speeds( ControlClient client )
{
const NetworkSpeed* speed = android_netspeeds;
const char* const format = " %-8s %s\r\n";
for ( ; speed->name; speed++ ) {
control_write( client, format, speed->name, speed->display );
}
control_write( client, format, "<num>", "selects both upload and download speed" );
control_write( client, format, "<up>:<down>", "select individual upload/download speeds" );
}
static int
do_network_speed( ControlClient client, char* args )
{
if ( !args ) {
control_write( client, "KO: missing <speed> argument, see 'help network speed'\r\n" );
return -1;
}
if ( android_parse_network_speed( args ) < 0 ) {
control_write( client, "KO: invalid <speed> argument, see 'help network speed' for valid values\r\n" );
return -1;
}
netshaper_set_rate( slirp_shaper_in, qemu_net_download_speed );
netshaper_set_rate( slirp_shaper_out, qemu_net_upload_speed );
if (android_modem) {
amodem_set_data_network_type( android_modem,
android_parse_network_type( args ) );
}
return 0;
}
static void
describe_network_speed( ControlClient client )
{
control_write( client,
"'network speed <speed>' allows you to dynamically change the speed of the emulated\r\n"
"network on the device, where <speed> is one of the following:\r\n\r\n" );
dump_network_speeds( client );
}
static int
do_network_delay( ControlClient client, char* args )
{
if ( !args ) {
control_write( client, "KO: missing <delay> argument, see 'help network delay'\r\n" );
return -1;
}
if ( android_parse_network_latency( args ) < 0 ) {
control_write( client, "KO: invalid <delay> argument, see 'help network delay' for valid values\r\n" );
return -1;
}
netdelay_set_latency( slirp_delay_in, qemu_net_min_latency, qemu_net_max_latency );
return 0;
}
static void
describe_network_delay( ControlClient client )
{
control_write( client,
"'network delay <latency>' allows you to dynamically change the latency of the emulated\r\n"
"network on the device, where <latency> is one of the following:\r\n\r\n" );
/* XXX: TODO */
}
static const CommandDefRec network_commands[] =
{
{ "status", "dump network status", NULL, NULL,
do_network_status, NULL },
{ "speed", "change network speed", NULL, describe_network_speed,
do_network_speed, NULL },
{ "delay", "change network latency", NULL, describe_network_delay,
do_network_delay, NULL },
{ NULL, NULL, NULL, NULL, NULL, NULL }
};
/********************************************************************************************/
/********************************************************************************************/
/***** ******/
/***** P O R T R E D I R E C T I O N S ******/
/***** ******/
/********************************************************************************************/
/********************************************************************************************/
static int
do_redir_list( ControlClient client, char* args )
{
ControlGlobal global = client->global;
if (global->num_redirs == 0)
control_write( client, "no active redirections\r\n" );
else {
int nn;
for (nn = 0; nn < global->num_redirs; nn++) {
Redir redir = &global->redirs[nn];
control_write( client, "%s:%-5d => %-5d\r\n",
redir->host_udp ? "udp" : "tcp",
redir->host_port,
redir->guest_port );
}
}
return 0;
}
/* parse a protocol:port specification */
static int
redir_parse_proto_port( char* args, int *pport, int *pproto )
{
int proto = -1;
int len = 0;
char* end;
if ( !memcmp( args, "tcp:", 4 ) ) {
proto = 0;
len = 4;
}
else if ( !memcmp( args, "udp:", 4 ) ) {
proto = 1;
len = 4;
}
else
return 0;
args += len;
*pproto = proto;
*pport = strtol( args, &end, 10 );
if (end == args)
return 0;
len += end - args;
return len;
}
static int
redir_parse_guest_port( char* arg, int *pport )
{
char* end;
*pport = strtoul( arg, &end, 10 );
if (end == arg)
return 0;
return end - arg;
}
static Redir
redir_find( ControlGlobal global, int port, int isudp )
{
int nn;
for (nn = 0; nn < global->num_redirs; nn++) {
Redir redir = &global->redirs[nn];
if (redir->host_port == port && redir->host_udp == isudp)
return redir;
}
return NULL;
}
static int
do_redir_add( ControlClient client, char* args )
{
int len, host_proto, host_port, guest_port;
struct in_addr guest_addr;
Redir redir;
if ( !args )
goto BadFormat;
if (!slirp_inited) {
slirp_inited = 1;
slirp_init();
}
len = redir_parse_proto_port( args, &host_port, &host_proto );
if (len == 0 || args[len] != ':')
goto BadFormat;
args += len + 1;
len = redir_parse_guest_port( args, &guest_port );
if (len == 0 || args[len] != 0)
goto BadFormat;
redir = redir_find( client->global, host_port, host_proto );
if ( redir != NULL ) {
control_write( client, "KO: host port already active, use 'redir del' to remove first\r\n" );
return -1;
}
if (!inet_aton("10.0.2.15", &guest_addr)) {
control_write( client, "KO: unexpected internal failure when resolving 10.0.2.15\r\n" );
return -1;
}
D(("pattern hport=%d gport=%d proto=%d\n", host_port, guest_port, host_proto ));
if ( control_global_add_redir( client->global, host_port, host_proto,
guest_addr.s_addr, guest_port ) < 0 )
{
control_write( client, "KO: not enough memory to allocate redirection\r\n" );
return -1;
}
if (slirp_redir(host_proto, host_port, guest_addr, guest_port) < 0) {
control_write( client, "KO: can't setup redirection, port probably used by another program on host\r\n" );
control_global_del_redir( client->global, host_port, host_proto );
return -1;
}
return 0;
BadFormat:
control_write( client, "KO: bad redirection format, try (tcp|udp):hostport:guestport\r\n", -1 );
return -1;
}
static int
do_redir_del( ControlClient client, char* args )
{
int len, proto, port;
Redir redir;
if ( !args )
goto BadFormat;
len = redir_parse_proto_port( args, &port, &proto );
if ( len == 0 || args[len] != 0 )
goto BadFormat;
redir = redir_find( client->global, port, proto );
if (redir == NULL) {
control_write( client, "KO: can't remove unknown redirection (%s:%d)\r\n",
proto ? "udp" : "tcp", port );
return -1;
}
slirp_unredir( redir->host_udp, redir->host_port );
control_global_del_redir( client->global, port, proto );\
return 0;
BadFormat:
control_write( client, "KO: bad redirection format, try (tcp|udp):hostport\r\n" );
return -1;
}
static const CommandDefRec redir_commands[] =
{
{ "list", "list current redirections",
"list current port redirections. use 'redir add' and 'redir del' to add and remove them\r\n", NULL,
do_redir_list, NULL },
{ "add", "add new redirection",
"add a new port redirection, arguments must be:\r\n\r\n"
" redir add <protocol>:<host-port>:<guest-port>\r\n\r\n"
"where: <protocol> is either 'tcp' or 'udp'\r\n"
" <host-port> a number indicating which port on the host to open\r\n"
" <guest-port> a number indicating which port to route to on the device\r\n"
"\r\nas an example, 'redir tcp:5000:6000' will allow any packets sent to\r\n"
"the host's TCP port 5000 to be routed to TCP port 6000 of the emulated device\r\n", NULL,
do_redir_add, NULL },
{ "del", "remove existing redirection",
"remove a port redirecion that was created with 'redir add', arguments must be:\r\n\r\n"
" redir del <protocol>:<host-port>\r\n\r\n"
"see the 'help redir add' for the meaning of <protocol> and <host-port>\r\n", NULL,
do_redir_del, NULL },
{ NULL, NULL, NULL, NULL, NULL, NULL }
};
/********************************************************************************************/
/********************************************************************************************/
/***** ******/
/***** G S M M O D E M ******/
/***** ******/
/********************************************************************************************/
/********************************************************************************************/
static const struct {
const char* name;
const char* display;
ARegistrationState state;
} _gsm_states[] = {
{ "unregistered", "no network available", A_REGISTRATION_UNREGISTERED },
{ "home", "on local network, non-roaming", A_REGISTRATION_HOME },
{ "roaming", "on roaming network", A_REGISTRATION_ROAMING },
{ "searching", "searching networks", A_REGISTRATION_SEARCHING },
{ "denied", "emergency calls only", A_REGISTRATION_DENIED },
{ "off", "same as 'unregistered'", A_REGISTRATION_UNREGISTERED },
{ "on", "same as 'home'", A_REGISTRATION_HOME },
{ NULL, NULL, A_REGISTRATION_UNREGISTERED }
};
static const char*
gsm_state_to_string( ARegistrationState state )
{
int nn;
for (nn = 0; _gsm_states[nn].name != NULL; nn++) {
if (state == _gsm_states[nn].state)
return _gsm_states[nn].name;
}
return "<unknown>";
}
static int
do_gsm_status( ControlClient client, char* args )
{
if (args) {
control_write( client, "KO: no argument required\r\n" );
return -1;
}
if (!android_modem) {
control_write( client, "KO: modem emulation not running\r\n" );
return -1;
}
control_write( client, "gsm voice state: %s\r\n",
gsm_state_to_string(
amodem_get_voice_registration(android_modem) ) );