1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "daemon_tcp.h"
16 #include <cstdlib>
17 #include "arpa/inet.h"
18 #include "netinet/in.h"
19 #include "new"
20 #include "sys/socket.h"
21 #include "system_depend.h"
22 #include "unistd.h"
23 #include "common.h"
24 #include "session.h"
25 
26 namespace Hdc {
HdcDaemonTCP(const bool serverOrDaemonIn, void *ptrMainBase)27 HdcDaemonTCP::HdcDaemonTCP(const bool serverOrDaemonIn, void *ptrMainBase)
28     : HdcTCPBase(serverOrDaemonIn, ptrMainBase)
29 {
30     // If the listening value for the property setting is obtained, it will be 0 randomly assigned.
31     string strTCPPort;
32     SystemDepend::GetDevItem("persist.hdc.port", strTCPPort);
33     tcpListenPort = atoi(strTCPPort.c_str());
34     if (tcpListenPort <= 0) {
35         WRITE_LOG(LOG_WARN, "persist.hdc.port is invalid, set default to 0");
36         tcpListenPort = 0;
37     }
38 }
39 
~HdcDaemonTCP()40 HdcDaemonTCP::~HdcDaemonTCP()
41 {
42 }
43 
Stop()44 void HdcDaemonTCP::Stop()
45 {
46     Base::TryCloseHandle((const uv_handle_t *)&servUDP);
47     Base::TryCloseHandle((const uv_handle_t *)&servTCP);
48     WRITE_LOG(LOG_DEBUG, "Stop tcpListenPort:%u", tcpListenPort);
49 }
50 
TransmitConfig(const sockaddr *addrSrc, uv_udp_t *handle)51 void HdcDaemonTCP::TransmitConfig(const sockaddr *addrSrc, uv_udp_t *handle)
52 {
53     char srcIP[BUF_SIZE_TINY] = "";
54     struct sockaddr addrSrcIPPort;
55     uv_udp_send_t *req = new uv_udp_send_t();
56     if (!req) {
57         return;
58     }
59     string sendBuf = Base::StringFormat("%s-%d", HANDSHAKE_MESSAGE.c_str(), tcpListenPort);
60     uv_buf_t sndbuf = uv_buf_init((char *)sendBuf.c_str(), sendBuf.size());
61     uv_ip4_name(const_cast<sockaddr_in *>(reinterpret_cast<const sockaddr_in *>(addrSrc)), srcIP, sizeof(srcIP));
62     uv_ip4_addr(srcIP, DEFAULT_PORT, const_cast<sockaddr_in *>(reinterpret_cast<const sockaddr_in *>(&addrSrcIPPort)));
63     uv_udp_send(req, handle, &sndbuf, 1, &addrSrcIPPort, SendUDPFinish);
64 }
65 
AcceptClient(uv_stream_t *server, int status)66 void HdcDaemonTCP::AcceptClient(uv_stream_t *server, int status)
67 {
68     uv_loop_t *ptrLoop = server->loop;
69     uv_tcp_t *pServTCP = (uv_tcp_t *)server;
70     HdcDaemonTCP *thisClass = (HdcDaemonTCP *)pServTCP->data;
71     HdcSessionBase *ptrConnect = reinterpret_cast<HdcSessionBase *>(thisClass->clsMainBase);
72     HdcSessionBase *daemon = reinterpret_cast<HdcSessionBase *>(thisClass->clsMainBase);
73     const uint16_t maxWaitTime = UV_DEFAULT_INTERVAL;
74     auto ctrl = daemon->BuildCtrlString(SP_START_SESSION, 0, nullptr, 0);
75     HSession hSession = ptrConnect->MallocSession(false, CONN_TCP, thisClass);
76     if (!hSession) {
77         WRITE_LOG(LOG_FATAL, "malloc tcp session failed");
78         return;
79     }
80     if (uv_accept(server, (uv_stream_t *)&hSession->hWorkTCP) < 0) {
81         WRITE_LOG(LOG_FATAL, "uv_accept error sessionId:%u", hSession->sessionId);
82         goto Finish;
83     }
84     if ((hSession->fdChildWorkTCP = Base::DuplicateUvSocket(&hSession->hWorkTCP)) < 0) {
85         WRITE_LOG(LOG_FATAL, "AcceptClient error fdChildWorkTCP:%d,errno:%d", hSession->fdChildWorkTCP, errno);
86         goto Finish;
87     }
88     Base::TryCloseHandle((uv_handle_t *)&hSession->hWorkTCP);
89     Base::StartWorkThread(ptrLoop, ptrConnect->SessionWorkThread, Base::FinishWorkThread, hSession);
90     // wait for thread up
91     while (hSession->childLoop.active_handles == 0) {
92         usleep(maxWaitTime);
93     }
94     Base::SendToPollFd(hSession->ctrlFd[STREAM_MAIN], ctrl.data(), ctrl.size());
95     return;
96 Finish:
97     ptrConnect->FreeSession(hSession->sessionId);
98 }
99 
RecvUDPEntry(const sockaddr *addrSrc, uv_udp_t *handle, const uv_buf_t *rcvbuf)100 void HdcDaemonTCP::RecvUDPEntry(const sockaddr *addrSrc, uv_udp_t *handle, const uv_buf_t *rcvbuf)
101 {
102     TransmitConfig(addrSrc, handle);
103 }
104 
SetUDPListen()105 void HdcDaemonTCP::SetUDPListen()
106 {
107     struct sockaddr_in addr;
108     HdcSessionBase *ptrConnect = (HdcSessionBase *)clsMainBase;
109     // udp broadcast
110     servUDP.data = this;
111     uv_udp_init(&ptrConnect->loopMain, &servUDP);
112     uv_ip4_addr("0.0.0.0", DEFAULT_PORT, &addr);
113     uv_udp_bind(&servUDP, (const struct sockaddr *)&addr, UV_UDP_REUSEADDR);
114     uv_udp_recv_start(&servUDP, AllocStreamUDP, RecvUDP);
115 }
116 
117 // Set the daemon-side TCP listening
SetTCPListen()118 int HdcDaemonTCP::SetTCPListen()
119 {
120     // tcp listen
121     HdcSessionBase *ptrConnect = (HdcSessionBase *)clsMainBase;
122     servTCP.data = this;
123     struct sockaddr_in addr = {};
124     int namelen;
125     const int DEFAULT_BACKLOG = 128;
126 
127     uv_tcp_init(&ptrConnect->loopMain, &servTCP);
128     uv_ip4_addr("0.0.0.0", tcpListenPort, &addr);  // tcpListenPort == 0
129     uv_tcp_bind(&servTCP, (const struct sockaddr *)&addr, 0);
130     if (uv_listen((uv_stream_t *)&servTCP, DEFAULT_BACKLOG, (uv_connection_cb)AcceptClient)) {
131         return ERR_API_FAIL;
132     }
133     // Get listen port
134     Base::ZeroStruct(addr);
135     namelen = sizeof(addr);
136     if (uv_tcp_getsockname(&servTCP, (sockaddr *)&addr, &namelen)) {
137         return ERR_API_FAIL;
138     }
139     tcpListenPort = ntohs(addr.sin_port);
140     SystemDepend::SetDevItem("persist.hdc.port", std::to_string(tcpListenPort).c_str());
141     return RET_SUCCESS;
142 }
143 
Initial()144 int HdcDaemonTCP::Initial()
145 {
146     WRITE_LOG(LOG_DEBUG, "HdcDaemonTCP init");
147     SetUDPListen();
148     if (SetTCPListen() != RET_SUCCESS) {
149         WRITE_LOG(LOG_FATAL, "TCP listen failed");
150         return ERR_GENERIC;
151     }
152 #ifndef UNIT_TEST
153     WRITE_LOG(LOG_INFO, "TCP listen on port:[%d]", tcpListenPort);
154 #endif
155     return RET_SUCCESS;
156 }
157 }  // namespace Hdc
158