The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups 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  valuestart = memchr(namestart, 0x00, length-2);
79  if (valuestart == NULL) {
80  return FALSE;
81  }
82  valuestart++;
83  g_return_val_if_fail(NULL != tlvptr, FALSE);
84  g_return_val_if_fail(NULL != pktend, FALSE);
85  g_return_val_if_fail(length > 0, FALSE);
86  g_return_val_if_fail(valuestart < namestart+1, FALSE);
87 
88  valueendplace = namestart + length;
89  valuexpectedplace = valueendplace-1;
90  return valuexpectedplace == memchr(valuestart, 0x00, 1+(valueendplace-valuestart));
91 }
92 
93 
97 nvpairframe_new(guint16 frame_type,
98  gchar* name,
99  gchar* value,
100  gsize framesize)
101 {
102  Frame* baseframe;
103  NVpairFrame* self;
104 
105  if (framesize < sizeof(NVpairFrame)){
106  framesize = sizeof(NVpairFrame);
107  }
108 
109  if (name != NULL) {
110  g_return_val_if_fail(value != NULL, NULL);
111  g_return_val_if_fail(name[0] != '\0', NULL);
112  }
113  baseframe = frame_new(frame_type, framesize);
115  if (name != NULL) {
116  name = g_strdup(name);
117  value = g_strdup(value);
118  baseframe->length = strlen(name) + strlen(value) + 2;
119  }
120  proj_class_register_subclassed (baseframe, "NVpairFrame");
121 
122  self = CASTTOCLASS(NVpairFrame, baseframe);
123  self->name = name;
124  self->value = value;
125  return self;
126 }
131 nvpairframe_tlvconstructor(gpointer tlvstart,
132  gconstpointer pktend,
133  gpointer* ignorednewpkt,
134  gpointer* ignoredpktend)
135 {
136  guint16 frametype = get_generic_tlv_type(tlvstart, pktend);
137  guint32 framelength = get_generic_tlv_len(tlvstart, pktend);
138  const guint8* framevalue = get_generic_tlv_value(tlvstart, pktend);
139  NVpairFrame * ret = nvpairframe_new(frametype, NULL, NULL, 0);
140  Frame * fret = CASTTOCLASS(Frame, ret);
141 
142  (void)ignorednewpkt; (void)ignoredpktend;
143  g_return_val_if_fail(ret != NULL, NULL);
144 
145  ret->baseclass.length = framelength;
148  ret->baseclass.setvalue(fret, g_memdup(framevalue, framelength), framelength, frame_default_valuefinalize);
149  return fret;
150 }
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:131
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: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 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 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:97
FSTATIC void frame_default_valuefinalize(gpointer value)
Finalize a Frame.
Definition: frame.c:56
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