mcd_var.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright (C) 2009, 2013 Henning Westerholt
  3. * Copyright (C) 2013 Charles Chance, sipcentric.com
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  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
  11. *
  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.
  16. *
  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/>.
  19. */
  20. /*!
  21. * \file
  22. * \brief memcached module
  23. */
  24. #include "mcd_var.h"
  25. #include "memcached.h"
  26. #include "../../ut.h"
  27. #include "../../mem/mem.h"
  28. #include "../../pvapi.h"
  29. #include "../pv/pv_svar.h"
  30. #include "../../md5utils.h"
  31. /*!
  32. * \brief Checks for '=>' delimiter in key name string and if present, extracts expiry value.
  33. * \param data string to parse
  34. * \param key output string name
  35. * \param exp output int expiry (if present)
  36. * \return 0 on success, negative on failure
  37. */
  38. static inline int pv_mcd_key_expiry_split_str(str *data, str *key, unsigned int *exp) {
  39. char *p;
  40. str str_exp;
  41. str_exp.s = NULL;
  42. str_exp.len = 0;
  43. if (data == NULL || data->s == NULL || data->len <= 0) {
  44. LM_ERR("invalid parameters\n");
  45. return -1;
  46. }
  47. p = data->s;
  48. key->s = p;
  49. key->len = 0;
  50. while(p < data->s + data->len) {
  51. if (*p == '=') {
  52. p++;
  53. if (*p == '>') {
  54. break;
  55. } else {
  56. key->len++;
  57. }
  58. } else {
  59. key->len++;
  60. p++;
  61. }
  62. }
  63. if (key->len < data->len) {
  64. /* delimiter is present, try to extract expiry value */
  65. p++;
  66. if (p < data->s + data->len) {
  67. str_exp.s = p;
  68. str_exp.len = 0;
  69. while(p<data->s+data->len) {
  70. str_exp.len++;
  71. p++;
  72. }
  73. }
  74. if (str_exp.len > 0) {
  75. /* convert to int */
  76. *exp = atoi(str_exp.s);
  77. }
  78. LM_DBG("key is %.*s expiry is %d\n", key->len, key->s, *exp);
  79. }
  80. return 0;
  81. }
  82. /*!
  83. * \brief Checks if the key is avaiable and not too long, hashing it with MD5 if necessary.
  84. * \param msg SIP message
  85. * \param param pseudo-variable input parameter
  86. * \param key output string name
  87. * \param exp output int expiry (if present)
  88. * \return 0 on success, negative on failure
  89. */
  90. static inline int pv_mcd_key_check(struct sip_msg *msg, pv_param_t *param, str * key, unsigned int * exp ) {
  91. str pvn;
  92. str tmp;
  93. static char hash[32];
  94. if (msg == NULL || param == NULL) {
  95. LM_ERR("bad parameters\n");
  96. return -1;
  97. }
  98. if (pv_printf_s(msg, param->pvn.u.dname, &pvn) != 0)
  99. {
  100. LM_ERR("cannot get pv name\n");
  101. return -1;
  102. }
  103. if (pv_mcd_key_expiry_split_str(&pvn, &tmp, exp) != 0) {
  104. return -1;
  105. }
  106. if (tmp.len < 250) {
  107. key->s = tmp.s;
  108. key->len = tmp.len;
  109. } else {
  110. LM_DBG("key too long (%d), hash it\n", tmp.len);
  111. MD5StringArray (hash, &tmp, 1);
  112. key->s = hash;
  113. key->len = 32;
  114. }
  115. return 0;
  116. }
  117. /*!
  118. * \brief Helper to get a cached value from memcached
  119. * \param msg SIP message
  120. * \param key value key
  121. * \param return_value returned value
  122. * \param flags returned flags
  123. * \return null on success, negative on failure
  124. */
  125. static int pv_get_mcd_value_helper(struct sip_msg *msg, str *key,
  126. char **return_value, uint32_t *flags) {
  127. memcached_return rc;
  128. size_t return_value_length;
  129. *return_value = memcached_get(memcached_h, key->s, key->len, &return_value_length, flags, &rc);
  130. if (*return_value == NULL) {
  131. if (rc == MEMCACHED_NOTFOUND) {
  132. LM_DBG("key %.*s not found\n", key->len, key->s);
  133. } else {
  134. LM_ERR("could not get result for key %.*s - error was '%s'\n", key->len, key->s, memcached_strerror(memcached_h, rc));
  135. }
  136. return -1;
  137. }
  138. LM_DBG("result: %s for key %.*s with flag %d\n", *return_value, key->len, key->s, *flags);
  139. return 0;
  140. }
  141. static void pv_free_mcd_value(char** buf) {
  142. if (*buf!=NULL) {
  143. if (mcd_memory) {
  144. pkg_free(*buf);
  145. } else {
  146. free(*buf);
  147. }
  148. }
  149. }
  150. /*!
  151. * \brief Get a cached value from memcached
  152. * \param msg SIP message
  153. * \param param parameter
  154. * \param res result
  155. * \return null on success, negative on failure
  156. */
  157. int pv_get_mcd_value(struct sip_msg *msg, pv_param_t *param, pv_value_t *res) {
  158. unsigned int res_int = 0;
  159. str key, res_str;
  160. unsigned int expiry = mcd_expire;
  161. char *return_value;
  162. uint32_t return_flags;
  163. if (pv_mcd_key_check(msg, param, &key, &expiry) < 0) {
  164. return pv_get_null(msg, param, res);
  165. }
  166. if (res==NULL)
  167. return pv_get_null(msg, param, res);
  168. if (pv_get_mcd_value_helper(msg, &key, &return_value, &return_flags) < 0) {
  169. goto errout;
  170. }
  171. res_str.len = strlen(return_value);
  172. res_str.s = return_value;
  173. /* apparently memcached adds whitespaces to the beginning of the value after atomic operations */
  174. trim_len(res_str.len, res_str.s, res_str);
  175. if(return_flags&VAR_VAL_STR || mcd_stringify) {
  176. res->rs.s = pv_get_buffer();
  177. res->rs.len = pv_get_buffer_size();
  178. if(res_str.len>=res->rs.len) {
  179. LM_ERR("value is too big (%d) - increase pv buffer size\n", res_str.len);
  180. goto errout;
  181. }
  182. memcpy(res->rs.s, res_str.s, res_str.len);
  183. res->rs.len = res_str.len;
  184. res->rs.s[res->rs.len] = '\0';
  185. res->flags = PV_VAL_STR;
  186. } else {
  187. if (str2int(&res_str, &res_int) < 0) {
  188. LM_ERR("could not convert string %.*s to integer value\n", res_str.len, res_str.s);
  189. goto errout;
  190. }
  191. res->rs = res_str;
  192. res->ri = res_int;
  193. res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
  194. }
  195. pv_free_mcd_value(&return_value);
  196. return 0;
  197. errout:
  198. pv_free_mcd_value(&return_value);
  199. return pv_get_null(msg, param, res);
  200. }
  201. /*!
  202. * \brief Set a value in the cache of memcached
  203. * \todo Replacement of already existing values is not done atomically at the moment.
  204. * Here the provided replace function should be used.
  205. * \param msg SIP message
  206. * \param param parameter
  207. * \param op not used
  208. * \param val value
  209. * \return 0 on success, -1 on failure
  210. */
  211. int pv_set_mcd_value(struct sip_msg* msg, pv_param_t *param, int op, pv_value_t *val) {
  212. unsigned int val_flag = 0;
  213. str val_str, key;
  214. unsigned int expiry = mcd_expire;
  215. if (pv_mcd_key_check(msg, param, &key, &expiry) < 0)
  216. return -1;
  217. if (val == NULL) {
  218. if (memcached_delete(memcached_h, key.s, key.len, 0) != MEMCACHED_SUCCESS) {
  219. LM_ERR("could not delete key %.*s\n", param->pvn.u.isname.name.s.len,
  220. param->pvn.u.isname.name.s.s);
  221. return -1;
  222. }
  223. LM_WARN("delete key %.*s\n", key.len, key.s);
  224. return 0;
  225. }
  226. if (val->flags&PV_VAL_INT) {
  227. val_str.s = int2str(val->ri, &val_str.len);
  228. } else {
  229. val_str = val->rs;
  230. val_flag = VAR_VAL_STR;
  231. }
  232. if (mcd_mode == 0) {
  233. if (memcached_set(memcached_h, key.s, key.len, val_str.s, val_str.len, expiry, val_flag) != MEMCACHED_SUCCESS) {
  234. LM_ERR("could not set value for key %.*s\n", key.len, key.s);
  235. return -1;
  236. }
  237. } else {
  238. if (memcached_add(memcached_h, key.s, key.len, val_str.s, val_str.len, expiry, val_flag) != MEMCACHED_SUCCESS) {
  239. LM_ERR("could not add value for key %.*s\n", key.len, key.s);
  240. return -1;
  241. }
  242. }
  243. LM_DBG("set value %.*s for key %.*s with flag %d\n", val_str.len, val_str.s, key.len, key.s, val_flag);
  244. return 0;
  245. }
  246. /*!
  247. * \brief Helper function for the memcached atomic operations
  248. * \note The checks on value existence and type are not done atomically, so there is a small
  249. * chance that the later atomic operation fails. This is hard to detect because this function
  250. * don't return a proper result code. Checking for the incremented value is also not possible,
  251. * because in the mean time the value could be incremented from some other client.
  252. * \param msg SIP message
  253. * \param param parameter
  254. * \param op not used
  255. * \param val value
  256. * \param atomic_ops function pointer to the atomic operation from the memcached library
  257. * \return 0 on success, -1 on failure
  258. */
  259. static int pv_mcd_atomic_helper(struct sip_msg* msg, pv_param_t *param, int op, pv_value_t *val,
  260. memcached_return (* atomic_ops) (memcached_st *mc, const char *key, size_t key_length, uint32_t offset, uint64_t *value)) {
  261. uint64_t value = 0;
  262. str key;
  263. unsigned int expiry = mcd_expire;
  264. char *return_value;
  265. uint32_t return_flags;
  266. memcached_return rc;
  267. if (!(val->flags&PV_VAL_INT)) {
  268. LM_ERR("invalid value %.*s for atomic operation, strings not allowed\n",
  269. val->rs.len, val->rs.s);
  270. return -1;
  271. }
  272. if (pv_mcd_key_check(msg, param, &key, &expiry) < 0)
  273. return -1;
  274. if (pv_get_mcd_value_helper(msg, &key, &return_value, &return_flags) < 0) {
  275. pv_free_mcd_value(&return_value);
  276. return -1;
  277. }
  278. pv_free_mcd_value(&return_value);
  279. if(return_flags&VAR_VAL_STR) {
  280. LM_ERR("could not do atomic operations on string for key %.*s\n", key.len, key.s);
  281. return -1;
  282. }
  283. if ((rc = atomic_ops(memcached_h, key.s, key.len, val->ri, &value)) != MEMCACHED_SUCCESS) {
  284. LM_ERR("error performing atomic operation on key %.*s - %s\n", key.len, key.s, memcached_strerror(memcached_h, rc));
  285. return -1;
  286. }
  287. return 0;
  288. }
  289. /*!
  290. * \brief Increment a key atomically in the cache
  291. * \param msg SIP message
  292. * \param param parameter
  293. * \param op not used
  294. * \param val value
  295. * \return 0 on success, -1 on failure
  296. */
  297. int inline pv_inc_mcd_value(struct sip_msg* msg, pv_param_t *param, int op, pv_value_t *val) {
  298. return pv_mcd_atomic_helper(msg, param, op, val, memcached_increment);
  299. }
  300. /*!
  301. * \brief Decrement a key atomically in the cache
  302. * \param msg SIP message
  303. * \param param parameter
  304. * \param op not used
  305. * \param val value
  306. * \return 0 on success, -1 on failure
  307. */
  308. int inline pv_dec_mcd_value(struct sip_msg* msg, pv_param_t *param, int op, pv_value_t *val) {
  309. return pv_mcd_atomic_helper(msg, param, op, val, memcached_decrement);
  310. }
  311. /*!
  312. * \brief Set the expire value in the cache of memcached
  313. * \note The memcache library don't provide functions to change the expiration
  314. * time for a certain key after creation, so we need to do a get and set here.
  315. * \param msg SIP message
  316. * \param param parameter
  317. * \param op not used
  318. * \param val value
  319. * \return 0 on success, -1 on failure
  320. */
  321. int pv_set_mcd_expire(struct sip_msg* msg, pv_param_t *param, int op, pv_value_t *val)
  322. {
  323. str key;
  324. unsigned int expiry = mcd_expire;
  325. char *return_value;
  326. uint32_t return_flags;
  327. memcached_return rc;
  328. if (!(val->flags&PV_VAL_INT)) {
  329. LM_ERR("invalid value %.*s for expire time, strings not allowed\n",
  330. val->rs.len, val->rs.s);
  331. return -1;
  332. }
  333. if (pv_mcd_key_check(msg, param, &key, &expiry) < 0)
  334. return -1;
  335. if (pv_get_mcd_value_helper(msg, &key, &return_value, &return_flags) < 0) {
  336. goto errout;
  337. }
  338. LM_DBG("set expire time %d for key %.*s with flag %d\n", val->ri, key.len, key.s, return_flags);
  339. if ((rc= memcached_set(memcached_h, key.s, key.len, return_value, strlen(return_value), val->ri, return_flags)) != MEMCACHED_SUCCESS) {
  340. 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));
  341. goto errout;
  342. }
  343. pv_free_mcd_value(&return_value);
  344. return 0;
  345. errout:
  346. pv_free_mcd_value(&return_value);
  347. return -1;
  348. }
  349. /*!
  350. * \brief Parse the pseudo-variable specification parameter
  351. * \param sp pseudo-variable specification
  352. * \param in parameter string
  353. * \return 0 on success, -1 on failure
  354. */
  355. int pv_parse_mcd_name(pv_spec_p sp, str *in) {
  356. pv_elem_t * tmp = NULL;
  357. if(sp==NULL || in==NULL || in->len<=0)
  358. return -1;
  359. tmp = pkg_malloc(sizeof(pv_elem_t));
  360. if (tmp == NULL) {
  361. PKG_MEM_ERROR;
  362. return -1;
  363. }
  364. memset(tmp, 0, sizeof(pv_elem_t));
  365. if(pv_parse_format(in, &tmp) || tmp==NULL) {
  366. LM_ERR("wrong format [%.*s]\n", in->len, in->s);
  367. return -1;
  368. }
  369. sp->pvp.pvn.u.dname = tmp;
  370. sp->pvp.pvn.type = PV_NAME_PVAR;
  371. return 0;
  372. }