mod_skel.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  3. * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
  4. *
  5. * Version: MPL 1.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Anthony Minessale II <anthm@freeswitch.org>
  21. * Portions created by the Initial Developer are Copyright (C)
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. *
  26. * Anthony Minessale II <anthm@freeswitch.org>
  27. * Neal Horman <neal at wanlink dot com>
  28. *
  29. *
  30. * mod_skel.c -- Framework Demo Module
  31. *
  32. */
  33. #include <switch.h>
  34. /* Prototypes */
  35. SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_skel_shutdown);
  36. SWITCH_MODULE_RUNTIME_FUNCTION(mod_skel_runtime);
  37. SWITCH_MODULE_LOAD_FUNCTION(mod_skel_load);
  38. /* SWITCH_MODULE_DEFINITION(name, load, shutdown, runtime)
  39. * Defines a switch_loadable_module_function_table_t and a static const char[] modname
  40. */
  41. SWITCH_MODULE_DEFINITION(mod_skel, mod_skel_load, mod_skel_shutdown, NULL);
  42. typedef enum {
  43. CODEC_NEGOTIATION_GREEDY = 1,
  44. CODEC_NEGOTIATION_GENEROUS = 2,
  45. CODEC_NEGOTIATION_EVIL = 3
  46. } codec_negotiation_t;
  47. static struct {
  48. char *codec_negotiation_str;
  49. codec_negotiation_t codec_negotiation;
  50. switch_bool_t sip_trace;
  51. int integer;
  52. } globals;
  53. static switch_status_t config_callback_siptrace(switch_xml_config_item_t *data, switch_config_callback_type_t callback_type, switch_bool_t changed)
  54. {
  55. switch_bool_t value = *(switch_bool_t *) data->ptr;
  56. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "In siptrace callback: value %s changed %s\n",
  57. value ? "true" : "false", changed ? "true" : "false");
  58. /*
  59. if ((callback_type == CONFIG_LOG || callback_type == CONFIG_RELOAD) && changed) {
  60. nua_set_params(((sofia_profile_t*)data->functiondata)->nua, TPTAG_LOG(value), TAG_END());
  61. }
  62. */
  63. return SWITCH_STATUS_SUCCESS;
  64. }
  65. static switch_xml_config_string_options_t config_opt_codec_negotiation = { NULL, 0, "greedy|generous|evil" };
  66. /* enforce_min, min, enforce_max, max */
  67. static switch_xml_config_int_options_t config_opt_integer = { SWITCH_TRUE, 0, SWITCH_TRUE, 10 };
  68. static switch_xml_config_enum_item_t config_opt_codec_negotiation_enum[] = {
  69. {"greedy", CODEC_NEGOTIATION_GREEDY},
  70. {"generous", CODEC_NEGOTIATION_GENEROUS},
  71. {"evil", CODEC_NEGOTIATION_EVIL},
  72. {NULL, 0}
  73. };
  74. static switch_xml_config_item_t instructions[] = {
  75. /* parameter name type reloadable pointer default value options structure */
  76. SWITCH_CONFIG_ITEM("codec-negotiation-str", SWITCH_CONFIG_STRING, CONFIG_RELOADABLE, &globals.codec_negotiation_str, "greedy",
  77. &config_opt_codec_negotiation,
  78. "greedy|generous|evil", "Specifies the codec negotiation scheme to be used."),
  79. SWITCH_CONFIG_ITEM("codec-negotiation", SWITCH_CONFIG_ENUM, CONFIG_RELOADABLE, &globals.codec_negotiation, (void *) CODEC_NEGOTIATION_GREEDY,
  80. &config_opt_codec_negotiation_enum,
  81. "greedy|generous|evil", "Specifies the codec negotiation scheme to be used."),
  82. SWITCH_CONFIG_ITEM_CALLBACK("sip-trace", SWITCH_CONFIG_BOOL, CONFIG_RELOADABLE, &globals.sip_trace, (void *) SWITCH_FALSE,
  83. (switch_xml_config_callback_t) config_callback_siptrace, NULL,
  84. "yes|no", "If enabled, print out sip messages on the console."),
  85. SWITCH_CONFIG_ITEM("integer", SWITCH_CONFIG_INT, CONFIG_RELOADABLE, &globals.integer, (void *) 100, &config_opt_integer,
  86. NULL, NULL),
  87. SWITCH_CONFIG_ITEM_END()
  88. };
  89. static switch_status_t do_config(switch_bool_t reload)
  90. {
  91. memset(&globals, 0, sizeof(globals));
  92. if (switch_xml_config_parse_module_settings("skel.conf", reload, instructions) != SWITCH_STATUS_SUCCESS) {
  93. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Could not open skel.conf\n");
  94. return SWITCH_STATUS_FALSE;
  95. }
  96. return SWITCH_STATUS_SUCCESS;
  97. }
  98. #include "switch_stun.h"
  99. #define _switch_stun_packet_next_attribute(attribute, end) (attribute && (attribute = (switch_stun_packet_attribute_t *) (attribute->value + ntohs(attribute->length))) && ((void *)attribute < end) && ntohs(attribute->length) && ((void *)(attribute + ntohs(attribute->length)) < end))
  100. #define _switch_stun_attribute_padded_length(attribute) ((uint16_t)(ntohs(attribute->length) + (sizeof(uint32_t)-1)) & ~sizeof(uint32_t))
  101. //#define _switch_stun_packet_next_attribute(attribute, end) (attribute && (attribute = (switch_stun_packet_attribute_t *) (attribute->value + _switch_stun_attribute_padded_length(attribute))) && ((void *)attribute < end) && ((void *)(attribute + _switch_stun_attribute_padded_length(attribute)) < end))
  102. SWITCH_STANDARD_API(skel_function)
  103. {
  104. switch_event_t *event;
  105. unsigned char frame_buffer[8192] = {0};
  106. uint8_t buf[256] = { 0 };
  107. switch_stun_packet_t *packet;
  108. char user_name[] = "0000000000000000:1111111111111111";
  109. //char user_name[] = "0000000000000000";
  110. void *end_buf;
  111. switch_stun_packet_attribute_t *attr;
  112. int xlen = 0;
  113. packet = switch_stun_packet_build_header(SWITCH_STUN_BINDING_REQUEST, NULL, buf);
  114. printf("1len %d %d\n", ntohs(packet->header.length), xlen);
  115. switch_stun_packet_attribute_add_username(packet, user_name, strlen(user_name));
  116. printf("2len %d %d\n", ntohs(packet->header.length), xlen);
  117. switch_stun_packet_attribute_add_controlled(packet);
  118. //switch_stun_packet_attribute_add_password(packet, user_name, strlen(user_name));
  119. //printf("3len %d %d\n", ntohs(packet->header.length), xlen);
  120. //switch_stun_packet_attribute_add_use_candidate(packet);
  121. switch_stun_packet_attribute_add_integrity(packet, "FUCK");
  122. switch_stun_packet_attribute_add_fingerprint(packet);
  123. end_buf = buf + ((sizeof(buf) > packet->header.length) ? packet->header.length : sizeof(buf));
  124. switch_stun_packet_first_attribute(packet, attr);
  125. xlen = sizeof(switch_stun_packet_header_t);
  126. printf("len %d %d\n", ntohs(packet->header.length), xlen);
  127. do {
  128. printf("WTF %p %d %d:(%d)\n", (void *)attr, ntohs(attr->type), ntohs(attr->length), switch_stun_attribute_padded_length_hbo(attr));
  129. if (!switch_stun_packet_next_attribute_hbo(attr, end_buf)) {
  130. break;
  131. }
  132. xlen += 4+switch_stun_attribute_padded_length_hbo(attr);
  133. } while (xlen <= ntohs(packet->header.length));
  134. return SWITCH_STATUS_SUCCESS;
  135. do_config(SWITCH_TRUE);
  136. if (switch_event_create(&event, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) {
  137. switch_size_t len = 0;
  138. int x = 0;
  139. /* populate the event with some headers */
  140. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "testing", "true");
  141. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "foo", "bar");
  142. for (x = 0; x < 10; x++) {
  143. char name[128];
  144. switch_snprintf(name, sizeof(name), "test-header-%d", x);
  145. switch_event_add_header(event, SWITCH_STACK_BOTTOM, name, "value-%d", x);
  146. }
  147. /* Nothing up my sleeve, here is the event */
  148. DUMP_EVENT(event);
  149. /* ok, serialize it into frame_buffer and destroy the event *poof* */
  150. len = sizeof(frame_buffer);
  151. switch_event_binary_serialize(event, (void *)frame_buffer, &len);
  152. switch_event_destroy(&event);
  153. /* wave the magic wand and feed frame_buffer to deserialize */
  154. switch_event_binary_deserialize(&event, (void *)frame_buffer, len, SWITCH_FALSE);
  155. /* TA DA */
  156. DUMP_EVENT(event);
  157. switch_event_destroy(&event);
  158. }
  159. return SWITCH_STATUS_SUCCESS;
  160. }
  161. static void mycb(switch_core_session_t *session, switch_channel_callstate_t callstate, switch_device_record_t *drec)
  162. {
  163. switch_channel_t *channel = switch_core_session_get_channel(session);
  164. switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_CRIT,
  165. "%s device: %s\nState: %s Dev State: %s/%s Total:%u Offhook:%u Active:%u Held:%u Hungup:%u Dur: %u %s\n",
  166. switch_channel_get_name(channel),
  167. drec->device_id,
  168. switch_channel_callstate2str(callstate),
  169. switch_channel_device_state2str(drec->last_state),
  170. switch_channel_device_state2str(drec->state),
  171. drec->stats.total,
  172. drec->stats.offhook,
  173. drec->stats.active,
  174. drec->stats.held,
  175. drec->stats.hup,
  176. drec->active_stop ? (uint32_t)(drec->active_stop - drec->active_start) / 1000 : 0,
  177. switch_channel_test_flag(channel, CF_FINAL_DEVICE_LEG) ? "FINAL LEG" : "");
  178. }
  179. /* Macro expands to: switch_status_t mod_skel_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool) */
  180. SWITCH_MODULE_LOAD_FUNCTION(mod_skel_load)
  181. {
  182. switch_api_interface_t *api_interface;
  183. /* connect my internal structure to the blank pointer passed to me */
  184. *module_interface = switch_loadable_module_create_module_interface(pool, modname);
  185. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Hello World!\n");
  186. do_config(SWITCH_FALSE);
  187. SWITCH_ADD_API(api_interface, "skel", "Skel API", skel_function, "syntax");
  188. switch_channel_bind_device_state_handler(mycb, NULL);
  189. /* indicate that the module should continue to be loaded */
  190. return SWITCH_STATUS_SUCCESS;
  191. }
  192. /*
  193. Called when the system shuts down
  194. Macro expands to: switch_status_t mod_skel_shutdown() */
  195. SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_skel_shutdown)
  196. {
  197. /* Cleanup dynamically allocated config settings */
  198. switch_channel_unbind_device_state_handler(mycb);
  199. switch_xml_config_cleanup(instructions);
  200. return SWITCH_STATUS_SUCCESS;
  201. }
  202. /*
  203. If it exists, this is called in it's own thread when the module-load completes
  204. If it returns anything but SWITCH_STATUS_TERM it will be called again automatically
  205. Macro expands to: switch_status_t mod_skel_runtime()
  206. SWITCH_MODULE_RUNTIME_FUNCTION(mod_skel_runtime)
  207. {
  208. while(looping)
  209. {
  210. switch_cond_next();
  211. }
  212. return SWITCH_STATUS_TERM;
  213. }
  214. */
  215. /* For Emacs:
  216. * Local Variables:
  217. * mode:c
  218. * indent-tabs-mode:t
  219. * tab-width:4
  220. * c-basic-offset:4
  221. * End:
  222. * For VIM:
  223. * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet
  224. */