0%

软总线模块之auth_interface的标注

auth_interface最主要的是在提供各个会话节点、各个链接节点、各个秘钥节点的管理,其中提供包括增删改查等功能;另外的还有一个发送数据的接口。

总览

头文件之中,结构体的定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
typedef struct SessionKey {
char key[AUTH_SESSION_KEY_LEN];
int index;
int fd;
} SessionKey;

//注:链表结构
typedef struct SessionKeyNode {
List head;//注:双向链表
SessionKey sKey;
} SessionKeyNode;

typedef struct AuthSession {
int isUsed;
long long seqId;
uint32_t sessionId;
AuthConn *conn;
} AuthSession;

下面是C源代码的解读:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#include "auth_interface.h"

#include "hichain.h"
#include "securec.h"

#include "auth_conn.h"
#include "bus_manager.h"
#include "os_adapter.h"
#include "wifi_auth_manager.h"

//注:全局变量SessionId,且每新建一个会话后会自增
static uint32_t g_authSessionId = 1;
static List *g_sessionKeyList = NULL;
//注:根据后续使用判断这应该是数组
static AuthSession *g_authSessionMap = NULL;
static hc_handle g_hcHandle = NULL;

/**
* @description: 根据AUTH_SESSION_MAX_NUM(现有版本中被定义为2)初始化全局变量g_authSessionMap
*/
static int AuthSessionMapInit(void)
{
if (g_authSessionMap != NULL) {
return 0;
}

int len = sizeof(AuthSession) * AUTH_SESSION_MAX_NUM;
g_authSessionMap = (AuthSession *)malloc(len);
if (g_authSessionMap == NULL) {
return -1;
}
//注:初始化,置零
(void)memset_s(g_authSessionMap, len, 0, len);
return 0;
}

/**
* @description: 查:根据seqId从g_authSessionMap中获取对应的AuthSession
* @param {long long} seqId
* @return {AuthSession}
*/
static AuthSession *AuthGetAuthSessionBySeqId(long long seqId)
{
if (g_authSessionMap == NULL) {
return NULL;
}

for (int i = 0; i < AUTH_SESSION_MAX_NUM; i++) {
//注:跳过没有被使用的AuthSession
if (g_authSessionMap[i].isUsed == 0) {
continue;
}

if (g_authSessionMap[i].seqId == seqId) {
return &g_authSessionMap[i];
}
}

return NULL;
}

/**
* @description: 查:根据sessionId从g_authSessionMap中获取对应的AuthSession
* @param {uint32_t} sessionId
* @return {AuthSession}
*/
static AuthSession *AuthGetAuthSessionBySessionId(uint32_t sessionId)
{
if (g_authSessionMap == NULL) {
return NULL;
}

for (int i = 0; i < AUTH_SESSION_MAX_NUM; i++) {
//注:跳过没有被使用的AuthSession
if (g_authSessionMap[i].isUsed == 0) {
continue;
}

if (g_authSessionMap[i].sessionId == sessionId) {
return &g_authSessionMap[i];
}
}

return NULL;
}

/**
* @description: 删:根据sessionId从g_authSessionMap中删除对应的AuthSession
* @param {uint32_t} sessionId
*/
static void AuthDelAuthSessionBySessionId(uint32_t sessionId)
{
if (g_authSessionMap == NULL) {
return;
}

for (int i = 0; i < AUTH_SESSION_MAX_NUM; i++) {
if (g_authSessionMap[i].sessionId == sessionId) {
(void)memset_s(&g_authSessionMap[i], sizeof(AuthSession), 0, sizeof(AuthSession));
//注:内容清零后重新标记为未使用
g_authSessionMap[i].isUsed = 0;
break;
}
}

return;
}

/**
* @description: 增:根据传入参数在g_authSessionMap中创建一个AuthSession
* @param {const AuthConn} *conn 一个连接
* @param {long long} seqId
* @param {uint32_t} sessionId
* @return {AuthSession}
*/
static AuthSession *AuthGetNewAuthSession(const AuthConn *conn, long long seqId, uint32_t sessionId)
{
if (conn == NULL || g_authSessionMap == NULL) {
return NULL;
}

for (int i = 0; i < AUTH_SESSION_MAX_NUM; i++) {
if (g_authSessionMap[i].isUsed == 0) {
g_authSessionMap[i].isUsed = 1;
g_authSessionMap[i].seqId = seqId;
g_authSessionMap[i].sessionId = sessionId;
g_authSessionMap[i].conn = (AuthConn *)conn;

return &g_authSessionMap[i];
}
}

return NULL;
}


