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(gconstpointer tlvstart,
132  gconstpointer pktend)
133 {
134  guint16 frametype = get_generic_tlv_type(tlvstart, pktend);
135  guint16 framelength = get_generic_tlv_len(tlvstart, pktend);
136  const guint8* framevalue = get_generic_tlv_value(tlvstart, pktend);
137  NVpairFrame * ret = nvpairframe_new(frametype, NULL, NULL, 0);
138  Frame * fret = CASTTOCLASS(Frame, ret);
139  g_return_val_if_fail(ret != NULL, NULL);
140 
141  ret->baseclass.length = framelength;
144  ret->baseclass.setvalue(fret, g_memdup(framevalue, framelength), framelength, frame_default_valuefinalize);
145  return fret;
146 }