The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
frame.c
Go to the documentation of this file.
1 
23 #include <frame.h>
24 #include <generic_tlv_min.h>
25 #include <memory.h>
26 
28 FSTATIC gsize _frame_dataspace(const Frame* f);
29 FSTATIC gboolean _frame_default_isvalid(const Frame *, gconstpointer, gconstpointer);
30 FSTATIC void _frame_setvalue(Frame *, gpointer, guint16, GDestroyNotify valnotify);
31 FSTATIC void _frame_updatedata(Frame *, gpointer, gconstpointer, FrameSet*);
32 FSTATIC void _frame_dump(const Frame *, const char * prefix);
33 
35 
40 
42 FSTATIC void
44 {
45  Frame* self = CASTTOCLASS(Frame, obj);
46  DEBUGMSG5("%s: Finalizing Frame at 0x%p", __FUNCTION__, self);
47  if (self->value && self->valuefinalize) {
48  self->valuefinalize(self->value);
49  }
50  memset(self, 0x00, sizeof(Frame));
51  _assimobj_finalize(obj);
52 }
54 FSTATIC void
56 {
57  if (value) {
58  DEBUGMSG4("%s: freeing Frame value at 0x%p", __FUNCTION__, value);
59  FREE(value);
60  }
61 }
62 
64 FSTATIC gsize
66 {
67  g_return_val_if_fail(f != NULL, 0);
68  return (gsize)(FRAME_INITSIZE + f->length);
69 }
70 
72 FSTATIC gboolean
74  gconstpointer tlvptr,
75  gconstpointer pktend)
76 {
77  (void)self;
78  (void)tlvptr;
79  (void)pktend;
80  return TRUE;
81 }
82 
84 FSTATIC void
86  gpointer value,
87  guint16 length,
88  GDestroyNotify valnotify)
89 {
90  if (self->value && self->valuefinalize) {
91  self->valuefinalize(self);
92  }
93  self->value = value;
94  self->length = length;
95  self->valuefinalize = valnotify;
96 }
97 
99 FSTATIC void
101  gpointer tlvptr,
102  gconstpointer pktend,
103  FrameSet* fs)
104 {
105  (void)fs;
106  // set_generic_tlv_value does pretty exhaustive error checking.
107  set_generic_tlv_value(tlvptr, self->value, self->length, pktend);
108 }
109 
112 Frame*
113 frame_new(guint16 frame_type,
114  gsize framesize)
115 {
116  AssimObj * newobj;
117  Frame * newframe = NULL;
118 
119  BINDDEBUG(Frame);
120  if (framesize < sizeof(Frame)) {
121  framesize = sizeof(Frame);
122  }
123  newobj = assimobj_new(framesize);
124  if (framesize == sizeof(Frame)) {
125  DEBUGMSG5("%s: Constructing New Frame at 0x%p", __FUNCTION__, newobj);
126  }
127  if (newobj != NULL) {
128  newframe = NEWSUBCLASS(Frame, newobj);
130  newframe->type = frame_type;
131  newframe->length = 0;
132  newframe->value = NULL;
133  newframe->dataspace = _frame_dataspace;
134  newframe->isvalid = _frame_default_isvalid;
135  newframe->setvalue = _frame_setvalue;
136  newframe->updatedata = _frame_updatedata;
137  newframe->dump = _frame_dump;
138  newframe->valuefinalize = NULL;
139  }
140  return newframe;
141 }
144 Frame*
145 frame_tlvconstructor(gconstpointer tlvstart,
146  gconstpointer pktend)
147 {
148  guint16 frametype = get_generic_tlv_type(tlvstart, pktend);
149  guint16 framelength = get_generic_tlv_len(tlvstart, pktend);
150  const guint8* framevalue = get_generic_tlv_value(tlvstart, pktend);
151  Frame * ret = frame_new(frametype, 0);
152  g_return_val_if_fail(ret != NULL, NULL);
153 
154  ret->length = framelength;
155  ret->setvalue(ret, g_memdup(framevalue, framelength), framelength, frame_default_valuefinalize);
156  return ret;
157 }
160 void
161 _frame_dump(const Frame * f,
162  const char * prefix)
163 {
164  g_debug("%s%s: type = %d, length = %d dataspace=%zd",
165  prefix,
167  f->type,
168  f->length,
169  f->dataspace(f));
170 }