iproute2/tc/m_ematch.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0 */
   2#ifndef __TC_EMATCH_H_
   3#define __TC_EMATCH_H_
   4
   5#include <ctype.h>
   6#include <stdlib.h>
   7#include <string.h>
   8#include <limits.h>
   9
  10#include "utils.h"
  11#include "tc_util.h"
  12
  13#define EMATCHKINDSIZ 16
  14
  15struct bstr {
  16        char    *data;
  17        unsigned int    len;
  18        int             quoted;
  19        struct bstr     *next;
  20};
  21
  22struct bstr *bstr_alloc(const char *text);
  23
  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(const 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
  48static inline struct bstr *bstr_next(struct bstr *b)
  49{
  50        return b->next;
  51}
  52
  53unsigned long bstrtoul(const struct bstr *b);
  54
  55struct ematch {
  56        struct bstr     *args;
  57        int             index;
  58        int             inverted;
  59        int             relation;
  60        int             child_ref;
  61        struct ematch   *child;
  62        struct ematch   *next;
  63};
  64
  65static inline struct ematch *new_ematch(struct bstr *args, int inverted)
  66{
  67        struct ematch *e = calloc(1, sizeof(*e));
  68
  69        if (e == NULL)
  70                return NULL;
  71
  72        e->args = args;
  73        e->inverted = inverted;
  74
  75        return e;
  76}
  77
  78void print_ematch_tree(const struct ematch *tree);
  79
  80struct ematch_util {
  81        char                    kind[EMATCHKINDSIZ];
  82        int                     kind_num;
  83        int     (*parse_eopt)(struct nlmsghdr *, struct tcf_ematch_hdr *,
  84                              struct bstr *);
  85        int     (*parse_eopt_argv)(struct nlmsghdr *, struct tcf_ematch_hdr *,
  86                                   int, char **);
  87        int     (*print_eopt)(FILE *, struct tcf_ematch_hdr *, void *, int);
  88        void    (*print_usage)(FILE *);
  89        struct ematch_util      *next;
  90};
  91
  92static inline int parse_layer(const struct bstr *b)
  93{
  94        if (*((char *) b->data) == 'l')
  95                return TCF_LAYER_LINK;
  96        else if (*((char *) b->data) == 'n')
  97                return TCF_LAYER_NETWORK;
  98        else if (*((char *) b->data) == 't')
  99                return TCF_LAYER_TRANSPORT;
 100        else
 101                return INT_MAX;
 102}
 103
 104__attribute__((format(printf, 5, 6)))
 105int em_parse_error(int err, struct bstr *args, struct bstr *carg,
 106                   struct ematch_util *, char *fmt, ...);
 107int print_ematch(FILE *, const struct rtattr *);
 108int parse_ematch(int *, char ***, int, struct nlmsghdr *);
 109
 110#endif
 111