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 Vstr_base *, size_t, size_t);
24 : extern Opt_serv_policy_opts *opt_policy_conf_find(Opt_serv_opts *,
25 : const Conf_parse *,
26 : Conf_token *);
27 :
28 : extern void opt_policy_add(Opt_serv_opts *, Opt_serv_policy_opts *)
29 : COMPILE_ATTR_NONNULL_A();
30 :
31 : extern int opt_policy_ipv4_make(Conf_parse *, Conf_token *,
32 : unsigned int, struct sockaddr *, int *)
33 : COMPILE_ATTR_NONNULL_A();
34 : extern int opt_policy_ipv4_cidr_eq(Opt_policy_ipv4 *, struct sockaddr *)
35 : COMPILE_ATTR_NONNULL_A();
36 :
37 :
38 : extern Opt_serv_policy_opts *opt_policy_sc_conf_make(Opt_serv_opts *,
39 : const Conf_parse *,
40 : const Conf_token *,
41 : const Vstr_sect_node *)
42 : COMPILE_ATTR_NONNULL_A();
43 : extern unsigned int opt_policy_sc_conf_parse(Opt_serv_opts *,
44 : const Conf_parse *, Conf_token *,
45 : Opt_serv_policy_opts **,
46 : Conf_token **)
47 : COMPILE_ATTR_NONNULL_A();
48 : extern void opt_policy_sc_all_ref_del(Opt_serv_opts *);
49 :
50 :
51 : #if !defined(OPT_POLICY_COMPILE_INLINE)
52 : # if ! COMPILE_DEBUG
53 : # define OPT_POLICY_COMPILE_INLINE 1
54 : # else
55 : # define OPT_POLICY_COMPILE_INLINE 0
56 : # endif
57 : #endif
58 :
59 : #if defined(VSTR_AUTOCONF_HAVE_INLINE) && OPT_POLICY_COMPILE_INLINE
60 :
61 : #if COMPILE_DEBUG
62 : # define OPT_POLICY__ASSERT ASSERT
63 : #else
64 : # define OPT_POLICY__ASSERT(x)
65 : #endif
66 :
67 : #ifndef OPT_POLICY__EI
68 : # define OPT_POLICY__EI extern inline
69 : #endif
70 :
71 : #define OPT_POLICY__TRUE 1
72 : #define OPT_POLICY__FALSE 0
73 :
74 : OPT_POLICY__EI int opt_policy_copy(Opt_serv_policy_opts *dst,
75 : const Opt_serv_policy_opts *src)
76 142 : {
77 142 : memcpy(&dst->io_limit, &src->io_limit, sizeof(struct Evnt_limit));
78 142 : memcpy(&dst->io_nslimit, &src->io_nslimit, sizeof(struct Evnt_limit));
79 :
80 142 : dst->idle_timeout = src->idle_timeout;
81 142 : dst->max_timeout = src->max_timeout;
82 142 : dst->max_connections = src->max_connections;
83 :
84 142 : dst->use_insta_close = src->use_insta_close;
85 :
86 142 : return (OPT_POLICY__TRUE);
87 : }
88 :
89 : OPT_POLICY__EI Opt_serv_policy_opts *opt_policy_find(Opt_serv_opts *beg_opts,
90 : const Vstr_base *s1,
91 : size_t pos, size_t len)
92 384 : {
93 384 : Opt_serv_policy_opts *opts = beg_opts->def_policy;
94 :
95 384 : if (vstr_cmp_cstr_eq(s1, pos, len, "<default>"))
96 42 : return (opts);
97 :
98 171 : OPT_POLICY__ASSERT(vstr_cmp_cstr_eq(opts->policy_name, 1,
99 : opts->policy_name->len, "<default>"));
100 :
101 2187 : while ((opts = opts->next))
102 : {
103 1926 : Vstr_base *tmp = opts->policy_name;
104 1926 : int cmp = 0;
105 :
106 963 : OPT_POLICY__ASSERT(!opts->next ||
107 : (tmp->len < opts->next->policy_name->len) ||
108 : ((tmp->len == opts->next->policy_name->len) &&
109 : vstr_cmp(tmp, 1, tmp->len,
110 : opts->next->policy_name, 1,
111 : opts->next->policy_name->len) < 0));
112 :
113 1926 : if (len > tmp->len)
114 944 : continue;
115 982 : if (len < tmp->len)
116 72 : break;
117 910 : cmp = vstr_cmp(s1, pos, len, tmp, 1, tmp->len);
118 910 : if (!cmp)
119 180 : return (opts);
120 730 : if (cmp < 0)
121 0 : break;
122 : }
123 :
124 162 : return (NULL);
125 : }
126 :
127 : OPT_POLICY__EI
128 : Opt_serv_policy_opts *opt_policy_conf_find(Opt_serv_opts *beg_opts,
129 : const Conf_parse *conf,
130 : Conf_token *token)
131 368 : {
132 368 : const Vstr_sect_node *val = conf_token_value(token);
133 :
134 368 : if (!val)
135 0 : return (NULL);
136 :
137 368 : return (opt_policy_find(beg_opts, conf->data, val->pos, val->len));
138 : }
139 :
140 : #endif
141 :
142 : #endif
|