options.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2009 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO 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 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO 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 DOUBANGO.
  20. *
  21. */
  22. #include "options.h"
  23. extern ctx_t* ctx;
  24. extern const session_t* session_handle_cmd(cmd_type_t , const opts_L_t*);
  25. int options_hack_aor(const tsip_response_t* resp);
  26. int options_handle_event(const tsip_event_t *_event)
  27. {
  28. const tsip_options_event_t* opt_event = TSIP_OPTIONS_EVENT(_event);
  29. const session_t* session;
  30. tsip_ssession_id_t sid;
  31. /* Find associated session */
  32. sid = tsip_ssession_get_id(_event->ss);
  33. if(!(session = session_get_by_sid(ctx->sessions, sid))) {
  34. TSK_DEBUG_WARN("Failed to match session event.");
  35. return -1;
  36. }
  37. switch(opt_event->type) {
  38. case tsip_i_options: /* incoming OPTIONS */
  39. break;
  40. case tsip_ao_options: /* answer to outgoing OPTIONS */
  41. #if HACK_AOR
  42. if(TSIP_MESSAGE_IS_RESPONSE(_event->sipmessage)) {
  43. options_hack_aor(_event->sipmessage);
  44. }
  45. #endif
  46. break;
  47. }
  48. return 0;
  49. }
  50. tsip_ssession_id_t options_handle_cmd(cmd_type_t cmd, const opts_L_t* opts)
  51. {
  52. const session_t* session = tsk_null;
  53. tsip_ssession_id_t id = TSIP_SSESSION_INVALID_ID;
  54. if(!(session = session_handle_cmd(cmd, opts))) {
  55. goto bail;
  56. }
  57. else {
  58. id = tsip_ssession_get_id(session->handle);
  59. }
  60. /* Execute command */
  61. switch(cmd) {
  62. case cmd_options: {
  63. /* Send SIP OPTIONS */
  64. tsip_action_handle_t* action_config = action_get_config(opts);
  65. tsip_api_options_send_options(session->handle,
  66. TSIP_ACTION_SET_CONFIG(action_config),
  67. /* Any other TSIP_ACTION_SET_*() macros */
  68. TSIP_ACTION_SET_NULL());
  69. TSK_OBJECT_SAFE_FREE(action_config);
  70. break;
  71. }
  72. default:
  73. /* already handled by session_handle_cmd() */
  74. break;
  75. }
  76. bail:
  77. return id;
  78. }
  79. /* used to hack the AOR. useful for devices behind NATs or emulators (e.g. ANDROID) */
  80. int options_hack_aor(const tsip_response_t* resp)
  81. {
  82. int32_t rport;
  83. if(resp && resp->firstVia) {
  84. if(resp->firstVia->rport <=0) {
  85. char* received_port_ext;
  86. /* Ericsson SDS
  87. ==> Via: SIP/2.0/TCP 192.168.0.12:49744;rport;branch=z9hG4bK1273100904048;received_port_ext=49744;received=192.168.0.12 */
  88. if((received_port_ext = tsip_header_get_param_value((const tsip_header_t*)resp->firstVia, "received_port_ext"))) {
  89. rport = (int32_t)atoi(received_port_ext);
  90. TSK_FREE(received_port_ext);
  91. }
  92. }
  93. else {
  94. rport = resp->firstVia->rport;
  95. }
  96. tsip_stack_set(ctx->stack,
  97. TSIP_STACK_SET_AOR(resp->firstVia->received, rport),
  98. TSIP_STACK_SET_NULL());
  99. return 0;
  100. }
  101. return -1;
  102. }