The Assimilation Project  based on Assimilation version 0.5.1427809010
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
generic_tlv_min.c
Go to the documentation of this file.
1 
28 // #include <stdio.h>
29 #include <string.h>
30 #include <tlvhelper.h>
31 #include <frametypes.h>
32 #include <generic_tlv_min.h>
33 
40 
41 
43 guint16
44 get_generic_tlv_type(gconstpointer tlv_vp,
45  gconstpointer pktend)
46 {
47  return tlv_get_guint16(tlv_vp, pktend);
48 }
49 
51 void
52 set_generic_tlv_type(gpointer tlv_vp,
53  guint16 newtype,
54  gconstpointer pktend)
55 {
56  tlv_set_guint16(tlv_vp, newtype, pktend);
57 }
58 
61 guint32
62 get_generic_tlv_len(gconstpointer tlv_vp,
63  gconstpointer pktend)
64 {
65  const guint8 * tlvp = tlv_vp;
66  guint32 tainted_len = tlv_get_guint24(tlvp+sizeof(guint16), pktend);
67  g_return_val_if_fail((tlvp + tainted_len) <= (const guint8*)pktend, TLV_BAD24);
68  return tainted_len;
69 }
70 
72 void
73 set_generic_tlv_len(gpointer tlv_vp,
74  guint32 newsize,
75  gconstpointer pktend)
76 {
77  guint8 * tlvp = tlv_vp;
78  tlv_set_guint24(tlvp+sizeof(guint16), newsize, pktend);
79 }
80 
82 gconstpointer
83 get_generic_tlv_value(gconstpointer tlv_vp,
84  gconstpointer pktend)
85 {
86  const guint8* tlvbytes = tlv_vp;
87  const guint8* ret = (tlvbytes + GENERICTLV_HDRSZ);
88  if (ret == (const guint8*)pktend) {
89  g_return_val_if_fail(get_generic_tlv_len(tlv_vp, pktend) == 0, TLV_BADPTR); // NULL
90  return ret;
91  }
92  g_return_val_if_fail(ret < (const guint8*)pktend, TLV_BADPTR); // NULL
93  return ret;
94 }
95 
97 gpointer
99  gconstpointer pktend)
100 {
101  guint8* tlvbytes = tlv_vp;
102  guint8* ret = (tlvbytes + GENERICTLV_HDRSZ);
103  g_return_val_if_fail(ret < (const guint8*)pktend, TLV_BADPTR); // NULL
104  return (tlvbytes + GENERICTLV_HDRSZ);
105 }
106 
109 void
110 set_generic_tlv_value(gpointer tlv_vp,
111  void* srcdata,
112  guint32 srcsize,
113  gconstpointer pktend)
114 {
115  guint8* tlvbytes = tlv_vp;
116  g_return_if_fail((gpointer)(tlvbytes+srcsize) <= pktend);
117  g_return_if_fail(srcsize == get_generic_tlv_len(tlv_vp, pktend));
118  memcpy(tlvbytes + GENERICTLV_HDRSZ, srcdata, srcsize);
119 }
120 
122 gboolean
123 is_valid_generic_tlv_packet(gconstpointer tlv_vp,//<[in] pointer to beginning of TLV packet
124  gconstpointer pktend) //<[in] pointer to first byte past the end of packet
125 {
126  const guint16 reqtypes [] = {FRAMETYPE_SIG};
127  unsigned j = 0;
128  //int lasttype = -1;
129  const guint8* next;
130  if (NULL == tlv_vp || ((const guint8*)tlv_vp+GENERICTLV_HDRSZ) > (const guint8*)pktend) {
131  g_warning("TLV Invalid because packet is too short");
132  return FALSE;
133  }
134  for (tlv_vp = get_generic_tlv_first(tlv_vp, pktend)
135  ; NULL != tlv_vp && tlv_vp < pktend
136  ; tlv_vp = next) {
137 
138  guint16 ttype;
139  guint32 length;
140 
141  if (tlv_vp >= pktend) {
142  return FALSE;
143  }
144  ttype = get_generic_tlv_type(tlv_vp, pktend);
145  //lasttype = ttype;
146  length = get_generic_tlv_len(tlv_vp, pktend);
147  next = (const guint8*)tlv_vp + (length+GENERICTLV_HDRSZ);
148  if (next > (const guint8*) pktend) {
149  g_warning("TLV Invalid because TLV entry extends past end");
150  return FALSE;
151  }
152 #if 0
153  // This is no longer true - in the presence of compression and encryption
154  // and any other kinds of frames we might want to invent that gobble up
155  // the rest of the packet to the end...
156  if (ttype == FRAMETYPE_END) {
157  if (get_generic_tlv_value(tlv_vp, pktend) == pktend) {
158  return length == 0;
159  }else{
160  g_warning("TLV Invalid because END item isn't at end of packet");
161  return FALSE;
162  }
163  }
164 #endif
165  if (j < DIMOF(reqtypes) && ttype != reqtypes[j]) {
166  g_warning("TLV Invalid because required TLV types aren't present in right order");
167  return FALSE;
168  }
169  j += 1;
170  }
171 #if 0
172  // See the note above...
173  g_warning("TLV Invalid because final type wasn't FRAMETYPE_END (it was %d)"
174  , lasttype);
175 #endif
176  return TRUE;
177 }
178 
179 gconstpointer
180 get_generic_tlv_first(gconstpointer packet,
181  gconstpointer pktend)
182 {
183  const guint8* inittlv = packet;
184  if (packet == NULL
185  || (inittlv + GENERICTLV_HDRSZ) > (const guint8*)pktend
186  || (inittlv + GENERICTLV_HDRSZ + get_generic_tlv_len(inittlv, pktend)) > (const guint8*)pktend) {
187  return TLV_BADPTR; // NULL
188  }
189  return inittlv;
190 }
191 
193 gconstpointer
194 get_generic_tlv_next(gconstpointer tlv_vp,
195  gconstpointer pktend)
196 {
197  const guint8* nexttlv;
198  const guint8* nextend;
199  if (tlv_vp == NULL
200  || ((const guint8*)tlv_vp+GENERICTLV_HDRSZ) > (const guint8*)pktend
201  || get_generic_tlv_type(tlv_vp, pktend) == FRAMETYPE_END) {
202  return TLV_BADPTR; // NULL
203  }
204  nexttlv = (const guint8*)tlv_vp + GENERICTLV_HDRSZ + get_generic_tlv_len(tlv_vp, pktend);
205  /* Watch out for malformed packets! (BLACKHAT, PARANOIA) */
206  nextend = nexttlv + GENERICTLV_HDRSZ + get_generic_tlv_len(nexttlv, pktend);
207  /* fprintf(stderr, "LOOK: cur:%p, next: %p, nextend: %p, vpend: %p\n"
208  , tlv_vp, nexttlv, nextend, pktend); */
209  return nextend > (const guint8*)pktend ? NULL : nexttlv;
210 }
211 
213 gconstpointer
214 find_next_generic_tlv_type(gconstpointer tlv_vp,
215  guint16 tlvtype,
216  gconstpointer pktend)
217 {
218  while (NULL != tlv_vp && ((const guint8*)tlv_vp+GENERICTLV_HDRSZ) <= (const guint8*)pktend) {
219  if (get_generic_tlv_type(tlv_vp, pktend) == tlvtype) {
220  return tlv_vp;
221  }
222  tlv_vp = get_generic_tlv_next(tlv_vp, pktend);
223  }
224  return NULL;
225 }
gconstpointer find_next_generic_tlv_type(gconstpointer tlv_vp, guint16 tlvtype, gconstpointer pktend)
Return pointer to the next TLV entry of the given type at or after the current location.
#define GENERICTLV_HDRSZ
Size of Generic TLV header - ((type + length) == 5)
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)
#define TLV_BADPTR
Definition: tlvhelper.h:34
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
void set_generic_tlv_value(gpointer tlv_vp, void *srcdata, guint32 srcsize, gconstpointer pktend)
Set the TLV data value to the given 'tlv_vp' pointer.
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 ...
void set_generic_tlv_type(gpointer tlv_vp, guint16 newtype, gconstpointer pktend)
Set the 'Type' of the given TLV TLV entry (first two bytes)
#define DIMOF(a)
Definition: lldp_min.c:30
Header file defining the data layouts for our Frames.
TLV helper interfaces definitions.
gboolean is_valid_generic_tlv_packet(gconstpointer tlv_vp, gconstpointer pktend)
Return TRUE if this is a valid generic TLV packet.
void set_generic_tlv_len(gpointer tlv_vp, guint32 newsize, gconstpointer pktend)
Set the 'Length' of the given generic TLV entry (first 3 bytes after type)
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.
#define FRAMETYPE_END
FRAMETYPE_END Frame (frametype 0) Frame subclass - Frame class.
Definition: frametypes.h:63
#define FRAMETYPE_SIG
FRAMETYPE_SIG Frame (frametype 1) Frame subclass - SignFrame class.
Definition: frametypes.h:82
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.
gconstpointer get_generic_tlv_next(gconstpointer tlv_vp, gconstpointer pktend)
Return pointer to the next generic TLV entry after the current location.
gconstpointer get_generic_tlv_first(gconstpointer packet, gconstpointer pktend)
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
#define TLV_BAD24
Definition: tlvhelper.h:31