tsip_dialog.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango[dot]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. /**@file tsip_dialog.h
  23. * @brief SIP dialog base class as per RFC 3261 subclause 17.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #ifndef TINYSIP_DIALOG_H
  29. #define TINYSIP_DIALOG_H
  30. #include "tinysip_config.h"
  31. #include "tsip.h"
  32. #include "tinysip/tsip_uri.h"
  33. #include "tinysip/tsip_timers.h"
  34. #include "tinysip/tsip_message.h"
  35. #include "tinysip/tsip_ssession.h"
  36. #include "tinysip/authentication/tsip_challenge.h"
  37. #include "tinysip/tsip_action.h"
  38. #include "tinysip/headers/tsip_header_Record_Route.h"
  39. #include "tsk_safeobj.h"
  40. #include "tsk_list.h"
  41. #include "tsk_string.h"
  42. #include "tsk_fsm.h"
  43. TSIP_BEGIN_DECLS
  44. #define TSIP_DIALOG(self) ((tsip_dialog_t*)(self))
  45. #define TSIP_DIALOG_GET_STATE(self) TSIP_DIALOG((self))->state
  46. #define TSIP_DIALOG_GET_FSM(self) TSIP_DIALOG((self))->fsm
  47. #define TSIP_DIALOG_GET_SS(self) TSIP_DIALOG((self))->ss
  48. #define TSIP_DIALOG_GET_STACK(self) TSIP_STACK(TSIP_DIALOG_GET_SS((self))->stack)
  49. #define TSIP_DIALOG_TIMER_CANCEL(TX) \
  50. tsk_timer_mgr_global_cancel(self->timer##TX.id)
  51. // TX MUST be in seconds
  52. #define TSIP_DIALOG_TIMER_SCHEDULE(name, TX) \
  53. self->timer##TX.id = tsk_timer_mgr_global_schedule(self->timer##TX.timeout, TSK_TIMER_CALLBACK_F(tsip_dialog_##name##_timer_callback), self)
  54. #define TSIP_DIALOG_SIGNAL(self, code, phrase) \
  55. tsip_event_signal(tsip_event_dialog, TSIP_DIALOG(self)->ss, code, phrase)
  56. #define TSIP_DIALOG_SIGNAL_2(self, code, phrase, message) \
  57. tsip_event_signal_2(tsip_event_dialog, TSIP_DIALOG(self)->ss, code, phrase, message)
  58. #if !defined(TSIP_DIALOG_SHUTDOWN_TIMEOUT)
  59. # define TSIP_DIALOG_SHUTDOWN_TIMEOUT 2000 /* miliseconds. */
  60. #endif
  61. #if !defined(TSIP_DIALOG_INVALID_ID)
  62. # define TSIP_DIALOG_INVALID_ID 0
  63. #endif
  64. typedef uint64_t tsip_dialog_id_t;
  65. typedef enum tsip_dialog_state_e {
  66. tsip_initial,
  67. tsip_early,
  68. tsip_established,
  69. tsip_terminated
  70. }
  71. tsip_dialog_state_t;
  72. typedef enum tsip_dialog_type_e {
  73. tsip_dialog_unknown,
  74. tsip_dialog_INVITE,
  75. tsip_dialog_MESSAGE,
  76. tsip_dialog_INFO,
  77. tsip_dialog_OPTIONS,
  78. tsip_dialog_PUBLISH,
  79. tsip_dialog_REGISTER,
  80. tsip_dialog_SUBSCRIBE,
  81. }
  82. tsip_dialog_type_t;
  83. typedef enum tsip_dialog_event_type_e {
  84. tsip_dialog_i_msg,
  85. tsip_dialog_o_msg,
  86. tsip_dialog_transac_ok,
  87. tsip_dialog_canceled,
  88. tsip_dialog_terminated,
  89. tsip_dialog_timedout,
  90. tsip_dialog_error,
  91. tsip_dialog_transport_error,
  92. }
  93. tsip_dialog_event_type_t;
  94. typedef int (*tsip_dialog_event_callback_f)(const void *arg, tsip_dialog_event_type_t type, const tsip_message_t *msg);
  95. #define TSIP_DIALOG_EVENT_CALLBACK_F(callback) ((tsip_dialog_event_callback_f)(callback))
  96. /*================================
  97. */
  98. typedef struct tsip_dialog_s {
  99. TSK_DECLARE_OBJECT;
  100. tsip_dialog_type_t type;
  101. tsip_dialog_id_t id;
  102. tsk_fsm_t* fsm;
  103. tsip_ssession_t* ss;
  104. tsip_action_t* curr_action;
  105. tsip_dialog_state_t state;
  106. tsk_bool_t initialized;
  107. tsk_bool_t running;
  108. tnet_fd_t connected_fd;
  109. struct {
  110. char* phrase;
  111. short code;
  112. tsip_message_t* message;
  113. } last_error;
  114. char* tag_local;
  115. tsip_uri_t* uri_local;
  116. char* tag_remote;
  117. tsip_uri_t* uri_remote;
  118. tsip_uri_t* uri_remote_target;
  119. struct sockaddr_storage remote_addr; // Only valid for Dgram
  120. uint32_t cseq_value;
  121. char* cseq_method;
  122. int64_t expires; /* in milliseconds */
  123. char* callid;
  124. tsip_header_Record_Routes_L_t *record_routes;
  125. tsip_challenges_L_t *challenges;
  126. tsip_dialog_event_callback_f callback;
  127. TSK_DECLARE_SAFEOBJ;
  128. }
  129. tsip_dialog_t;
  130. #define TSIP_DECLARE_DIALOG tsip_dialog_t __dialog__
  131. typedef tsk_list_t tsip_dialogs_L_t;
  132. /*
  133. ================================*/
  134. tsip_request_t *tsip_dialog_request_new(const tsip_dialog_t *self, const char* method);
  135. int tsip_dialog_request_send(const tsip_dialog_t *self, tsip_request_t* request);
  136. tsip_response_t *tsip_dialog_response_new(tsip_dialog_t *self, short status, const char* phrase, const tsip_request_t* request);
  137. int tsip_dialog_response_send(const tsip_dialog_t *self, tsip_response_t* response);
  138. int tsip_dialog_apply_action(tsip_message_t* message, const tsip_action_t* action);
  139. int64_t tsip_dialog_get_newdelay(tsip_dialog_t *self, const tsip_message_t* message);
  140. int tsip_dialog_update(tsip_dialog_t *self, const tsip_response_t* response);
  141. int tsip_dialog_update_2(tsip_dialog_t *self, const tsip_request_t* invite);
  142. int tsip_dialog_getCKIK(tsip_dialog_t *self, AKA_CK_T *ck, AKA_IK_T *ik);
  143. int tsip_dialog_init(tsip_dialog_t *self, tsip_dialog_type_t type, const char* call_id, tsip_ssession_t* ss, tsk_fsm_state_id curr, tsk_fsm_state_id term);
  144. int tsip_dialog_fsm_act(tsip_dialog_t* self, tsk_fsm_action_id , const tsip_message_t* , const tsip_action_handle_t*);
  145. #define tsip_dialog_fsm_act_2(self, action_id) tsip_dialog_fsm_act((self), (action_id), tsk_null, tsk_null)
  146. tsk_bool_t tsip_dialog_keep_action(const tsip_dialog_t* self, const tsip_response_t *response);
  147. int tsip_dialog_set_connected_fd(tsip_dialog_t* self, tnet_fd_t fd);
  148. int tsip_dialog_set_curr_action(tsip_dialog_t* self, const tsip_action_t* action);
  149. int tsip_dialog_set_lasterror(tsip_dialog_t* self, const char* phrase, short code);
  150. int tsip_dialog_set_lasterror_2(tsip_dialog_t* self, const char* phrase, short code, const tsip_message_t *message);
  151. int tsip_dialog_get_lasterror(const tsip_dialog_t* self, short *code, const char** phrase, const tsip_message_t **message);
  152. int tsip_dialog_hangup(tsip_dialog_t *self, const tsip_action_t* action);
  153. int tsip_dialog_shutdown(tsip_dialog_t *self, const tsip_action_t* action);
  154. int tsip_dialog_signal_transport_error(tsip_dialog_t *self);
  155. int tsip_dialog_remove(const tsip_dialog_t* self);
  156. int tsip_dialog_cmp(const tsip_dialog_t *d1, const tsip_dialog_t *d2);
  157. int tsip_dialog_deinit(tsip_dialog_t *self);
  158. TSIP_END_DECLS
  159. #endif /* TINYSIP_DIALOG_H */