/**
* @description: 根据sessionId将数据发送到指定的连接上
* @param {uint32_t} sessionId
* @param {int} module
* @param {const char} *data 被发送的数据
* @return {int} 发送结果
*/
static int AuthSendData(uint32_t sessionId, int module, const char *data)
{
AuthSession *auth = AuthGetAuthSessionBySessionId(sessionId);
if (auth == NULL || auth->conn == NULL) {
return -1;
}

int ret = AuthConnPostBytes(auth->conn->fd, module, 0, auth->seqId, data);
if (ret != 0) {
return -1;
}

SOFTBUS_PRINT("[AUTH] AuthSendData ok\n");
return 0;
}

/**
* @description: 根据参数中identity的session_id将数据发送出去
* @param {const struct session_identity} *identity
* @param {const void} *data 被发送的数据
* @param {uint32_t} length
*/
static void AuthOnTransmit(const struct session_identity *identity, const void *data, uint32_t length)
{
SOFTBUS_PRINT("[AUTH] AuthOnTransmit begin\n");
if (identity == NULL || data == NULL || length == 0) {
return;
}

int ret = AuthSendData(identity->session_id, MODULE_AUTH_SDK, data);
if (ret != 0) {
SOFTBUS_PRINT("[AUTH] AuthOnTransmit send data fail\n");
return;
}
}

static void AuthGetProtocolParams(const struct session_identity *identity, int32_t operationCode,
struct hc_pin *hcPin, struct operation_parameter *para)
{
(void)operationCode;
(void)hcPin;
if (identity == NULL || para == NULL) {
return;
}

para->key_length = AUTH_SESSION_KEY_LEN;
AuthSession *authSes = AuthGetAuthSessionBySessionId(identity->session_id);
if (authSes == NULL || authSes->conn == NULL) {
SOFTBUS_PRINT("[AUTH] AuthGetProtocolParams get session fail\n");
return;
}

para->peer_auth_id.length = strlen(authSes->conn->authId);
int ret = memcpy_s(para->peer_auth_id.auth_id, sizeof(para->peer_auth_id.auth_id),
authSes->conn->authId, strlen(authSes->conn->authId));
if (ret != EOK) {
return;
}

DeviceInfo *info = BusGetLocalDeviceInfo();
if (info == NULL) {
return;
}
para->self_auth_id.length = strlen(info->deviceId);
ret = memcpy_s(para->self_auth_id.auth_id, sizeof(para->self_auth_id.auth_id),
info->deviceId, strlen(info->deviceId));
if (ret != EOK) {
return;
}

SOFTBUS_PRINT("[AUTH] AuthGetProtocolParams ok\n");
return;
}

/**
* @description: 根据传入的fd遍历g_sessionKeyList找到对应的SessionKeyNode,并将其删除
* @param {int} fd
*/
void ClearSessionKeyByFd(int fd)
{
if (g_sessionKeyList == NULL) {
return;
}

bool flag = false;
List *pos = NULL;
List *tmp = NULL;
SessionKeyNode *node = NULL;

//注:遍历g_sessionKeyList来找到fd与参数相同的SessionKeyNode:for (pos = g_sessionKeyList->next, tmp = pos->next; pos != g_sessionKeyList; pos = tmp, tmp = pos->next)
LIST_FOR_EACH_SAFE(pos, tmp, g_sessionKeyList) {
node = (SessionKeyNode *)pos;
if (node->sKey.fd == fd) {
flag = true;
break;
}
}

//注:删除指定的节点
if (flag) {
ListRemoveNode(&(node->head));
free(node);
node = NULL;
}
SOFTBUS_PRINT("[AUTH] ClearSessionKeyByFd(%d) ok\n", fd);
return;
}

/**
* @description: 根据传入的seq遍历g_sessionKeyList找到对应的SessionKeyNode,并将其删除
* @param {long long} seq
*/
static void ClearSessionKeyBySeq(long long seq)
{
if (g_sessionKeyList == NULL) {
return;
}

bool flag = false;
List *pos = NULL;
List *tmp = NULL;
SessionKeyNode *node = NULL;

//注:遍历g_sessionKeyList来找到seq与参数相同的SessionKeyNode
LIST_FOR_EACH_SAFE(pos, tmp, g_sessionKeyList) {
node = (SessionKeyNode *)pos;
if (node->sKey.index == (int)seq) {
flag = true;
break;
}
}

//注:删除指定的节点
if (flag) {
ListRemoveNode(&(node->head));
free(node);
node = NULL;
}

return;
}

