rename unused session inner exdata
This commit is contained in:
@@ -1046,7 +1046,6 @@ void session_manager_free_session(struct session_manager *mgr, struct session *s
|
||||
break;
|
||||
}
|
||||
|
||||
session_free_all_ex_data(sess);
|
||||
packet_free((struct packet *)session_get_first_packet(sess, FLOW_DIRECTION_C2S));
|
||||
packet_free((struct packet *)session_get_first_packet(sess, FLOW_DIRECTION_S2C));
|
||||
session_set_first_packet(sess, FLOW_DIRECTION_C2S, NULL);
|
||||
|
||||
@@ -4,23 +4,6 @@
|
||||
#include "session_utils.h"
|
||||
#include "session_manager.h"
|
||||
|
||||
#define EX_KEY_MAX_LEN 64
|
||||
|
||||
struct session_exdata_schema
|
||||
{
|
||||
char key[EX_KEY_MAX_LEN];
|
||||
session_ex_free_cb *free_cb;
|
||||
void *args;
|
||||
};
|
||||
|
||||
struct session_exdata_manager
|
||||
{
|
||||
struct session_exdata_schema schemas[EX_DATA_MAX_COUNT];
|
||||
uint8_t count;
|
||||
};
|
||||
|
||||
static struct session_exdata_manager g_ex_manager = {};
|
||||
|
||||
/******************************************************************************
|
||||
* session set/get
|
||||
******************************************************************************/
|
||||
@@ -274,108 +257,6 @@ void session_free_tcp_segment(struct session *sess, struct tcp_segment *seg)
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* session ex data
|
||||
******************************************************************************/
|
||||
|
||||
/*
|
||||
* the exdata prodoced by user, and comsumed by same user.
|
||||
* so, the exdata is not shared by different user.
|
||||
* otherwise, the exdata need dup by refer count, and free by refer count.
|
||||
*
|
||||
* if key exist, not allow update, return original index.
|
||||
*/
|
||||
uint8_t session_get_ex_new_index(const char *key, session_ex_free_cb *free_cb, void *args)
|
||||
{
|
||||
if (g_ex_manager.count >= EX_DATA_MAX_COUNT)
|
||||
{
|
||||
abort();
|
||||
return EX_DATA_MAX_COUNT;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < g_ex_manager.count; i++)
|
||||
{
|
||||
if (strcmp(g_ex_manager.schemas[i].key, key) == 0)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t idx = g_ex_manager.count;
|
||||
g_ex_manager.count++;
|
||||
|
||||
struct session_exdata_schema *schema = &g_ex_manager.schemas[idx];
|
||||
strncpy(schema->key, key, EX_KEY_MAX_LEN);
|
||||
schema->free_cb = free_cb;
|
||||
schema->args = args;
|
||||
|
||||
return idx;
|
||||
}
|
||||
|
||||
/*
|
||||
* Support update ex_data.
|
||||
*
|
||||
* if key exist: run free_cb free old value, then set new value.
|
||||
* if not run free_cb, old value will be memory leak.
|
||||
* if not allow update, new value will be memory leak.
|
||||
* if key not exist: set new value.
|
||||
*/
|
||||
void session_set_ex_data(struct session *sess, uint8_t idx, void *val)
|
||||
{
|
||||
if (idx >= g_ex_manager.count)
|
||||
{
|
||||
assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
session_free_ex_data(sess, idx);
|
||||
sess->ex_data[idx] = val;
|
||||
}
|
||||
|
||||
void *session_get0_ex_data(const struct session *sess, uint8_t idx)
|
||||
{
|
||||
if (idx >= g_ex_manager.count)
|
||||
{
|
||||
assert(0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return sess->ex_data[idx];
|
||||
}
|
||||
|
||||
/*
|
||||
* after set ex_data, the owner of ex_data is session, so user should not free it directly.
|
||||
* if user want to free ex_data, should use session_free_ex_data.
|
||||
*/
|
||||
void session_free_ex_data(struct session *sess, uint8_t idx)
|
||||
{
|
||||
if (idx >= g_ex_manager.count)
|
||||
{
|
||||
assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
struct session_exdata_schema *schema = &g_ex_manager.schemas[idx];
|
||||
if (schema->free_cb != NULL && sess->ex_data[idx] != NULL)
|
||||
{
|
||||
printf("free ex_data, idx: %d, key: %s, val: %p\n", idx, schema->key, sess->ex_data[idx]);
|
||||
schema->free_cb(sess, idx, sess->ex_data[idx], schema->args);
|
||||
}
|
||||
|
||||
sess->ex_data[idx] = NULL;
|
||||
}
|
||||
|
||||
void session_free_all_ex_data(struct session *sess)
|
||||
{
|
||||
if (sess)
|
||||
{
|
||||
for (uint8_t i = 0; i < g_ex_manager.count; i++)
|
||||
{
|
||||
session_free_ex_data(sess, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* to string
|
||||
******************************************************************************/
|
||||
|
||||
@@ -88,38 +88,6 @@ void *session_get_user_data(const struct session *sess);
|
||||
struct tcp_segment *session_get_tcp_segment(struct session *sess);
|
||||
void session_free_tcp_segment(struct session *sess, struct tcp_segment *seg);
|
||||
|
||||
/******************************************************************************
|
||||
* session ex data
|
||||
******************************************************************************/
|
||||
|
||||
typedef void session_ex_free_cb(struct session *sess, uint8_t idx, void *ex_ptr, void *arg);
|
||||
|
||||
/*
|
||||
* the exdata prodoced by user, and comsumed by same user.
|
||||
* so, the exdata is not shared by different user.
|
||||
* otherwise, the exdata need dup by refer count, and free by refer count.
|
||||
*
|
||||
* if key exist, not allow update, return original index.
|
||||
*/
|
||||
uint8_t session_get_ex_new_index(const char *key, session_ex_free_cb *free_cb, void *args);
|
||||
|
||||
/*
|
||||
* Support update ex_data.
|
||||
*
|
||||
* if key exist: run free_cb free old value, then set new value.
|
||||
* if not run free_cb, old value will be memory leak.
|
||||
* if not allow update, new value will be memory leak.
|
||||
* if key not exist: set new value.
|
||||
*/
|
||||
void session_set_ex_data(struct session *sess, uint8_t idx, void *val);
|
||||
void *session_get0_ex_data(const struct session *sess, uint8_t idx);
|
||||
/*
|
||||
* after set ex_data, the owner of ex_data is session, so user should not free it directly.
|
||||
* if user want to free ex_data, should use session_free_ex_data.
|
||||
*/
|
||||
void session_free_ex_data(struct session *sess, uint8_t idx);
|
||||
void session_free_all_ex_data(struct session *sess);
|
||||
|
||||
/******************************************************************************
|
||||
* to string
|
||||
******************************************************************************/
|
||||
|
||||
@@ -2,9 +2,6 @@
|
||||
# gtest utils
|
||||
###############################################################################
|
||||
|
||||
add_executable(gtest_session gtest_session.cpp)
|
||||
target_link_libraries(gtest_session session_manager gtest)
|
||||
|
||||
add_executable(gtest_session_pool gtest_session_pool.cpp)
|
||||
target_link_libraries(gtest_session_pool session_manager gtest)
|
||||
|
||||
@@ -116,7 +113,6 @@ target_link_libraries(gtest_case_tcp_fast_open session_manager gtest)
|
||||
###############################################################################
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(gtest_session)
|
||||
gtest_discover_tests(gtest_session_pool)
|
||||
gtest_discover_tests(gtest_session_table)
|
||||
gtest_discover_tests(gtest_session_timer)
|
||||
|
||||
@@ -1,93 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "session_private.h"
|
||||
#include "session_utils.h"
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
|
||||
#define SESSION_KEY_IPV4_TCP(name) \
|
||||
struct tuple6 name; \
|
||||
memset(&name, 0, sizeof(struct tuple6)); \
|
||||
(name).addr_family = AF_INET; \
|
||||
(name).src_addr.v4.s_addr = inet_addr("192.168.1.2"); \
|
||||
(name).dst_addr.v4.s_addr = inet_addr("192.168.1.3"); \
|
||||
(name).src_port = htons(1234); \
|
||||
(name).dst_port = htons(5678); \
|
||||
(name).ip_proto = IPPROTO_TCP; \
|
||||
(name).domain = 0;
|
||||
|
||||
#define SESSION_KEY_IPV6_UDP(name) \
|
||||
struct tuple6 name; \
|
||||
memset(&name, 0, sizeof(struct tuple6)); \
|
||||
(name).addr_family = AF_INET6; \
|
||||
inet_pton(AF_INET6, "2001:db8:0:0:0:ff00:42:8329", &(name).src_addr.v6); \
|
||||
inet_pton(AF_INET6, "2001:db8:0:0:0:ff00:42:832a", &(name).dst_addr.v6); \
|
||||
(name).src_port = htons(1234); \
|
||||
(name).dst_port = htons(5678); \
|
||||
(name).ip_proto = IPPROTO_UDP; \
|
||||
(name).domain = 0;
|
||||
|
||||
#define SESSION_KEY_IPV6_TCP(name) \
|
||||
struct tuple6 name; \
|
||||
memset(&name, 0, sizeof(struct tuple6)); \
|
||||
(name).addr_family = AF_INET6; \
|
||||
inet_pton(AF_INET6, "2001:db8:0:0:0:ff00:42:8329", &(name).src_addr.v6); \
|
||||
inet_pton(AF_INET6, "2001:db8:0:0:0:ff00:42:832a", &(name).dst_addr.v6); \
|
||||
(name).src_port = htons(1234); \
|
||||
(name).dst_port = htons(5678); \
|
||||
(name).ip_proto = IPPROTO_TCP; \
|
||||
(name).domain = 0;
|
||||
|
||||
void free_ex_data(struct session *sess, uint8_t idx, void *ex_ptr, void *arg)
|
||||
{
|
||||
if (ex_ptr)
|
||||
{
|
||||
printf("free_ex_data: %s\n", (char *)ex_ptr);
|
||||
free(ex_ptr);
|
||||
ex_ptr = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(SESSION, EX_NEW_INDEX)
|
||||
{
|
||||
uint8_t idx1 = session_get_ex_new_index("key1", NULL, NULL);
|
||||
uint8_t idx2 = session_get_ex_new_index("key2", NULL, NULL);
|
||||
uint8_t idx3 = session_get_ex_new_index("key1", NULL, NULL);
|
||||
EXPECT_TRUE(idx1 != idx2);
|
||||
EXPECT_TRUE(idx1 == idx3);
|
||||
}
|
||||
|
||||
TEST(SESSION, EX_SET_GET)
|
||||
{
|
||||
struct session sess;
|
||||
session_init(&sess);
|
||||
uint8_t idx = session_get_ex_new_index("ex_set_get", NULL, NULL);
|
||||
session_set_ex_data(&sess, idx, (void *)0x1234);
|
||||
EXPECT_TRUE(session_get0_ex_data(&sess, idx) == (void *)0x1234);
|
||||
}
|
||||
|
||||
TEST(SESSION, EX_FREE_BY_RESET)
|
||||
{
|
||||
struct session sess;
|
||||
session_init(&sess);
|
||||
uint8_t idx = session_get_ex_new_index("ex_free_by_reset", free_ex_data, NULL);
|
||||
char *ptr = strdup("hello");
|
||||
session_set_ex_data(&sess, idx, ptr);
|
||||
session_set_ex_data(&sess, idx, NULL);
|
||||
}
|
||||
|
||||
TEST(SESSION, EX_FREE_BY_CB)
|
||||
{
|
||||
struct session sess;
|
||||
session_init(&sess);
|
||||
uint8_t idx = session_get_ex_new_index("ex_free_by_cb", free_ex_data, NULL);
|
||||
char *ptr = strdup("hello");
|
||||
session_set_ex_data(&sess, idx, ptr);
|
||||
session_free_ex_data(&sess, idx);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in New Issue
Block a user