[netfilter-cvslog] r3467 - trunk/nfsim/core
rusty at netfilter.org
rusty at netfilter.org
Mon Dec 20 05:31:48 CET 2004
Author: rusty at netfilter.org
Date: 2004-12-20 05:31:48 +0100 (Mon, 20 Dec 2004)
New Revision: 3467
Added:
trunk/nfsim/core/field.c
trunk/nfsim/core/field.h
Modified:
trunk/nfsim/core/Makefile
Log:
Create dynamic ability to add fields to structures, so we don't need to disturb kernel code (eg. sk_buff).
Modified: trunk/nfsim/core/Makefile
===================================================================
--- trunk/nfsim/core/Makefile 2004-12-20 04:24:54 UTC (rev 3466)
+++ trunk/nfsim/core/Makefile 2004-12-20 04:31:48 UTC (rev 3467)
@@ -1,2 +1,2 @@
-OBJS += core/utils.o core/core.o core/message.o core/$(TYPE)/$(TYPE).o core/seq_file.o core/talloc.o core/failtest.o
+OBJS += core/utils.o core/core.o core/message.o core/$(TYPE)/$(TYPE).o core/seq_file.o core/talloc.o core/failtest.o core/field.o
HELP_OBJS += core/tui.o core/expect.o core/log.o core/proc.o
Added: trunk/nfsim/core/field.c
===================================================================
--- trunk/nfsim/core/field.c 2004-12-20 04:24:54 UTC (rev 3466)
+++ trunk/nfsim/core/field.c 2004-12-20 04:31:48 UTC (rev 3467)
@@ -0,0 +1,88 @@
+/*
+
+Copyright (c) 2004 Rusty Russell
+
+This file is part of nfsim.
+
+nfsim is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+nfsim is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with nfsim; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* FIXME: just want list macros. */
+#include <kernelenv.h>
+#include <core.h>
+#include <talloc.h>
+#include <field.h>
+#include <utils.h>
+
+struct field
+{
+ struct list_head list;
+ const char *name;
+ const void *strct;
+ void *val;
+};
+
+static LIST_HEAD(fields);
+static void *field_ctx;
+
+static int field_destroy(void *f)
+{
+ struct field *field = f;
+ list_del(&field->list);
+ return 0;
+}
+
+void field_attach(const void *strct, const char *name, void *val)
+{
+ struct field *field = talloc(field_ctx, struct field);
+
+ field->strct = strct;
+ field->name = name;
+ field->val = val;
+ list_add(&field->list, &fields);
+ talloc_set_destructor(field, field_destroy);
+}
+
+static struct field *__field_find(const void *strct, const char *name)
+{
+ struct field *i;
+
+ list_for_each_entry(i, &fields, list) {
+ if (strct == i->strct && streq(name, i->name))
+ return i;
+ }
+ return NULL;
+}
+
+bool field_exists(const void *strct, const char *name)
+{
+ return (__field_find(strct, name) != NULL);
+}
+
+void *field_value(const void *strct, const char *name)
+{
+ return __field_find(strct, name)->val;
+}
+
+void field_detach(const void *strct, const char *name)
+{
+ talloc_free(__field_find(strct, name));
+}
+
+static void setup_field(void)
+{
+ field_ctx = talloc_named_const(nfsim_tallocs, 1, "Extra fields");
+}
+init_call(setup_field);
Added: trunk/nfsim/core/field.h
===================================================================
--- trunk/nfsim/core/field.h 2004-12-20 04:24:54 UTC (rev 3466)
+++ trunk/nfsim/core/field.h 2004-12-20 04:31:48 UTC (rev 3467)
@@ -0,0 +1,14 @@
+#ifndef __NFSIM_FIELD_H
+#define __NFSIM_FIELD_H
+/* Dynamically attach a "field" to a structure. This is better than
+ * actually adding a field which might accidentally clash with a real
+ * field (or someone developing under nfsim might think it's a real field!).
+ */
+#include <stdbool.h>
+
+void field_attach(const void *strct, const char *name, void *val);
+bool field_exists(const void *strct, const char *name);
+void *field_value(const void *strct, const char *name);
+void field_detach(const void *strct, const char *name);
+#endif /* __NFSIM_FIELD_H */
+
More information about the netfilter-cvslog
mailing list