The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups 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  // NOTE - this gets rid of the "const" coming out of get_generic_tlv_value...
130  void * pktpos = GINT_TO_POINTER(GPOINTER_TO_INT(get_generic_tlv_value(tlvptr, pktend)));
131  (void)fs;
132  g_return_if_fail(NULL != pktpos);
133 
134  switch (self->baseclass.length) {
135  case 1: tlv_set_guint8(pktpos, (guint8)self->_value, pktend);
136  break;
137  case 2: tlv_set_guint16(pktpos, (guint16)self->_value, pktend);
138  break;
139  case 3: tlv_set_guint24(pktpos, (guint32)self->_value, pktend);
140  break;
141  case 4: tlv_set_guint32(pktpos, (guint32)self->_value, pktend);
142  break;
143  case 8: tlv_set_guint64(pktpos, (guint64)self->_value, pktend);
144  break;
145  default:g_return_if_reached();
146  break;
147  }
148 }
149 
151 FSTATIC gboolean
153  gconstpointer tlvptr,
154  gconstpointer pktend)
155 {
156  int length = self->length;
157  if (tlvptr != NULL) {
158  int tlvlen = get_generic_tlv_len(tlvptr, pktend);
159  if (length != tlvlen) {
160  return FALSE;
161  }
162  }
163  switch (self->length) {
164  case 1: case 2: case 3: case 4: case 8:
165  return TRUE;
166  }
167  return FALSE;
168 }
169 
171 FSTATIC gchar*
172 _intframe_toString(gconstpointer obj)
173 {
174  const IntFrame* self = CASTTOCONSTCLASS(IntFrame, obj);
175  return g_strdup_printf("IntFrame(%d, %d, "FMT_64BIT"d)"
176  , self->baseclass.type, self->baseclass.length, self->_value);
177 }
178 
180 IntFrame*
181 intframe_new(guint16 frametype,
182  int intbytes)
183 {
184  Frame* frame;
185  IntFrame* iframe;
186  if (intbytes != 1 && intbytes != 2 && intbytes != 3 && intbytes != 4 && intbytes != 8) {
187  return NULL;
188  }
189  frame = frame_new(frametype, sizeof(IntFrame));
190  proj_class_register_subclassed(frame, "IntFrame");
191  iframe = CASTTOCLASS(IntFrame, frame);
192  iframe->baseclass.length = intbytes;
193  iframe->intlength = _intframe_intlength;
194  iframe->setint = _intframe_setint;
195  iframe->getint = _intframe_getint;
198  iframe->baseclass.value = NULL;
199  iframe->baseclass.setvalue = NULL;
200  iframe->baseclass.valuefinalize = NULL;
201  iframe->baseclass.length = intbytes;
203  return iframe;
204 }
207 Frame*
208 intframe_tlvconstructor(gconstpointer tlvstart,
209  gconstpointer pktend,
210  gpointer* ignorednewpkt,
211  gpointer* ignoredpktend)
212 {
213  guint16 frametype = get_generic_tlv_type(tlvstart, pktend);
214  guint16 framelength = get_generic_tlv_len(tlvstart, pktend);
215  const guint8* framevalue = get_generic_tlv_value(tlvstart, pktend);
216  IntFrame * ret = intframe_new(frametype, framelength);
217  guint64 intvalue = G_MAXUINT64;
218 
219  (void)ignorednewpkt; (void)ignoredpktend;
220  g_return_val_if_fail(ret != NULL, NULL);
221 
222  ret->baseclass.length = framelength;
223  switch (ret->baseclass.length) {
224  case 1:
225  intvalue = tlv_get_guint8(framevalue, pktend);
226  break;
227  case 2:
228  intvalue = tlv_get_guint16(framevalue, pktend);
229  break;
230  case 3:
231  intvalue = tlv_get_guint24(framevalue, pktend);
232  break;
233  case 4:
234  intvalue = tlv_get_guint32(framevalue, pktend);
235  break;
236  case 8:
237  intvalue = tlv_get_guint64(framevalue, pktend);
238  break;
239  }
240  ret->setint(ret, intvalue);
241  return CASTTOCLASS(Frame, ret);
242 }
243 
244 
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:172
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:30
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:107
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:181
#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:146
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:68
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:124
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:131
#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)
Project common header file.
Header file defining the data layouts for our Frames.
TLV helper interfaces definitions.
Frame * intframe_tlvconstructor(gconstpointer tlvstart, gconstpointer pktend, gpointer *ignorednewpkt, gpointer *ignoredpktend)
Given marshalled data corresponding to an IntFrame (integer frame), return that corresponding Frame I...
Definition: intframe.c:208
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:41
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:45
gchar *(* toString)(gconstpointer)
Produce malloc-ed string representation.
Definition: assimobj.h:58
void(* setvalue)(Frame *self, gpointer value, guint32 length, GDestroyNotify valfinal)
member function for setting value
Definition: frame.h:52
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:152
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:53
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:119
#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