1 /*
2 * Copyright (C) 2021-2022 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
16 #include <string>
17 #include <map>
18 #include <list>
19 #include <thread>
20 #include <mutex>
21 #include "securec.h"
22
23 #include "dhcp_client_service_impl.h"
24 #include "dhcp_logger.h"
25 #include "../../../interfaces/inner_api/include/dhcp_define.h"
26 #include "dhcp_result.h"
27
28 DEFINE_DHCPLOG_DHCP_LABEL("DhcpResult");
29
PublishDhcpIpv4ResultEvent(const int code,const char * data,const char * ifname)30 bool PublishDhcpIpv4ResultEvent(const int code, const char *data, const char *ifname)
31 {
32 DHCP_LOGI("PublishDhcpIpv4ResultEvent ifname:%{public}s code:%{public}d", ifname, code);
33 std::string strIfName = ifname;
34 std::string strData = data;
35 #ifndef OHOS_ARCH_LITE
36 OHOS::sptr<OHOS::Wifi::DhcpClientServiceImpl> clientImpl = OHOS::Wifi::DhcpClientServiceImpl::GetInstance();
37 #else
38 std::shared_ptr<OHOS::Wifi::DhcpClientServiceImpl> clientImpl = OHOS::Wifi::DhcpClientServiceImpl::GetInstance();
39 #endif
40 if (code == PUBLISH_CODE_TIMEOUT) { // timeout
41 if ((clientImpl != nullptr) && (clientImpl->DhcpIpv4ResultTimeOut(strIfName) != OHOS::Wifi::DHCP_OPT_SUCCESS)) {
42 DHCP_LOGE("PublishDhcpIpv4ResultEvent DhcpIpv4ResultTimeOut failed!");
43 return false;
44 }
45 } else {
46 if (DhcpEventResultHandle(code, strData) != OHOS::Wifi::DHCP_OPT_SUCCESS) {
47 DHCP_LOGE("PublishDhcpIpv4ResultEvent DhcpEventResultHandlefailed!");
48 return false;
49 }
50 }
51 return true;
52 }
53
DhcpEventResultHandle(const int code,const std::string & data)54 int DhcpEventResultHandle(const int code, const std::string &data)
55 {
56 if (data.empty()) {
57 DHCP_LOGE("DhcpEventResultHandle() error, data is empty!");
58 return OHOS::Wifi::DHCP_OPT_FAILED;
59 }
60 DHCP_LOGI("Enter DhcpEventResultHandle() code:%{public}d, data:%{private}s", code, data.c_str());
61 /* Data format - ipv4:ifname,time,cliIp,lease,servIp,subnet,dns1,dns2,router1,router2,vendor */
62 std::string strData(data);
63 std::string strFlag;
64 std::string strResult;
65 if (strData.find(OHOS::Wifi::EVENT_DATA_IPV4) != std::string::npos) {
66 strFlag = strData.substr(0, (int)OHOS::Wifi::EVENT_DATA_IPV4.size());
67 if (strFlag != OHOS::Wifi::EVENT_DATA_IPV4) {
68 DHCP_LOGE("DhcpEventResultHandle() %{public}s ipv4flag:%{public}s error!", data.c_str(), strFlag.c_str());
69 return OHOS::Wifi::DHCP_OPT_FAILED;
70 }
71 /* Skip separator ":" */
72 strResult = strData.substr((int)OHOS::Wifi::EVENT_DATA_IPV4.size() + 1);
73 } else if (strData.find(OHOS::Wifi::EVENT_DATA_IPV6) != std::string::npos) {
74 strFlag = strData.substr(0, (int)OHOS::Wifi::EVENT_DATA_IPV6.size());
75 if (strFlag != OHOS::Wifi::EVENT_DATA_IPV6) {
76 DHCP_LOGE("DhcpEventResultHandle() %{public}s ipv6flag:%{public}s error!", data.c_str(), strFlag.c_str());
77 return OHOS::Wifi::DHCP_OPT_FAILED;
78 }
79 strResult = strData.substr((int)OHOS::Wifi::EVENT_DATA_IPV6.size() + 1);
80 } else {
81 DHCP_LOGE("DhcpEventResultHandle() data:%{public}s error, no find ipflag!", data.c_str());
82 return OHOS::Wifi::DHCP_OPT_FAILED;
83 }
84 DHCP_LOGI("DhcpEventResultHandle() flag:%{public}s, result:%{private}s", strFlag.c_str(), strResult.c_str());
85 if (strFlag == OHOS::Wifi::EVENT_DATA_IPV4) {
86 std::vector<std::string> vecSplits;
87 if (!SplitStr(strResult, OHOS::Wifi::EVENT_DATA_DELIMITER, OHOS::Wifi::EVENT_DATA_NUM, vecSplits)) {
88 DHCP_LOGE("DhcpEventResultHandle() SplitString strResult:%{public}s failed!", strResult.c_str());
89 return OHOS::Wifi::DHCP_OPT_FAILED;
90 }
91 if (GetDhcpEventIpv4Result(code, vecSplits) != OHOS::Wifi::DHCP_OPT_SUCCESS) {
92 DHCP_LOGE("DhcpEventResultHandle() GetDhcpEventIpv4Result failed!");
93 return OHOS::Wifi::DHCP_OPT_FAILED;
94 }
95 }
96 return OHOS::Wifi::DHCP_OPT_SUCCESS;
97 }
98
GetDhcpEventIpv4Result(const int code,const std::vector<std::string> & splits)99 int GetDhcpEventIpv4Result(const int code, const std::vector<std::string> &splits)
100 {
101 /* Check field ifname and time. */
102 if (splits[OHOS::Wifi::DHCP_NUM_ZERO].empty() || splits[OHOS::Wifi::DHCP_NUM_ONE].empty()) {
103 DHCP_LOGE("GetDhcpEventIpv4Result() ifname or time is empty!");
104 return OHOS::Wifi::DHCP_OPT_FAILED;
105 }
106
107 /* Check field cliIp. */
108 if (((code == PUBLISH_CODE_SUCCESS) && (splits[OHOS::Wifi::DHCP_NUM_TWO] == OHOS::Wifi::INVALID_STRING))
109 || ((code == PUBLISH_CODE_FAILED) && (splits[OHOS::Wifi::DHCP_NUM_TWO] !=
110 OHOS::Wifi::INVALID_STRING))) {
111 DHCP_LOGE("GetDhcpEventIpv4Result() code:%{public}d,%{public}s error!", code,
112 splits[OHOS::Wifi::DHCP_NUM_TWO].c_str());
113 return OHOS::Wifi::DHCP_OPT_FAILED;
114 }
115
116 std::string ifname = splits[OHOS::Wifi::DHCP_NUM_ZERO];
117 #ifndef OHOS_ARCH_LITE
118 OHOS::sptr<OHOS::Wifi::DhcpClientServiceImpl> clientImpl = OHOS::Wifi::DhcpClientServiceImpl::GetInstance();
119 #else
120 std::shared_ptr<OHOS::Wifi::DhcpClientServiceImpl> clientImpl = OHOS::Wifi::DhcpClientServiceImpl::GetInstance();
121 #endif
122 if (code == PUBLISH_CODE_FAILED) {
123 return clientImpl->DhcpIpv4ResultFail(splits); /* Get failed. */
124 }
125 /* Get success. */
126 if (clientImpl->DhcpIpv4ResultSuccess(splits) != OHOS::Wifi::DHCP_OPT_SUCCESS) {
127 DHCP_LOGE("GetDhcpEventIpv4Result() GetSuccessIpv4Result failed!");
128 return OHOS::Wifi::DHCP_OPT_FAILED;
129 }
130 DHCP_LOGI("GetDhcpEventIpv4Result() ifname:%{public}s ok", ifname.c_str());
131 return OHOS::Wifi::DHCP_OPT_SUCCESS;
132 }
133
SplitStr(const std::string src,const std::string delim,const int count,std::vector<std::string> & splits)134 bool SplitStr(const std::string src, const std::string delim, const int count, std::vector<std::string> &splits)
135 {
136 if (src.empty() || delim.empty()) {
137 DHCP_LOGE("SplitString() error, src or delim is empty!");
138 return false;
139 }
140 splits.clear();
141 std::string strData(src);
142 int nDelim = 0;
143 char *pSave = NULL;
144 char *pTok = strtok_r(const_cast<char *>(strData.c_str()), delim.c_str(), &pSave);
145 while (pTok != NULL) {
146 splits.push_back(std::string(pTok));
147 nDelim++;
148 pTok = strtok_r(NULL, delim.c_str(), &pSave);
149 }
150
151 if (nDelim < count) {
152 DHCP_LOGI("SplitString() %{public}s failed, nDelim:%{public}d,count:%{public}d!", src.c_str(), nDelim, count);
153 return false;
154 }
155 DHCP_LOGI("SplitString() %{private}s success, delim:%{public}s, count:%{public}d, splits.size():%{public}d.",
156 src.c_str(), delim.c_str(), count, (int)splits.size());
157 return true;
158 }