/**
* @description: 根据传入的fd与index新建一个SessionKeyNode并插入到g_sessionKeyList尾部
* @param {int} fd
* @param {int} index
* @param {const struct hc_session_key} *session
* @return {int} 创建结果
*/
static int AddNewSessionNode(int fd, int index, const struct hc_session_key *session)
{
SessionKeyNode *node = calloc(1, sizeof(SessionKeyNode));
if (node == NULL) {
return -1;
}

node->sKey.index = index;
node->sKey.fd = fd;
int ret = memcpy_s(node->sKey.key, sizeof(node->sKey.key), session->session_key, session->length);
if (ret != EOK) {
free(node);
node = NULL;
return -1;
}

ListInsertTail(g_sessionKeyList, &node->head);
return 0;
}

/**
* @description: 从g_sessionKeyList中取出第一个SessionKeyNode,并在赋予新值后插入到尾部
* @param {int} fd
* @param {int} index
* @param {const struct hc_session_key} *session
*/
static int ReplaceOldSessionNode(int fd, int index, const struct hc_session_key *session)
{
SessionKeyNode *node = (SessionKeyNode *)ListPopFront(g_sessionKeyList);
if (node == NULL) {
return -1;
}

(void)memset_s(node, sizeof(SessionKeyNode), 0, sizeof(SessionKeyNode));
node->sKey.index = index;
node->sKey.fd = fd;
int ret = memcpy_s(node->sKey.key, sizeof(node->sKey.key), session->session_key, session->length);
if (ret != EOK) {
free(node);
node = NULL;
return -1;
}

ListInsertTail(g_sessionKeyList, &node->head);
return 0;
}

/**
* @description: 依据传入的identity(其session_id对应的AuthSession)与session创建SessionKeyNode并插入到g_sessionKeyList中
* @param {const struct session_identity} *identity
* @param {const struct hc_session_key} *session
* @return {int}
*/
static int AddSessionKey(const struct session_identity *identity, const struct hc_session_key *session)
{
AuthSession *auth = AuthGetAuthSessionBySessionId(identity->session_id);
if (auth == NULL || auth->conn == NULL) {
return -1;
}

if (g_sessionKeyList == NULL) {
g_sessionKeyList = calloc(1, sizeof(List));
if (g_sessionKeyList == NULL) {
return -1;
}
ListInitHead(g_sessionKeyList);
}

int ret;
int index = (int)auth->seqId;
//注:根据g_sessionKeyList的长度来判断是新增还是替代原有的
int num = ListLength(g_sessionKeyList);
if (num >= AUTH_SESSION_KEY_MAX_NUM) {
ret = ReplaceOldSessionNode(auth->conn->fd, index, session);
} else {
ret = AddNewSessionNode(auth->conn->fd, index, session);
}

SOFTBUS_PRINT("[AUTH] AddSessionKey ret = %d\n", ret);
return ret;
}

/**
* @description: 查:根据index从g_sessionKeyList(SessionKeyNode->sKey)中获取对应的SessionKey
* @param {int} index
* @return {SessionKey}
*/
SessionKey *AuthGetSessionKeyByIndex(int index)
{
if (g_sessionKeyList == NULL) {
return NULL;
}

List *pos = NULL;
List *tmp = NULL;
SessionKeyNode *node = NULL;

LIST_FOR_EACH_SAFE(pos, tmp, g_sessionKeyList) {
node = (SessionKeyNode *)pos;
if (node->sKey.index == index) {
return &node->sKey;
}
}

return NULL;
}

/**
* @description: 返回g_sessionKeyList的最后一个SessionKeyNode中的SessionKey
* @return {SessionKey} sKey 会话密钥
*/
SessionKey *AuthGetNewSessionKey(void)
{
if (g_sessionKeyList == NULL) {
return NULL;
}

List *pos = NULL;
List *tmp = NULL;
SessionKeyNode *node = NULL;
SessionKey *sKey = NULL;

//注:for (pos = g_sessionKeyList->next, tmp = pos->next; pos != g_sessionKeyList; pos = tmp, tmp = pos->next) {
LIST_FOR_EACH_SAFE(pos, tmp, g_sessionKeyList) {
node = (SessionKeyNode *)pos;
sKey = &node->sKey;
}

return sKey;
}

