Skip to content

Commit 2f7d7ed

Browse files
authored
Merge pull request #29 from felix-jia/lookup-and-create
Allow to lookup and create flows.
2 parents c577951 + f0ced3f commit 2f7d7ed

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

ginetflow.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,30 @@ GInetFlow *g_inet_flow_get_full(GInetFlowTable * table,
10491049
return flow;
10501050
}
10511051

1052+
GInetFlow *g_inet_flow_create(GInetFlowTable * table, GInetTuple *tuple)
1053+
{
1054+
GInetFlow *flow;
1055+
1056+
/* Check if max table size is reached */
1057+
if (table->max > 0 && g_hash_table_size(table->table) >= table->max) {
1058+
return NULL;
1059+
}
1060+
1061+
flow = (GInetFlow *) g_object_new(G_INET_TYPE_FLOW, NULL);
1062+
flow->table = table;
1063+
flow->list.data = flow;
1064+
/* Set default lifetime before processing further */
1065+
flow->lifetime = G_INET_FLOW_DEFAULT_NEW_TIMEOUT;
1066+
flow->family = ((struct sockaddr *) &(tuple->src))->sa_family;
1067+
flow->hash = g_inet_tuple_hash(tuple);
1068+
flow->tuple = *tuple;
1069+
g_hash_table_replace(table->table, (gpointer) flow, (gpointer) flow);
1070+
flow->timestamp = get_time_us();
1071+
insert_flow_by_expiry(table, flow, flow->lifetime);
1072+
1073+
return flow;
1074+
}
1075+
10521076
static void g_inet_flow_table_finalize(GObject * object)
10531077
{
10541078
int i;
@@ -1152,3 +1176,11 @@ GInetTuple *g_inet_flow_parse(const guint8 * frame, guint length, GList ** fragm
11521176
flow_parse(result, frame, length, 0, fragments, NULL, 0, NULL);
11531177
return result;
11541178
}
1179+
1180+
GInetFlow *g_inet_flow_lookup (GInetFlowTable * table, GInetTuple *tuple)
1181+
{
1182+
GInetFlow packet;
1183+
1184+
packet.tuple = *tuple;
1185+
return (GInetFlow *) g_hash_table_lookup(table->table, &packet);
1186+
}

ginetflow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ GInetFlow *g_inet_flow_get(GInetFlowTable * table, const guint8 * frame, guint l
5050
GInetFlow *g_inet_flow_get_full(GInetFlowTable * table, const guint8 * frame,
5151
guint length, guint16 hash, guint64 timestamp,
5252
gboolean update, gboolean l2, const uint8_t ** iphr);
53+
GInetFlow *g_inet_flow_create(GInetFlowTable * table, GInetTuple *tuple);
5354
GInetFlow *g_inet_flow_expire(GInetFlowTable * table, guint64 ts);
5455

5556
/* 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
6061
typedef void (*GIFFunc) (GInetFlow * flow, gpointer user_data);
6162
void g_inet_flow_foreach(GInetFlowTable * table, GIFFunc func, gpointer user_data);
6263
void g_inet_flow_table_max_set(GInetFlowTable * table, guint64 value);
64+
GInetFlow *g_inet_flow_lookup(GInetFlowTable * table, GInetTuple *tuple);
6365

6466
G_END_DECLS
6567
#endif /* __G_INET_FLOW_H__ */

0 commit comments

Comments
 (0)