Fixed newline characters throughout the code
[com/gs-lite.git] / include / lfta / schema_prototypes.h
1 #ifndef __SCHEMA_PROTOTYPES__
2 #define __SCHEMA_PROTOTYPES__
3
4 /* ------------------------------------------------
5  Copyright 2014 AT&T Intellectual Property
6  Licensed under the Apache License, Version 2.0 (the "License");
7  you may not use this file except in compliance with the License.
8  You may obtain a copy of the License at
9  
10  http://www.apache.org/licenses/LICENSE-2.0
11  
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17  ------------------------------------------- */
18
19
20 #define PRECOMP
21 #include "gsconfig.h"
22 #include "gstypes.h"
23
24 #include "fta.h"
25 #include "rts_external.h"
26 #include "packet.h"
27 #include "md_stdlib.h"
28 #include "schemaparser.h"
29
30
31 // *** SAMPLING RELATED CONSTANTS
32 // ******************************
33
34 // Make sure the hash table sizes are powers of 2-1 since they are used to compute the module with an and.
35 // collions on the flow hash are handled properly so they are not that bad
36 #define MAX_FLOWRECORD_HASH 0x1fffff
37 // keep the collision rate low on denies
38 #define MAX_DENY_HASH 0x7fffff
39 // one out of SAMPLING_RATE+1 sampling
40 #define SAMPLING_RATE 19
41 // sampling probability
42 #define FLOWSAMPPROB (((gs_float_t)1)/((gs_float_t)SAMPLING_RATE+1))
43 // wait for SAMPLED_FLOW_TIMEOUT  seconds idle time to time a flow out
44 #define SAMPLED_FLOW_TIMEOUT 30
45 // check if we haven't seen that flow in SAMPLED_FLOW_IDLE_TIME seconds to make sure we don't catch it in the midle
46 #define SAMPLED_FLOW_IDLE_TIME 30
47
48 // PACKET SAMPLING RATE one in SAMPLED_PACKETS will be sampled
49 #define SAMPLED_PACKETS 200
50 // SAMPLING probability
51 #define PACKETSAMPPROB (((gs_float_t)1)/((gs_float_t)SAMPLED_PACKETS))
52
53 // COMBINE probability
54 #define COMBINEDPROB (((gs_float_t)1)-(((gs_float_t)1)-FLOWSAMPPROB)*(((gs_float_t)1)-PACKETSAMPPROB))
55
56 /* General packet access functions */
57
58 static inline gs_retval_t get_system_time(struct packet * p, gs_uint32_t * t)
59 {
60         *t=(gs_uint32_t) p->systemTime;
61         return 0;
62 }
63 static inline gs_retval_t get_schemaId(struct packet * p, gs_uint32_t * t)
64 {
65         *t=(gs_uint32_t) p->schema;
66         return 0;
67 }
68
69 /* CSV access function using position as 3rd argument */
70
71 static inline gs_retval_t get_csv_uint(struct packet * p, gs_uint32_t * t,gs_uint32_t pos)
72 {
73         if (p->ptype != PTYPE_CSV) return -1;
74         if (p->record.csv.numberfields < pos) return -1;
75     *t = strtoul((const char*)p->record.csv.fields[pos-1], NULL, 10);
76         return 0;
77 }
78
79 static inline gs_retval_t get_csv_ullong(struct packet * p, gs_uint64_t * t,gs_uint32_t pos)
80 {
81     if (p->ptype != PTYPE_CSV) return -1;
82     if (p->record.csv.numberfields < pos) return -1;
83     *t = strtoull((const char*)p->record.csv.fields[pos-1], NULL, 10);    
84     return 0;
85 }
86
87 static inline gs_retval_t get_csv_ip(struct packet * p, gs_uint32_t * t,gs_uint32_t pos)
88 {
89         unsigned ip1,ip2,ip3,ip4;
90     if (p->ptype != PTYPE_CSV) return -1;
91     if (p->record.csv.numberfields < pos) return -1;
92     sscanf((const char*) p->record.csv.fields[pos-1],"%u.%u.%u.%u",&ip1,&ip2,&ip3,&ip4);
93         *t=(ip1<<24)|(ip2<<16)|(ip3<<8)|ip4;
94     return 0;
95 }
96 static inline gs_retval_t get_csv_ipv6(struct packet * p, struct ipv6_str * t,gs_uint32_t pos)
97 {
98     gs_uint32_t v[8];
99         if (p->ptype != PTYPE_CSV) return -1;
100     if (p->record.csv.numberfields < pos) return -1;
101     sscanf((const char*) p->record.csv.fields[pos-1],"%x:%x:%x:%x:%x:%x:%x:%x",&v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
102         t->v[0]=htonl(v[0]<<16|v[1]);
103         t->v[1]=htonl(v[2]<<16|v[3]);
104         t->v[2]=htonl(v[4]<<16|v[5]);
105         t->v[3]=htonl(v[6]<<16|v[7]);
106         
107     return 0;
108 }
109 static inline gs_retval_t get_csv_string(struct packet * p, struct gs_string * t,gs_uint32_t pos)
110 {
111     
112     if (p->ptype != PTYPE_CSV) return -1;
113     if (p->record.csv.numberfields < pos) return -1;
114         t->data=(gs_sp_t)p->record.csv.fields[pos-1];
115     if (pos == p->record.csv.numberfields)
116         t->length=strlen((const char*)p->record.csv.fields[pos-1]);
117     else
118         t->length=p->record.csv.fields[pos] - p->record.csv.fields[pos-1] - 1;
119         t->owner=0;
120     return 0;
121 }
122 static inline gs_retval_t get_csv_bool(struct packet * p, gs_uint32_t * t,gs_uint32_t pos)
123 {
124     if (p->ptype != PTYPE_CSV) return -1;
125     if (p->record.csv.numberfields < pos) return -1;
126         *t=0;
127         if ((strlen((const char*)p->record.csv.fields[pos-1])==4) &&
128                 (strncasecmp("TRUE",(const char*)p->record.csv.fields[pos-1],4) ==0) ) {
129                 *t=1;
130         }
131     return 0;
132 }
133 static inline gs_retval_t get_csv_int(struct packet * p, gs_int32_t * t,gs_uint32_t pos)
134 {
135     if (p->ptype != PTYPE_CSV) return -1;
136     if (p->record.csv.numberfields < pos) return -1;
137     *t = strtol((const char*)p->record.csv.fields[pos-1], NULL, 10);
138     return 0;
139 }
140 static inline gs_retval_t get_csv_llong(struct packet * p, gs_int64_t * t,gs_uint32_t pos)
141 {
142     if (p->ptype != PTYPE_CSV) return -1;
143     if (p->record.csv.numberfields < pos) return -1;
144     *t = strtoll((const char*)p->record.csv.fields[pos-1], NULL, 10);
145     return 0;
146 }
147 static inline gs_retval_t get_csv_float(struct packet * p, gs_float_t * t,gs_uint32_t pos)
148 {
149     if (p->ptype != PTYPE_CSV) return -1;
150     if (p->record.csv.numberfields < pos) return -1;
151     *t = strtod((const char*)p->record.csv.fields[pos-1], NULL);
152     return 0;
153 }
154
155 #include <lfta/csv_macro.h>
156
157 static inline __attribute__((always_inline)) unsigned int gs_strtoul (const char *str, size_t len) {
158         unsigned int value = 0;
159
160         switch (len) { // handle up to 10 digits, assume we're 32-bit
161             case 10:    value += (str[len-10] - '0') * 1000000000;
162             case  9:    value += (str[len- 9] - '0') * 100000000;
163             case  8:    value += (str[len- 8] - '0') * 10000000;
164             case  7:    value += (str[len- 7] - '0') * 1000000;
165             case  6:    value += (str[len- 6] - '0') * 100000;
166             case  5:    value += (str[len- 5] - '0') * 10000;
167             case  4:    value += (str[len- 4] - '0') * 1000;
168             case  3:    value += (str[len- 3] - '0') * 100;
169             case  2:    value += (str[len- 2] - '0') * 10;
170             case  1:    value += (str[len- 1] - '0');
171                 return value;
172             default:
173                 return 0;
174         }
175 }
176
177 static inline __attribute__((always_inline)) unsigned long long gs_strtoull (const char *str, size_t len) {
178         unsigned long long value = 0;
179
180         switch (len) { // handle up to 10 digits, assume we're 32-bit
181             case 20:    value += (str[len-20] - '0') * 10000000000000000000UL;
182             case 19:    value += (str[len-19] - '0') * 1000000000000000000UL;
183             case 18:    value += (str[len-18] - '0') * 100000000000000000UL;
184             case 17:    value += (str[len-17] - '0') * 10000000000000000UL;
185             case 16:    value += (str[len-16] - '0') * 1000000000000000UL;
186             case 15:    value += (str[len-15] - '0') * 100000000000000UL;
187             case 14:    value += (str[len-14] - '0') * 10000000000000UL;
188             case 13:    value += (str[len-13] - '0') * 1000000000000UL;
189             case 12:    value += (str[len-12] - '0') * 100000000000UL;
190             case 11:    value += (str[len-11] - '0') * 10000000000UL;
191             case 10:    value += (str[len-10] - '0') * 1000000000UL;
192             case  9:    value += (str[len- 9] - '0') * 100000000UL;
193             case  8:    value += (str[len- 8] - '0') * 10000000UL;
194             case  7:    value += (str[len- 7] - '0') * 1000000UL;
195             case  6:    value += (str[len- 6] - '0') * 100000UL;
196             case  5:    value += (str[len- 5] - '0') * 10000UL;
197             case  4:    value += (str[len- 4] - '0') * 1000UL;
198             case  3:    value += (str[len- 3] - '0') * 100UL;
199             case  2:    value += (str[len- 2] - '0') * 10UL;
200             case  1:    value += (str[len- 1] - '0');
201                 return value;
202             default:
203                 return 0;
204         }
205 }
206
207 static inline gs_retval_t get_csv2_uint(struct packet * p, gs_uint32_t * t,gs_uint32_t pos)
208 {
209 //    *t = strtoul((const char*)p->record.csv2.fields[pos-1], NULL, 10);
210     *t = gs_strtoul((const char*)p->record.csv2.fields[pos-1], p->record.csv2.field_lens[pos-1]);
211     
212         return 0;
213 }
214
215 static inline gs_retval_t get_csv2_ullong(struct packet * p, gs_uint64_t * t,gs_uint32_t pos)
216 {
217 //    *t = strtoull((const char*)p->record.csv2.fields[pos-1], NULL, 10);    
218     *t = gs_strtoull((const char*)p->record.csv2.fields[pos-1], p->record.csv2.field_lens[pos-1]);
219     return 0;
220 }
221
222 static inline gs_retval_t get_csv2_ip(struct packet * p, gs_uint32_t * t,gs_uint32_t pos)
223 {
224         unsigned ip1,ip2,ip3,ip4;
225     sscanf((const char*) p->record.csv2.fields[pos-1],"%u.%u.%u.%u",&ip1,&ip2,&ip3,&ip4);
226         *t=(ip1<<24)|(ip2<<16)|(ip3<<8)|ip4;
227     return 0;
228 }
229 static inline gs_retval_t get_csv2_ipv6(struct packet * p, struct ipv6_str * t,gs_uint32_t pos)
230 {
231     gs_uint32_t v[8];
232     sscanf((const char*) p->record.csv2.fields[pos-1],"%x:%x:%x:%x:%x:%x:%x:%x",&v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6],&v[7]);
233         t->v[0]=htonl(v[0]<<16|v[1]);
234         t->v[1]=htonl(v[2]<<16|v[3]);
235         t->v[2]=htonl(v[4]<<16|v[5]);
236         t->v[3]=htonl(v[6]<<16|v[7]);
237         
238     return 0;
239 }
240 static inline gs_retval_t get_csv2_string(struct packet * p, struct gs_string * t,gs_uint32_t pos)
241
242         t->data=(gs_sp_t)p->record.csv2.fields[pos-1];
243     /*
244     if (pos == p->record.csv2.numberfields)
245         t->length=strlen((const char*)p->record.csv2.fields[pos-1]);
246     else
247         t->length=p->record.csv2.fields[pos] - p->record.csv2.fields[pos-1] - 1;
248     */
249     t->length=p->record.csv2.field_lens[pos-1];
250         t->owner=0;
251     return 0;
252 }
253 static inline gs_retval_t get_csv2_bool(struct packet * p, gs_uint32_t * t,gs_uint32_t pos)
254 {
255         *t=0;
256         if ((strlen((const char*)p->record.csv2.fields[pos-1])==4) &&
257                 (strncasecmp("TRUE",(const char*)p->record.csv2.fields[pos-1],4) ==0) ) {
258                 *t=1;
259         }
260     return 0;
261 }
262 static inline gs_retval_t get_csv2_int(struct packet * p, gs_int32_t * t,gs_uint32_t pos)
263 {
264     *t = strtol((const char*)p->record.csv2.fields[pos-1], NULL, 10);
265     return 0;
266 }
267 static inline gs_retval_t get_csv2_llong(struct packet * p, gs_int64_t * t,gs_uint32_t pos)
268 {
269     *t = strtoll((const char*)p->record.csv2.fields[pos-1], NULL, 10);
270     return 0;
271 }
272 static inline gs_retval_t get_csv2_float(struct packet * p, gs_float_t * t,gs_uint32_t pos)
273 {
274     *t = strtod((const char*)p->record.csv2.fields[pos-1], NULL);
275     return 0;
276 }
277
278 #include <lfta/csv2_macro.h>
279
280 /* GDAT access function using position as 3rd argument */
281
282 //#define GDATDEBUG
283
284 static inline gs_retval_t get_gdat_uint(struct packet * p, gs_uint32_t * t,gs_uint32_t pos)
285 {
286     struct access_result ar;
287 #ifdef GDATDEBUG
288         fprintf(stderr,"Decode uint");
289 #endif
290         if (p->ptype != PTYPE_GDAT) return -1;
291 #ifdef GDATDEBUG
292     fprintf(stderr,".");
293 #endif
294         if (p->record.gdat.numfields<pos) return -1;
295 #ifdef GDATDEBUG
296     fprintf(stderr,".");
297 #endif
298         ar=ftaschema_get_field_by_index(p->record.gdat.schema,pos-1,p->record.gdat.data,p->record.gdat.datasz);
299 #ifdef GDATDEBUG
300     fprintf(stderr,".");
301 #endif
302         if (ar.field_data_type!=UINT_TYPE) return -1;
303 #ifdef GDATDEBUG
304     fprintf(stderr,"DONE\n");
305 #endif
306         *t=ar.r.ui;
307     return 0;
308 }
309
310 static inline gs_retval_t get_gdat_ullong(struct packet * p, gs_uint64_t * t,gs_uint32_t pos)
311 {
312     struct access_result ar;
313 #ifdef GDATDEBUG
314     fprintf(stderr,"Decode ullong");
315 #endif
316     if (p->ptype != PTYPE_GDAT) return -1;
317 #ifdef GDATDEBUG
318     fprintf(stderr,".");
319 #endif
320     if (p->record.gdat.numfields<pos) return -1;
321 #ifdef GDATDEBUG
322     fprintf(stderr,".");
323 #endif
324     ar=ftaschema_get_field_by_index(p->record.gdat.schema,pos-1,p->record.gdat.data,p->record.gdat.datasz);
325 #ifdef GDATDEBUG
326     fprintf(stderr,".");
327 #endif
328     if (ar.field_data_type!=ULLONG_TYPE) return -1;
329 #ifdef GDATDEBUG
330     fprintf(stderr,"DONE\n");
331 #endif
332     *t=ar.r.ul;
333     return 0;
334 }
335
336 static inline gs_retval_t get_gdat_ip(struct packet * p, gs_uint32_t * t,gs_uint32_t pos)
337 {
338     struct access_result ar;
339     if (p->ptype != PTYPE_GDAT) return -1;
340 #ifdef GDATDEBUG
341     fprintf(stderr,"Decode ip");
342 #endif
343     if (p->record.gdat.numfields<pos) return -1;
344 #ifdef GDATDEBUG
345     fprintf(stderr,".");
346 #endif
347     ar=ftaschema_get_field_by_index(p->record.gdat.schema,pos-1,p->record.gdat.data,p->record.gdat.datasz);
348 #ifdef GDATDEBUG
349     fprintf(stderr,".");
350 #endif
351     if (ar.field_data_type!=IP_TYPE) return -1;
352 #ifdef GDATDEBUG
353     fprintf(stderr,"DONE\n");
354 #endif
355     *t=ar.r.ui;
356     return 0;
357 }
358 static inline gs_retval_t get_gdat_ipv6(struct packet * p, struct ipv6_str * t,gs_uint32_t pos)
359 {
360     struct access_result ar;
361     if (p->ptype != PTYPE_GDAT) return -1;
362 #ifdef GDATDEBUG
363     fprintf(stderr,"Decode ipv6");
364 #endif
365     if (p->record.gdat.numfields<pos) return -1;
366 #ifdef GDATDEBUG
367     fprintf(stderr,".");
368 #endif
369     ar=ftaschema_get_field_by_index(p->record.gdat.schema,pos-1,p->record.gdat.data,p->record.gdat.datasz);
370 #ifdef GDATDEBUG
371     fprintf(stderr,".");
372 #endif
373     if (ar.field_data_type!=IPV6_TYPE) return -1;
374 #ifdef GDATDEBUG
375     fprintf(stderr,"DONE\n");
376 #endif
377     t->v[0]=ar.r.ip6.v[0];
378     t->v[1]=ar.r.ip6.v[1];
379     t->v[2]=ar.r.ip6.v[2];
380     t->v[3]=ar.r.ip6.v[3];
381     return 0;
382 }
383 static inline gs_retval_t get_gdat_string(struct packet * p, struct gs_string * t,gs_uint32_t pos)
384 {
385     struct access_result ar;
386 #ifdef GDATDEBUG
387     fprintf(stderr,"Decode string");
388 #endif
389     if (p->ptype != PTYPE_GDAT) return -1;
390 #ifdef GDATDEBUG
391     fprintf(stderr,".");
392 #endif
393     if (p->record.gdat.numfields<pos) return -1;
394 #ifdef GDATDEBUG
395     fprintf(stderr,".");
396 #endif
397     ar=ftaschema_get_field_by_index(p->record.gdat.schema,pos-1,p->record.gdat.data,p->record.gdat.datasz);
398 #ifdef GDATDEBUG
399     fprintf(stderr,".");
400 #endif
401     if (ar.field_data_type!=VSTR_TYPE) return -1;
402 #ifdef GDATDEBUG
403     fprintf(stderr,"DONE\n");
404 #endif
405     t->data=(gs_sp_t)ar.r.vs.offset;
406         t->length=ar.r.vs.length;
407     return 0;
408 }
409 static inline gs_retval_t get_gdat_bool(struct packet * p, gs_uint32_t * t,gs_uint32_t pos)
410 {
411     struct access_result ar;
412 #ifdef GDATDEBUG
413     fprintf(stderr,"Decode bool");
414 #endif
415     if (p->ptype != PTYPE_GDAT) return -1;
416 #ifdef GDATDEBUG
417     fprintf(stderr,".");
418 #endif
419     if (p->record.gdat.numfields<pos) return -1;
420 #ifdef GDATDEBUG
421     fprintf(stderr,".");
422 #endif
423     ar=ftaschema_get_field_by_index(p->record.gdat.schema,pos-1,p->record.gdat.data,p->record.gdat.datasz);
424 #ifdef GDATDEBUG
425     fprintf(stderr,".");
426 #endif
427     if (ar.field_data_type!=BOOL_TYPE) return -1;
428 #ifdef GDATDEBUG
429     fprintf(stderr,"DONE\n");
430 #endif
431     *t=ar.r.ui;
432     return 0;
433 }
434 static inline gs_retval_t get_gdat_int(struct packet * p, gs_int32_t * t,gs_uint32_t pos)
435 {
436     struct access_result ar;
437 #ifdef GDATDEBUG
438     fprintf(stderr,"Decode int");
439 #endif
440     if (p->ptype != PTYPE_GDAT) return -1;
441 #ifdef GDATDEBUG
442     fprintf(stderr,".");
443 #endif
444     if (p->record.gdat.numfields<pos) return -1;
445 #ifdef GDATDEBUG
446     fprintf(stderr,".");
447 #endif
448     ar=ftaschema_get_field_by_index(p->record.gdat.schema,pos-1,p->record.gdat.data,p->record.gdat.datasz);
449 #ifdef GDATDEBUG
450     fprintf(stderr,".");
451 #endif
452     if (ar.field_data_type!=INT_TYPE) return -1;
453 #ifdef GDATDEBUG
454     fprintf(stderr,"DONE\n");
455 #endif
456     *t=ar.r.i;
457     return 0;
458 }
459 static inline gs_retval_t get_gdat_llong(struct packet * p, gs_int64_t * t,gs_uint32_t pos)
460 {
461     struct access_result ar;
462 #ifdef GDATDEBUG
463     fprintf(stderr,"Decode llong");
464 #endif
465     if (p->ptype != PTYPE_GDAT) return -1;
466 #ifdef GDATDEBUG
467     fprintf(stderr,".");
468 #endif
469     if (p->record.gdat.numfields<pos) return -1;
470 #ifdef GDATDEBUG
471     fprintf(stderr,".");
472 #endif
473     ar=ftaschema_get_field_by_index(p->record.gdat.schema,pos-1,p->record.gdat.data,p->record.gdat.datasz);
474 #ifdef GDATDEBUG
475     fprintf(stderr,".");
476 #endif
477     if (ar.field_data_type!=LLONG_TYPE) return -1;
478 #ifdef GDATDEBUG
479     fprintf(stderr,"DONE\n");
480 #endif
481     *t=ar.r.l;
482     return 0;
483 }
484 static inline gs_retval_t get_gdat_float(struct packet * p, gs_float_t * t,gs_uint32_t pos)
485 {
486     struct access_result ar;
487 #ifdef GDATDEBUG
488     fprintf(stderr,"Decode float");
489 #endif
490     if (p->ptype != PTYPE_GDAT) return -1;
491 #ifdef GDATDEBUG
492     fprintf(stderr,".");
493 #endif
494     if (p->record.gdat.numfields<pos) return -1;
495 #ifdef GDATDEBUG
496     fprintf(stderr,".");
497 #endif
498     ar=ftaschema_get_field_by_index(p->record.gdat.schema,pos-1,p->record.gdat.data,p->record.gdat.datasz);
499 #ifdef GDATDEBUG
500     fprintf(stderr,".");
501 #endif
502     if (ar.field_data_type!=FLOAT_TYPE) return -1;
503 #ifdef GDATDEBUG
504     fprintf(stderr,"DONE\n");
505 #endif
506     *t=ar.r.f;
507     return 0;
508 }
509
510 #include <lfta/gdat_macro.h>
511
512 // External functions
513
514
515
516
517 #endif
518