jsonrpc_request.c 3.9 KB

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