本文将继续介绍HiChain本端接收数据的处理过程中的消息处理阶段的剩余内容。
这一模块的源码位于:/base/security/deviceauth。
/*
函数功能:构建HiChain子对象
函数参数:
hichain:HiChain实例
modular:消息模块类型
is_client:是否是client
params:构建参数
函数返回值:
成功:返回0 HC_OK
失败:返回错误码
详细:
*/
int32_t build_object(struct hichain *hichain, int32_t modular, bool is_client, const void *params)
{
//初始化HC对象表map
const struct object_map map[] = { { PAKE_MODULAR, true, (void **)&hichain->pake_client },//pake客户端
{ PAKE_MODULAR, false, (void **)&hichain->pake_server },//pake服务端
{ STS_MODULAR, true, (void **)&hichain->sts_client },//sts客户端
{ STS_MODULAR, false, (void **)&hichain->sts_server },//sts服务端
{ ADD_MODULAR, true, (void **)&hichain->auth_info },//认证信息
{ REMOVE_MODULAR, true, (void **)&hichain->auth_info },//认证信息
{ SEC_CLONE_MODULAR, false, (void **)&hichain->sec_clone_server } };//安全克隆服务端
void **object = get_object(map, sizeof(map) / sizeof(map[0]), modular, is_client);//根据消息模块类型获取HC对象
if ((object == NULL) || (*object != NULL)) {//获取失败
DBG_OUT("No sub-objects need to be applied for");
return HC_OK;
}
if (check_mutex_object_is_null(map, sizeof(map) / sizeof(map[0]), modular, is_client) == false) {//检查互斥对象是否为空???
LOGE("The mutex sub-object have been created, create %d:%d sub-object failed", modular, is_client);
return HC_REPEATED_REFERENCE;
}
if (check_depend_object_is_not_null(map, sizeof(map) / sizeof(map[0]), modular, is_client) == false) {//检查依赖对象是否为非空???
LOGE("The depend sub-object is not created, create %d:%d sub-object failed", modular, is_client);
return HC_NEED_DEPEND;
}
*object = build_object_by_modular(hichain, modular, is_client, params);//根据消息模块类型构建子对象
if (*object == NULL) {
LOGE("Create %d:%d sub-object failed", modular, is_client);
return HC_BUILD_OBJECT_FAILED;//构建失败
}
DBG_OUT("Create %d:%d sub-object success", modular, is_client);
return HC_OK;
}
//根据消息模块类型获取对象
static void **get_object(const struct object_map *map, uint32_t n, int32_t modular, bool is_client)
{
for (uint32_t i = 0; i < n; i++) {//遍历对象表,查询指定对象类型
if ((modular == map[i].modular) && (is_client == map[i].is_client)) {
return map[i].object;
}
}
return NULL;
}
//检查互斥对象是否为空,若为空返回true,否则返回false
static bool check_mutex_object_is_null(const struct object_map *map, uint32_t n, int32_t modular, bool is_client)
{
//初始化对象关系互斥表
const struct object_relation mutex_map[] = { { PAKE_MODULAR, STS_MODULAR, true, false },
{ STS_MODULAR, PAKE_MODULAR, false, true },
{ PAKE_MODULAR, STS_MODULAR, true, true },
{ STS_MODULAR, PAKE_MODULAR, true, true },
{ PAKE_MODULAR, STS_MODULAR, false, false },
{ STS_MODULAR, PAKE_MODULAR, false, false },
{ PAKE_MODULAR, STS_MODULAR, false, true },
{ STS_MODULAR, PAKE_MODULAR, true, false },
{ STS_MODULAR, STS_MODULAR, true, false },
{ STS_MODULAR, STS_MODULAR, false, true },
{ ADD_MODULAR, STS_MODULAR, true, false },
{ STS_MODULAR, ADD_MODULAR, false, true },
{ REMOVE_MODULAR, STS_MODULAR, true, false },
{ STS_MODULAR, REMOVE_MODULAR, false, true },
{ PAKE_MODULAR, SEC_CLONE_MODULAR, false, false },
{ SEC_CLONE_MODULAR, PAKE_MODULAR, false, false } };
object_relation_ptr select_map[sizeof(mutex_map) / sizeof(mutex_map[0])] = {0};//定义并初始化一个select_map
uint32_t count = select_relation_map(mutex_map, sizeof(mutex_map) / sizeof(mutex_map[0]), modular,
is_client, select_map);//根据modular和is_client从关系互斥表中查找对应的项并赋值给select_map,用count记录数量并返回
if (count == 0) { /* no muutex sub object 无互斥子对象*/
return true;
}
for (uint32_t i = 0; i < n; i++) {
if ((map[i].modular == modular) && (map[i].is_client == is_client)) { /* skip sub object that will be created 跳过即将被创建的子对象*/
continue;
}
if (*map[i].object == NULL) { /* null sub object is correct even mutex */
continue;
}
for (uint32_t j = 0; j < count; j++) {
if ((map[i].modular == select_map[j]->dst_modular) && (map[i].is_client == select_map[j]->dst_is_client)) {
return false; /* mutex sub object and not null 互斥子对象为非空*/
}
}
}
return true;
}
/*
函数功能:检查依赖对象是否为非空
函数参数:
map:对象表
n:对象个数
modular:消息模块
is_client:是否是客户端
函数返回值:
*/
static bool check_depend_object_is_not_null(const struct object_map *map, uint32_t n, int32_t modular, bool is_client)
{
const struct object_relation depend_map[] = { { ADD_MODULAR, STS_MODULAR, true, true },
{ REMOVE_MODULAR, STS_MODULAR, true, true },
{ SEC_CLONE_MODULAR, STS_MODULAR, false, false } };//依赖对象关系表
object_relation_ptr select_map[sizeof(depend_map) / sizeof(depend_map[0])] = {0};
uint32_t count = select_relation_map(depend_map, sizeof(depend_map) / sizeof(depend_map[0]),
modular, is_client, select_map);
if (count == 0) { /* no dependent sub object 无依赖子对象*/
return true;
}
for (uint32_t i = 0; i < n; i++) {
if ((map[i].modular == modular) && (map[i].is_client == is_client)) { /* skip sub object that will be created 跳过即将被创建的子对象*/
continue;
}
if (*map[i].object != NULL) { /* null sub object is correct even dependent */
continue;
}
for (uint32_t j = 0; j < count; j++) {
if ((map[i].modular == select_map[j]->dst_modular) && (map[i].is_client == select_map[j]->dst_is_client)) {
return false; /* depentend sub object and not null 依赖子对象为非空*/
}
}
}
return true;
}
//根据modular和is_client从关系表中查找对应的项并赋值给select_map,用count记录数量并返回
static uint32_t select_relation_map(const struct object_relation *map, uint32_t n, int32_t modular, bool is_client,
object_relation_ptr *select_map)
{
uint32_t count = 0;
for (uint32_t i = 0; i < n; i++) {
if ((modular == map[i].src_modular) && (is_client == map[i].src_is_client)) {
select_map[count] = &map[i];
count++;//记录数量
}
}
return count;
}
//根据消息模块类型构建子对象
static void *build_object_by_modular(struct hichain *hichain, int32_t modular, bool is_client, const void *params)
{
//初始化子对象表
const struct build_sub_object_map map[] = { { PAKE_MODULAR, true, build_pake_client_object },//构建pake客户端对象
{ PAKE_MODULAR, false, build_pake_server_object },//构建pake服务端对象
{ STS_MODULAR, true, build_sts_client_object },//构建sts客户端对象
{ STS_MODULAR, false, build_sts_server_object },//构建sts服务端对象
{ ADD_MODULAR, true, build_auth_info_client_object },//构建认证信息客户端对象
{ REMOVE_MODULAR, true, build_auth_info_client_object },//构建认证信息客户端对象
{ SEC_CLONE_MODULAR, false, build_sec_clone_server_object } };//构建安全克隆服务端对象
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i++) {//遍历子对象构建表,根据modular及is_client查询对应子对象的回调函数
if ((map[i].modular == modular) && (map[i].is_client == is_client)) {
return map[i].build_func(hichain, params);//执行对应回调函数构建子对象!!!
}
}
return NULL;
}
/*
函数功能:根据modular和is_request_msg查询全局分布式消息表,找到对应的消息处理函数并执行
函数参数:
handle:HiChain处理实例
nav:导航消息头
receive:接收到的解析后的消息
send:传出参数,保存处理后的回复消息
函数返回值:
*/
int32_t proc_message(struct hichain *handle, struct header_analysis *nav,
struct message *receive, struct message *send)
{
for (uint32_t i = 0; i < sizeof(G_DISTRIBUTION_MESSAGE) / sizeof(G_DISTRIBUTION_MESSAGE[0]); i++) {//遍历全局分布式消息表
if ((nav->modular == G_DISTRIBUTION_MESSAGE[i].modular) &&
(nav->is_request_msg == G_DISTRIBUTION_MESSAGE[i].is_request_msg)) {
int32_t ret = G_DISTRIBUTION_MESSAGE[i].func(handle, nav, receive, send);//处理消息
LOGI("Proc_message return code is %d", ret);
return ret;
}
}
return HC_UNKNOW_MESSAGE;
}
/*
函数功能:连接消息
函数参数:
handle:hichain实例对象
nav:导航消息
send:发送消息
函数返回值:
成功:返回0
失败:返回错误码
*/
int32_t connect_message(struct hichain *handle, struct header_analysis *nav, struct message *send)
{
if (nav->modular != STS_MODULAR && nav->modular != PAKE_MODULAR) {//如果消息既不属于pake协议也不属于sts协议,则直接返回
return HC_OK;
}
if (nav->msg_type != STS_END_MSG) {//如果消息类型不是sts结束消息,则直接返回
return HC_OK;
}
if (nav->is_request_msg) { /* server does not need message connection 服务端不需要消息连接*/
return HC_OK;
}
int32_t ret;
switch (handle->operation_code) {//根据操作码进行不同的处理
case BIND:
ret = send_exchange_request(handle, send);//发送交换信息请求
LOGI("Client build exchange request message return value is %d", ret);
return ret;
case AUTHENTICATE:
send->msg_code = INVALID_MESSAGE;
return HC_OK;
case ADD_AUTHINFO:
ret = send_add_request(handle, send);//发送添加认证信息请求
LOGI("Client build add auth info request message return value is %d", ret);
return ret;
case REMOVE_AUTHINFO:
ret = send_remove_request(handle, send);//发送移除请求
LOGI("Client build remove auth info request message return value is %d", ret);
return ret;
default:
return HC_INNER_ERROR;
}
return HC_OK;
}
至此,HiChain本端接收数据的处理过程结束,至于具体的不同协议的处理过程及响应过程,将在接下来的博客中进行介绍。