2 * Copyright (C) 2009, 2013 Henning Westerholt
3 * Copyright (C) 2013 Charles Chance, sipcentric.com
5 * This file is part of Kamailio, a free SIP server.
7 * Kamailio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version
12 * Kamailio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
23 * \brief memcached module
28 #include "memcached.h"
29 #include "../../core/ut.h"
30 #include "../../core/mem/mem.h"
31 #include "../../core/pvapi.h"
32 #include "../pv/pv_svar.h"
33 #include "../../core/md5utils.h"
37 * \brief Checks for '=>' delimiter in key name string and if present, extracts expiry value.
38 * \param data string to parse
39 * \param key output string name
40 * \param exp output int expiry (if present)
41 * \return 0 on success, negative on failure
43 static inline int pv_mcd_key_expiry_split_str(str *data, str *key, unsigned int *exp) {
49 if (data == NULL || data->s == NULL || data->len <= 0) {
50 LM_ERR("invalid parameters\n");
58 while(p < data->s + data->len) {
72 if (key->len < data->len) {
73 /* delimiter is present, try to extract expiry value */
75 if (p < data->s + data->len) {
78 while(p<data->s+data->len) {
83 if (str_exp.len > 0) {
85 *exp = atoi(str_exp.s);
87 LM_DBG("key is %.*s expiry is %d\n", key->len, key->s, *exp);
94 * \brief Checks if the key is avaiable and not too long, hashing it with MD5 if necessary.
95 * \param msg SIP message
96 * \param param pseudo-variable input parameter
97 * \param key output string name
98 * \param exp output int expiry (if present)
99 * \return 0 on success, negative on failure
101 static inline int pv_mcd_key_check(struct sip_msg *msg, pv_param_t *param, str * key, unsigned int * exp ) {
106 static char hash[32];
108 if (msg == NULL || param == NULL) {
109 LM_ERR("bad parameters\n");
113 if (pv_printf_s(msg, param->pvn.u.dname, &pvn) != 0)
115 LM_ERR("cannot get pv name\n");
119 if (pv_mcd_key_expiry_split_str(&pvn, &tmp, exp) != 0) {
127 LM_DBG("key too long (%d), hash it\n", tmp.len);
128 MD5StringArray (hash, &tmp, 1);
136 * \brief Helper to get a cached value from memcached
137 * \param msg SIP message
138 * \param key value key
139 * \param return_value returned value
140 * \param flags returned flags
141 * \return null on success, negative on failure
143 static int pv_get_mcd_value_helper(struct sip_msg *msg, str *key,
144 char **return_value, uint32_t *flags) {
147 size_t return_value_length;
149 *return_value = memcached_get(memcached_h, key->s, key->len, &return_value_length, flags, &rc);
151 if (*return_value == NULL) {
152 if (rc == MEMCACHED_NOTFOUND) {
153 LM_DBG("key %.*s not found\n", key->len, key->s);
155 LM_ERR("could not get result for key %.*s - error was '%s'\n", key->len, key->s, memcached_strerror(memcached_h, rc));
160 LM_DBG("result: %s for key %.*s with flag %d\n", *return_value, key->len, key->s, *flags);
165 static void pv_free_mcd_value(char** buf) {
176 * \brief Get a cached value from memcached
177 * \param msg SIP message
178 * \param param parameter
180 * \return null on success, negative on failure
182 int pv_get_mcd_value(struct sip_msg *msg, pv_param_t *param, pv_value_t *res) {
184 unsigned int res_int = 0;
186 unsigned int expiry = mcd_expire;
189 uint32_t return_flags;
191 if (pv_mcd_key_check(msg, param, &key, &expiry) < 0) {
192 return pv_get_null(msg, param, res);
196 return pv_get_null(msg, param, res);
198 if (pv_get_mcd_value_helper(msg, &key, &return_value, &return_flags) < 0) {
203 res_str.len = strlen(return_value);
204 res_str.s = return_value;
207 /* apparently memcached adds whitespaces to the beginning of the value after atomic operations */
209 trim_len(res_str.len, res_str.s, res_str);
211 if(return_flags&VAR_VAL_STR || mcd_stringify) {
212 res->rs.s = pv_get_buffer();
213 res->rs.len = pv_get_buffer_size();
214 if(res_str.len>=res->rs.len) {
215 LM_ERR("value is too big (%d) - increase pv buffer size\n", res_str.len);
218 memcpy(res->rs.s, res_str.s, res_str.len);
219 res->rs.len = res_str.len;
220 res->rs.s[res->rs.len] = '\0';
221 res->flags = PV_VAL_STR;
223 if (str2int(&res_str, &res_int) < 0) {
224 LM_ERR("could not convert string %.*s to integer value\n", res_str.len, res_str.s);
229 res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
232 pv_free_mcd_value(&return_value);
236 pv_free_mcd_value(&return_value);
237 return pv_get_null(msg, param, res);
243 * \brief Set a value in the cache of memcached
244 * \todo Replacement of already existing values is not done atomically at the moment.
245 * Here the provided replace function should be used.
246 * \param msg SIP message
247 * \param param parameter
250 * \return 0 on success, -1 on failure
252 int pv_set_mcd_value(struct sip_msg* msg, pv_param_t *param, int op, pv_value_t *val) {
254 unsigned int val_flag = 0;
256 unsigned int expiry = mcd_expire;
258 if (pv_mcd_key_check(msg, param, &key, &expiry) < 0)
261 if (val == NULL || val->flags&PV_VAL_NULL) {
262 if (memcached_delete(memcached_h, key.s, key.len, 0) != MEMCACHED_SUCCESS) {
263 LM_ERR("could not delete key %.*s\n", param->pvn.u.isname.name.s.len,
264 param->pvn.u.isname.name.s.s);
267 LM_DBG("delete key %.*s\n", key.len, key.s);
271 if (val->flags&PV_VAL_INT) {
272 val_str.s = int2str(val->ri, &val_str.len);
275 val_flag = VAR_VAL_STR;
279 if (memcached_set(memcached_h, key.s, key.len, val_str.s, val_str.len, expiry, val_flag) != MEMCACHED_SUCCESS) {
280 LM_ERR("could not set value for key %.*s\n", key.len, key.s);
284 if (memcached_add(memcached_h, key.s, key.len, val_str.s, val_str.len, expiry, val_flag) != MEMCACHED_SUCCESS) {
285 LM_ERR("could not add value for key %.*s\n", key.len, key.s);
289 LM_DBG("set value %.*s for key %.*s with flag %d\n", val_str.len, val_str.s, key.len, key.s, val_flag);
296 * \brief Helper function for the memcached atomic operations
297 * \note The checks on value existence and type are not done atomically, so there is a small
298 * chance that the later atomic operation fails. This is hard to detect because this function
299 * don't return a proper result code. Checking for the incremented value is also not possible,
300 * because in the mean time the value could be incremented from some other client.
301 * \param msg SIP message
302 * \param param parameter
305 * \param atomic_ops function pointer to the atomic operation from the memcached library
306 * \return 0 on success, -1 on failure
308 static int pv_mcd_atomic_helper(struct sip_msg* msg, pv_param_t *param, int op, pv_value_t *val,
309 memcached_return (* atomic_ops) (memcached_st *mc, const char *key, size_t key_length, uint32_t offset, uint64_t *value)) {
313 unsigned int expiry = mcd_expire;
315 uint32_t return_flags;
318 if (!(val->flags&PV_VAL_INT)) {
319 LM_ERR("invalid value %.*s for atomic operation, strings not allowed\n",
320 val->rs.len, val->rs.s);
324 if (pv_mcd_key_check(msg, param, &key, &expiry) < 0)
327 if (pv_get_mcd_value_helper(msg, &key, &return_value, &return_flags) < 0) {
328 pv_free_mcd_value(&return_value);
332 pv_free_mcd_value(&return_value);
334 if(return_flags&VAR_VAL_STR) {
335 LM_ERR("could not do atomic operations on string for key %.*s\n", key.len, key.s);
339 if ((rc = atomic_ops(memcached_h, key.s, key.len, val->ri, &value)) != MEMCACHED_SUCCESS) {
340 LM_ERR("error performing atomic operation on key %.*s - %s\n", key.len, key.s, memcached_strerror(memcached_h, rc));
350 * \brief Increment a key atomically in the cache
351 * \param msg SIP message
352 * \param param parameter
355 * \return 0 on success, -1 on failure
357 int inline pv_inc_mcd_value(struct sip_msg* msg, pv_param_t *param, int op, pv_value_t *val) {
358 return pv_mcd_atomic_helper(msg, param, op, val, memcached_increment);
363 * \brief Decrement a key atomically in the cache
364 * \param msg SIP message
365 * \param param parameter
368 * \return 0 on success, -1 on failure
370 int inline pv_dec_mcd_value(struct sip_msg* msg, pv_param_t *param, int op, pv_value_t *val) {
371 return pv_mcd_atomic_helper(msg, param, op, val, memcached_decrement);
376 * \brief Set the expire value in the cache of memcached
377 * \note The memcache library don't provide functions to change the expiration
378 * time for a certain key after creation, so we need to do a get and set here.
379 * \param msg SIP message
380 * \param param parameter
383 * \return 0 on success, -1 on failure
385 int pv_set_mcd_expire(struct sip_msg* msg, pv_param_t *param, int op, pv_value_t *val)
388 unsigned int expiry = mcd_expire;
390 uint32_t return_flags;
393 if (!(val->flags&PV_VAL_INT)) {
394 LM_ERR("invalid value %.*s for expire time, strings not allowed\n",
395 val->rs.len, val->rs.s);
399 if (pv_mcd_key_check(msg, param, &key, &expiry) < 0)
402 if (pv_get_mcd_value_helper(msg, &key, &return_value, &return_flags) < 0) {
406 LM_DBG("set expire time %d for key %.*s with flag %d\n", val->ri, key.len, key.s, return_flags);
408 if ((rc= memcached_set(memcached_h, key.s, key.len, return_value, strlen(return_value), val->ri, return_flags)) != MEMCACHED_SUCCESS) {
409 LM_ERR("could not set expire time %d for key %.*s - error was %s\n", val->ri, key.len, key.s, memcached_strerror(memcached_h, rc));
413 pv_free_mcd_value(&return_value);
417 pv_free_mcd_value(&return_value);
423 * \brief Parse the pseudo-variable specification parameter
424 * \param sp pseudo-variable specification
425 * \param in parameter string
426 * \return 0 on success, -1 on failure
428 int pv_parse_mcd_name(pv_spec_p sp, str *in) {
430 pv_elem_t * tmp = NULL;
432 if(sp==NULL || in==NULL || in->len<=0)
436 tmp = pkg_malloc(sizeof(pv_elem_t));
441 memset(tmp, 0, sizeof(pv_elem_t));
443 if(pv_parse_format(in, &tmp) || tmp==NULL) {
444 LM_ERR("wrong format [%.*s]\n", in->len, in->s);
445 if(tmp) pkg_free(tmp);
449 sp->pvp.pvn.u.dname = tmp;
450 sp->pvp.pvn.type = PV_NAME_PVAR;