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 tcpListenPort = 0;
36 }
37 #ifdef HDC_DEBUG
38 const uint16_t DEBUG_TCP_PORT = 10178;
39 tcpListenPort = DEBUG_TCP_PORT;
40 #endif
41 }
42
~HdcDaemonTCP()43 HdcDaemonTCP::~HdcDaemonTCP()
44 {
45 }
46
Stop()47 void HdcDaemonTCP::Stop()
48 {
49 Base::TryCloseHandle((const uv_handle_t *)&servUDP);
50 Base::TryCloseHandle((const uv_handle_t *)&servTCP);
51 WRITE_LOG(LOG_DEBUG, "~HdcDaemonTCP");
52 }
53
TransmitConfig(const sockaddr * addrSrc,uv_udp_t * handle)54 void HdcDaemonTCP::TransmitConfig(const sockaddr *addrSrc, uv_udp_t *handle)
55 {
56 char srcIP[BUF_SIZE_TINY] = "";
57 struct sockaddr addrSrcIPPort;
58 uv_udp_send_t *req = new uv_udp_send_t();
59 if (!req) {
60 return;
61 }
62 string sendBuf = Base::StringFormat("%s-%d", HANDSHAKE_MESSAGE.c_str(), tcpListenPort);
63 uv_buf_t sndbuf = uv_buf_init((char *)sendBuf.c_str(), sendBuf.size());
64 uv_ip4_name(const_cast<sockaddr_in *>(reinterpret_cast<const sockaddr_in *>(addrSrc)), srcIP, sizeof(srcIP));
65 uv_ip4_addr(srcIP, DEFAULT_PORT, const_cast<sockaddr_in *>(reinterpret_cast<const sockaddr_in *>(&addrSrcIPPort)));
66 uv_udp_send(req, handle, &sndbuf, 1, &addrSrcIPPort, SendUDPFinish);
67 }
68
AcceptClient(uv_stream_t * server,int status)69 void HdcDaemonTCP::AcceptClient(uv_stream_t *server, int status)
70 {
71 uv_loop_t *ptrLoop = server->loop;
72 uv_tcp_t *pServTCP = (uv_tcp_t *)server;
73 HdcDaemonTCP *thisClass = (HdcDaemonTCP *)pServTCP->data;
74 HdcSessionBase *ptrConnect = reinterpret_cast<HdcSessionBase *>(thisClass->clsMainBase);
75 HdcSessionBase *daemon = reinterpret_cast<HdcSessionBase *>(thisClass->clsMainBase);
76 const uint16_t maxWaitTime = UV_DEFAULT_INTERVAL;
77 auto ctrl = daemon->BuildCtrlString(SP_START_SESSION, 0, nullptr, 0);
78 HSession hSession = ptrConnect->MallocSession(false, CONN_TCP, thisClass);
79 if (!hSession) {
80 return;
81 }
82 if (uv_accept(server, (uv_stream_t *)&hSession->hWorkTCP) < 0) {
83 goto Finish;
84 }
85 if ((hSession->fdChildWorkTCP = Base::DuplicateUvSocket(&hSession->hWorkTCP)) < 0) {
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 return RET_SUCCESS;
141 }
142
Initial()143 int HdcDaemonTCP::Initial()
144 {
145 WRITE_LOG(LOG_DEBUG, "HdcDaemonTCP init");
146 SetUDPListen();
147 if (SetTCPListen() != RET_SUCCESS) {
148 WRITE_LOG(LOG_FATAL, "TCP listen failed");
149 return ERR_GENERIC;
150 }
151 #ifndef UNIT_TEST
152 WRITE_LOG(LOG_INFO, "TCP listen on port:[%d]", tcpListenPort);
153 #endif
154 return RET_SUCCESS;
155 }
156 } // namespace Hdc
157