/**
* @description: 依据传入的identity(其session_id对应的AuthSession)与session创建SessionKeyNode并插入到g_sessionKeyList中
* @param {const struct session_identity} *identity
* @param {const struct hc_session_key} *session
*/
static void AuthSetSessionKey(const struct session_identity *identity, const struct hc_session_key *session)
{
SOFTBUS_PRINT("[AUTH] AuthSetSessionKey\n");
if (identity == NULL || session == NULL) {
return;
}

if (AddSessionKey(identity, session) != 0) {
SOFTBUS_PRINT("[AUTH] AuthSetSessionKey add key fail\n");
return;
}
}

/**
* @description: 根据sessionId删除g_sessionKeyList中对应的SessionKeyNode
* @param {uint32_t} sessionId
*/
static void AuthFail(uint32_t sessionId)
{
AuthSession *auth = AuthGetAuthSessionBySessionId(sessionId);
if (auth == NULL) {
return;
}

ClearSessionKeyBySeq(auth->seqId);
}

/**
* @description: 取消当前正在初始化链路这一状态,(结束握手)?
*/
static void DestroyHiChain(void)
{
SOFTBUS_PRINT("[AUTH] DestroyHiChain\n");
if (g_hcHandle != NULL) {
destroy(&g_hcHandle);
g_hcHandle = NULL;
}
}

/**
* @description: 回调时,根据result来判断是否删除对应的SessionKeyNode(密钥节点)与AuthSession(会话节点)
* @param {const struct session_identity} *identity
* @param {int32_t} result
* @return {*}
*/
static void AuthSetServiceResult(const struct session_identity *identity, int32_t result)
{
SOFTBUS_PRINT("[AUTH] AuthSetServiceResult result = %d\n", result);
if (identity == NULL) {
return;
}

if (result == END_FAILED) {
AuthFail(identity->session_id);
}

if (result == END_SUCCESS || result == END_FAILED) {
AuthDelAuthSessionBySessionId(identity->session_id);
DestroyHiChain();
}
}

/* Callback interface, not used now */
static int32_t AuthConfirmReceiveRequest(const struct session_identity *identity, int32_t operationCode)
{
(void)identity;
(void)operationCode;
return 0;
}

/**
* @description: 对发现设备,准备进行连接的这一活动进行初始化,(握手)?
* @param {uint32_t} sessionId
* @return {*}
*/
static int AuthInitHiChain(uint32_t sessionId)
{
SOFTBUS_PRINT("[AUTH] AuthInitHiChain begin\n");
struct session_identity serverIdentity = {
sessionId,
{AUTH_DEFAULT_ID_LEN, AUTH_DEFAULT_ID},
{AUTH_DEFAULT_ID_LEN, AUTH_DEFAULT_ID},
0
};

struct hc_call_back hiChainCallback = {
AuthOnTransmit,
AuthGetProtocolParams,
AuthSetSessionKey,
AuthSetServiceResult,
AuthConfirmReceiveRequest
};

g_hcHandle = get_instance(&serverIdentity, HC_ACCESSORY, &hiChainCallback);
if (g_hcHandle == NULL) {
return -1;
}

SOFTBUS_PRINT("[AUTH] AuthInitHiChain ok\n");
return 0;
}

/**
* @description: 接收数据,且在此之前会对该连接进行检查
* @param {uint32_t} sessionId
* @param {const char} *data
* @param {int} dataLen
*/
static void AuthProcessReceivedData(uint32_t sessionId, const char *data, int dataLen)
{
if (g_hcHandle == NULL) {
if (AuthInitHiChain(sessionId) != 0) {
AuthDelAuthSessionBySessionId(sessionId);
return;
}
}

struct uint8_buff request = {(uint8_t *)data, dataLen, dataLen};
if (receive_data(g_hcHandle, &request) != HC_OK) {
return;
}
}

/**
* @description: 对外提供的接收数据的接口
* @param {const AuthConn} *conn
* @param {int} module
* @param {long long} seqId
* @param {const char} *data
* @param {int} dataLen
*/
void AuthInterfaceOnDataReceived(const AuthConn *conn, int module, long long seqId, const char *data, int dataLen)
{
SOFTBUS_PRINT("[AUTH] AuthInterfaceOnDataReceived begin\n");
if (conn == NULL || data == NULL || dataLen > PACKET_DATA_SIZE) {
return;
}
if (AuthSessionMapInit() != 0) {
return;
}

AuthSession *auth = AuthGetAuthSessionBySeqId(seqId);
if (auth == NULL) {
auth = AuthGetNewAuthSession(conn, seqId, g_authSessionId);
if (auth == NULL) {
return;
}
++g_authSessionId;
}

switch (module) {
case MODULE_AUTH_SDK:
AuthProcessReceivedData(auth->sessionId, data, dataLen);
break;
default:
break;
}

return;
}