Skip to content

Commit

Permalink
Merge pull request #29 from felix-jia/lookup-and-create
Browse files Browse the repository at this point in the history
Allow to lookup and create flows.
  • Loading branch information
carlatat authored Jan 10, 2018
2 parents c577951 + f0ced3f commit 2f7d7ed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
32 changes: 32 additions & 0 deletions ginetflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions ginetflow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__ */

0 comments on commit 2f7d7ed

Please sign in to comment.