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 <string.h>
29 #include <tlvhelper.h>
30 #include <frametypes.h>
31 #include <generic_tlv_min.h>
32 
39 
40 
42 guint16
43 get_generic_tlv_type(gconstpointer tlv_vp,
44  gconstpointer pktend)
45 {
46  return tlv_get_guint16(tlv_vp, pktend);
47 }
48 
50 void
51 set_generic_tlv_type(gpointer tlv_vp,
52  guint16 newtype,
53  gconstpointer pktend)
54 {
55  tlv_set_guint16(tlv_vp, newtype, pktend);
56 }
57 
59 guint32
60 get_generic_tlv_len(gconstpointer tlv_vp,
61  gconstpointer pktend)
62 {
63  const guint8 * tlvp = tlv_vp;
64  return tlv_get_guint24(tlvp+sizeof(guint16), pktend);
65 }
66 
68 void
69 set_generic_tlv_len(gpointer tlv_vp,
70  guint32 newsize,
71  gconstpointer pktend)
72 {
73  guint8 * tlvp = tlv_vp;
74  tlv_set_guint24(tlvp+sizeof(guint16), newsize, pktend);
75 }
76 
78 gconstpointer
79 get_generic_tlv_value(gconstpointer tlv_vp,
80  gconstpointer pktend)
81 {
82  const guint8* tlvbytes = tlv_vp;
83  (void)pktend;
84  return (tlvbytes + GENERICTLV_HDRSZ);
85 }
86 
88 gpointer
90  gconstpointer pktend)
91 {
92  guint8* tlvbytes = tlv_vp;
93  (void)pktend;
94  return (tlvbytes + GENERICTLV_HDRSZ);
95 }
96 
99 void
100 set_generic_tlv_value(gpointer tlv_vp,
101  void* srcdata,
102  guint32 srcsize,
103  gconstpointer pktend)
104 {
105  guint8* tlvbytes = tlv_vp;
106  g_return_if_fail((gpointer)(tlvbytes+srcsize) <= pktend);
107  g_return_if_fail(srcsize == get_generic_tlv_len(tlv_vp, pktend));
108  memcpy(tlvbytes + GENERICTLV_HDRSZ, srcdata, srcsize);
109 }
110 
112 gboolean
113 is_valid_generic_tlv_packet(gconstpointer tlv_vp,//<[in] pointer to beginning of TLV packet
114  gconstpointer pktend) //<[in] pointer to first byte past the end of packet
115 {
116  const guint16 reqtypes [] = {FRAMETYPE_SIG};
117  unsigned j = 0;
118  int lasttype = -1;
119  if (NULL == tlv_vp || ((const guint8*)tlv_vp+GENERICTLV_HDRSZ) > (const guint8*)pktend) {
120  g_warning("TLV Invalid because packet is too short");
121  return FALSE;
122  }
123  for (tlv_vp = get_generic_tlv_first(tlv_vp, pktend)
124  ; NULL != tlv_vp
125  ; tlv_vp = get_generic_tlv_next(tlv_vp, pktend)) {
126 
127  guint16 ttype;
128  guint32 length;
129  const guint8* next;
130 
131  if (tlv_vp >= pktend) {
132  return FALSE;
133  }
134  ttype = get_generic_tlv_type(tlv_vp, pktend);
135  lasttype = ttype;
136  length = get_generic_tlv_len(tlv_vp, pktend);
137  next = (const guint8*)tlv_vp + (length+GENERICTLV_HDRSZ);
138  if (next > (const guint8*) pktend) {
139  g_warning("TLV Invalid because TLV entry extends past end");
140  return FALSE;
141  }
142  if (ttype == FRAMETYPE_END) {
143  if (get_generic_tlv_value(tlv_vp, pktend) == pktend) {
144  return length == 0;
145  }else{
146  g_warning("TLV Invalid because END item isn't at end of packet");
147  return FALSE;
148  }
149  }
150  if (j < DIMOF(reqtypes) && ttype != reqtypes[j]) {
151  g_warning("TLV Invalid because required TLV types aren't present in right order");
152  return FALSE;
153  }
154  j += 1;
155  }
156  g_warning("TLV Invalid because final type wasn't FRAMETYPE_END (it was %d)"
157  , lasttype);
158  return FALSE;
159 }
160 
161 gconstpointer
162 get_generic_tlv_first(gconstpointer packet,
163  gconstpointer pktend)
164 {
165  const guint8* inittlv = packet;
166  if (packet == NULL
167  || (inittlv + GENERICTLV_HDRSZ) > (const guint8*)pktend
168  || (inittlv + GENERICTLV_HDRSZ + get_generic_tlv_len(inittlv, pktend)) > (const guint8*)pktend) {
169  return NULL;
170  }
171  return inittlv;
172 }
173 
175 gconstpointer
176 get_generic_tlv_next(gconstpointer tlv_vp,
177  gconstpointer pktend)
178 {
179  const guint8* nexttlv;
180  const guint8* nextend;
181  if (tlv_vp == NULL
182  || ((const guint8*)tlv_vp+GENERICTLV_HDRSZ) > (const guint8*)pktend
183  || get_generic_tlv_type(tlv_vp, pktend) == FRAMETYPE_END) {
184  return NULL;
185  }
186  nexttlv = (const guint8*)tlv_vp + GENERICTLV_HDRSZ + get_generic_tlv_len(tlv_vp, pktend);
187  /* Watch out for malformed packets! (BLACKHAT, PARANOIA) */
188  nextend = nexttlv + GENERICTLV_HDRSZ + get_generic_tlv_len(nexttlv, pktend);
189  /* fprintf(stderr, "LOOK: cur:%p, next: %p, nextend: %p, vpend: %p\n"
190  , tlv_vp, nexttlv, nextend, pktend); */
191  return nextend > (const guint8*)pktend ? NULL : nexttlv;
192 }
193 
195 gconstpointer
196 find_next_generic_tlv_type(gconstpointer tlv_vp,
197  guint16 tlvtype,
198  gconstpointer pktend)
199 {
200  while (NULL != tlv_vp && ((const guint8*)tlv_vp+GENERICTLV_HDRSZ) <= (const guint8*)pktend) {
201  if (get_generic_tlv_type(tlv_vp, pktend) == tlvtype) {
202  return tlv_vp;
203  }
204  tlv_vp = get_generic_tlv_next(tlv_vp, pktend);
205  }
206  return NULL;
207 }
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