message.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "message.h"
  23. #include "tinysms.h" /* Binary SMS API*/
  24. #include <ctype.h>
  25. extern ctx_t* ctx;
  26. extern const session_t* session_handle_cmd(cmd_type_t , const opts_L_t*);
  27. tsk_bool_t is_valid_telnum(const tsip_uri_t* uri);
  28. tsk_buffer_t* sms_submit(const tsip_uri_t* smsc, const tsip_uri_t* dest, const char* ascii_pay);
  29. int message_handle_event(const tsip_event_t *_event)
  30. {
  31. const tsip_message_event_t* msg_event = TSIP_MESSAGE_EVENT(_event);
  32. const session_t* session = tsk_null;
  33. tsip_ssession_id_t sid;
  34. int ret = 0;
  35. /* Find associated session */
  36. sid = tsip_ssession_get_id(_event->ss);
  37. if(!(session = session_get_by_sid(ctx->sessions, sid))) {
  38. if(tsip_ssession_have_ownership(_event->ss)) {
  39. /* it's or own session and we fail to match it ==> should never happen */
  40. TSK_DEBUG_ERROR("Failed to match session event.");
  41. ret = -2;
  42. goto bail;
  43. }
  44. else {
  45. /* it's a "server-side-session" (incoming MESSAGE) */
  46. session_t* _session;
  47. if((_session = session_server_create(st_message, _event->ss)) && (session = _session)) {
  48. tsk_list_push_back_data(ctx->sessions, (void**)&_session);
  49. }
  50. else {
  51. TSK_DEBUG_ERROR("Failed to create \"sever-side-session\".");
  52. ret = -3;
  53. goto bail;
  54. }
  55. }
  56. }
  57. switch(msg_event->type) {
  58. case tsip_ao_message: { /* Answer to outgoing MESSAGE */
  59. TSK_DEBUG_INFO("Event: Answer to outgoing MESSAGE. Code=%d", TSIP_RESPONSE_CODE(_event->sipmessage));
  60. if(TSIP_RESPONSE_IS_2XX(_event->sipmessage)) {
  61. TSK_DEBUG_INFO("Message successfully sent.");
  62. }
  63. break;
  64. }
  65. case tsip_i_message: { /* Incoming MESSAGE */
  66. const char* content_type = TSIP_MESSAGE_CONTENT_TYPE(_event->sipmessage);
  67. TSK_DEBUG_INFO("Event: Incoming MESSAGE.");
  68. if(TSIP_MESSAGE_HAS_CONTENT(_event->sipmessage)) {
  69. const tsk_buffer_t* content = TSIP_MESSAGE_CONTENT(_event->sipmessage);
  70. TSK_DEBUG_INFO("MESSAGE Content-Type: %s", content_type);
  71. TSK_DEBUG_INFO("MESSAGE Content: %s", content->data);
  72. }
  73. /* accept() the MESSAGE to terminate the dialog */
  74. if(tsk_striequals("plain/text", content_type) || tsk_striequals("text/html", content_type)) {
  75. tsip_api_common_accept(session->handle,
  76. TSIP_ACTION_SET_HEADER("Info", "I've accept()ed your message"),// just for test
  77. TSIP_ACTION_SET_NULL());
  78. }
  79. /* reject() the MESSAGE to terminate the dialog */
  80. else {
  81. tsip_api_common_reject(session->handle,
  82. TSIP_ACTION_SET_HEADER("Info", "I've reject()ed your message"),// just for test
  83. TSIP_ACTION_SET_HEADER("In-Reply-To", "apb03a0s09dkjdfglkj49112"),// just for test
  84. TSIP_ACTION_SET_NULL());
  85. }
  86. break;
  87. }
  88. default: {
  89. /* Any other event */
  90. TSK_DEBUG_WARN("%d not a valid SIP Messaging event.", msg_event->type);
  91. break;
  92. }
  93. }
  94. bail:
  95. return ret;
  96. }
  97. tsip_ssession_id_t message_handle_cmd(cmd_type_t cmd, const opts_L_t* opts)
  98. {
  99. const session_t* session = tsk_null;
  100. const opt_t* opt;
  101. tsip_ssession_id_t id = TSIP_SSESSION_INVALID_ID;
  102. if(!(session = session_handle_cmd(cmd, opts))) {
  103. goto bail;
  104. }
  105. else {
  106. id = tsip_ssession_get_id(session->handle);
  107. }
  108. switch(cmd) {
  109. case cmd_message: {
  110. /* Send SIP MESSAGE */
  111. tsip_action_handle_t* action_config = action_get_config(opts);
  112. tsip_api_message_send_message(session->handle,
  113. TSIP_ACTION_SET_CONFIG(action_config),
  114. /* Any other TSIP_ACTION_SET_*() macros */
  115. TSIP_ACTION_SET_NULL());
  116. TSK_OBJECT_SAFE_FREE(action_config);
  117. break;
  118. }
  119. case cmd_sms: {
  120. /* Binary SMS (RP-DATA) */
  121. const char* ascii_pay = tsk_null;
  122. tsk_buffer_t* binary_pay = tsk_null;
  123. if((opt = opt_get_by_type(opts, opt_smsc)) && !tsk_strnullORempty(opt->value)) {
  124. tsip_uri_t* smsc = tsip_uri_parse(opt->value, tsk_strlen(opt->value));
  125. tsip_uri_t* dest = tsk_null;
  126. if(smsc) {
  127. /* Valid phone number for the SMSC address? */
  128. if(!is_valid_telnum(smsc)) {
  129. TSK_DEBUG_ERROR("[%s] contains invalid telephone number", opt->value);
  130. goto done;
  131. }
  132. tsip_ssession_set(session->handle,
  133. TSIP_SSESSION_SET_TO(opt->value),
  134. TSIP_SSESSION_SET_NULL());
  135. /* Destination URI */
  136. if((opt = opt_get_by_type(opts, opt_to)) && !tsk_strnullORempty(opt->value)) {
  137. if((dest = tsip_uri_parse(opt->value, tsk_strlen(opt->value)))) {
  138. if(!is_valid_telnum(dest)) {
  139. TSK_DEBUG_ERROR("[%s] contains invalid telephone number", opt->value);
  140. goto done;
  141. }
  142. }
  143. else {
  144. TSK_DEBUG_ERROR("[%s] is an invalid SIP/tel uri", opt->value);
  145. goto done;
  146. }
  147. }
  148. else {
  149. TSK_DEBUG_ERROR("++sms command need --to");
  150. goto done;
  151. }
  152. /* Payload? */
  153. if((opt = opt_get_by_type(opts, opt_payload)) && !tsk_strnullORempty(opt->value)) {
  154. ascii_pay = opt->value;
  155. }
  156. else {
  157. TSK_DEBUG_ERROR("++sms command need --to");
  158. goto done;
  159. }
  160. /* Create the binary content */
  161. if(!(binary_pay = sms_submit(smsc, dest, ascii_pay))) {
  162. TSK_DEBUG_ERROR("Failed to encode RP-DATA(SMS-SUBMIT) message.");
  163. goto done;
  164. }
  165. /* Send the message */
  166. tsip_api_message_send_message(session->handle,
  167. /* TSIP_ACTION_SET_HEADER("Content-Type", "application/vnd.3gpp.sms"), */
  168. /* TSIP_ACTION_SET_HEADER("Transfer-Encoding", "binary"),*/
  169. TSIP_ACTION_SET_PAYLOAD(binary_pay->data, binary_pay->size),
  170. TSIP_ACTION_SET_NULL());
  171. done:
  172. TSK_OBJECT_SAFE_FREE(binary_pay);
  173. TSK_OBJECT_SAFE_FREE(dest);
  174. TSK_OBJECT_SAFE_FREE(smsc);
  175. }
  176. else {
  177. TSK_DEBUG_ERROR("[%s] is an invalid SIP/tel uri", opt->value);
  178. break;
  179. }
  180. }
  181. else {
  182. TSK_DEBUG_ERROR("++sms command need --smsc");
  183. break;
  184. }
  185. break;
  186. }
  187. default:
  188. /* already handled by session_handle_cmd() */
  189. break;
  190. }
  191. bail:
  192. return id;
  193. }
  194. tsk_bool_t is_valid_telnum(const tsip_uri_t* uri)
  195. {
  196. tsk_size_t i;
  197. tsk_size_t len;
  198. if(!uri || tsk_strnullORempty(uri->user_name)) {
  199. return tsk_false;
  200. }
  201. for(i = 0, len = tsk_strlen(uri->user_name); i<len; i++) {
  202. if(uri->user_name[i] != '+' && !isdigit(uri->user_name[i])) {
  203. return tsk_false;
  204. }
  205. }
  206. return tsk_true;
  207. }
  208. tsk_buffer_t* sms_submit(const tsip_uri_t* smsc, const tsip_uri_t* dest, const char* ascii_pay)
  209. {
  210. static uint8_t mr = 0x00;
  211. int ret;
  212. tsk_buffer_t* buffer = tsk_null;
  213. tsms_tpdu_submit_t* sms_submit = tsk_null;
  214. tsms_rpdu_data_t* rp_data = tsk_null;
  215. // create SMS-SUBMIT message
  216. sms_submit = tsms_tpdu_submit_create(++mr, (const uint8_t*)smsc->user_name, (const uint8_t*)dest->user_name);
  217. // Set content for SMS-SUBMIT
  218. if((buffer = tsms_pack_to_7bit(ascii_pay))) {
  219. if((ret = tsms_tpdu_submit_set_userdata(sms_submit, buffer, tsms_alpha_7bit))) {
  220. goto bail;
  221. }
  222. TSK_OBJECT_SAFE_FREE(buffer);
  223. }
  224. // create Mobile Originated (MO) RP-DATA message
  225. if((rp_data = tsms_rpdu_data_create_mo(mr, (const uint8_t*)smsc->user_name, TSMS_TPDU_MESSAGE(sms_submit)))) {
  226. // serialize into a buffer
  227. if((buffer = tsk_buffer_create_null())) {
  228. ret = tsms_rpdu_data_serialize(rp_data, buffer);
  229. }
  230. }
  231. bail:
  232. if(ret) { /* Failed? */
  233. TSK_OBJECT_SAFE_FREE(buffer);
  234. }
  235. TSK_OBJECT_SAFE_FREE(sms_submit);
  236. TSK_OBJECT_SAFE_FREE(rp_data);
  237. return buffer;
  238. }