km_core.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2009
  5. *
  6. * This file is part of SIP-Router.org, a free SIP server.
  7. *
  8. * SIP-Router is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio 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
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include "../../dprint.h"
  26. #include "../../dset.h"
  27. #include "../../flags.h"
  28. #include "../../pvar.h"
  29. #include "../../lvalue.h"
  30. #include "../../mod_fix.h"
  31. #include "km_core.h"
  32. int w_setdsturi(struct sip_msg *msg, char *uri, str *s2)
  33. {
  34. str s;
  35. /* todo: fixup */
  36. s.s = uri;
  37. s.len = strlen(uri);
  38. if(set_dst_uri(msg, &s)!=0)
  39. return -1;
  40. /* dst_uri changes, so it makes sense to re-use the current uri for
  41. forking */
  42. ruri_mark_new(); /* re-use uri for serial forking */
  43. return 1;
  44. }
  45. int w_resetdsturi(struct sip_msg *msg, char *uri, str *s2)
  46. {
  47. if(msg->dst_uri.s!=0)
  48. pkg_free(msg->dst_uri.s);
  49. msg->dst_uri.s = 0;
  50. msg->dst_uri.len = 0;
  51. return 1;
  52. }
  53. int w_isdsturiset(struct sip_msg *msg, char *uri, str *s2)
  54. {
  55. if(msg->dst_uri.s==0 || msg->dst_uri.len<=0)
  56. return -1;
  57. return 1;
  58. }
  59. int pv_printf_fixup(void** param, int param_no)
  60. {
  61. pv_spec_t *spec=NULL;
  62. pv_elem_t *pvmodel=NULL;
  63. str tstr;
  64. if(param_no==1)
  65. {
  66. spec = (pv_spec_t*)pkg_malloc(sizeof(pv_spec_t));
  67. if(spec==NULL)
  68. {
  69. LM_ERR("out of pkg\n");
  70. return -1;
  71. }
  72. memset(spec, 0, sizeof(pv_spec_t));
  73. tstr.s = (char*)(*param);
  74. tstr.len = strlen(tstr.s);
  75. if(pv_parse_spec(&tstr, spec)==NULL)
  76. {
  77. LM_ERR("unknown script variable in first parameter");
  78. pkg_free(spec);
  79. return -1;
  80. }
  81. if(!pv_is_w(spec))
  82. {
  83. LM_ERR("read-only script variable in first parameter");
  84. pkg_free(spec);
  85. return -1;
  86. }
  87. *param = spec;
  88. } else if(param_no==2) {
  89. pvmodel = 0;
  90. tstr.s = (char*)(*param);
  91. tstr.len = strlen(tstr.s);
  92. if(pv_parse_format(&tstr, &pvmodel)<0)
  93. {
  94. LM_ERR("error in second parameter");
  95. return -1;
  96. }
  97. *param = pvmodel;
  98. }
  99. return 0;
  100. }
  101. int w_pv_printf(struct sip_msg *msg, char *s1, str *s2)
  102. {
  103. pv_spec_t *spec=NULL;
  104. pv_elem_t *model=NULL;
  105. pv_value_t val;
  106. spec = (pv_spec_t*)s1;
  107. model = (pv_elem_t*)s2;
  108. memset(&val, 0, sizeof(pv_value_t));
  109. if(pv_printf_s(msg, model, &val.rs)!=0)
  110. {
  111. LM_ERR("cannot eval second parameter\n");
  112. goto error;
  113. }
  114. val.flags = PV_VAL_STR;
  115. if(spec->setf(msg, &spec->pvp, EQ_T, &val)<0)
  116. {
  117. LM_ERR("setting PV failed\n");
  118. goto error;
  119. }
  120. return 1;
  121. error:
  122. return -1;
  123. }