blob: 8ef24fbe068ca398d67bfe9846cefd3b52d69212 [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 Kondik55db0532017-06-12 11:27:18 -070022#include "VNCFlinger.h"
23
24using namespace android;
25
Steve Kondik5c8655c2017-06-19 00:28:47 -070026static sp<VNCFlinger> gVNC;
27
28static const char* const shortOpts = "4:6:p:s:vh";
29static const option longOpts[] = {
30 {"listen", 1, nullptr, '4'},
31 {"listen6", 1, nullptr, '6'},
32 {"port", 1, nullptr, 'p'},
33 {"password", 1, nullptr, 's'},
34 {"version", 0, nullptr, 'v'},
35 {"help", 0, nullptr, 'h'},
36};
Steve Kondik46798992017-06-15 23:58:54 -070037
38static void onSignal(int /* signal */) {
Steve Kondik5c8655c2017-06-19 00:28:47 -070039 gVNC->stop();
40}
41
42static void printVersion() {
43 std::cout << "VNCFlinger-" << VNCFLINGER_VERSION << std::endl;
44 exit(0);
45}
46
47static void printHelp() {
48 std::cout << "Usage: vncflinger [OPTIONS]\n\n"
49 << " -4 <addr> IPv4 address to listen (default: localhost)\n"
50 << " -6 <addr> IPv6 address to listen (default: localhost)\n"
51 << " -p <num> Port to listen on (default: 5900)\n"
52 << " -s <pass> Store server password\n"
53 << " -c Clear server password\n"
54 << " -v Show server version\n"
55 << " -h Show help\n\n";
56 exit(1);
57}
58
59static void parseArgs(int argc, char** argv) {
60 String8 arg;
61
62 while (true) {
63 const auto opt = getopt_long(argc, argv, shortOpts, longOpts, nullptr);
64
65 if (opt < 0) {
66 break;
67 }
68
69 switch (opt) {
70 case 's':
71 arg = optarg;
72 if (gVNC->setPassword(arg) != OK) {
73 std::cerr << "Failed to set password\n";
74 exit(1);
75 }
76 exit(0);
77
78 case 'c':
79 if (gVNC->clearPassword() != OK) {
80 std::cerr << "Failed to clear password\n";
81 exit(1);
82 }
83 exit(0);
84
85 case '4':
86 arg = optarg;
87 if (gVNC->setListenAddress(arg, false) != OK) {
88 std::cerr << "Failed to set IPv4 address\n";
89 exit(1);
90 }
91 break;
92
93 case '6':
94 arg = optarg;
95 if (gVNC->setListenAddress(arg, true) != OK) {
96 std::cerr << "Failed to set IPv6 address\n";
97 exit(1);
98 }
99 break;
100
101 case 'p':
102 std::cerr << "port=" << optarg << std::endl;
103 if (gVNC->setPort(std::stoi(optarg)) != OK) {
104 std::cerr << "Failed to set port\n";
105 exit(1);
106 }
107 break;
108
109 case 'v':
110 printVersion();
111 break;
112
113 case 'h':
114 case '?':
115 default:
116 printHelp();
117 break;
118 }
119 }
Steve Kondik46798992017-06-15 23:58:54 -0700120}
121
Steve Kondik2c9d0cf2017-06-15 23:39:29 -0700122int main(int argc, char** argv) {
Steve Kondik46798992017-06-15 23:58:54 -0700123 std::signal(SIGINT, onSignal);
124 std::signal(SIGHUP, onSignal);
125
Steve Kondik5c8655c2017-06-19 00:28:47 -0700126 gVNC = new VNCFlinger();
127
128 parseArgs(argc, argv);
129
130 gVNC->start();
Steve Kondik55db0532017-06-12 11:27:18 -0700131}