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
81 while ((opt = getopt(argc, argv, "d:I:p:va:")) != -1)
82 {
83 switch (opt)
84 {
85 case 'a':
86 httpListenAddr = optarg;
87 break;
88 case 'd':
89 logLevel = static_cast<otbrLogLevel>(atoi(optarg));
90 break;
91 case 'I':
92 interfaceName = optarg;
93 break;
94
95 case 'p':
96 httpPort = optarg;
97 VerifyOrExit(httpPort != nullptr);
98 port = atoi(httpPort);
99 break;
100
101 case 'v':
102 PrintVersion();
103 ExitNow();
104 break;
105
106 default:
107 fprintf(stderr, "Usage: %s [-d DEBUG_LEVEL] [-I interfaceName] [-p port] [-a listenAddress] [-v]\n",
108 argv[0]);
109 ExitNow(ret = -1);
110 break;
111 }
112 }
113
114 otbrLogInit(argv[0], logLevel, true, false);
115 otbrLogInfo("Running %s", OTBR_PACKAGE_VERSION);
116
117 if (interfaceName == nullptr)
118 {
119 interfaceName = kDefaultInterfaceName;
120 printf("interfaceName not specified, using default %s\n", interfaceName);
121 }
122
123 if (httpListenAddr == nullptr)
124 {
125 httpListenAddr = kDefaultListenAddr;
126 printf("listenAddr not specified, using default %s\n", httpListenAddr);
127 }
128
129 if (httpPort == nullptr)
130 {
131 printf("http port not specified, using default %d\n", port);
132 }
133
134 otbrLogInfo("Border router web started on %s", interfaceName);
135
136 // allow quitting elegantly
137 signal(SIGTERM, HandleSignal);
138 signal(SIGINT, HandleSignal);
139
140 sServer.reset(new otbr::Web::WebServer());
141 sServer->StartWebServer(interfaceName, httpListenAddr, port);
142
143 otbrLogDeinit();
144
145 exit:
146 return ret;
147 }
148