The Assimilation Project  based on Assimilation version 0.5.1435932971
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
nvpairframe.c
Go to the documentation of this file.
1 
24 #include <string.h>
25 #include <projectcommon.h>
26 #include <frameset.h>
27 #include <nvpairframe.h>
28 #include <frametypes.h>
29 #include <generic_tlv_min.h>
30 #include <tlvhelper.h>
31 
32 
33 #if defined(_MSC_VER) && _MSC_VER < 1400
34 // Probably should be defined in some other file - for the case the system doesn't supply it.
35 gsize
36 strnlen(char* str, size_t maxlen)
37 {
38  char* p = memchr(str, '\0', maxlen);
39 
40  if (p == NULL) {
41  return maxlen;
42  }
43  return (p - str);
44 }
45 #endif
46 
47 
48 FSTATIC gboolean _nvpairframe_default_isvalid(const Frame *, gconstpointer, gconstpointer);
49 
54 
56 gboolean
58  gconstpointer tlvptr,
59  gconstpointer pktend)
60 {
61  const NVpairFrame* nvself;
62  gsize length;
63  const guint8* namestart;
64  const guint8* valuestart;
65  const guint8* valueendplace;
66  const guint8* valuexpectedplace;
67 
68  nvself = CASTTOCONSTCLASS(NVpairFrame, self);
69  if (tlvptr == NULL) {
70  if (nvself->value == NULL || nvself->name == NULL) {
71  return FALSE;
72  }
73  return (strnlen(nvself->name, self->length) + strnlen(nvself->value, self->length))
74  == (gsize)(self->length-2);
75  }
76  length = get_generic_tlv_len(tlvptr, pktend);
77  namestart = get_generic_tlv_value(tlvptr, pktend);
78  if (0 == length || TLV_BAD24 == length || namestart == NULL
79  || length > (gsize)((const guint8*)pktend-namestart)) {
80  return FALSE;
81  }
82  valuestart = memchr(namestart, 0x00, length-2);
83  if (valuestart == NULL) {
84  return FALSE;
85  }
86  valuestart++;
87  g_return_val_if_fail(NULL != tlvptr, FALSE);
88  g_return_val_if_fail(NULL != pktend, FALSE);
89  g_return_val_if_fail(length > 0, FALSE);
90  g_return_val_if_fail(valuestart < namestart+1, FALSE);
91 
92  valueendplace = namestart + length;
93  valuexpectedplace = valueendplace-1;
94  return valuexpectedplace == memchr(valuestart, 0x00, 1+(valueendplace-valuestart));
95 }
96 
97 
101 nvpairframe_new(guint16 frame_type,
102  gchar* name,
103  gchar* value,
104  gsize framesize)
105 {
106  Frame* baseframe;
107  NVpairFrame* self;
108 
109  if (framesize < sizeof(NVpairFrame)){
110  framesize = sizeof(NVpairFrame);
111  }
112 
113  if (name != NULL) {
114  g_return_val_if_fail(value != NULL, NULL);
115  g_return_val_if_fail(name[0] != '\0', NULL);
116  }
117  baseframe = frame_new(frame_type, framesize);
119  if (name != NULL) {
120  name = g_strdup(name);
121  value = g_strdup(value);
122  baseframe->length = strlen(name) + strlen(value) + 2;
123  }
124  proj_class_register_subclassed (baseframe, "NVpairFrame");
125 
126  self = CASTTOCLASS(NVpairFrame, baseframe);
127  self->name = name;
128  self->value = value;
129  return self;
130 }
135 nvpairframe_tlvconstructor(gpointer tlvstart,
136  gconstpointer pktend,
137  gpointer* ignorednewpkt,
138  gpointer* ignoredpktend)
139 {
140  guint16 frametype = get_generic_tlv_type(tlvstart, pktend);
141  guint32 framelength = get_generic_tlv_len(tlvstart, pktend);
142  const guint8* framevalue = get_generic_tlv_value(tlvstart, pktend);
143  NVpairFrame * ret = nvpairframe_new(frametype, NULL, NULL, 0);
144  Frame * fret = CASTTOCLASS(Frame, ret);
145 
146  (void)ignorednewpkt; (void)ignoredpktend;
147  g_return_val_if_fail(ret != NULL, NULL);
148 
149  ret->baseclass.length = framelength;
152  ret->baseclass.setvalue(fret, g_memdup(framevalue, framelength), framelength, frame_default_valuefinalize);
153  return fret;
154 }
guint32 length
Frame Length.
Definition: frame.h:46
WINEXPORT Frame * nvpairframe_tlvconstructor(gpointer tlvstart, gconstpointer pktend, gpointer *ignorednewpkt, gpointer *ignoredpktend)
Given marshalled packet data corresponding to an NVpairFrame (name/value pair) return the correspondi...
Definition: nvpairframe.c:135
Implements minimal client-oriented Frame and Frameset capabilities.
This is the base Frame class object (in-memory TLV (type, length, value)) for every general component...
Definition: frame.h:43
FSTATIC gboolean _nvpairframe_default_isvalid(const Frame *, gconstpointer, gconstpointer)
NVpairFrame class 'isvalid' member function (checks for valid name/value pairs)
Definition: nvpairframe.c:57
gchar * name
Definition: nvpairframe.h:36
gchar * value
Definition: nvpairframe.h:37
struct _NVpairFrame NVpairFrame
Definition: nvpairframe.h:31
#define WINEXPORT
Definition: projectcommon.h:45
#define FSTATIC
Definition: projectcommon.h:31
guint16 get_generic_tlv_type(gconstpointer tlv_vp, gconstpointer pktend)
Return the 'Type' of the given TLV TLV entry (first two bytes)
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 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.
Frame baseclass
Definition: nvpairframe.h:35
NVpairFrame * nvpairframe_new(guint16 frame_type, gchar *name, gchar *value, gsize framesize)
Construct a new NVpairFrame - allowing for "derived" frame types...
Definition: nvpairframe.c:101
FSTATIC void frame_default_valuefinalize(gpointer value)
Finalize a Frame.
Definition: frame.c:57
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.
This is our NVpairFrame class object - used for holding Name/Value pairs.
Definition: nvpairframe.h:34
void(* setvalue)(Frame *self, gpointer value, guint32 length, GDestroyNotify valfinal)
member function for setting value
Definition: frame.h:52
Describes interfaces to name/value pair Frame (NVpairFrame) C-Class It holds conventional zero-termin...
#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
#define TLV_BAD24
Definition: tlvhelper.h:31