1 /*
2 * Copyright (c) 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 "auth_request.h"
17
18 #include "auth_common.h"
19 #include "auth_log.h"
20 #include "softbus_adapter_mem.h"
21 #include "softbus_def.h"
22
23 static ListNode g_authRequestList = {&g_authRequestList, &g_authRequestList};
24
FindAuthRequestByRequestId(uint64_t requestId)25 static AuthRequest *FindAuthRequestByRequestId(uint64_t requestId)
26 {
27 AuthRequest *item = NULL;
28 LIST_FOR_EACH_ENTRY(item, &g_authRequestList, AuthRequest, node) {
29 if (item->requestId == requestId) {
30 return item;
31 }
32 }
33 return NULL;
34 }
35
GetAuthRequestWaitNum(const AuthRequest *request)36 static uint32_t GetAuthRequestWaitNum(const AuthRequest *request)
37 {
38 uint32_t num = 0;
39 AuthRequest *item = NULL;
40 AuthRequest *next = NULL;
41 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_authRequestList, AuthRequest, node) {
42 if (item->type != request->type || !CompareConnInfo(&request->connInfo, &item->connInfo, true)) {
43 continue;
44 }
45 if (item->requestId == request->requestId) {
46 num++;
47 continue;
48 }
49 if (request->addTime - item->addTime < AUTH_REQUEST_TIMTOUR) {
50 AUTH_LOGD(AUTH_CONN,
51 "The two request addr are same. requestId1=%{public}u, requestId2=%{public}u",
52 request->requestId, item->requestId);
53 num++;
54 continue;
55 }
56 if (CheckAuthConnCallback(&item->connCb)) {
57 item->connCb.onConnOpenFailed(item->requestId, SOFTBUS_AUTH_CONN_FAIL);
58 } else if (CheckVerifyCallback(&item->verifyCb)) {
59 item->verifyCb.onVerifyFailed(item->requestId, SOFTBUS_AUTH_CONN_FAIL);
60 }
61 ListDelete(&item->node);
62 SoftBusFree(item);
63 num++;
64 }
65 return num;
66 }
67
AddAuthRequest(const AuthRequest *request)68 uint32_t AddAuthRequest(const AuthRequest *request)
69 {
70 AUTH_CHECK_AND_RETURN_RET_LOGE(request != NULL, 0, AUTH_CONN, "request is NULL");
71 AuthRequest *newRequest = SoftBusCalloc(sizeof(AuthRequest));
72 if (newRequest == NULL) {
73 AUTH_LOGE(AUTH_CONN, "malloc AuthRequest fail");
74 return 0;
75 }
76 *newRequest = *request;
77 if (!RequireAuthLock()) {
78 SoftBusFree(newRequest);
79 return 0;
80 }
81 newRequest->addTime = GetCurrentTimeMs();
82 ListTailInsert(&g_authRequestList, &newRequest->node);
83 uint32_t waitNum = GetAuthRequestWaitNum(newRequest);
84 ReleaseAuthLock();
85 return waitNum;
86 }
87
GetAuthRequest(uint32_t requestId, AuthRequest *request)88 int32_t GetAuthRequest(uint32_t requestId, AuthRequest *request)
89 {
90 AUTH_CHECK_AND_RETURN_RET_LOGE(request != NULL, SOFTBUS_INVALID_PARAM, AUTH_CONN, "request is NULL");
91 if (!RequireAuthLock()) {
92 return SOFTBUS_LOCK_ERR;
93 }
94 AuthRequest *item = FindAuthRequestByRequestId(requestId);
95 if (item == NULL) {
96 ReleaseAuthLock();
97 return SOFTBUS_NOT_FIND;
98 }
99 *request = *item;
100 ReleaseAuthLock();
101 return SOFTBUS_OK;
102 }
103
GetAuthRequestNoLock(uint32_t requestId, AuthRequest *request)104 int32_t GetAuthRequestNoLock(uint32_t requestId, AuthRequest *request)
105 {
106 AUTH_CHECK_AND_RETURN_RET_LOGE(request != NULL, SOFTBUS_INVALID_PARAM, AUTH_CONN, "request is NULL");
107 AuthRequest *item = FindAuthRequestByRequestId(requestId);
108 if (item == NULL) {
109 AUTH_LOGE(AUTH_CONN, "find auth request failed");
110 return SOFTBUS_NOT_FIND;
111 }
112 *request = *item;
113 return SOFTBUS_OK;
114 }
115
FindAuthRequestByConnInfo(const AuthConnInfo *connInfo, AuthRequest *request)116 int32_t FindAuthRequestByConnInfo(const AuthConnInfo *connInfo, AuthRequest *request)
117 {
118 AUTH_CHECK_AND_RETURN_RET_LOGE(connInfo != NULL, SOFTBUS_INVALID_PARAM, AUTH_CONN, "connInfo is NULL");
119 AUTH_CHECK_AND_RETURN_RET_LOGE(request != NULL, SOFTBUS_INVALID_PARAM, AUTH_CONN, "request is NULL");
120 if (!RequireAuthLock()) {
121 return SOFTBUS_LOCK_ERR;
122 }
123 AuthRequest *item = NULL;
124 LIST_FOR_EACH_ENTRY(item, &g_authRequestList, AuthRequest, node) {
125 if (item->type != REQUEST_TYPE_VERIFY ||
126 !CompareConnInfo(&item->connInfo, connInfo, true)) {
127 continue;
128 }
129 *request = *item;
130 ReleaseAuthLock();
131 return SOFTBUS_OK;
132 }
133 ReleaseAuthLock();
134 return SOFTBUS_NOT_FIND;
135 }
136
FindAndDelAuthRequestByConnInfo(uint32_t requestId, const AuthConnInfo *connInfo)137 int32_t FindAndDelAuthRequestByConnInfo(uint32_t requestId, const AuthConnInfo *connInfo)
138 {
139 AUTH_CHECK_AND_RETURN_RET_LOGE(connInfo != NULL, SOFTBUS_INVALID_PARAM, AUTH_CONN, "connInfo is NULL");
140 if (!RequireAuthLock()) {
141 return SOFTBUS_LOCK_ERR;
142 }
143 AuthRequest *item = NULL;
144 AuthRequest *next = NULL;
145 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_authRequestList, AuthRequest, node) {
146 if (!CompareConnInfo(&item->connInfo, connInfo, true)) {
147 continue;
148 }
149 if (item->requestId == requestId) {
150 ListDelete(&item->node);
151 SoftBusFree(item);
152 continue;
153 }
154 if (CheckAuthConnCallback(&item->connCb)) {
155 item->connCb.onConnOpenFailed(item->requestId, SOFTBUS_AUTH_CONN_FAIL);
156 } else if (CheckVerifyCallback(&item->verifyCb)) {
157 item->verifyCb.onVerifyFailed(item->requestId, SOFTBUS_AUTH_CONN_FAIL);
158 }
159 ListDelete(&item->node);
160 SoftBusFree(item);
161 }
162 ReleaseAuthLock();
163 return SOFTBUS_NOT_FIND;
164 }
165
DelAuthRequest(uint32_t requestId)166 void DelAuthRequest(uint32_t requestId)
167 {
168 if (!RequireAuthLock()) {
169 return;
170 }
171 AuthRequest *item = FindAuthRequestByRequestId(requestId);
172 if (item == NULL) {
173 ReleaseAuthLock();
174 return;
175 }
176 AUTH_LOGD(AUTH_CONN, "del auth request requestId=%{public}u", requestId);
177 ListDelete(&item->node);
178 SoftBusFree(item);
179 ReleaseAuthLock();
180 }
181
ClearAuthRequest(void)182 void ClearAuthRequest(void)
183 {
184 if (!RequireAuthLock()) {
185 return;
186 }
187 AuthRequest *item = NULL;
188 AuthRequest *next = NULL;
189 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_authRequestList, AuthRequest, node) {
190 ListDelete(&item->node);
191 SoftBusFree(item);
192 }
193 ListInit(&g_authRequestList);
194 ReleaseAuthLock();
195 }
196
CheckVerifyCallback(const AuthVerifyCallback *verifyCb)197 bool CheckVerifyCallback(const AuthVerifyCallback *verifyCb)
198 {
199 if (verifyCb == NULL) {
200 AUTH_LOGE(AUTH_CONN, "verifyCb is null");
201 return false;
202 }
203 if (verifyCb->onVerifyPassed == NULL || verifyCb->onVerifyFailed == NULL) {
204 AUTH_LOGE(AUTH_CONN, "onVerifyPassed or onVerifyFailed is null");
205 return false;
206 }
207 return true;
208 }
209
CheckAuthConnCallback(const AuthConnCallback *connCb)210 bool CheckAuthConnCallback(const AuthConnCallback *connCb)
211 {
212 if (connCb == NULL) {
213 AUTH_LOGE(AUTH_CONN, "connCb is null");
214 return false;
215 }
216 if (connCb->onConnOpened == NULL || connCb->onConnOpenFailed == NULL) {
217 AUTH_LOGE(AUTH_CONN, "onConnOpened or onConnOpenFailed is null");
218 return false;
219 }
220 return true;
221 }
222
PerformVerifyCallback(uint32_t requestId, int32_t result, AuthHandle authHandle, const NodeInfo *info)223 void PerformVerifyCallback(uint32_t requestId, int32_t result, AuthHandle authHandle, const NodeInfo *info)
224 {
225 if (authHandle.type < AUTH_LINK_TYPE_WIFI || authHandle.type >= AUTH_LINK_TYPE_MAX) {
226 AUTH_LOGE(AUTH_CONN, "authHandle type error");
227 return;
228 }
229 AuthRequest request;
230 if (GetAuthRequest(requestId, &request) != SOFTBUS_OK) {
231 AUTH_LOGE(AUTH_CONN, "get auth request failed");
232 return;
233 }
234 if (!CheckVerifyCallback(&request.verifyCb)) {
235 AUTH_LOGE(AUTH_CONN, "check verifyCb failed");
236 return;
237 }
238 if (result == SOFTBUS_OK) {
239 request.verifyCb.onVerifyPassed(request.requestId, authHandle, info);
240 } else {
241 request.verifyCb.onVerifyFailed(request.requestId, result);
242 }
243 }
244
PerformAuthConnCallback(uint32_t requestId, int32_t result, int64_t authId)245 void PerformAuthConnCallback(uint32_t requestId, int32_t result, int64_t authId)
246 {
247 AuthRequest request;
248 if (GetAuthRequest(requestId, &request) != SOFTBUS_OK) {
249 AUTH_LOGE(AUTH_CONN, "get auth request failed");
250 return;
251 }
252 if (!CheckAuthConnCallback(&request.connCb)) {
253 AUTH_LOGE(AUTH_CONN, "check connCb failed");
254 return;
255 }
256 AuthHandle authHandle = { .authId = authId, .type = request.connInfo.type };
257 if (result == SOFTBUS_OK) {
258 request.connCb.onConnOpened(request.requestId, authHandle);
259 } else {
260 request.connCb.onConnOpenFailed(request.requestId, result);
261 }
262 }
263