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