1 /*
2 * Copyright (c) 2017, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file
31 * This file is the entry of the program, it starts a Web service.
32 */
33 #define OT_HTTP_PORT 80
34 #define OTBR_LOG_TAG "WEB"
35
36 #include "openthread-br/config.h"
37
38 #include <errno.h>
39 #include <signal.h>
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44
45 #include "common/code_utils.hpp"
46 #include "common/logging.hpp"
47 #include "web/web-service/web_server.hpp"
48
49 static const char kDefaultInterfaceName[] = "wpan0";
50 static const char kDefaultListenAddr[] = "::";
51
52 std::unique_ptr<otbr::Web::WebServer> sServer(nullptr);
53
HandleSignal(int aSignal)54 static void HandleSignal(int aSignal)
55 {
56 signal(aSignal, SIG_DFL);
57
58 otbrLogCrit("Stopping web server");
59
60 if (sServer != nullptr)
61 {
62 sServer->StopWebServer();
63 }
64 }
65
PrintVersion(void)66 static void PrintVersion(void)
67 {
68 printf("%s\n", OTBR_PACKAGE_VERSION);
69 }
70
main(int argc,char ** argv)71 int main(int argc, char **argv)
72 {
73 const char *interfaceName = nullptr;
74 const char *httpListenAddr = nullptr;
75 const char *httpPort = nullptr;
76 otbrLogLevel logLevel = OTBR_LOG_INFO;
77 int ret = 0;
78 int opt;
79 uint16_t port = OT_HTTP_PORT;
80 bool syslogDisable = false;
81
82 while ((opt = getopt(argc, argv, "d:I:p:va:s")) != -1)
83 {
84 switch (opt)
85 {
86 case 'a':
87 httpListenAddr = optarg;
88 break;
89 case 'd':
90 logLevel = static_cast<otbrLogLevel>(atoi(optarg));
91 break;
92 case 'I':
93 interfaceName = optarg;
94 break;
95
96 case 'p':
97 httpPort = optarg;
98 VerifyOrExit(httpPort != nullptr);
99 port = atoi(httpPort);
100 break;
101
102 case 'v':
103 PrintVersion();
104 ExitNow();
105 break;
106
107 case 's':
108 syslogDisable = true;
109 break;
110
111 default:
112 fprintf(stderr, "Usage: %s [-d DEBUG_LEVEL] [-I interfaceName] [-p port] [-a listenAddress] [-v]\n",
113 argv[0]);
114 ExitNow(ret = -1);
115 break;
116 }
117 }
118
119 otbrLogInit(argv[0], logLevel, true, syslogDisable);
120 otbrLogInfo("Running %s", OTBR_PACKAGE_VERSION);
121
122 if (interfaceName == nullptr)
123 {
124 interfaceName = kDefaultInterfaceName;
125 printf("interfaceName not specified, using default %s\n", interfaceName);
126 }
127
128 if (httpListenAddr == nullptr)
129 {
130 httpListenAddr = kDefaultListenAddr;
131 printf("listenAddr not specified, using default %s\n", httpListenAddr);
132 }
133
134 if (httpPort == nullptr)
135 {
136 printf("http port not specified, using default %d\n", port);
137 }
138
139 otbrLogInfo("Border router web started on %s", interfaceName);
140
141 // allow quitting elegantly
142 signal(SIGTERM, HandleSignal);
143 signal(SIGINT, HandleSignal);
144
145 sServer.reset(new otbr::Web::WebServer());
146 sServer->StartWebServer(interfaceName, httpListenAddr, port);
147
148 otbrLogDeinit();
149
150 exit:
151 return ret;
152 }
153