• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __TC_EMATCH_H_
2 #define __TC_EMATCH_H_
3 
4 #include <ctype.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <limits.h>
8 
9 #include "utils.h"
10 #include "tc_util.h"
11 
12 #define EMATCHKINDSIZ 16
13 
14 struct bstr
15 {
16 	char	*data;
17 	unsigned int	len;
18 	int		quoted;
19 	struct bstr	*next;
20 };
21 
22 extern struct bstr * bstr_alloc(const char *text);
23 
bstr_new(char * data,unsigned int len)24 static 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 
bstrcmp(struct bstr * b,const char * text)37 static 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)
43 		return strncmp(b->data, text, len);
44 
45 	return d;
46 }
47 
bstr_next(struct bstr * b)48 static inline struct bstr *bstr_next(struct bstr *b)
49 {
50 	return b->next;
51 }
52 
53 extern unsigned long bstrtoul(const struct bstr *b);
54 extern void bstr_print(FILE *fd, const struct bstr *b, int ascii);
55 
56 
57 struct 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 
new_ematch(struct bstr * args,int inverted)68 static 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 
81 extern void print_ematch_tree(const struct ematch *tree);
82 
83 
84 struct 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 
parse_layer(struct bstr * b)95 static 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 
107 extern int em_parse_error(int err, struct bstr *args, struct bstr *carg,
108 		   struct ematch_util *, char *fmt, ...);
109 extern int print_ematch(FILE *, const struct rtattr *);
110 extern int parse_ematch(int *, char ***, int, struct nlmsghdr *);
111 
112 #endif
113