TSG-13347 统一术语使用session替换stream
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "stream_addr.h"
|
||||
#include "addr_tuple4.h"
|
||||
|
||||
char *stream_addr_to_str(const struct stream_addr *addr)
|
||||
char *addr_tuple4_to_str(const struct addr_tuple4 *addr)
|
||||
{
|
||||
char *str_ret = NULL;
|
||||
|
||||
if (addr->addr_type == STREAM_ADDR_TYPE_V4)
|
||||
if (addr->addr_type == ADDR_TUPLE4_TYPE_V4)
|
||||
{
|
||||
char src_addr[INET_ADDRSTRLEN] = {0};
|
||||
char dst_addr[INET_ADDRSTRLEN] = {0};
|
||||
@@ -18,7 +19,7 @@ char *stream_addr_to_str(const struct stream_addr *addr)
|
||||
asprintf(&str_ret, "%s %u %s %u", src_addr, src_port, dst_addr, dst_port);
|
||||
}
|
||||
|
||||
if (addr->addr_type == STREAM_ADDR_TYPE_V6)
|
||||
if (addr->addr_type == ADDR_TUPLE4_TYPE_V6)
|
||||
{
|
||||
char src_addr[INET6_ADDRSTRLEN] = {0};
|
||||
char dst_addr[INET6_ADDRSTRLEN] = {0};
|
||||
@@ -30,4 +31,27 @@ char *stream_addr_to_str(const struct stream_addr *addr)
|
||||
}
|
||||
|
||||
return str_ret;
|
||||
}
|
||||
|
||||
void addr_tuple4_reverse(const struct addr_tuple4 *orin, struct addr_tuple4 *out)
|
||||
{
|
||||
memset(out, 0, sizeof(struct addr_tuple4));
|
||||
|
||||
if (orin->addr_type == ADDR_TUPLE4_TYPE_V4)
|
||||
{
|
||||
out->addr_type = ADDR_TUPLE4_TYPE_V4;
|
||||
out->addr_v4.src_addr = orin->addr_v4.dst_addr;
|
||||
out->addr_v4.dst_addr = orin->addr_v4.src_addr;
|
||||
out->addr_v4.src_port = orin->addr_v4.dst_port;
|
||||
out->addr_v4.dst_port = orin->addr_v4.src_port;
|
||||
}
|
||||
|
||||
if (orin->addr_type == ADDR_TUPLE4_TYPE_V6)
|
||||
{
|
||||
out->addr_type = ADDR_TUPLE4_TYPE_V6;
|
||||
out->addr_v6.src_addr = orin->addr_v6.dst_addr;
|
||||
out->addr_v6.dst_addr = orin->addr_v6.src_addr;
|
||||
out->addr_v6.src_port = orin->addr_v6.dst_port;
|
||||
out->addr_v6.dst_port = orin->addr_v6.src_port;
|
||||
}
|
||||
}
|
||||
195
common/src/session_table.cpp
Normal file
195
common/src/session_table.cpp
Normal file
@@ -0,0 +1,195 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include "session_table.h"
|
||||
#include "log.h"
|
||||
|
||||
struct session_table
|
||||
{
|
||||
struct session_node *root_by_id;
|
||||
struct session_node *root_by_addr;
|
||||
uint64_t session_node_count;
|
||||
};
|
||||
|
||||
// Note: session_addr must be initialized by memset(0) before use !!!
|
||||
|
||||
struct session_table *session_table_create()
|
||||
{
|
||||
struct session_table *table = (struct session_table *)calloc(1, sizeof(struct session_table));
|
||||
assert(table);
|
||||
table->session_node_count = 0;
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
void session_table_destory(struct session_table *table)
|
||||
{
|
||||
if (table)
|
||||
{
|
||||
struct session_node *temp = NULL;
|
||||
struct session_node *node = NULL;
|
||||
HASH_ITER(hh1, table->root_by_id, node, temp)
|
||||
{
|
||||
HASH_DELETE(hh1, table->root_by_id, node);
|
||||
HASH_DELETE(hh2, table->root_by_addr, node);
|
||||
|
||||
if (node->val_freecb && node->val_data)
|
||||
{
|
||||
node->val_freecb(node->val_data);
|
||||
}
|
||||
|
||||
free(node);
|
||||
node = NULL;
|
||||
}
|
||||
|
||||
free(table);
|
||||
table = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t session_table_count(struct session_table *table)
|
||||
{
|
||||
if (table)
|
||||
{
|
||||
return table->session_node_count;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// session_addr : deep copy
|
||||
// val_data : shallow copy (malloc by user, free by val_freecb)
|
||||
int session_table_insert(struct session_table *table, uint64_t session_id, const struct addr_tuple4 *session_addr, void *val_data, const fn_free_cb *val_freecb)
|
||||
{
|
||||
struct session_node *temp = NULL;
|
||||
HASH_FIND(hh1, table->root_by_id, &session_id, sizeof(session_id), temp);
|
||||
if (temp)
|
||||
{
|
||||
LOG_DEBUG("session table insert: key %lu exists", session_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
temp = (struct session_node *)calloc(1, sizeof(struct session_node));
|
||||
assert(temp);
|
||||
|
||||
temp->session_id = session_id;
|
||||
memcpy(&temp->session_addr, session_addr, sizeof(struct addr_tuple4));
|
||||
temp->val_data = val_data;
|
||||
temp->val_freecb = val_freecb;
|
||||
|
||||
HASH_ADD(hh1, table->root_by_id, session_id, sizeof(temp->session_id), temp);
|
||||
HASH_ADD(hh2, table->root_by_addr, session_addr, sizeof(temp->session_addr), temp);
|
||||
|
||||
LOG_DEBUG("session table insert: key %lu success", session_id);
|
||||
table->session_node_count++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int session_table_delete_by_id(struct session_table *table, uint64_t session_id)
|
||||
{
|
||||
struct session_node *temp = NULL;
|
||||
HASH_FIND(hh1, table->root_by_id, &session_id, sizeof(session_id), temp);
|
||||
if (!temp)
|
||||
{
|
||||
LOG_DEBUG("session table delete: key %lu not exists", session_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
HASH_DELETE(hh1, table->root_by_id, temp);
|
||||
HASH_DELETE(hh2, table->root_by_addr, temp);
|
||||
|
||||
if (temp->val_freecb && temp->val_data)
|
||||
{
|
||||
temp->val_freecb(temp->val_data);
|
||||
temp->val_data = NULL;
|
||||
}
|
||||
|
||||
free(temp);
|
||||
temp = NULL;
|
||||
|
||||
LOG_DEBUG("session table delete: key %lu success", session_id);
|
||||
table->session_node_count--;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int session_table_delete_by_addr(struct session_table *table, const struct addr_tuple4 *session_addr)
|
||||
{
|
||||
struct session_node *temp = NULL;
|
||||
char *addr_str = addr_tuple4_to_str(session_addr);
|
||||
HASH_FIND(hh2, table->root_by_addr, session_addr, sizeof(struct addr_tuple4), temp);
|
||||
if (!temp)
|
||||
{
|
||||
struct addr_tuple4 reverse_addr;
|
||||
addr_tuple4_reverse(session_addr, &reverse_addr);
|
||||
HASH_FIND(hh2, table->root_by_addr, &reverse_addr, sizeof(struct addr_tuple4), temp);
|
||||
if (!temp)
|
||||
{
|
||||
LOG_DEBUG("session table delete: key %s not exists", addr_str);
|
||||
free(addr_str);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
HASH_DELETE(hh1, table->root_by_id, temp);
|
||||
HASH_DELETE(hh2, table->root_by_addr, temp);
|
||||
|
||||
if (temp->val_freecb && temp->val_data)
|
||||
{
|
||||
temp->val_freecb(temp->val_data);
|
||||
temp->val_data = NULL;
|
||||
}
|
||||
|
||||
free(temp);
|
||||
temp = NULL;
|
||||
|
||||
LOG_DEBUG("session table delete: key %s success", addr_str);
|
||||
free(addr_str);
|
||||
addr_str = NULL;
|
||||
table->session_node_count--;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct session_node *session_table_search_by_id(struct session_table *table, uint64_t session_id)
|
||||
{
|
||||
struct session_node *temp = NULL;
|
||||
HASH_FIND(hh1, table->root_by_id, &session_id, sizeof(session_id), temp);
|
||||
if (!temp)
|
||||
{
|
||||
LOG_DEBUG("session table search: key %lu not exists", session_id);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LOG_DEBUG("session table search: key %lu success", session_id);
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
struct session_node *session_table_search_by_addr(struct session_table *table, const struct addr_tuple4 *session_addr)
|
||||
{
|
||||
struct session_node *temp = NULL;
|
||||
char *addr_str = addr_tuple4_to_str(session_addr);
|
||||
HASH_FIND(hh2, table->root_by_addr, session_addr, sizeof(struct addr_tuple4), temp);
|
||||
if (!temp)
|
||||
{
|
||||
struct addr_tuple4 reverse_addr;
|
||||
addr_tuple4_reverse(session_addr, &reverse_addr);
|
||||
HASH_FIND(hh2, table->root_by_addr, &reverse_addr, sizeof(struct addr_tuple4), temp);
|
||||
if (!temp)
|
||||
{
|
||||
LOG_DEBUG("session table search: key %s not exists", addr_str);
|
||||
free(addr_str);
|
||||
addr_str = NULL;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
LOG_DEBUG("session table search: key %s success", addr_str);
|
||||
free(addr_str);
|
||||
addr_str = NULL;
|
||||
|
||||
return temp;
|
||||
}
|
||||
@@ -1,161 +0,0 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include "stream_table.h"
|
||||
#include "log.h"
|
||||
|
||||
struct stream_table
|
||||
{
|
||||
struct stream_node *streamid_root;
|
||||
struct stream_node *streamaddr_root;
|
||||
};
|
||||
|
||||
// Note: key_streamaddr must be initialized by memset(0) before use !!!
|
||||
|
||||
struct stream_table *stream_table_create()
|
||||
{
|
||||
struct stream_table *table = (struct stream_table *)calloc(1, sizeof(struct stream_table));
|
||||
assert(table);
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
void stream_table_destory(struct stream_table *table)
|
||||
{
|
||||
if (table)
|
||||
{
|
||||
struct stream_node *temp = NULL;
|
||||
struct stream_node *node = NULL;
|
||||
HASH_ITER(hh1, table->streamid_root, node, temp)
|
||||
{
|
||||
HASH_DELETE(hh1, table->streamid_root, node);
|
||||
HASH_DELETE(hh2, table->streamaddr_root, node);
|
||||
|
||||
if (node->val_freecb && node->val_data)
|
||||
{
|
||||
node->val_freecb(node->val_data);
|
||||
}
|
||||
|
||||
free(node);
|
||||
node = NULL;
|
||||
}
|
||||
|
||||
free(table);
|
||||
table = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// key_streamaddr : deep copy
|
||||
// val_data : shallow copy (malloc by user, free by val_freecb)
|
||||
int stream_table_insert(struct stream_table *table, uint64_t key_streamid, const struct stream_addr *key_streamaddr, void *val_data, const fn_free_cb *val_freecb)
|
||||
{
|
||||
struct stream_node *temp = NULL;
|
||||
HASH_FIND(hh1, table->streamid_root, &key_streamid, sizeof(key_streamid), temp);
|
||||
if (temp)
|
||||
{
|
||||
LOG_DEBUG("stream table insert: key %lu exists", key_streamid);
|
||||
return -1;
|
||||
}
|
||||
|
||||
temp = (struct stream_node *)calloc(1, sizeof(struct stream_node));
|
||||
assert(temp);
|
||||
|
||||
temp->key_streamid = key_streamid;
|
||||
memcpy(&temp->key_streamaddr, key_streamaddr, sizeof(struct stream_addr));
|
||||
temp->val_data = val_data;
|
||||
temp->val_freecb = val_freecb;
|
||||
|
||||
HASH_ADD(hh1, table->streamid_root, key_streamid, sizeof(temp->key_streamid), temp);
|
||||
HASH_ADD(hh2, table->streamaddr_root, key_streamaddr, sizeof(temp->key_streamaddr), temp);
|
||||
|
||||
LOG_DEBUG("stream table insert: key %lu success", key_streamid);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stream_table_delete_by_streamid(struct stream_table *table, uint64_t key_streamid)
|
||||
{
|
||||
struct stream_node *temp = NULL;
|
||||
HASH_FIND(hh1, table->streamid_root, &key_streamid, sizeof(key_streamid), temp);
|
||||
if (!temp)
|
||||
{
|
||||
LOG_DEBUG("stream table delete: key %lu not exists", key_streamid);
|
||||
return -1;
|
||||
}
|
||||
|
||||
HASH_DELETE(hh1, table->streamid_root, temp);
|
||||
HASH_DELETE(hh2, table->streamaddr_root, temp);
|
||||
|
||||
if (temp->val_freecb && temp->val_data)
|
||||
{
|
||||
temp->val_freecb(temp->val_data);
|
||||
}
|
||||
|
||||
free(temp);
|
||||
temp = NULL;
|
||||
|
||||
LOG_DEBUG("stream table delete: key %lu success", key_streamid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stream_table_delete_by_streamaddr(struct stream_table *table, const struct stream_addr *key_streamaddr)
|
||||
{
|
||||
struct stream_node *temp = NULL;
|
||||
char *addr_str = stream_addr_to_str(key_streamaddr);
|
||||
HASH_FIND(hh2, table->streamaddr_root, key_streamaddr, sizeof(*key_streamaddr), temp);
|
||||
if (!temp)
|
||||
{
|
||||
LOG_DEBUG("stream table delete: key %s not exists", addr_str);
|
||||
free(addr_str);
|
||||
return -1;
|
||||
}
|
||||
|
||||
HASH_DELETE(hh1, table->streamid_root, temp);
|
||||
HASH_DELETE(hh2, table->streamaddr_root, temp);
|
||||
|
||||
if (temp->val_freecb && temp->val_data)
|
||||
{
|
||||
temp->val_freecb(temp->val_data);
|
||||
}
|
||||
|
||||
free(temp);
|
||||
temp = NULL;
|
||||
|
||||
LOG_DEBUG("stream table delete: key %s success", addr_str);
|
||||
free(addr_str);
|
||||
addr_str = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct stream_node *stream_table_search_by_streamid(struct stream_table *table, uint64_t key_streamid)
|
||||
{
|
||||
struct stream_node *temp = NULL;
|
||||
HASH_FIND(hh1, table->streamid_root, &key_streamid, sizeof(key_streamid), temp);
|
||||
if (!temp)
|
||||
{
|
||||
LOG_DEBUG("stream table search: key %lu not exists", key_streamid);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LOG_DEBUG("stream table search: key %lu success", key_streamid);
|
||||
return temp;
|
||||
}
|
||||
|
||||
struct stream_node *stream_table_search_by_streamaddr(struct stream_table *table, const struct stream_addr *key_streamaddr)
|
||||
{
|
||||
struct stream_node *temp = NULL;
|
||||
char *addr_str = stream_addr_to_str(key_streamaddr);
|
||||
HASH_FIND(hh2, table->streamaddr_root, key_streamaddr, sizeof(*key_streamaddr), temp);
|
||||
if (!temp)
|
||||
{
|
||||
LOG_DEBUG("stream table search: key %s not exists", addr_str);
|
||||
free(addr_str);
|
||||
addr_str = NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LOG_DEBUG("stream table search: key %s success", addr_str);
|
||||
free(addr_str);
|
||||
addr_str = NULL;
|
||||
return temp;
|
||||
}
|
||||
Reference in New Issue
Block a user