1 /*
2  * include/proto/sample.h
3  * Functions for samples management.
4  *
5  * Copyright (C) 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
6  * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation, version 2.1
11  * exclusively.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22 
23 #ifndef _PROTO_SAMPLE_H
24 #define _PROTO_SAMPLE_H
25 
26 #include <types/sample.h>
27 #include <types/stick_table.h>
28 
29 extern const char *smp_to_type[SMP_TYPES];
30 
31 struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err, struct arg_list *al);
32 struct sample_conv *find_sample_conv(const char *kw, int len);
33 struct sample *sample_process(struct proxy *px, struct session *sess,
34                               struct stream *strm, unsigned int opt,
35                               struct sample_expr *expr, struct sample *p);
36 struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
37                                    struct stream *strm, unsigned int opt,
38                                    struct sample_expr *expr, int smp_type);
39 int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type);
40 int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp);
41 int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp);
42 void release_sample_expr(struct sample_expr *expr);
43 void sample_register_fetches(struct sample_fetch_kw_list *psl);
44 void sample_register_convs(struct sample_conv_kw_list *psl);
45 const char *sample_src_names(unsigned int use);
46 const char *sample_ckp_names(unsigned int use);
47 struct sample_fetch *find_sample_fetch(const char *kw, int len);
48 struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx);
49 struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx);
50 int smp_resolve_args(struct proxy *p);
51 int smp_expr_output_type(struct sample_expr *expr);
52 int c_none(struct sample *smp);
53 int smp_dup(struct sample *smp);
54 
55 /*
56  * This function just apply a cast on sample. It returns 0 if the cast is not
57  * available or if the cast fails, otherwise returns 1. It does not modify the
58  * input sample on failure.
59  */
60 static inline
sample_convert(struct sample * sample,int req_type)61 int sample_convert(struct sample *sample, int req_type)
62 {
63 	if (!sample_casts[sample->data.type][req_type])
64 		return 0;
65 	if (sample_casts[sample->data.type][req_type] == c_none)
66 		return 1;
67 	return sample_casts[sample->data.type][req_type](sample);
68 }
69 
70 static inline
smp_set_owner(struct sample * smp,struct proxy * px,struct session * sess,struct stream * strm,int opt)71 struct sample *smp_set_owner(struct sample *smp, struct proxy *px,
72                              struct session *sess, struct stream *strm, int opt)
73 {
74 	smp->px   = px;
75 	smp->sess = sess;
76 	smp->strm = strm;
77 	smp->opt  = opt;
78 	return smp;
79 }
80 
81 
82 /* Returns 1 if a sample may be safely used. It performs a few checks on the
83  * string length versus size, same for the binary version, and ensures that
84  * strings are properly terminated by a zero. If this last point is not granted
85  * but the string is not const, then the \0 is appended. Otherwise it returns 0,
86  * meaning the caller may need to call smp_dup() before going further.
87  */
88 static inline
smp_is_safe(struct sample * smp)89 int smp_is_safe(struct sample *smp)
90 {
91 	switch (smp->data.type) {
92 	case SMP_T_METH:
93 		if (smp->data.u.meth.meth != HTTP_METH_OTHER)
94 			return 1;
95 		/* Fall through */
96 
97 	case SMP_T_STR:
98 		if (!smp->data.u.str.size || smp->data.u.str.data >= smp->data.u.str.size)
99 			return 0;
100 
101 		if (smp->data.u.str.area[smp->data.u.str.data] == 0)
102 			return 1;
103 
104 		if (smp->flags & SMP_F_CONST)
105 			return 0;
106 
107 		smp->data.u.str.area[smp->data.u.str.data] = 0;
108 		return 1;
109 
110 	case SMP_T_BIN:
111 		return !smp->data.u.str.size || smp->data.u.str.data <= smp->data.u.str.size;
112 
113 	default:
114 		return 1;
115 	}
116 }
117 
118 /* checks that a sample may freely be used, or duplicates it to normalize it.
119  * Returns 1 on success, 0 if the sample must not be used. The function also
120  * checks for NULL to simplify the calling code.
121  */
122 static inline
smp_make_safe(struct sample * smp)123 int smp_make_safe(struct sample *smp)
124 {
125 	return smp && (smp_is_safe(smp) || smp_dup(smp));
126 }
127 
128 /* Returns 1 if a sample may be safely modified in place. It performs a few
129  * checks on the string length versus size, same for the binary version, and
130  * ensures that strings are properly terminated by a zero, and of course that
131  * the size is allocate and that the SMP_F_CONST flag is not set. If only the
132  * trailing zero is missing, it is appended. Otherwise it returns 0, meaning
133  * the caller may need to call smp_dup() before going further.
134  */
135 static inline
smp_is_rw(struct sample * smp)136 int smp_is_rw(struct sample *smp)
137 {
138 	if (smp->flags & SMP_F_CONST)
139 		return 0;
140 
141 	switch (smp->data.type) {
142 	case SMP_T_METH:
143 		if (smp->data.u.meth.meth != HTTP_METH_OTHER)
144 			return 1;
145 		/* Fall through */
146 
147 	case SMP_T_STR:
148 		if (!smp->data.u.str.size ||
149 		    smp->data.u.str.data >= smp->data.u.str.size)
150 			return 0;
151 
152 		if (smp->data.u.str.area[smp->data.u.str.data] != 0)
153 			smp->data.u.str.area[smp->data.u.str.data] = 0;
154 		return 1;
155 
156 	case SMP_T_BIN:
157 		return smp->data.u.str.size &&
158 		       smp->data.u.str.data <= smp->data.u.str.size;
159 
160 	default:
161 		return 1;
162 	}
163 }
164 
165 /* checks that a sample may freely be modified, or duplicates it to normalize
166  * it and make it R/W. Returns 1 on success, 0 if the sample must not be used.
167  * The function also checks for NULL to simplify the calling code.
168  */
169 static inline
smp_make_rw(struct sample * smp)170 int smp_make_rw(struct sample *smp)
171 {
172 	return smp && (smp_is_rw(smp) || smp_dup(smp));
173 }
174 
175 #endif /* _PROTO_SAMPLE_H */
176