blob: e1e8ac1e4897834d57585993fff546d7a488e1e1 [file] [log] [blame]
Steve Kondik95027ea2017-06-14 17:22:58 -07001//
2// vncflinger - Copyright (C) 2017 Steve Kondik
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//
Steve Kondik5c8655c2017-06-19 00:28:47 -070017#include <getopt.h>
Steve Kondik95027ea2017-06-14 17:22:58 -070018
Steve Kondik46798992017-06-15 23:58:54 -070019#include <csignal>
Steve Kondik5c8655c2017-06-19 00:28:47 -070020#include <iostream>
Steve Kondik46798992017-06-15 23:58:54 -070021
Steve Kondik3db07472017-06-19 22:13:45 -070022#include <binder/IServiceManager.h>
23
Steve Kondik55db0532017-06-12 11:27:18 -070024#include "VNCFlinger.h"
Steve Kondik3db07472017-06-19 22:13:45 -070025#include "VNCService.h"
Steve Kondik55db0532017-06-12 11:27:18 -070026
27using namespace android;
28
Steve Kondik5c8655c2017-06-19 00:28:47 -070029static sp<VNCFlinger> gVNC;
30
31static const char* const shortOpts = "4:6:p:s:vh";
32static const option longOpts[] = {
33 {"listen", 1, nullptr, '4'},
34 {"listen6", 1, nullptr, '6'},
35 {"port", 1, nullptr, 'p'},
36 {"password", 1, nullptr, 's'},
37 {"version", 0, nullptr, 'v'},
38 {"help", 0, nullptr, 'h'},
39};
Steve Kondik46798992017-06-15 23:58:54 -070040
41static void onSignal(int /* signal */) {
Steve Kondik5c8655c2017-06-19 00:28:47 -070042 gVNC->stop();
43}
44
45static void printVersion() {
46 std::cout << "VNCFlinger-" << VNCFLINGER_VERSION << std::endl;
47 exit(0);
48}
49
50static void printHelp() {
51 std::cout << "Usage: vncflinger [OPTIONS]\n\n"
52 << " -4 <addr> IPv4 address to listen (default: localhost)\n"
53 << " -6 <addr> IPv6 address to listen (default: localhost)\n"
54 << " -p <num> Port to listen on (default: 5900)\n"
55 << " -s <pass> Store server password\n"
56 << " -c Clear server password\n"
57 << " -v Show server version\n"
58 << " -h Show help\n\n";
59 exit(1);
60}
61
62static void parseArgs(int argc, char** argv) {
63 String8 arg;
64
65 while (true) {
66 const auto opt = getopt_long(argc, argv, shortOpts, longOpts, nullptr);
67
68 if (opt < 0) {
69 break;
70 }
71
72 switch (opt) {
73 case 's':
74 arg = optarg;
75 if (gVNC->setPassword(arg) != OK) {
76 std::cerr << "Failed to set password\n";
77 exit(1);
78 }
79 exit(0);
80
81 case 'c':
82 if (gVNC->clearPassword() != OK) {
83 std::cerr << "Failed to clear password\n";
84 exit(1);
85 }
86 exit(0);
87
88 case '4':
89 arg = optarg;
Steve Kondik3db07472017-06-19 22:13:45 -070090 if (gVNC->setV4Address(arg) != OK) {
Steve Kondik5c8655c2017-06-19 00:28:47 -070091 std::cerr << "Failed to set IPv4 address\n";
92 exit(1);
93 }
94 break;
95
96 case '6':
97 arg = optarg;
Steve Kondik3db07472017-06-19 22:13:45 -070098 if (gVNC->setV6Address(arg) != OK) {
Steve Kondik5c8655c2017-06-19 00:28:47 -070099 std::cerr << "Failed to set IPv6 address\n";
100 exit(1);
101 }
102 break;
103
104 case 'p':
Steve Kondik5c8655c2017-06-19 00:28:47 -0700105 if (gVNC->setPort(std::stoi(optarg)) != OK) {
106 std::cerr << "Failed to set port\n";
107 exit(1);
108 }
109 break;
110
111 case 'v':
112 printVersion();
113 break;
114
115 case 'h':
116 case '?':
117 default:
118 printHelp();
119 break;
120 }
121 }
Steve Kondik46798992017-06-15 23:58:54 -0700122}
123
Steve Kondik2c9d0cf2017-06-15 23:39:29 -0700124int main(int argc, char** argv) {
Steve Kondik46798992017-06-15 23:58:54 -0700125 std::signal(SIGINT, onSignal);
126 std::signal(SIGHUP, onSignal);
127
Steve Kondik5c8655c2017-06-19 00:28:47 -0700128 gVNC = new VNCFlinger();
129
130 parseArgs(argc, argv);
131
Steve Kondik3db07472017-06-19 22:13:45 -0700132 // binder interface
133 defaultServiceManager()->addService(String16("vnc"), new VNCService(gVNC));
134
Steve Kondik5c8655c2017-06-19 00:28:47 -0700135 gVNC->start();
Steve Kondik55db0532017-06-12 11:27:18 -0700136}