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