From f0ced3f26a9bca837b63c4e3cddafe6d3c4b6675 Mon Sep 17 00:00:00 2001 From: Felix Jia Date: Wed, 10 Jan 2018 15:22:12 +1300 Subject: [PATCH] Allow to lookup and create flows. --- ginetflow.c | 32 ++++++++++++++++++++++++++++++++ ginetflow.h | 2 ++ 2 files changed, 34 insertions(+) diff --git a/ginetflow.c b/ginetflow.c index f197add..b7a4f07 100644 --- a/ginetflow.c +++ b/ginetflow.c @@ -1049,6 +1049,30 @@ GInetFlow *g_inet_flow_get_full(GInetFlowTable * table, return flow; } +GInetFlow *g_inet_flow_create(GInetFlowTable * table, GInetTuple *tuple) +{ + GInetFlow *flow; + + /* Check if max table size is reached */ + if (table->max > 0 && g_hash_table_size(table->table) >= table->max) { + return NULL; + } + + flow = (GInetFlow *) g_object_new(G_INET_TYPE_FLOW, NULL); + flow->table = table; + flow->list.data = flow; + /* Set default lifetime before processing further */ + flow->lifetime = G_INET_FLOW_DEFAULT_NEW_TIMEOUT; + flow->family = ((struct sockaddr *) &(tuple->src))->sa_family; + flow->hash = g_inet_tuple_hash(tuple); + flow->tuple = *tuple; + g_hash_table_replace(table->table, (gpointer) flow, (gpointer) flow); + flow->timestamp = get_time_us(); + insert_flow_by_expiry(table, flow, flow->lifetime); + + return flow; +} + static void g_inet_flow_table_finalize(GObject * object) { int i; @@ -1152,3 +1176,11 @@ GInetTuple *g_inet_flow_parse(const guint8 * frame, guint length, GList ** fragm flow_parse(result, frame, length, 0, fragments, NULL, 0, NULL); return result; } + +GInetFlow *g_inet_flow_lookup (GInetFlowTable * table, GInetTuple *tuple) +{ + GInetFlow packet; + + packet.tuple = *tuple; + return (GInetFlow *) g_hash_table_lookup(table->table, &packet); +} diff --git a/ginetflow.h b/ginetflow.h index 2180001..bb42a24 100644 --- a/ginetflow.h +++ b/ginetflow.h @@ -50,6 +50,7 @@ GInetFlow *g_inet_flow_get(GInetFlowTable * table, const guint8 * frame, guint l GInetFlow *g_inet_flow_get_full(GInetFlowTable * table, const guint8 * frame, guint length, guint16 hash, guint64 timestamp, gboolean update, gboolean l2, const uint8_t ** iphr); +GInetFlow *g_inet_flow_create(GInetFlowTable * table, GInetTuple *tuple); GInetFlow *g_inet_flow_expire(GInetFlowTable * table, guint64 ts); /* g_inet_flow_parse will populate result if result is not null, otherwise it will malloc a structure @@ -60,6 +61,7 @@ GInetTuple *g_inet_flow_parse(const guint8 * frame, guint length, GList ** fragm typedef void (*GIFFunc) (GInetFlow * flow, gpointer user_data); void g_inet_flow_foreach(GInetFlowTable * table, GIFFunc func, gpointer user_data); void g_inet_flow_table_max_set(GInetFlowTable * table, guint64 value); +GInetFlow *g_inet_flow_lookup(GInetFlowTable * table, GInetTuple *tuple); G_END_DECLS #endif /* __G_INET_FLOW_H__ */