qos_handlers.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2007 SOMA Networks, Inc.
  5. * Written by Ovidiu Sas (osas)
  6. *
  7. * This file is part of Kamailio, a free SIP server.
  8. *
  9. * Kamailio is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * Kamailio is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * 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
  22. * USA
  23. *
  24. * History:
  25. * --------
  26. * 2007-07-16 initial version (osas)
  27. */
  28. #include <stdio.h> /* for snprintf() */
  29. #include <string.h> /* for memset() */
  30. #include <stdlib.h> /* For atoi() */
  31. #include "../../parser/parser_f.h"
  32. #include "../../parser/sdp/sdp.h"
  33. #include "../../ut.h"
  34. #include "../../dprint.h"
  35. #include "../dialog/dlg_hash.h"
  36. #include "qos_mi.h"
  37. #include "qos_handlers.h"
  38. #include "qos_ctx_helpers.h"
  39. /**
  40. * The binding to the dialog module functions. Most importantly the
  41. * register_dlgcb function.
  42. */
  43. extern struct dlg_binds *dlg_binds;
  44. /**
  45. * Local function prototypes. See function definition for
  46. * documentation.
  47. */
  48. static void setup_dialog_callbacks(struct dlg_cell *did, qos_ctx_t *ctx);
  49. static void qos_dialog_destroy_CB(struct dlg_cell* did, int type, struct dlg_cb_params * params);
  50. static void qos_dialog_request_CB(struct dlg_cell* did, int type, struct dlg_cb_params * params);
  51. static void qos_dialog_response_CB(struct dlg_cell* did, int type,struct dlg_cb_params * params);
  52. static void qos_dialog_rpc_context_CB(struct dlg_cell* did, int type,struct dlg_cb_params * params);
  53. /**
  54. * The value of the message flag to flag an INVITE we want to process
  55. * through the QoS module.
  56. */
  57. //static int qos_flag = 0;
  58. /**
  59. * A helper function to setup all the callbacks from the dialog module
  60. * after we find intrest in the dialog.
  61. *
  62. * @param did The Dialog ID.
  63. * @param info The qos information.
  64. *
  65. */
  66. static void setup_dialog_callbacks(struct dlg_cell *did, qos_ctx_t *ctx)
  67. {
  68. dlg_binds->register_dlgcb(did, DLGCB_REQ_WITHIN,
  69. qos_dialog_request_CB, (void *)ctx, NULL);
  70. dlg_binds->register_dlgcb(did, DLGCB_RESPONSE_FWDED|DLGCB_RESPONSE_WITHIN,
  71. qos_dialog_response_CB, (void *)ctx, NULL);
  72. dlg_binds->register_dlgcb(did, DLGCB_DESTROY,
  73. qos_dialog_destroy_CB, (void *)ctx, NULL);
  74. dlg_binds->register_dlgcb(did, DLGCB_MI_CONTEXT,
  75. qos_dialog_mi_context_CB, (void *)ctx, NULL);
  76. dlg_binds->register_dlgcb(did, DLGCB_RPC_CONTEXT,
  77. qos_dialog_rpc_context_CB, (void *)ctx, NULL);
  78. return;
  79. }
  80. /**
  81. * Every time a new dialog is created (from a new INVITE) the dialog
  82. * module will call this callback function. We need to track the
  83. * dialogs lifespan from this point forward until it is terminated
  84. * with a BYE, CANCEL, etc. In the process, we will see if either or
  85. * both ends of the conversation are trying to re-negotiate the media.
  86. *
  87. * This function will setup the other types of dialog callbacks
  88. * required to track the lifespan of the dialog.
  89. *
  90. *
  91. * @param did - The dialog ID
  92. * @param type - The trigger event type (CREATED)
  93. * @param msg - The SIP message that triggered the callback (INVITE)
  94. * @param param - The pointer to nothing. As we did not attach
  95. * anything to this callback in the dialog module.
  96. */
  97. void qos_dialog_created_CB(struct dlg_cell *did, int type, struct dlg_cb_params * params)
  98. {
  99. qos_ctx_t *qos_ctx = NULL;
  100. struct sip_msg* msg = params->req;
  101. unsigned int dir = params->direction, role, other_role;
  102. if (dir == DLG_DIR_UPSTREAM) {
  103. role = QOS_CALLEE;
  104. other_role = QOS_CALLER;
  105. } else if (dir == DLG_DIR_DOWNSTREAM) {
  106. role = QOS_CALLER;
  107. other_role = QOS_CALLEE;
  108. } else {
  109. LM_ERR("Unknown dir %d\n", dir);
  110. return;
  111. }
  112. if (msg == NULL || msg == FAKED_REPLY) {
  113. LM_ERR("Improper msg\n");
  114. return;
  115. }
  116. /* look only at INVITE */
  117. if (msg->first_line.type != SIP_REQUEST ||
  118. msg->first_line.u.request.method_value != METHOD_INVITE) {
  119. LM_WARN("Dialog create callback called with a non-INVITE req.\n");
  120. return;
  121. }
  122. qos_ctx = build_new_qos_ctx();
  123. if (qos_ctx==NULL) {
  124. /* Error message printed in build_new_qos_ctx() */
  125. return;
  126. }
  127. LM_DBG("setup_dialog_callbacks( %p , %p )\n", did, qos_ctx);
  128. setup_dialog_callbacks(did, qos_ctx);
  129. run_create_cbs(qos_ctx, msg);
  130. if (0 == parse_sdp(msg)) {
  131. lock_get(&qos_ctx->lock);
  132. add_sdp(qos_ctx, dir, msg, role, other_role);
  133. lock_release(&qos_ctx->lock);
  134. }
  135. return;
  136. }
  137. /**
  138. * This callback is called when ever a dialog isdestroyed.
  139. *
  140. * @param did - The Dialog ID / structure pointer. Used as an ID only.
  141. * @param type - The termination cause/reason.
  142. * @param params - pointer to the dlg_cb params
  143. */
  144. static void qos_dialog_destroy_CB(struct dlg_cell* did, int type, struct dlg_cb_params * params)
  145. {
  146. struct sip_msg* msg = params->req;
  147. qos_ctx_t* qos_ctx = (qos_ctx_t*)*(params->param);
  148. /* run the QOSCB_TERMINATED callback */
  149. run_qos_callbacks(QOSCB_TERMINATED, qos_ctx, NULL, 0, msg);
  150. /* Free the param qos_info_t memory */
  151. if (qos_ctx) {
  152. destroy_qos_ctx(qos_ctx);
  153. params->param = NULL;
  154. }
  155. return;
  156. }
  157. /**
  158. *
  159. * @param did - The dialog structure. The pointer is used as an ID.
  160. * @param type - The reason for the callback. DLGCB_REQ_WITHIN
  161. * @param msg - The SIP message that causes the callback.
  162. * @param param - The qos information
  163. */
  164. static void qos_dialog_request_CB(struct dlg_cell* did, int type, struct dlg_cb_params * params)
  165. {
  166. struct sip_msg* msg = params->req;
  167. unsigned int dir = params->direction, role, other_role;
  168. qos_ctx_t* qos_ctx = (qos_ctx_t*)*(params->param);
  169. if (dir == DLG_DIR_UPSTREAM) {
  170. role = QOS_CALLEE;
  171. other_role = QOS_CALLER;
  172. } else if (dir == DLG_DIR_DOWNSTREAM) {
  173. role = QOS_CALLER;
  174. other_role = QOS_CALLEE;
  175. } else {
  176. LM_ERR("Unknown dir %d\n", dir);
  177. return;
  178. }
  179. if (msg->first_line.type == SIP_REQUEST) {
  180. if ( (msg->first_line.u.request.method_value == METHOD_INVITE) ||
  181. (msg->first_line.u.request.method_value == METHOD_UPDATE) ||
  182. (msg->first_line.u.request.method_value == METHOD_ACK) ||
  183. (msg->first_line.u.request.method_value == METHOD_PRACK)) {
  184. if (0 == parse_sdp(msg)) {
  185. lock_get(&qos_ctx->lock);
  186. add_sdp(qos_ctx, dir, msg, role, other_role);
  187. lock_release(&qos_ctx->lock);
  188. }
  189. } else {
  190. LM_DBG("Ignoring non-carrying SDP req\n");
  191. return;
  192. }
  193. } else {
  194. LM_ERR("not a SIP_REQUEST\n");
  195. return;
  196. }
  197. return;
  198. }
  199. /**
  200. * This callback is called on any response message in the lifespan of
  201. * the dialog. The callback is called just before the message is
  202. * copied to pkg memory so it is still mutable.
  203. *
  204. * @param did - The dialog structure. The pointer is used as an ID.
  205. * @param type - The reason for the callback. DLGCB_CONFIRMED
  206. * @param msg - The SIP message that causes the callback.
  207. * @param param - The qos information
  208. */
  209. static void qos_dialog_response_CB(struct dlg_cell* did, int type, struct dlg_cb_params * params)
  210. {
  211. struct sip_msg* msg = params->rpl;
  212. unsigned int dir = params->direction, role, other_role;
  213. qos_ctx_t* qos_ctx = (qos_ctx_t*)*(params->param);
  214. if (dir == DLG_DIR_UPSTREAM) {
  215. role = QOS_CALLEE;
  216. other_role = QOS_CALLER;
  217. } else if (dir == DLG_DIR_DOWNSTREAM) {
  218. role = QOS_CALLER;
  219. other_role = QOS_CALLEE;
  220. } else {
  221. LM_ERR("Unknown dir %d\n", dir);
  222. return;
  223. }
  224. if (msg->first_line.type == SIP_REPLY) {
  225. if (msg->first_line.u.reply.statuscode > 100 &&
  226. msg->first_line.u.reply.statuscode < 300) {
  227. if (0 == parse_sdp(msg)) {
  228. lock_get(&qos_ctx->lock);
  229. add_sdp(qos_ctx, dir, msg, role, other_role);
  230. lock_release(&qos_ctx->lock);
  231. }
  232. } else if (msg->first_line.u.reply.statuscode > 399 &&
  233. msg->first_line.u.reply.statuscode < 700) {
  234. lock_get(&qos_ctx->lock);
  235. remove_sdp(qos_ctx, dir, msg, role, other_role);
  236. lock_release(&qos_ctx->lock);
  237. }
  238. } else {
  239. LM_ERR("not a SIP_REPLY\n");
  240. return;
  241. }
  242. return;
  243. }
  244. /********************************* RPC *********************************/
  245. static inline void internal_rpc_print_qos_stream_payloads(rpc_t *rpc, void *c, sdp_stream_cell_t* stream)
  246. {
  247. int i;
  248. sdp_payload_attr_t *sdp_payload = stream->payload_attr;
  249. for(i=stream->payloads_num-1;i>=0;i--){
  250. if (!sdp_payload) {
  251. LM_ERR("got NULL sdp_payload\n");
  252. return;
  253. }
  254. rpc->rpl_printf(c, "\t\t\t\tpayload[%d]=%.*s codec=%.*s",
  255. i, sdp_payload->rtp_payload.len, sdp_payload->rtp_payload.s,
  256. sdp_payload->rtp_enc.len, sdp_payload->rtp_enc.s);
  257. sdp_payload = sdp_payload->next;
  258. }
  259. }
  260. static inline void internal_rpc_print_qos_stream(rpc_t *rpc, void *c, sdp_session_cell_t* session)
  261. {
  262. int i;
  263. sdp_stream_cell_t *stream = session->streams;
  264. for(i=session->streams_num-1;i>=0;i--){
  265. if (!stream) {
  266. LM_ERR("got NULL stream\n");
  267. return;
  268. }
  269. rpc->rpl_printf(c, "\t\t\tmedia=%.*s IP:port=%.*s:%.*s trans=%.*s sendrecv=%.*s ptime=%.*s payload:%d",
  270. stream->media.len, stream->media.s,
  271. stream->ip_addr.len, stream->ip_addr.s,
  272. stream->port.len, stream->port.s,
  273. stream->transport.len, stream->transport.s,
  274. stream->sendrecv_mode.len, stream->sendrecv_mode.s,
  275. stream->ptime.len, stream->ptime.s,
  276. stream->payloads_num);
  277. internal_rpc_print_qos_stream_payloads(rpc, c, stream);
  278. stream = stream->next;
  279. }
  280. }
  281. static inline void internal_rpc_print_qos(rpc_t *rpc, void *c, qos_sdp_t *qos_sdp)
  282. {
  283. int i;
  284. sdp_session_cell_t *session;
  285. rpc->rpl_printf(c, "\t\tm_dir=%u m_id=%u method=%.*s cseq=%.*s negotiation=%u",
  286. qos_sdp->method_dir, qos_sdp->method_id,
  287. qos_sdp->method.len, qos_sdp->method.s,
  288. qos_sdp->cseq.len, qos_sdp->cseq.s, qos_sdp->negotiation);
  289. for (i=1;i>=0;i--){
  290. session = qos_sdp->sdp_session[i];
  291. if (session) {
  292. rpc->rpl_printf(c, "\t\tcalle%s: cnt_disp=%.*s bw_type=%.*s bw_width=%.*s",
  293. i?"e":"r",
  294. session->cnt_disp.len, session->cnt_disp.s,
  295. session->bw_type.len, session->bw_type.s,
  296. session->bw_width.len, session->bw_width.s);
  297. internal_rpc_print_qos_stream(rpc, c, session);
  298. }
  299. }
  300. }
  301. void qos_dialog_rpc_context_CB(struct dlg_cell* did, int type, struct dlg_cb_params * params)
  302. {
  303. rpc_cb_ctx_t *rpc_cb = (rpc_cb_ctx_t*)(params->dlg_data);
  304. rpc_t *rpc = rpc_cb->rpc;
  305. void *c = rpc_cb->c;
  306. qos_ctx_t* qos_ctx = (qos_ctx_t*)*(params->param);
  307. qos_sdp_t* qos_sdp;
  308. qos_sdp = qos_ctx->pending_sdp;
  309. if (qos_sdp) {
  310. rpc->rpl_printf(c, "\tqos:pending_sdp");
  311. internal_rpc_print_qos(rpc, c, qos_sdp);
  312. }
  313. qos_sdp = qos_ctx->negotiated_sdp;
  314. if (qos_sdp) {
  315. rpc->rpl_printf(c, "\tqos:negotiated_sdp");
  316. internal_rpc_print_qos(rpc, c, qos_sdp);
  317. }
  318. return;
  319. }