1 /*
2 * Copyright (C) 2021-2023 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 "../../../interfaces/kits/c/dhcp_c_api.h"
16 #include "../../../interfaces/inner_api/dhcp_client.h"
17 #include "../../../interfaces/inner_api/dhcp_server.h"
18 #include "../../include/dhcp_sdk_define.h"
19 #include "../inc/dhcp_c_utils.h"
20 #include "../../include/dhcp_event.h"
21 #include "dhcp_logger.h"
22 #ifndef OHOS_ARCH_LITE
23 #include <string_ex.h>
24 #endif
25 DEFINE_DHCPLOG_DHCP_LABEL("DhcpCService");
26 std::shared_ptr<OHOS::Wifi::DhcpClient> dhcpClientPtr = OHOS::Wifi::DhcpClient::GetInstance(DHCP_CLIENT_ABILITY_ID);
27 std::shared_ptr<OHOS::Wifi::DhcpServer> dhcpServerPtr = OHOS::Wifi::DhcpServer::GetInstance(DHCP_SERVER_ABILITY_ID);
28
29 #ifdef OHOS_ARCH_LITE
30 static std::shared_ptr<DhcpClientCallBack> dhcpClientCallBack =
31 std::shared_ptr<DhcpClientCallBack>(new (std::nothrow)DhcpClientCallBack());
32 static std::shared_ptr<DhcpServerCallBack> dhcpServerCallBack =
33 std::shared_ptr<DhcpServerCallBack>(new (std::nothrow)DhcpServerCallBack());
34 #else
35 static OHOS::sptr<DhcpClientCallBack> dhcpClientCallBack =
36 OHOS::sptr<DhcpClientCallBack>(new (std::nothrow)DhcpClientCallBack());
37 static OHOS::sptr<DhcpServerCallBack> dhcpServerCallBack =
38 OHOS::sptr<DhcpServerCallBack>(new (std::nothrow)DhcpServerCallBack());
39 #endif
40
RegisterDhcpClientCallBack(const char * ifname,const ClientCallBack * event)41 NO_SANITIZE("cfi") DhcpErrorCode RegisterDhcpClientCallBack(const char *ifname, const ClientCallBack *event)
42 {
43 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
44 CHECK_PTR_RETURN(event, DHCP_INVALID_PARAM);
45 CHECK_PTR_RETURN(dhcpClientPtr, DHCP_INVALID_PARAM);
46 CHECK_PTR_RETURN(dhcpClientCallBack, DHCP_INVALID_PARAM);
47 dhcpClientCallBack->RegisterCallBack(ifname, event);
48 return GetCErrorCode(dhcpClientPtr->RegisterDhcpClientCallBack(ifname, dhcpClientCallBack));
49 }
50
StartDhcpClient(const char * ifname,bool bIpv6)51 NO_SANITIZE("cfi") DhcpErrorCode StartDhcpClient(const char *ifname, bool bIpv6)
52 {
53 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
54 CHECK_PTR_RETURN(dhcpClientPtr, DHCP_INVALID_PARAM);
55 return GetCErrorCode(dhcpClientPtr->StartDhcpClient(ifname, bIpv6));
56 }
57
StopDhcpClient(const char * ifname,bool bIpv6)58 NO_SANITIZE("cfi") DhcpErrorCode StopDhcpClient(const char *ifname, bool bIpv6)
59 {
60 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
61 CHECK_PTR_RETURN(dhcpClientPtr, DHCP_INVALID_PARAM);
62 return GetCErrorCode(dhcpClientPtr->StopDhcpClient(ifname, bIpv6));
63 }
64
RenewDhcpClient(const char * ifname)65 NO_SANITIZE("cfi") DhcpErrorCode RenewDhcpClient(const char *ifname)
66 {
67 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
68 CHECK_PTR_RETURN(dhcpClientPtr, DHCP_INVALID_PARAM);
69 return GetCErrorCode(dhcpClientPtr->RenewDhcpClient(ifname));
70 }
71
RegisterDhcpServerCallBack(const char * ifname,const ServerCallBack * event)72 NO_SANITIZE("cfi") DhcpErrorCode RegisterDhcpServerCallBack(const char *ifname, const ServerCallBack *event)
73 {
74 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
75 CHECK_PTR_RETURN(event, DHCP_INVALID_PARAM);
76 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
77 CHECK_PTR_RETURN(dhcpServerCallBack, DHCP_INVALID_PARAM);
78 dhcpServerCallBack->RegisterCallBack(ifname, event);
79 return GetCErrorCode(dhcpServerPtr->RegisterDhcpServerCallBack(ifname, dhcpServerCallBack));
80 }
81
StartDhcpServer(const char * ifname)82 NO_SANITIZE("cfi") DhcpErrorCode StartDhcpServer(const char *ifname)
83 {
84 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
85 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
86 return GetCErrorCode(dhcpServerPtr->StartDhcpServer(ifname));
87 }
88
StopDhcpServer(const char * ifname)89 NO_SANITIZE("cfi") DhcpErrorCode StopDhcpServer(const char *ifname)
90 {
91 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
92 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
93 return GetCErrorCode(dhcpServerPtr->StopDhcpServer(ifname));
94 }
95
SetDhcpRange(const char * ifname,const DhcpRange * range)96 NO_SANITIZE("cfi") DhcpErrorCode SetDhcpRange(const char *ifname, const DhcpRange *range)
97 {
98 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
99 CHECK_PTR_RETURN(range, DHCP_INVALID_PARAM);
100 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
101 OHOS::Wifi::DhcpRange rangeNew;
102 rangeNew.iptype = range->iptype;
103 rangeNew.strStartip = range->strStartip;
104 rangeNew.strEndip = range->strEndip;
105 rangeNew.strSubnet = range->strSubnet;
106 rangeNew.strTagName = range->strTagName;
107 return GetCErrorCode(dhcpServerPtr->SetDhcpRange(ifname, rangeNew));
108 }
109
SetDhcpName(const char * ifname,const char * tagName)110 NO_SANITIZE("cfi") DhcpErrorCode SetDhcpName(const char *ifname, const char *tagName)
111 {
112 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
113 CHECK_PTR_RETURN(tagName, DHCP_INVALID_PARAM);
114 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
115 return GetCErrorCode(dhcpServerPtr->SetDhcpName(ifname, tagName));
116 }
117
PutDhcpRange(const char * tagName,const DhcpRange * range)118 NO_SANITIZE("cfi") DhcpErrorCode PutDhcpRange(const char *tagName, const DhcpRange *range)
119 {
120 CHECK_PTR_RETURN(tagName, DHCP_INVALID_PARAM);
121 CHECK_PTR_RETURN(range, DHCP_INVALID_PARAM);
122 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
123 OHOS::Wifi::DhcpRange rangeNew;
124 rangeNew.iptype = range->iptype;
125 rangeNew.strStartip = range->strStartip;
126 rangeNew.strEndip = range->strEndip;
127 rangeNew.strSubnet = range->strSubnet;
128 rangeNew.strTagName = range->strTagName;
129 return GetCErrorCode(dhcpServerPtr->PutDhcpRange(tagName, rangeNew));
130 }
131
RemoveAllDhcpRange(const char * tagName)132 NO_SANITIZE("cfi") DhcpErrorCode RemoveAllDhcpRange(const char *tagName)
133 {
134 CHECK_PTR_RETURN(tagName, DHCP_INVALID_PARAM);
135 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
136 return GetCErrorCode(dhcpServerPtr->RemoveAllDhcpRange(tagName));
137 }
138
RemoveDhcpRange(const char * tagName,const void * range)139 DhcpErrorCode RemoveDhcpRange(const char *tagName, const void *range)
140 {
141 CHECK_PTR_RETURN(tagName, DHCP_INVALID_PARAM);
142 CHECK_PTR_RETURN(range, DHCP_INVALID_PARAM);
143 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
144 return GetCErrorCode(dhcpServerPtr->RemoveDhcpRange(tagName, *(OHOS::Wifi::DhcpRange *)range));
145 }
146
GetDhcpClientInfos(const char * ifname,int staNumber,DhcpStationInfo * staInfo,int * staSize)147 DhcpErrorCode GetDhcpClientInfos(const char *ifname, int staNumber, DhcpStationInfo *staInfo, int *staSize)
148 {
149 CHECK_PTR_RETURN(ifname, DHCP_INVALID_PARAM);
150 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
151 std::vector<std::string> vecInfo;
152 DhcpErrorCode ret = GetCErrorCode(dhcpServerPtr->GetDhcpClientInfos(ifname, vecInfo));
153 if (ret != DHCP_SUCCESS) {
154 DHCP_LOGE("GetDhcpClientInfos failed!");
155 return DHCP_FAILED;
156 }
157 int size = (int)vecInfo.size();
158 DHCP_LOGI("GetDhcpClientInfos size: %{public}d", size);
159 int i;
160 for (i = 0; i < size; i++) {
161 std::vector<std::string> tmp;
162 std::string str = vecInfo[i];
163 OHOS::SplitStr(str, " ", tmp);
164 if (tmp.empty()) {
165 continue;
166 }
167 if (tmp[0] == "duid") {
168 break;
169 }
170 if (tmp.size() < DHCP_LEASE_FORMAT_SIZE) {
171 continue;
172 }
173 std::string mac = tmp[DHCP_LEASE_MAC_ADDR_POS];
174 std::string ipAddr = tmp[DHCP_LEASE_IP_ADDR_POS];
175 std::string deviceName = tmp[DHCP_LEASE_HOSTNAME_POS];
176 DHCP_LOGI("GetDhcpClientInfos mac:%{public}s", mac.c_str());
177
178 if (i >= staNumber) {
179 break;
180 }
181
182 if (strcpy_s(staInfo[i].macAddr, MAC_ADDR_MAX_LEN, mac.c_str()) != EOK) {
183 DHCP_LOGE("get dhcp client info, cpy mac failed! srclen=%{public}zu", strlen(mac.c_str()));
184 return DHCP_FAILED;
185 }
186 if (strcpy_s(staInfo[i].ipAddr, INET_ADDRSTRLEN, ipAddr.c_str()) != EOK) {
187 DHCP_LOGE("get dhcp client info, cpy ip failed!");
188 return DHCP_FAILED;
189 }
190 if (strcpy_s(staInfo[i].deviceName, DHCP_LEASE_DATA_MAX_LEN, deviceName.c_str()) != EOK) {
191 DHCP_LOGE("get dhcp client info, cpy name failed!");
192 return DHCP_FAILED;
193 }
194 }
195 *staSize = i;
196
197 DHCP_LOGI("GetDhcpClientInfos3 %{public}d macAddr:%{public}s", i, staInfo[0].macAddr);
198
199 return DHCP_SUCCESS;
200 }
201
UpdateLeasesTime(const char * leaseTime)202 NO_SANITIZE("cfi") DhcpErrorCode UpdateLeasesTime(const char *leaseTime)
203 {
204 CHECK_PTR_RETURN(leaseTime, DHCP_INVALID_PARAM);
205 CHECK_PTR_RETURN(dhcpServerPtr, DHCP_INVALID_PARAM);
206 return GetCErrorCode(dhcpServerPtr->UpdateLeasesTime(leaseTime));
207 }
208