TSG-14649 tsg-service-chaining-engine适配新的控制报文格式

This commit is contained in:
luwenpeng
2023-04-28 15:00:46 +08:00
parent d1b04d50b8
commit a2d0f40f76
26 changed files with 16188 additions and 164 deletions

View File

@@ -1,8 +1,10 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <cjson/cJSON.h>
#include "log.h"
#include "mpack.h"
#include "utils.h"
#include "ctrl_packet.h"
@@ -30,7 +32,151 @@ void ctrl_packet_parser_init(struct ctrl_pkt_parser *handler)
// return 0 : success
// return -1 : error
int ctrl_packet_parser_parse(struct ctrl_pkt_parser *handler, const char *data, size_t length)
int ctrl_packet_parser_mpack(struct ctrl_pkt_parser *handler, const char *data, size_t length)
{
mpack_tree_t tree;
mpack_node_t root;
mpack_node_t temp;
mpack_node_t item;
mpack_error_t ret;
char buffer[16];
mpack_tree_init_data(&tree, data, length);
mpack_tree_parse(&tree);
root = mpack_tree_root(&tree);
if (mpack_node_is_nil(root))
{
LOG_ERROR("%s: unexpected control packet: (invalid mpack format)", LOG_TAG_CTRLPKT);
goto error_out;
}
// tsync
temp = mpack_node_map_cstr(root, "tsync");
if (mpack_node_is_nil(temp))
{
LOG_ERROR("%s: unexpected control packet: (tsync no found)", LOG_TAG_CTRLPKT);
goto error_out;
}
mpack_node_copy_cstr(temp, handler->tsync, sizeof(handler->tsync));
if (strcasecmp(handler->tsync, "2.0") != 0)
{
LOG_ERROR("%s: unexpected control packet: (invalid tsync value) %s", LOG_TAG_CTRLPKT, handler->tsync);
goto error_out;
}
// session_id
temp = mpack_node_map_cstr(root, "session_id");
if (mpack_node_is_nil(temp))
{
LOG_ERROR("%s: unexpected control packet: (session_id no found)", LOG_TAG_CTRLPKT);
goto error_out;
}
handler->session_id = mpack_node_u64(temp);
if (handler->session_id == 0)
{
LOG_ERROR("%s: unexpected control packet: (invalid session_id value) %lu", LOG_TAG_CTRLPKT, handler->session_id);
goto error_out;
}
// state
temp = mpack_node_map_cstr(root, "state");
if (mpack_node_is_nil(temp))
{
LOG_ERROR("%s: unexpected control packet: (state no found)", LOG_TAG_CTRLPKT);
goto error_out;
}
mpack_node_copy_cstr(temp, buffer, sizeof(buffer));
if (strcasecmp(buffer, "opening") == 0)
{
handler->state = SESSION_STATE_OPENING;
}
else if (strcasecmp(buffer, "active") == 0)
{
handler->state = SESSION_STATE_ACTIVE;
}
else if (strcasecmp(buffer, "closing") == 0)
{
handler->state = SESSION_STATE_CLOSING;
}
else if (strcasecmp(buffer, "resetall") == 0)
{
handler->state = SESSION_STATE_RESETALL;
}
else
{
LOG_ERROR("%s: unexpected control packet: (invalid state value) %s", LOG_TAG_CTRLPKT, buffer);
goto error_out;
}
if (handler->state != SESSION_STATE_ACTIVE)
{
goto success_out;
}
// method
temp = mpack_node_map_cstr(root, "method");
if (mpack_node_is_nil(temp))
{
LOG_ERROR("%s: unexpected control packet: (method no found)", LOG_TAG_CTRLPKT);
goto error_out;
}
mpack_node_copy_cstr(temp, handler->method, sizeof(handler->method));
if (strcasecmp(handler->method, "policy_update") != 0)
{
LOG_ERROR("%s: unexpected control packet: (invalid method value) %s", LOG_TAG_CTRLPKT, handler->method);
goto error_out;
}
// params
temp = mpack_node_map_cstr(root, "params");
if (mpack_node_is_nil(temp))
{
LOG_ERROR("%s: unexpected control packet: (params no found)", LOG_TAG_CTRLPKT);
goto error_out;
}
// params->sce
temp = mpack_node_map_cstr(temp, "sce");
if (mpack_node_is_nil(temp))
{
LOG_ERROR("%s: unexpected control packet: (sce no found)", LOG_TAG_CTRLPKT);
goto error_out;
}
// params->sce->rule_ids
temp = mpack_node_map_cstr(temp, "rule_ids");
if (mpack_node_is_nil(temp))
{
LOG_ERROR("%s: unexpected control packet: (rule_ids no found)", LOG_TAG_CTRLPKT);
goto error_out;
}
handler->rule_id_num = MIN(mpack_node_array_length(temp), (int)(sizeof(handler->rule_ids) / sizeof(handler->rule_ids[0])));
if (handler->rule_id_num <= 0)
{
LOG_ERROR("%s: unexpected control packet: (invalid rule id num) %ld", LOG_TAG_CTRLPKT, mpack_node_array_length(temp));
goto error_out;
}
for (int i = 0; i < handler->rule_id_num; i++)
{
item = mpack_node_array_at(temp, i);
handler->rule_ids[i] = mpack_node_u64(item);
}
success_out:
ret = mpack_tree_destroy(&tree);
if (ret != mpack_ok)
{
LOG_ERROR("%s: unexpected control packet: (mpack return error) %d", LOG_TAG_CTRLPKT, ret);
return -1;
}
return 0;
error_out:
mpack_tree_destroy(&tree);
return -1;
}
// return 0 : success
// return -1 : error
int ctrl_packet_parser_cjson(struct ctrl_pkt_parser *handler, const char *data, size_t length)
{
int iter = 0;
cJSON *item = NULL;
@@ -152,6 +298,14 @@ error_out:
return -1;
}
// return 0 : success
// return -1 : error
int ctrl_packet_parser_parse(struct ctrl_pkt_parser *handler, const char *data, size_t length)
{
// return ctrl_packet_parser_cjson(handler, data, length);
return ctrl_packet_parser_mpack(handler, data, length);
}
void ctrl_packet_parser_dump(struct ctrl_pkt_parser *handler)
{
if (handler)
@@ -164,7 +318,7 @@ void ctrl_packet_parser_dump(struct ctrl_pkt_parser *handler)
for (int i = 0; i < handler->rule_id_num; i++)
{
LOG_INFO("%s: rule_ids[%03d] : %lu", LOG_TAG_POLICY, i, handler->rule_ids[i]);
LOG_INFO("%s: rule_ids[%03d] : %lu", LOG_TAG_POLICY, i, handler->rule_ids[i]);
}
}
}