rtpproxy_stream.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* $Id: rtpproxy_stream.c,v 1.2 2008/11/04 22:28:04 sobomax Exp $
  2. *
  3. * Copyright (C) 2008 Sippy Software, Inc., http://www.sippysoft.com
  4. *
  5. * This file is part of ser, a free SIP server.
  6. *
  7. * ser 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. * For a license to use the ser software under conditions
  13. * other than those described here, or to purchase support for this
  14. * software, please contact iptel.org by e-mail at the following addresses:
  15. * [email protected]
  16. *
  17. * ser is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. #include <stdio.h>
  28. #include <sys/uio.h>
  29. #include "../../ip_addr.h"
  30. #include "../../parser/msg_parser.h"
  31. #include "../../sr_module.h"
  32. #include "../../ut.h"
  33. #include "rtpproxy.h"
  34. #include "rtpproxy_funcs.h"
  35. int
  36. fixup_var_str_int(void **param, int param_no)
  37. {
  38. int ret;
  39. pv_elem_t *model;
  40. str s;
  41. if (param_no == 1) {
  42. model = NULL;
  43. s.s = (char *)(*param);
  44. s.len = strlen(s.s);
  45. if (pv_parse_format(&s, &model) < 0) {
  46. LM_ERR("wrong format[%s]!\n", (char *)(*param));
  47. return E_UNSPEC;
  48. }
  49. if (model == NULL) {
  50. LM_ERR("empty parameter!\n");
  51. return E_UNSPEC;
  52. }
  53. *param = (void *)model;
  54. } else if (param_no == 2) {
  55. /* According to
  56. * http://www.kamailio.org/docs/modules/1.5.x/nathelper.html#rtpproxy_stream2xxx
  57. * this could be -1 */
  58. s.s = (char *)(*param);
  59. s.len = strlen(s.s);
  60. if (str2sint(&s, &ret)==0) {
  61. pkg_free(*param);
  62. *param = (void *)(long)ret;
  63. } else {
  64. LM_ERR("bad number <%s>\n", (char *)(*param));
  65. return E_CFG;
  66. }
  67. }
  68. return 0;
  69. }
  70. static int
  71. rtpproxy_stream(struct sip_msg* msg, str *pname, int count, int stream2uac)
  72. {
  73. int nitems;
  74. str callid, from_tag, to_tag;
  75. struct rtpp_node *node;
  76. char cbuf[16];
  77. struct iovec v[] = {
  78. {NULL, 0},
  79. {cbuf, 0}, /* 1 P<count> */
  80. {" ", 1},
  81. {NULL, 0}, /* 3 callid */
  82. {" ", 1},
  83. {NULL, 0}, /* 5 pname */
  84. {" session ", 9},
  85. {NULL, 0}, /* 7 from tag */
  86. {";1 ", 3},
  87. {NULL, 0}, /* 9 to tag */
  88. {";1", 2}
  89. };
  90. if (get_callid(msg, &callid) == -1 || callid.len == 0) {
  91. LM_ERR("can't get Call-Id field\n");
  92. return -1;
  93. }
  94. if (get_to_tag(msg, &to_tag) == -1) {
  95. LM_ERR("can't get To tag\n");
  96. return -1;
  97. }
  98. if (get_from_tag(msg, &from_tag) == -1 || from_tag.len == 0) {
  99. LM_ERR("can't get From tag\n");
  100. return -1;
  101. }
  102. v[1].iov_len = sprintf(cbuf, "P%d", count);
  103. STR2IOVEC(callid, v[3]);
  104. STR2IOVEC(*pname, v[5]);
  105. node = select_rtpp_node(callid, 1);
  106. if (!node) {
  107. LM_ERR("no available proxies\n");
  108. return -1;
  109. }
  110. if (node->rn_ptl_supported == 0) {
  111. LM_ERR("required functionality is not "
  112. "supported by the version of the RTPproxy running on the selected "
  113. "node. Please upgrade the RTPproxy and try again.\n");
  114. return -1;
  115. }
  116. nitems = 11;
  117. if (stream2uac == 0) {
  118. if (to_tag.len == 0)
  119. return -1;
  120. STR2IOVEC(to_tag, v[7]);
  121. STR2IOVEC(from_tag, v[9]);
  122. } else {
  123. STR2IOVEC(from_tag, v[7]);
  124. STR2IOVEC(to_tag, v[9]);
  125. if (to_tag.len <= 0)
  126. nitems -= 2;
  127. }
  128. send_rtpp_command(node, v, nitems);
  129. return 1;
  130. }
  131. static int
  132. rtpproxy_stream2_f(struct sip_msg *msg, char *str1, int count, int stream2uac)
  133. {
  134. str pname;
  135. if (str1 == NULL || pv_printf_s(msg, (pv_elem_p)str1, &pname) != 0)
  136. return -1;
  137. return rtpproxy_stream(msg, &pname, count, stream2uac);
  138. }
  139. int
  140. rtpproxy_stream2uac2_f(struct sip_msg* msg, char* str1, char* str2)
  141. {
  142. return rtpproxy_stream2_f(msg, str1, (int)(long)str2, 1);
  143. }
  144. int
  145. rtpproxy_stream2uas2_f(struct sip_msg* msg, char* str1, char* str2)
  146. {
  147. return rtpproxy_stream2_f(msg, str1, (int)(long)str2, 0);
  148. }
  149. static int
  150. rtpproxy_stop_stream(struct sip_msg* msg, int stream2uac)
  151. {
  152. int nitems;
  153. str callid, from_tag, to_tag;
  154. struct rtpp_node *node;
  155. struct iovec v[] = {
  156. {NULL, 0},
  157. {"S", 1}, /* 1 */
  158. {" ", 1},
  159. {NULL, 0}, /* 3 callid */
  160. {" ", 1},
  161. {NULL, 0}, /* 5 from tag */
  162. {";1 ", 3},
  163. {NULL, 0}, /* 7 to tag */
  164. {";1", 2}
  165. };
  166. if (get_callid(msg, &callid) == -1 || callid.len == 0) {
  167. LM_ERR("can't get Call-Id field\n");
  168. return -1;
  169. }
  170. if (get_to_tag(msg, &to_tag) == -1) {
  171. LM_ERR("can't get To tag\n");
  172. return -1;
  173. }
  174. if (get_from_tag(msg, &from_tag) == -1 || from_tag.len == 0) {
  175. LM_ERR("can't get From tag\n");
  176. return -1;
  177. }
  178. STR2IOVEC(callid, v[3]);
  179. node = select_rtpp_node(callid, 1);
  180. if (!node) {
  181. LM_ERR("no available proxies\n");
  182. return -1;
  183. }
  184. if (node->rn_ptl_supported == 0) {
  185. LM_ERR("required functionality is not "
  186. "supported by the version of the RTPproxy running on the selected "
  187. "node. Please upgrade the RTPproxy and try again.\n");
  188. return -1;
  189. }
  190. nitems = 9;
  191. if (stream2uac == 0) {
  192. if (to_tag.len == 0)
  193. return -1;
  194. STR2IOVEC(to_tag, v[5]);
  195. STR2IOVEC(from_tag, v[7]);
  196. } else {
  197. STR2IOVEC(from_tag, v[5]);
  198. STR2IOVEC(to_tag, v[7]);
  199. if (to_tag.len <= 0)
  200. nitems -= 2;
  201. }
  202. send_rtpp_command(node, v, nitems);
  203. return 1;
  204. }
  205. int
  206. rtpproxy_stop_stream2uac2_f(struct sip_msg* msg, char* str1, char* str2)
  207. {
  208. return rtpproxy_stop_stream(msg, 1);
  209. }
  210. int
  211. rtpproxy_stop_stream2uas2_f(struct sip_msg* msg, char* str1, char* str2)
  212. {
  213. return rtpproxy_stop_stream(msg, 0);
  214. }