1 #ifndef OPT_POLICY_H
2 #define OPT_POLICY_H
3 
4 #include "opt_serv.h"
5 
6 #include <netinet/in.h>
7 
8 #define OPT_POLICY_CONF_DEF_POLICY_NAME "<default>"
9 
10 typedef struct Opt_policy_ipv4
11 {
12   unsigned char ipv4[4];
13   unsigned int cidr;
14 } Opt_policy_ipv4;
15 
16 extern void opt_policy_exit(Opt_serv_policy_opts *);
17 extern int opt_policy_init(Opt_serv_opts *, Opt_serv_policy_opts *);
18 extern Opt_serv_policy_opts *opt_policy_make(Opt_serv_opts *);
19 extern int opt_policy_copy(Opt_serv_policy_opts *,
20                            const Opt_serv_policy_opts *);
21 
22 extern Opt_serv_policy_opts *opt_policy_find(Opt_serv_opts *,
23                                              const Conf_parse *, Conf_token *);
24 
25 extern void opt_policy_add(Opt_serv_opts *, Opt_serv_policy_opts *);
26 
27 extern int opt_policy_name_eq(const Conf_parse *, Conf_token *,
28                               const Opt_serv_policy_opts *, int *);
29 
30 extern int opt_policy_ipv4_make(Conf_parse *, Conf_token *,
31                                 unsigned int, struct sockaddr *, int *);
32 extern int opt_policy_ipv4_cidr_eq(Opt_policy_ipv4 *, struct sockaddr *);
33 
34 
35 extern Opt_serv_policy_opts *opt_policy_sc_conf_make(Opt_serv_opts *,
36                                                      const Conf_parse *,
37                                                      const Conf_token *,
38                                                      const Vstr_sect_node *);
39 extern unsigned int opt_policy_sc_conf_parse(Opt_serv_opts *,
40                                              const Conf_parse *, Conf_token *,
41                                              Opt_serv_policy_opts **);
42 extern void opt_policy_sc_all_ref_del(Opt_serv_opts *);
43 
44 
45 #if !defined(OPT_POLICY_COMPILE_INLINE)
46 # ifdef VSTR_AUTOCONF_NDEBUG
47 #  define OPT_POLICY_COMPILE_INLINE 1
48 # else
49 #  define OPT_POLICY_COMPILE_INLINE 0
50 # endif
51 #endif
52 
53 #if defined(VSTR_AUTOCONF_HAVE_INLINE) && OPT_POLICY_COMPILE_INLINE
54 
55 #ifndef VSTR_AUTOCONF_NDEBUG
56 # define OPT_POLICY__ASSERT ASSERT
57 #else
58 # define OPT_POLICY__ASSERT(x)
59 #endif
60 
61 #define OPT_POLICY__TRUE  1
62 #define OPT_POLICY__FALSE 0
63 
opt_policy_copy(Opt_serv_policy_opts * dst,const Opt_serv_policy_opts * src)64 extern inline int opt_policy_copy(Opt_serv_policy_opts *dst,
65                                   const Opt_serv_policy_opts *src)
66 {
67   dst->idle_timeout    = src->idle_timeout;
68   dst->max_connections = src->max_connections;
69 
70   return (OPT_POLICY__TRUE);
71 }
72 
opt_policy_find(Opt_serv_opts * beg_opts,const Conf_parse * conf,Conf_token * token)73 extern inline Opt_serv_policy_opts *opt_policy_find(Opt_serv_opts *beg_opts,
74                                                     const Conf_parse *conf,
75                                                     Conf_token *token)
76 {
77   const Vstr_sect_node *val = conf_token_value(token);
78   Opt_serv_policy_opts *opts = NULL;
79 
80   if (!val)
81     return (NULL);
82 
83   opts = beg_opts->def_policy;
84 
85   if (vstr_cmp_cstr_eq(conf->data, val->pos, val->len, "<default>"))
86     return (opts);
87 
88   while ((opts = opts->next))
89   {
90     Vstr_base *tmp = opts->policy_name;
91     int cmp = 0;
92 
93     OPT_POLICY__ASSERT(!opts->next ||
94                        (tmp->len < opts->next->policy_name->len) ||
95                        ((tmp->len == opts->next->policy_name->len) &&
96                         vstr_cmp(tmp, 1, tmp->len,
97                                  opts->next->policy_name, 1,
98                                  opts->next->policy_name->len) < 0));
99 
100     if (val->len > tmp->len)
101       continue;
102     if (val->len < tmp->len)
103       break;
104     cmp = vstr_cmp(conf->data, val->pos, val->len, tmp, 1, tmp->len);
105     if (!cmp)
106       return (opts);
107     if (cmp < 0)
108       break;
109   }
110 
111   return (NULL);
112 }
113 
opt_policy_name_eq(const Conf_parse * conf,Conf_token * token,const Opt_serv_policy_opts * policy,int * matches)114 extern inline int opt_policy_name_eq(const Conf_parse *conf, Conf_token *token,
115                                      const Opt_serv_policy_opts *policy,
116                                      int *matches)
117 {
118   const Vstr_base *tmp = policy->policy_name;
119 
120   CONF_SC_PARSE_TOP_TOKEN_RET(conf, token, OPT_POLICY__FALSE);
121 
122   *matches = conf_token_cmp_val_eq(conf, token, tmp, 1, tmp->len);
123 
124   return (OPT_POLICY__TRUE);
125 }
126 
127 #endif
128 
129 #endif
130