The Assimilation Project  based on Assimilation version 0.5.1435932971
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
intframe.c
Go to the documentation of this file.
1 
23 #include <string.h>
24 #include <projectcommon.h>
25 #include <frameset.h>
26 #include <intframe.h>
27 #include <frametypes.h>
28 #include <generic_tlv_min.h>
29 #include <tlvhelper.h>
76 FSTATIC void _intframe_setint(IntFrame * self, guint64 value);
77 FSTATIC guint64 _intframe_getint(IntFrame * self);
78 FSTATIC void _intframe_updatedata(Frame*, gpointer, gconstpointer, FrameSet*);
79 FSTATIC gboolean _intframe_isvalid(const Frame*, gconstpointer, gconstpointer);
80 FSTATIC gchar* _intframe_toString(gconstpointer obj);
81 
86 
88 FSTATIC int
90 {
91  return self->baseclass.length;
92 }
94 FSTATIC void
95 _intframe_setint(IntFrame * self, guint64 value)
96 {
97  guint64 limit = 0;
98  switch (self->baseclass.length) {
99  case 1: limit = G_MAXUINT8;
100  break;
101  case 2: limit = G_MAXUINT16;
102  break;
103  case 3: limit = 0x00ffffffL;
104  break;
105  case 4: limit = G_MAXUINT32;
106  break;
107  case 8: limit = G_MAXUINT64;
108  break;
109  }
110  g_return_if_fail(value <= limit);
111  self->_value = value;
112 }
113 
115 FSTATIC guint64
117 {
118  return self->_value;
119 }
120 
121 FSTATIC void
123  gpointer tlvptr,
124  gconstpointer pktend,
125  FrameSet* fs)
126 {
127  IntFrame* self = CASTTOCLASS(IntFrame, fself);
128  void * pktpos = get_generic_tlv_nonconst_value(tlvptr, pktend);
129  (void)fs;
130  g_return_if_fail(NULL != pktpos);
131 
132  switch (self->baseclass.length) {
133  case 1: tlv_set_guint8(pktpos, (guint8)self->_value, pktend);
134  break;
135  case 2: tlv_set_guint16(pktpos, (guint16)self->_value, pktend);
136  break;
137  case 3: tlv_set_guint24(pktpos, (guint32)self->_value, pktend);
138  break;
139  case 4: tlv_set_guint32(pktpos, (guint32)self->_value, pktend);
140  break;
141  case 8: tlv_set_guint64(pktpos, (guint64)self->_value, pktend);
142  break;
143  default:g_return_if_reached();
144  break;
145  }
146 }
147 
149 FSTATIC gboolean
151  gconstpointer tlvptr,
152  gconstpointer pktend)
153 {
154  int length = self->length;
155  if (tlvptr != NULL) {
156  int tlvlen = get_generic_tlv_len(tlvptr, pktend);
157  if (length != tlvlen) {
158  return FALSE;
159  }
160  }
161  switch (self->length) {
162  case 1: case 2: case 3: case 4: case 8:
163  return TRUE;
164  }
165  return FALSE;
166 }
167 
169 FSTATIC gchar*
170 _intframe_toString(gconstpointer obj)
171 {
172  const IntFrame* self = CASTTOCONSTCLASS(IntFrame, obj);
173  return g_strdup_printf("IntFrame(%d, %d, "FMT_64BIT"d)"
174  , self->baseclass.type, self->baseclass.length, self->_value);
175 }
176 
178 IntFrame*
179 intframe_new(guint16 frametype,
180  int intbytes)
181 {
182  Frame* frame;
183  IntFrame* iframe;
184  if (intbytes != 1 && intbytes != 2 && intbytes != 3 && intbytes != 4 && intbytes != 8) {
185  return NULL;
186  }
187  frame = frame_new(frametype, sizeof(IntFrame));
188  proj_class_register_subclassed(frame, "IntFrame");
189  iframe = CASTTOCLASS(IntFrame, frame);
190  iframe->baseclass.length = intbytes;
191  iframe->intlength = _intframe_intlength;
192  iframe->setint = _intframe_setint;
193  iframe->getint = _intframe_getint;
196  iframe->baseclass.value = NULL;
197  iframe->baseclass.setvalue = NULL;
198  iframe->baseclass.valuefinalize = NULL;
199  iframe->baseclass.length = intbytes;
201  return iframe;
202 }
205 Frame*
206 intframe_tlvconstructor(gpointer tlvstart,
207  gconstpointer pktend,
208  gpointer* ignorednewpkt,
209  gpointer* ignoredpktend)
210 {
211  guint16 frametype = get_generic_tlv_type(tlvstart, pktend);
212  guint16 framelength = get_generic_tlv_len(tlvstart, pktend);
213  const guint8* framevalue = get_generic_tlv_value(tlvstart, pktend);
214  IntFrame * ret = intframe_new(frametype, framelength);
215  guint64 intvalue = G_MAXUINT64;
216 
217  (void)ignorednewpkt; (void)ignoredpktend;
218  g_return_val_if_fail(ret != NULL, NULL);
219 
220  ret->baseclass.length = framelength;
221  switch (ret->baseclass.length) {
222  case 1:
223  intvalue = tlv_get_guint8(framevalue, pktend);
224  break;
225  case 2:
226  intvalue = tlv_get_guint16(framevalue, pktend);
227  break;
228  case 3:
229  intvalue = tlv_get_guint24(framevalue, pktend);
230  break;
231  case 4:
232  intvalue = tlv_get_guint32(framevalue, pktend);
233  break;
234  case 8:
235  intvalue = tlv_get_guint64(framevalue, pktend);
236  break;
237  }
238  ret->setint(ret, intvalue);
239  return CASTTOCLASS(Frame, ret);
240 }
241 
242 
guint32 length
Frame Length.
Definition: frame.h:46
FSTATIC int _intframe_intlength(IntFrame *)
Return number of bytes in this integer.
Definition: intframe.c:89
FSTATIC guint64 _intframe_getint(IntFrame *self)
Get the integer value associated with an IntFrame.
Definition: intframe.c:116
GDestroyNotify valuefinalize
method for finalizing value
Definition: frame.h:57
This is an IntFrame class TLV (type, length, value) frame - representing an integer of some specified...
Definition: intframe.h:39
FSTATIC gchar * _intframe_toString(gconstpointer obj)
Return a printable representation of our IntFrame object.
Definition: intframe.c:170
guint8 tlv_get_guint8(const void *vitem, const void *bufend)
Retrieve an unsigned 8 bit integer from the given location with error checking.
Definition: tlvhelper.c:31
AssimObj baseclass
Base object class for our Class system.
Definition: frame.h:44
Implements minimal client-oriented Frame and Frameset capabilities.
guint64 tlv_get_guint64(const void *vitem, const void *bufend)
Retrieve an unsigned 64 bit integer from the given location with error checking and without caring ab...
Definition: tlvhelper.c:108
This is the base Frame class object (in-memory TLV (type, length, value)) for every general component...
Definition: frame.h:43
IntFrame * intframe_new(guint16 frametype, int intbytes)
Construct new IntFrame object.
Definition: intframe.c:179
#define FSTATIC
Definition: projectcommon.h:31
gpointer value
Frame Value (pointer)
Definition: frame.h:47
void tlv_set_guint24(void *vitem, guint32 item, const void *bufend)
Set an unsigned 24 bit (3-byte) integer to the given location with error checking and without caring ...
Definition: tlvhelper.c:148
void tlv_set_guint16(void *vitem, guint16 item, const void *bufend)
Set an unsigned 16 bit integer to the given location with error checking and without caring about byt...
Definition: tlvhelper.c:67
guint16 get_generic_tlv_type(gconstpointer tlv_vp, gconstpointer pktend)
Return the 'Type' of the given TLV TLV entry (first two bytes)
Implements the IntFrame class - integers in a frame.
guint32 tlv_get_guint32(const void *vitem, const void *bufend)
Retrieve an unsigned 32 bit integer from the given location with error checking and without caring ab...
Definition: tlvhelper.c:81
Frame * frame_new(guint16 frame_type, gsize framesize)
Construct a new frame - allowing for "derived" frame types...
Definition: frame.c:125
gpointer proj_class_register_subclassed(gpointer object, const char *static_subclassname)
Log the creation of a subclassed object from a superclassed object.
Definition: proj_classes.c:192
gboolean(* isvalid)(const Frame *self, gconstpointer tlvptr, gconstpointer pktend)
TRUE if TLV data looks valid...
Definition: frame.h:50
#define FMT_64BIT
Format designator for a 64 bit integer.
Definition: projectcommon.h:32
guint32 tlv_get_guint24(const void *vitem, const void *bufend)
Retrieve an unsigned 24 bit (3-byte) integer from the given location with error checking and without ...
Definition: tlvhelper.c:133
#define CASTTOCONSTCLASS(Cclass, obj)
Safely cast 'obj' to const C-class 'class' - verifying that it was registered as being of type class ...
Definition: proj_classes.h:71
guint32 get_generic_tlv_len(gconstpointer tlv_vp, gconstpointer pktend)
Return the 'Length' of the given generic TLV entry (first 3 bytes after type) Note: return result is ...
Project common header file.
Header file defining the data layouts for our Frames.
TLV helper interfaces definitions.
void tlv_set_guint8(void *vitem, guint8 item, const void *bufend)
Set an unsigned 8 bit integer to the given location with error checking.
Definition: tlvhelper.c:42
void tlv_set_guint32(void *vitem, guint32 item, const void *bufend)
Set an unsigned 32 bit integer to the given location with error checking and without caring about byt...
Definition: tlvhelper.c:93
Provides definitions for using our generic TLV capabilities.
gconstpointer get_generic_tlv_value(gconstpointer tlv_vp, gconstpointer pktend)
Return a const pointer to the 'Value' of the given generic TLV entry.
Frame baseclass
base Frame class object
Definition: intframe.h:40
void(* setint)(IntFrame *self, guint64 value)
set the integer to the given value
Definition: intframe.h:43
int(* intlength)(IntFrame *self)
get length of integer this IntFrame supports
Definition: intframe.h:41
FSTATIC void _intframe_updatedata(Frame *, gpointer, gconstpointer, FrameSet *)
Definition: intframe.c:122
void(* updatedata)(Frame *self, gpointer tlvptr, gconstpointer pktend, FrameSet *fs)
Update packet data.
Definition: frame.h:49
FrameSet class - used for collecting Frames when not on the wire, and for marshalling/demarshalling t...
Definition: frameset.h:46
gchar *(* toString)(gconstpointer)
Produce malloc-ed string representation.
Definition: assimobj.h:58
gpointer get_generic_tlv_nonconst_value(gpointer tlv_vp, gconstpointer pktend)
Return a non-const (mutable) pointer to the 'Value' of the given generic TLV entry.
void(* setvalue)(Frame *self, gpointer value, guint32 length, GDestroyNotify valfinal)
member function for setting value
Definition: frame.h:52
Frame * intframe_tlvconstructor(gpointer tlvstart, gconstpointer pktend, gpointer *ignorednewpkt, gpointer *ignoredpktend)
Given marshalled data corresponding to an IntFrame (integer frame), return that corresponding Frame I...
Definition: intframe.c:206
guint64(* getint)(IntFrame *self)
get value of integer in this IntFrame
Definition: intframe.h:42
FSTATIC void _intframe_setint(IntFrame *self, guint64 value)
Set the integer value associated with an IntFrame.
Definition: intframe.c:95
FSTATIC gboolean _intframe_isvalid(const Frame *, gconstpointer, gconstpointer)
Return TRUE if this integer is valid - basically is it of one of our supported lengths...
Definition: intframe.c:150
guint16 tlv_get_guint16(const void *vitem, const void *bufend)
Retrieve an unsigned 16 bit integer from the given location with error checking and without caring ab...
Definition: tlvhelper.c:55
void tlv_set_guint64(void *vitem, guint64 item, const void *bufend)
Set an unsigned 64 bit integer to the given location with error checking and without caring about byt...
Definition: tlvhelper.c:120
#define CASTTOCLASS(Cclass, obj)
Safely cast 'obj' to C-class 'class' - verifying that it was registerd as being of type class ...
Definition: proj_classes.h:66