shemminger | 6a805a6 | 2005-06-23 20:27:02 +0000 | [diff] [blame] | 1 | #ifndef __TC_EMATCH_H_ |
| 2 | #define __TC_EMATCH_H_ |
| 3 | |
| 4 | #include <ctype.h> |
shemminger | 737f15f | 2005-07-08 22:08:47 +0000 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
Strake | 5bd9dd4 | 2012-12-23 08:46:04 -0500 | [diff] [blame] | 7 | #include <limits.h> |
shemminger | 6a805a6 | 2005-06-23 20:27:02 +0000 | [diff] [blame] | 8 | |
| 9 | #include "utils.h" |
| 10 | #include "tc_util.h" |
| 11 | |
| 12 | #define EMATCHKINDSIZ 16 |
| 13 | |
| 14 | struct bstr |
| 15 | { |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 16 | char *data; |
shemminger | 6a805a6 | 2005-06-23 20:27:02 +0000 | [diff] [blame] | 17 | unsigned int len; |
| 18 | int quoted; |
| 19 | struct bstr *next; |
| 20 | }; |
| 21 | |
Stephen Hemminger | b6da1af | 2008-05-29 11:54:19 -0700 | [diff] [blame] | 22 | extern struct bstr * bstr_alloc(const char *text); |
shemminger | 6a805a6 | 2005-06-23 20:27:02 +0000 | [diff] [blame] | 23 | |
| 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 | |
| 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) |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 43 | return strncmp(b->data, text, len); |
shemminger | 6a805a6 | 2005-06-23 20:27:02 +0000 | [diff] [blame] | 44 | |
| 45 | return d; |
| 46 | } |
| 47 | |
shemminger | 6a805a6 | 2005-06-23 20:27:02 +0000 | [diff] [blame] | 48 | static inline struct bstr *bstr_next(struct bstr *b) |
| 49 | { |
| 50 | return b->next; |
| 51 | } |
| 52 | |
Stephen Hemminger | b6da1af | 2008-05-29 11:54:19 -0700 | [diff] [blame] | 53 | extern unsigned long bstrtoul(const struct bstr *b); |
| 54 | extern void bstr_print(FILE *fd, const struct bstr *b, int ascii); |
| 55 | |
| 56 | |
shemminger | 6a805a6 | 2005-06-23 20:27:02 +0000 | [diff] [blame] | 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 | |
| 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 | |
Stephen Hemminger | b6da1af | 2008-05-29 11:54:19 -0700 | [diff] [blame] | 81 | extern void print_ematch_tree(const struct ematch *tree); |
shemminger | 6a805a6 | 2005-06-23 20:27:02 +0000 | [diff] [blame] | 82 | |
shemminger | 6a805a6 | 2005-06-23 20:27:02 +0000 | [diff] [blame] | 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 | |
| 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 |