osp_mod.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Kamailio osp module.
  3. *
  4. * This module enables Kamailio to communicate with an Open Settlement
  5. * Protocol (OSP) server. The Open Settlement Protocol is an ETSI
  6. * defined standard for Inter-Domain VoIP pricing, authorization
  7. * and usage exchange. The technical specifications for OSP
  8. * (ETSI TS 101 321 V4.1.1) are available at www.etsi.org.
  9. *
  10. * Uli Abend was the original contributor to this module.
  11. *
  12. * Copyright (C) 2001-2005 Fhg Fokus
  13. *
  14. * This file is part of Kamailio, a free SIP server.
  15. *
  16. * Kamailio is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version
  20. *
  21. * Kamailio is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  29. *
  30. * History:
  31. * ---------
  32. * 2006-03-13 RR functions are loaded via API function (bogdan)
  33. */
  34. #include <osp/osp.h>
  35. #include "../rr/api.h"
  36. #include "../../modules/siputils/siputils.h"
  37. #include "osp_mod.h"
  38. #include "orig_transaction.h"
  39. #include "term_transaction.h"
  40. #include "usage.h"
  41. #include "tm.h"
  42. #include "provider.h"
  43. MODULE_VERSION
  44. extern unsigned int _osp_sp_number;
  45. extern char* _osp_sp_uris[];
  46. extern unsigned long _osp_sp_weights[];
  47. extern char* _osp_device_ip;
  48. extern char* _osp_device_port;
  49. extern unsigned char* _osp_private_key;
  50. extern unsigned char* _osp_local_certificate;
  51. extern unsigned char* _osp_ca_certificate;
  52. extern int _osp_crypto_hw;
  53. extern int _osp_validate_callid;
  54. extern int _osp_token_format;
  55. extern int _osp_ssl_lifetime;
  56. extern int _osp_persistence;
  57. extern int _osp_retry_delay;
  58. extern int _osp_retry_limit;
  59. extern int _osp_timeout;
  60. extern int _osp_max_dests;
  61. extern int _osp_use_rpid;
  62. extern int _osp_redir_uri;
  63. extern char _osp_PRIVATE_KEY[];
  64. extern char _osp_LOCAL_CERTIFICATE[];
  65. extern char _osp_CA_CERTIFICATE[];
  66. extern char* _osp_snid_avp;
  67. extern int_str _osp_snid_avpname;
  68. extern unsigned short _osp_snid_avptype;
  69. extern OSPTPROVHANDLE _osp_provider;
  70. struct rr_binds osp_rr;
  71. siputils_api_t osp_siputils;
  72. int osp_index[OSP_DEF_SPS];
  73. static int ospInitMod(void);
  74. static void ospDestMod(void);
  75. static int ospInitChild(int);
  76. static int ospVerifyParameters(void);
  77. static void ospDumpParameters(void);
  78. static cmd_export_t cmds[]={
  79. {"checkospheader", (cmd_function)ospCheckHeader, 0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE},
  80. {"validateospheader", (cmd_function)ospValidateHeader, 0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE},
  81. {"requestosprouting", (cmd_function)ospRequestRouting, 0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE},
  82. {"checkosproute", (cmd_function)ospCheckRoute, 0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE},
  83. {"prepareosproute", (cmd_function)ospPrepareRoute, 0, 0, 0, BRANCH_ROUTE},
  84. {"prepareallosproutes", (cmd_function)ospPrepareAllRoutes, 0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE},
  85. {"checkcallingtranslation", (cmd_function)ospCheckTranslation, 0, 0, 0, BRANCH_ROUTE},
  86. {"reportospusage", (cmd_function)ospReportUsage, 1, 0, 0, REQUEST_ROUTE},
  87. {0, 0, 0, 0, 0, 0}
  88. };
  89. static param_export_t params[]={
  90. {"sp1_uri", PARAM_STRING, &_osp_sp_uris[0]},
  91. {"sp2_uri", PARAM_STRING, &_osp_sp_uris[1]},
  92. {"sp3_uri", PARAM_STRING, &_osp_sp_uris[2]},
  93. {"sp4_uri", PARAM_STRING, &_osp_sp_uris[3]},
  94. {"sp5_uri", PARAM_STRING, &_osp_sp_uris[4]},
  95. {"sp6_uri", PARAM_STRING, &_osp_sp_uris[5]},
  96. {"sp7_uri", PARAM_STRING, &_osp_sp_uris[6]},
  97. {"sp8_uri", PARAM_STRING, &_osp_sp_uris[7]},
  98. {"sp9_uri", PARAM_STRING, &_osp_sp_uris[8]},
  99. {"sp10_uri", PARAM_STRING, &_osp_sp_uris[9]},
  100. {"sp11_uri", PARAM_STRING, &_osp_sp_uris[10]},
  101. {"sp12_uri", PARAM_STRING, &_osp_sp_uris[11]},
  102. {"sp13_uri", PARAM_STRING, &_osp_sp_uris[12]},
  103. {"sp14_uri", PARAM_STRING, &_osp_sp_uris[13]},
  104. {"sp15_uri", PARAM_STRING, &_osp_sp_uris[14]},
  105. {"sp16_uri", PARAM_STRING, &_osp_sp_uris[15]},
  106. {"sp1_weight", INT_PARAM, &(_osp_sp_weights[0])},
  107. {"sp2_weight", INT_PARAM, &(_osp_sp_weights[1])},
  108. {"sp3_weight", INT_PARAM, &(_osp_sp_weights[2])},
  109. {"sp4_weight", INT_PARAM, &(_osp_sp_weights[3])},
  110. {"sp5_weight", INT_PARAM, &(_osp_sp_weights[4])},
  111. {"sp6_weight", INT_PARAM, &(_osp_sp_weights[5])},
  112. {"sp7_weight", INT_PARAM, &(_osp_sp_weights[6])},
  113. {"sp8_weight", INT_PARAM, &(_osp_sp_weights[7])},
  114. {"sp9_weight", INT_PARAM, &(_osp_sp_weights[8])},
  115. {"sp10_weight", INT_PARAM, &(_osp_sp_weights[9])},
  116. {"sp11_weight", INT_PARAM, &(_osp_sp_weights[10])},
  117. {"sp12_weight", INT_PARAM, &(_osp_sp_weights[11])},
  118. {"sp13_weight", INT_PARAM, &(_osp_sp_weights[12])},
  119. {"sp14_weight", INT_PARAM, &(_osp_sp_weights[13])},
  120. {"sp15_weight", INT_PARAM, &(_osp_sp_weights[14])},
  121. {"sp16_weight", INT_PARAM, &(_osp_sp_weights[15])},
  122. {"device_ip", PARAM_STRING, &_osp_device_ip},
  123. {"device_port", PARAM_STRING, &_osp_device_port},
  124. {"private_key", PARAM_STRING, &_osp_private_key},
  125. {"local_certificate", PARAM_STRING, &_osp_local_certificate},
  126. {"ca_certificates", PARAM_STRING, &_osp_ca_certificate},
  127. {"enable_crypto_hardware_support", INT_PARAM, &_osp_crypto_hw},
  128. {"validate_callid", INT_PARAM, &(_osp_validate_callid)},
  129. {"token_format", INT_PARAM, &_osp_token_format},
  130. {"ssl_lifetime", INT_PARAM, &_osp_ssl_lifetime},
  131. {"persistence", INT_PARAM, &_osp_persistence},
  132. {"retry_delay", INT_PARAM, &_osp_retry_delay},
  133. {"retry_limit", INT_PARAM, &_osp_retry_limit},
  134. {"timeout", INT_PARAM, &_osp_timeout},
  135. {"max_destinations", INT_PARAM, &_osp_max_dests},
  136. {"use_rpid_for_calling_number", INT_PARAM, &_osp_use_rpid},
  137. {"redirection_uri_format", INT_PARAM, &_osp_redir_uri},
  138. {"source_networkid_avp", PARAM_STRING, &_osp_snid_avp},
  139. {0,0,0}
  140. };
  141. struct module_exports exports = {
  142. "osp",
  143. DEFAULT_DLFLAGS, /* dlopen flags */
  144. cmds,
  145. params,
  146. 0, /* exported statistics */
  147. 0, /* exported MI functions */
  148. 0, /* exported pseudo-variables */
  149. 0, /* extra processes */
  150. ospInitMod, /* module initialization function */
  151. 0, /* response function*/
  152. ospDestMod, /* destroy function */
  153. ospInitChild, /* per-child init function */
  154. };
  155. /*
  156. * Initialize OSP module
  157. * return 0 success, -1 failure
  158. */
  159. static int ospInitMod(void)
  160. {
  161. bind_siputils_t bind_su;
  162. if (ospVerifyParameters() != 0) {
  163. /* At least one parameter incorrect -> error */
  164. return -1;
  165. }
  166. /* Load the RR API */
  167. if (load_rr_api(&osp_rr) != 0) {
  168. LM_WARN("failed to load the RR API. Check if you load the rr module\n");
  169. LM_WARN("add_rr_param is required for reporting duration for OSP transactions\n");
  170. memset(&osp_rr, 0, sizeof(osp_rr));
  171. }
  172. /* Load the AUTH API */
  173. bind_su = (bind_siputils_t)find_export("bind_siputils", 1, 0);
  174. if ((bind_su == NULL) || (bind_su(&osp_siputils) != 0)) {
  175. LM_WARN("failed to load the SIPUTILS API. Check if you load the auth module.\n");
  176. LM_WARN("rpid_avp & rpid_avp_type is required for calling number translation\n");
  177. memset(&osp_siputils, 0, sizeof(osp_siputils));
  178. }
  179. if (ospInitTm() < 0) {
  180. return -1;
  181. }
  182. /* everything is fine, initialization done */
  183. return 0;
  184. }
  185. /*
  186. * Destrroy OSP module
  187. */
  188. static void ospDestMod(void)
  189. {
  190. }
  191. /*
  192. * Initializeild process of OSP module
  193. * param rank
  194. * return 0 success, -1 failure
  195. */
  196. static int ospInitChild(
  197. int rank)
  198. {
  199. int code = -1;
  200. code = ospSetupProvider();
  201. LM_DBG("provider '%d' (%d)\n", _osp_provider, code);
  202. return 0;
  203. }
  204. /*
  205. * Verify parameters for OSP module
  206. * return 0 success, -1 failure
  207. */
  208. static int ospVerifyParameters(void)
  209. {
  210. int i;
  211. pv_spec_t avp_spec;
  212. str avp_str;
  213. int result = 0;
  214. /* Default location for the cert files is in the compile time variable CFG_DIR */
  215. if (_osp_private_key == NULL) {
  216. sprintf(_osp_PRIVATE_KEY, "%spkey.pem", CFG_DIR);
  217. _osp_private_key = (unsigned char*)_osp_PRIVATE_KEY;
  218. }
  219. if (_osp_local_certificate == NULL) {
  220. sprintf(_osp_LOCAL_CERTIFICATE, "%slocalcert.pem", CFG_DIR);
  221. _osp_local_certificate = (unsigned char*)_osp_LOCAL_CERTIFICATE;
  222. }
  223. if (_osp_ca_certificate == NULL) {
  224. sprintf(_osp_CA_CERTIFICATE, "%scacert_0.pem", CFG_DIR);
  225. _osp_ca_certificate = (unsigned char*)_osp_CA_CERTIFICATE;
  226. }
  227. if (_osp_device_ip == NULL) {
  228. _osp_device_ip = "";
  229. }
  230. if (_osp_device_port == NULL) {
  231. _osp_device_port = "";
  232. }
  233. if (_osp_max_dests > OSP_DEF_DESTS || _osp_max_dests < 1) {
  234. _osp_max_dests = OSP_DEF_DESTS;
  235. LM_WARN("max_destinations is out of range, reset to %d\n", OSP_DEF_DESTS);
  236. }
  237. if (_osp_token_format < 0 || _osp_token_format > 2) {
  238. _osp_token_format = OSP_DEF_TOKEN;
  239. LM_WARN("token_format is out of range, reset to %d\n", OSP_DEF_TOKEN);
  240. }
  241. _osp_sp_number = 0;
  242. for (i = 0; i < OSP_DEF_SPS; i++) {
  243. if (_osp_sp_uris[i] != NULL) {
  244. if (_osp_sp_number != i) {
  245. _osp_sp_uris[_osp_sp_number] = _osp_sp_uris[i];
  246. _osp_sp_weights[_osp_sp_number] = _osp_sp_weights[i];
  247. _osp_sp_uris[i] = NULL;
  248. _osp_sp_weights[i] = OSP_DEF_WEIGHT;
  249. }
  250. osp_index[_osp_sp_number] = i + 1;
  251. _osp_sp_number++;
  252. }
  253. }
  254. if (_osp_sp_number == 0) {
  255. LM_ERR("at least one service point uri must be configured\n");
  256. result = -1;
  257. }
  258. if (_osp_snid_avp && *_osp_snid_avp) {
  259. avp_str.s = _osp_snid_avp;
  260. avp_str.len = strlen(_osp_snid_avp);
  261. if (pv_parse_spec(&avp_str, &avp_spec) == NULL ||
  262. avp_spec.type != PVT_AVP ||
  263. pv_get_avp_name(0, &(avp_spec.pvp), &_osp_snid_avpname, &_osp_snid_avptype) != 0)
  264. {
  265. LM_WARN("'%s' invalid AVP definition\n", _osp_snid_avp);
  266. _osp_snid_avpname.n = 0;
  267. _osp_snid_avptype = 0;
  268. }
  269. } else {
  270. _osp_snid_avpname.n = 0;
  271. _osp_snid_avptype = 0;
  272. }
  273. ospDumpParameters();
  274. return result;
  275. }
  276. /*
  277. * Dump OSP module configuration
  278. */
  279. static void ospDumpParameters(void)
  280. {
  281. int i;
  282. LM_INFO("module configuration: ");
  283. LM_INFO(" number of service points '%d'", _osp_sp_number);
  284. for (i = 0; i < _osp_sp_number; i++) {
  285. LM_INFO(" sp%d_uri '%s' sp%d_weight '%ld' ",
  286. osp_index[i], _osp_sp_uris[i], osp_index[i], _osp_sp_weights[i]);
  287. }
  288. LM_INFO(" device_ip '%s' device_port '%s' ", _osp_device_ip, _osp_device_port);
  289. LM_INFO(" private_key '%s' ", _osp_private_key);
  290. LM_INFO(" local_certificate '%s' ", _osp_local_certificate);
  291. LM_INFO(" ca_certificates '%s' ", _osp_ca_certificate);
  292. LM_INFO(" enable_crypto_hardware_support '%d' ", _osp_crypto_hw);
  293. LM_INFO(" token_format '%d' ", _osp_token_format);
  294. LM_INFO(" ssl_lifetime '%d' ", _osp_ssl_lifetime);
  295. LM_INFO(" persistence '%d' ", _osp_persistence);
  296. LM_INFO(" retry_delay '%d' ", _osp_retry_delay);
  297. LM_INFO(" retry_limit '%d' ", _osp_retry_limit);
  298. LM_INFO(" timeout '%d' ", _osp_timeout);
  299. LM_INFO(" validate_call_id '%d' ", _osp_validate_callid);
  300. LM_INFO(" use_rpid_for_calling_number '%d' ", _osp_use_rpid);
  301. LM_INFO(" redirection_uri_format '%d' ", _osp_redir_uri);
  302. LM_INFO(" max_destinations '%d'\n", _osp_max_dests);
  303. if (_osp_snid_avpname.n == 0) {
  304. LM_INFO(" source network ID disabled\n");
  305. } else if (_osp_snid_avptype & AVP_NAME_STR) {
  306. LM_INFO(" source network ID AVP name '%.*s'\n", _osp_snid_avpname.s.len, _osp_snid_avpname.s.s);
  307. } else {
  308. LM_INFO(" source network ID AVP ID '%d'\n", _osp_snid_avpname.n);
  309. }
  310. }