blob: 81456aac3d5a80dc18f58d5d22a9b10dfc4ce8f8 [file] [log] [blame]
shemminger6a805a62005-06-23 20:27:02 +00001#ifndef __TC_EMATCH_H_
2#define __TC_EMATCH_H_
3
4#include <ctype.h>
shemminger737f15f2005-07-08 22:08:47 +00005#include <stdlib.h>
6#include <string.h>
Strake5bd9dd42012-12-23 08:46:04 -05007#include <limits.h>
shemminger6a805a62005-06-23 20:27:02 +00008
9#include "utils.h"
10#include "tc_util.h"
11
12#define EMATCHKINDSIZ 16
13
14struct bstr
15{
shemmingerf332d162005-07-05 22:37:15 +000016 char *data;
shemminger6a805a62005-06-23 20:27:02 +000017 unsigned int len;
18 int quoted;
19 struct bstr *next;
20};
21
Stephen Hemmingerb6da1af2008-05-29 11:54:19 -070022extern struct bstr * bstr_alloc(const char *text);
shemminger6a805a62005-06-23 20:27:02 +000023
24static inline struct bstr * bstr_new(char *data, unsigned int len)
25{
26 struct bstr *b = calloc(1, sizeof(*b));
27
28 if (b == NULL)
29 return NULL;
30
31 b->data = data;
32 b->len = len;
33
34 return b;
35}
36
37static inline int bstrcmp(struct bstr *b, const char *text)
38{
39 int len = strlen(text);
40 int d = b->len - len;
41
42 if (d == 0)
shemmingerf332d162005-07-05 22:37:15 +000043 return strncmp(b->data, text, len);
shemminger6a805a62005-06-23 20:27:02 +000044
45 return d;
46}
47
shemminger6a805a62005-06-23 20:27:02 +000048static inline struct bstr *bstr_next(struct bstr *b)
49{
50 return b->next;
51}
52
Stephen Hemmingerb6da1af2008-05-29 11:54:19 -070053extern unsigned long bstrtoul(const struct bstr *b);
54extern void bstr_print(FILE *fd, const struct bstr *b, int ascii);
55
56
shemminger6a805a62005-06-23 20:27:02 +000057struct ematch
58{
59 struct bstr *args;
60 int index;
61 int inverted;
62 int relation;
63 int child_ref;
64 struct ematch *child;
65 struct ematch *next;
66};
67
68static inline struct ematch * new_ematch(struct bstr *args, int inverted)
69{
70 struct ematch *e = calloc(1, sizeof(*e));
71
72 if (e == NULL)
73 return NULL;
74
75 e->args = args;
76 e->inverted = inverted;
77
78 return e;
79}
80
Stephen Hemmingerb6da1af2008-05-29 11:54:19 -070081extern void print_ematch_tree(const struct ematch *tree);
shemminger6a805a62005-06-23 20:27:02 +000082
shemminger6a805a62005-06-23 20:27:02 +000083
84struct ematch_util
85{
86 char kind[EMATCHKINDSIZ];
87 int kind_num;
88 int (*parse_eopt)(struct nlmsghdr *,struct tcf_ematch_hdr *,
89 struct bstr *);
90 int (*print_eopt)(FILE *, struct tcf_ematch_hdr *, void *, int);
91 void (*print_usage)(FILE *);
92 struct ematch_util *next;
93};
94
95static inline int parse_layer(struct bstr *b)
96{
97 if (*((char *) b->data) == 'l')
98 return TCF_LAYER_LINK;
99 else if (*((char *) b->data) == 'n')
100 return TCF_LAYER_NETWORK;
101 else if (*((char *) b->data) == 't')
102 return TCF_LAYER_TRANSPORT;
103 else
104 return INT_MAX;
105}
106
107extern int em_parse_error(int err, struct bstr *args, struct bstr *carg,
108 struct ematch_util *, char *fmt, ...);
109extern int print_ematch(FILE *, const struct rtattr *);
110extern int parse_ematch(int *, char ***, int, struct nlmsghdr *);
111
112#endif