jsonrpc_request.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2011 Flowroute LLC (flowroute.com)
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * This file is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. *
  12. * This file 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
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #include "../../mod_fix.h"
  23. #include "../../pvar.h"
  24. #include "../../lvalue.h"
  25. #include "../tm/tm_load.h"
  26. #include "jsonrpc_request.h"
  27. #include "jsonrpc_io.h"
  28. struct tm_binds tmb;
  29. static char *shm_strdup(str *src);
  30. int memory_error() {
  31. LM_ERR("Out of memory!");
  32. return -1;
  33. }
  34. int jsonrpc_request(struct sip_msg* _m, char* _method, char* _params, char* _cb_route, char* _err_route, char* _cb_pv)
  35. {
  36. str method;
  37. str params;
  38. str cb_route;
  39. str err_route;
  40. if (fixup_get_svalue(_m, (gparam_p)_method, &method) != 0) {
  41. LM_ERR("cannot get method value\n");
  42. return -1;
  43. }
  44. if (fixup_get_svalue(_m, (gparam_p)_params, &params) != 0) {
  45. LM_ERR("cannot get params value\n");
  46. return -1;
  47. }
  48. if (fixup_get_svalue(_m, (gparam_p)_cb_route, &cb_route) != 0) {
  49. LM_ERR("cannot get cb_route value\n");
  50. return -1;
  51. }
  52. if (fixup_get_svalue(_m, (gparam_p)_err_route, &err_route) != 0) {
  53. LM_ERR("cannot get err_route value\n");
  54. return -1;
  55. }
  56. tm_cell_t *t = 0;
  57. t = tmb.t_gett();
  58. if (t==NULL || t==T_UNDEFINED)
  59. {
  60. if(tmb.t_newtran(_m)<0)
  61. {
  62. LM_ERR("cannot create the transaction\n");
  63. return -1;
  64. }
  65. t = tmb.t_gett();
  66. if (t==NULL || t==T_UNDEFINED)
  67. {
  68. LM_ERR("cannot look up the transaction\n");
  69. return -1;
  70. }
  71. }
  72. unsigned int hash_index;
  73. unsigned int label;
  74. if (tmb.t_suspend(_m, &hash_index, &label) < 0) {
  75. LM_ERR("t_suspend() failed\n");
  76. return -1;
  77. }
  78. struct jsonrpc_pipe_cmd *cmd;
  79. if (!(cmd = (struct jsonrpc_pipe_cmd *) shm_malloc(sizeof(struct jsonrpc_pipe_cmd))))
  80. return memory_error();
  81. memset(cmd, 0, sizeof(struct jsonrpc_pipe_cmd));
  82. pv_spec_t *cb_pv = (pv_spec_t*)shm_malloc(sizeof(pv_spec_t));
  83. if (!cb_pv)
  84. return memory_error();
  85. cb_pv = memcpy(cb_pv, (pv_spec_t *)_cb_pv, sizeof(pv_spec_t));
  86. cmd->method = shm_strdup(&method);
  87. cmd->params = shm_strdup(&params);
  88. cmd->cb_route = shm_strdup(&cb_route);
  89. cmd->err_route = shm_strdup(&err_route);
  90. cmd->cb_pv = cb_pv;
  91. cmd->msg = _m;
  92. cmd->t_hash = hash_index;
  93. cmd->t_label = label;
  94. if (write(cmd_pipe, &cmd, sizeof(cmd)) != sizeof(cmd)) {
  95. LM_ERR("failed to write to io pipe: %s\n", strerror(errno));
  96. return -1;
  97. }
  98. return 0;
  99. }
  100. int jsonrpc_notification(struct sip_msg* _m, char* _method, char* _params)
  101. {
  102. str method;
  103. str params;
  104. if (fixup_get_svalue(_m, (gparam_p)_method, &method) != 0) {
  105. LM_ERR("cannot get method value\n");
  106. return -1;
  107. }
  108. if (fixup_get_svalue(_m, (gparam_p)_params, &params) != 0) {
  109. LM_ERR("cannot get params value\n");
  110. return -1;
  111. }
  112. struct jsonrpc_pipe_cmd *cmd;
  113. if (!(cmd = (struct jsonrpc_pipe_cmd *) shm_malloc(sizeof(struct jsonrpc_pipe_cmd))))
  114. return memory_error();
  115. memset(cmd, 0, sizeof(struct jsonrpc_pipe_cmd));
  116. cmd->method = shm_strdup(&method);
  117. cmd->params = shm_strdup(&params);
  118. cmd->notify_only = 1;
  119. if (write(cmd_pipe, &cmd, sizeof(cmd)) != sizeof(cmd)) {
  120. LM_ERR("failed to write to io pipe: %s\n", strerror(errno));
  121. return -1;
  122. }
  123. return 1;
  124. }
  125. static char *shm_strdup(str *src)
  126. {
  127. char *res;
  128. if (!src || !src->s)
  129. return NULL;
  130. if (!(res = (char *) shm_malloc(src->len + 1)))
  131. return NULL;
  132. strncpy(res, src->s, src->len);
  133. res[src->len] = 0;
  134. return res;
  135. }