The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups 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 
60 guint32
61 get_generic_tlv_len(gconstpointer tlv_vp,
62  gconstpointer pktend)
63 {
64  const guint8 * tlvp = tlv_vp;
65  return tlv_get_guint24(tlvp+sizeof(guint16), pktend);
66 }
67 
69 void
70 set_generic_tlv_len(gpointer tlv_vp,
71  guint32 newsize,
72  gconstpointer pktend)
73 {
74  guint8 * tlvp = tlv_vp;
75  tlv_set_guint24(tlvp+sizeof(guint16), newsize, pktend);
76 }
77 
79 gconstpointer
80 get_generic_tlv_value(gconstpointer tlv_vp,
81  gconstpointer pktend)
82 {
83  const guint8* tlvbytes = tlv_vp;
84  (void)pktend;
85  return (tlvbytes + GENERICTLV_HDRSZ);
86 }
87 
89 gpointer
91  gconstpointer pktend)
92 {
93  guint8* tlvbytes = tlv_vp;
94  (void)pktend;
95  return (tlvbytes + GENERICTLV_HDRSZ);
96 }
97 
100 void
101 set_generic_tlv_value(gpointer tlv_vp,
102  void* srcdata,
103  guint32 srcsize,
104  gconstpointer pktend)
105 {
106  guint8* tlvbytes = tlv_vp;
107  g_return_if_fail((gpointer)(tlvbytes+srcsize) <= pktend);
108  g_return_if_fail(srcsize == get_generic_tlv_len(tlv_vp, pktend));
109  memcpy(tlvbytes + GENERICTLV_HDRSZ, srcdata, srcsize);
110 }
111 
113 gboolean
114 is_valid_generic_tlv_packet(gconstpointer tlv_vp,//<[in] pointer to beginning of TLV packet
115  gconstpointer pktend) //<[in] pointer to first byte past the end of packet
116 {
117  const guint16 reqtypes [] = {FRAMETYPE_SIG};
118  unsigned j = 0;
119  //int lasttype = -1;
120  const guint8* next;
121  if (NULL == tlv_vp || ((const guint8*)tlv_vp+GENERICTLV_HDRSZ) > (const guint8*)pktend) {
122  g_warning("TLV Invalid because packet is too short");
123  return FALSE;
124  }
125  for (tlv_vp = get_generic_tlv_first(tlv_vp, pktend)
126  ; NULL != tlv_vp && tlv_vp < pktend
127  ; tlv_vp = next) {
128 
129  guint16 ttype;
130  guint32 length;
131 
132  if (tlv_vp >= pktend) {
133  return FALSE;
134  }
135  ttype = get_generic_tlv_type(tlv_vp, pktend);
136  //lasttype = ttype;
137  length = get_generic_tlv_len(tlv_vp, pktend);
138  next = (const guint8*)tlv_vp + (length+GENERICTLV_HDRSZ);
139  if (next > (const guint8*) pktend) {
140  g_warning("TLV Invalid because TLV entry extends past end");
141  return FALSE;
142  }
143 #if 0
144  // This is no longer true - in the presence of compression and encryption
145  // and any other kinds of frames we might want to invent that gobble up
146  // the rest of the packet to the end...
147  if (ttype == FRAMETYPE_END) {
148  if (get_generic_tlv_value(tlv_vp, pktend) == pktend) {
149  return length == 0;
150  }else{
151  g_warning("TLV Invalid because END item isn't at end of packet");
152  return FALSE;
153  }
154  }
155 #endif
156  if (j < DIMOF(reqtypes) && ttype != reqtypes[j]) {
157  g_warning("TLV Invalid because required TLV types aren't present in right order");
158  return FALSE;
159  }
160  j += 1;
161  }
162 #if 0
163  // See the note above...
164  g_warning("TLV Invalid because final type wasn't FRAMETYPE_END (it was %d)"
165  , lasttype);
166 #endif
167  return TRUE;
168 }
169 
170 gconstpointer
171 get_generic_tlv_first(gconstpointer packet,
172  gconstpointer pktend)
173 {
174  const guint8* inittlv = packet;
175  if (packet == NULL
176  || (inittlv + GENERICTLV_HDRSZ) > (const guint8*)pktend
177  || (inittlv + GENERICTLV_HDRSZ + get_generic_tlv_len(inittlv, pktend)) > (const guint8*)pktend) {
178  return NULL;
179  }
180  return inittlv;
181 }
182 
184 gconstpointer
185 get_generic_tlv_next(gconstpointer tlv_vp,
186  gconstpointer pktend)
187 {
188  const guint8* nexttlv;
189  const guint8* nextend;
190  if (tlv_vp == NULL
191  || ((const guint8*)tlv_vp+GENERICTLV_HDRSZ) > (const guint8*)pktend
192  || get_generic_tlv_type(tlv_vp, pktend) == FRAMETYPE_END) {
193  return NULL;
194  }
195  nexttlv = (const guint8*)tlv_vp + GENERICTLV_HDRSZ + get_generic_tlv_len(tlv_vp, pktend);
196  /* Watch out for malformed packets! (BLACKHAT, PARANOIA) */
197  nextend = nexttlv + GENERICTLV_HDRSZ + get_generic_tlv_len(nexttlv, pktend);
198  /* fprintf(stderr, "LOOK: cur:%p, next: %p, nextend: %p, vpend: %p\n"
199  , tlv_vp, nexttlv, nextend, pktend); */
200  return nextend > (const guint8*)pktend ? NULL : nexttlv;
201 }
202 
204 gconstpointer
205 find_next_generic_tlv_type(gconstpointer tlv_vp,
206  guint16 tlvtype,
207  gconstpointer pktend)
208 {
209  while (NULL != tlv_vp && ((const guint8*)tlv_vp+GENERICTLV_HDRSZ) <= (const guint8*)pktend) {
210  if (get_generic_tlv_type(tlv_vp, pktend) == tlvtype) {
211  return tlv_vp;
212  }
213  tlv_vp = get_generic_tlv_next(tlv_vp, pktend);
214  }
215  return NULL;
216 }
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: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)
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
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)
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:53