Msrp.cxx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (C) 2010-2011 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 "Msrp.h"
  23. #include "SipSession.h"
  24. #include "Common.h"
  25. /* ======================== MsrpMessage ========================*/
  26. MsrpMessage::MsrpMessage()
  27. :m_pMessage(tsk_null)
  28. {
  29. }
  30. MsrpMessage::MsrpMessage(tmsrp_message_t *_message)
  31. {
  32. m_pMessage = (tmsrp_message_t *)tsk_object_ref(_message);
  33. }
  34. MsrpMessage::~MsrpMessage()
  35. {
  36. TSK_OBJECT_SAFE_FREE(m_pMessage);
  37. }
  38. bool MsrpMessage::isRequest()
  39. {
  40. return (m_pMessage->type == tmsrp_request);
  41. }
  42. short MsrpMessage::getCode()
  43. {
  44. return TMSRP_RESPONSE_CODE(m_pMessage);
  45. }
  46. const char* MsrpMessage::getPhrase()
  47. {
  48. return TMSRP_RESPONSE_PHRASE(m_pMessage);
  49. }
  50. tmsrp_request_type_t MsrpMessage::getRequestType()
  51. {
  52. if(TMSRP_MESSAGE_IS_REQUEST(m_pMessage)) {
  53. return m_pMessage->line.request.type;
  54. }
  55. return tmsrp_NONE;
  56. }
  57. void MsrpMessage::getByteRange(int64_t* start, int64_t* end, int64_t* total)
  58. {
  59. if(m_pMessage->ByteRange) {
  60. *start = m_pMessage->ByteRange->start;
  61. *end = m_pMessage->ByteRange->end;
  62. *total = m_pMessage->ByteRange->total;
  63. }
  64. else {
  65. *start = *end = *total = -1;
  66. }
  67. }
  68. bool MsrpMessage::isLastChunck()
  69. {
  70. if(TMSRP_MESSAGE_IS_REQUEST(m_pMessage)) {
  71. return (m_pMessage->end_line.cflag == '$');
  72. }
  73. else {
  74. if(m_pMessage->ByteRange) {
  75. return (m_pMessage->ByteRange->end >= m_pMessage->ByteRange->total);
  76. }
  77. }
  78. return false;
  79. }
  80. bool MsrpMessage::isSuccessReport()
  81. {
  82. if(TMSRP_REQUEST_IS_REPORT(m_pMessage)) {
  83. if(m_pMessage->Status) {
  84. return m_pMessage->Status->code >= 200 && m_pMessage->Status->code <= 299;
  85. }
  86. }
  87. return false;
  88. }
  89. bool MsrpMessage::isFirstChunck()
  90. {
  91. return (m_pMessage && m_pMessage->ByteRange->start == 1);
  92. }
  93. char* MsrpMessage::getMsrpHeaderValue(const char* name)
  94. {
  95. const tmsrp_header_t* header = this->getMsrpHeader(name, 0);
  96. if(header) {
  97. return tmsrp_header_tostring(header);
  98. }
  99. return tsk_null;
  100. }
  101. char* MsrpMessage::getMsrpHeaderParamValue(const char* name, const char* param)
  102. {
  103. return tsk_null;
  104. }
  105. unsigned MsrpMessage::getMsrpContentLength()
  106. {
  107. if(m_pMessage &&
  108. m_pMessage->Content &&
  109. m_pMessage->Content->data &&
  110. m_pMessage->Content->size) {
  111. return m_pMessage->Content->size;
  112. }
  113. return 0;
  114. }
  115. unsigned MsrpMessage::getMsrpContent(void* output, unsigned maxsize)
  116. {
  117. unsigned retsize = 0;
  118. if(!output ||
  119. !m_pMessage ||
  120. !m_pMessage->Content ||
  121. !m_pMessage->Content->data ||
  122. !m_pMessage->Content->size) {
  123. return 0;
  124. }
  125. retsize = (m_pMessage->Content->size > maxsize) ? maxsize : m_pMessage->Content->size;
  126. memcpy(output, m_pMessage->Content->data, retsize);
  127. return retsize;
  128. }
  129. const tmsrp_header_t* MsrpMessage::getMsrpHeader(const char* name, unsigned index /*= 0*/)
  130. {
  131. tsk_size_t pos = 0;
  132. const tmsrp_header_t* hdr = tsk_null;
  133. const tsk_list_item_t *item;
  134. /* From tmsrp_message_get_headerAt() */
  135. if(!m_pMessage || !name) {
  136. return tsk_null;
  137. }
  138. if(tsk_striequals(name, "To-Path")) {
  139. if(index == 0) {
  140. hdr = (const tmsrp_header_t*)m_pMessage->To;
  141. goto bail;
  142. }
  143. else {
  144. pos++;
  145. }
  146. }
  147. if(tsk_striequals(name, "From-Path")) {
  148. if(index == 0) {
  149. hdr = (const tmsrp_header_t*)m_pMessage->From;
  150. goto bail;
  151. }
  152. else {
  153. pos++;
  154. }
  155. }
  156. if(tsk_striequals(name, "Message-ID")) {
  157. if(index == 0) {
  158. hdr = (const tmsrp_header_t*)m_pMessage->MessageID;
  159. goto bail;
  160. }
  161. else {
  162. pos++;
  163. }
  164. }
  165. if(tsk_striequals(name, "Byte-Range")) {
  166. if(index == 0) {
  167. hdr = (const tmsrp_header_t*)m_pMessage->ByteRange;
  168. goto bail;
  169. }
  170. else {
  171. pos++;
  172. }
  173. }
  174. if(tsk_striequals(name, "Failure-Report")) {
  175. if(index == 0) {
  176. hdr = (const tmsrp_header_t*)m_pMessage->FailureReport;
  177. goto bail;
  178. }
  179. else {
  180. pos++;
  181. }
  182. }
  183. if(tsk_striequals(name, "Success-Report")) {
  184. if(index == 0) {
  185. hdr = (const tmsrp_header_t*)m_pMessage->SuccessReport;
  186. goto bail;
  187. }
  188. else {
  189. pos++;
  190. }
  191. }
  192. if(tsk_striequals(name, "Status")) {
  193. if(index == 0) {
  194. hdr = (const tmsrp_header_t*)m_pMessage->Status;
  195. goto bail;
  196. }
  197. else {
  198. pos++;
  199. }
  200. }
  201. if(tsk_striequals(name, "Content-Type")) {
  202. if(index == 0) {
  203. hdr = (const tmsrp_header_t*)m_pMessage->ContentType;
  204. goto bail;
  205. }
  206. else {
  207. pos++;
  208. }
  209. }
  210. /* All other headers */
  211. tsk_list_foreach(item, m_pMessage->headers) {
  212. if(tsk_striequals(tmsrp_header_get_nameex(TMSRP_HEADER(item->data)), name)) {
  213. if(pos++ >= index) {
  214. hdr = (const tmsrp_header_t*)item->data;
  215. break;
  216. }
  217. }
  218. }
  219. bail:
  220. return hdr;
  221. }
  222. /* ======================== MsrpEvent ========================*/
  223. MsrpEvent::MsrpEvent(const tmsrp_event_t *_msrpevent)
  224. {
  225. this->_event = _msrpevent;
  226. if(this->_event && this->_event->message) {
  227. m_pMessage = new MsrpMessage((tmsrp_message_t *)this->_event->message);
  228. }
  229. else {
  230. m_pMessage = tsk_null;
  231. }
  232. }
  233. MsrpEvent::~MsrpEvent()
  234. {
  235. if(m_pMessage) {
  236. delete m_pMessage;
  237. }
  238. }
  239. tmsrp_event_type_t MsrpEvent::getType()
  240. {
  241. if(this->_event) {
  242. return this->_event->type;
  243. }
  244. return tmsrp_event_type_none;
  245. }
  246. const MsrpSession* MsrpEvent::getSipSession()
  247. {
  248. if(this->_event && this->_event->callback_data) {
  249. return dyn_cast<const MsrpSession*>((const MsrpSession*)this->_event->callback_data);
  250. }
  251. return tsk_null;
  252. }
  253. const MsrpMessage* MsrpEvent::getMessage() const
  254. {
  255. return m_pMessage;
  256. }
  257. int twrap_msrp_cb(const tmsrp_event_t* _event)
  258. {
  259. const MsrpSession* session = (const MsrpSession*)_event->callback_data;
  260. MsrpCallback* callback;
  261. int ret = -1;
  262. if(session) {
  263. if((callback = session->getCallback())) {
  264. MsrpEvent* e = new MsrpEvent(_event);
  265. ret = callback->OnEvent(e);
  266. delete e;
  267. }
  268. else {
  269. return 0;
  270. }
  271. }
  272. else {
  273. TSK_DEBUG_ERROR("Invalid parameter");
  274. }
  275. return ret;
  276. }