osp_mod.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * ser osp module.
  3. *
  4. * This module enables ser 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 ser, a free SIP server.
  15. *
  16. * ser 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. * For a license to use the ser software under conditions
  22. * other than those described here, or to purchase support for this
  23. * software, please contact iptel.org by e-mail at the following addresses:
  24. * [email protected]
  25. *
  26. * ser is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  34. */
  35. #include <osp/osp.h>
  36. #include "../../sr_module.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 OSPTPROVHANDLE _osp_provider;
  67. int osp_index[OSP_DEF_SPS];
  68. static int ospInitMod(void);
  69. static void ospDestMod(void);
  70. static int ospInitChild(int);
  71. static int ospVerifyParameters(void);
  72. static void ospDumpParameters(void);
  73. static cmd_export_t cmds[]={
  74. {"checkospheader", ospCheckHeader, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE},
  75. {"validateospheader", ospValidateHeader, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE},
  76. {"requestosprouting", ospRequestRouting, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE},
  77. {"checkosproute", ospCheckRoute, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE},
  78. {"prepareospfirstroute", ospPrepareFirstRoute,0, 0, REQUEST_ROUTE|FAILURE_ROUTE},
  79. {"prepareospnextroute", ospPrepareNextRoute, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE},
  80. {"prepareallosproutes", ospPrepareAllRoutes, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE},
  81. {"appendospheaders", ospAppendHeaders, 0, 0, BRANCH_ROUTE},
  82. {"reportospusage", ospReportUsage, 0, 0, REQUEST_ROUTE},
  83. {0, 0, 0, 0, 0}
  84. };
  85. static param_export_t params[]={
  86. {"sp1_uri", PARAM_STRING, &_osp_sp_uris[0]},
  87. {"sp2_uri", PARAM_STRING, &_osp_sp_uris[1]},
  88. {"sp3_uri", PARAM_STRING, &_osp_sp_uris[2]},
  89. {"sp4_uri", PARAM_STRING, &_osp_sp_uris[3]},
  90. {"sp5_uri", PARAM_STRING, &_osp_sp_uris[4]},
  91. {"sp6_uri", PARAM_STRING, &_osp_sp_uris[5]},
  92. {"sp7_uri", PARAM_STRING, &_osp_sp_uris[6]},
  93. {"sp8_uri", PARAM_STRING, &_osp_sp_uris[7]},
  94. {"sp9_uri", PARAM_STRING, &_osp_sp_uris[8]},
  95. {"sp10_uri", PARAM_STRING, &_osp_sp_uris[9]},
  96. {"sp11_uri", PARAM_STRING, &_osp_sp_uris[10]},
  97. {"sp12_uri", PARAM_STRING, &_osp_sp_uris[11]},
  98. {"sp13_uri", PARAM_STRING, &_osp_sp_uris[12]},
  99. {"sp14_uri", PARAM_STRING, &_osp_sp_uris[13]},
  100. {"sp15_uri", PARAM_STRING, &_osp_sp_uris[14]},
  101. {"sp16_uri", PARAM_STRING, &_osp_sp_uris[15]},
  102. {"sp1_weight", INT_PARAM, &(_osp_sp_weights[0])},
  103. {"sp2_weight", INT_PARAM, &(_osp_sp_weights[1])},
  104. {"sp3_weight", INT_PARAM, &(_osp_sp_weights[2])},
  105. {"sp4_weight", INT_PARAM, &(_osp_sp_weights[3])},
  106. {"sp5_weight", INT_PARAM, &(_osp_sp_weights[4])},
  107. {"sp6_weight", INT_PARAM, &(_osp_sp_weights[5])},
  108. {"sp7_weight", INT_PARAM, &(_osp_sp_weights[6])},
  109. {"sp8_weight", INT_PARAM, &(_osp_sp_weights[7])},
  110. {"sp9_weight", INT_PARAM, &(_osp_sp_weights[8])},
  111. {"sp10_weight", INT_PARAM, &(_osp_sp_weights[9])},
  112. {"sp11_weight", INT_PARAM, &(_osp_sp_weights[10])},
  113. {"sp12_weight", INT_PARAM, &(_osp_sp_weights[11])},
  114. {"sp13_weight", INT_PARAM, &(_osp_sp_weights[12])},
  115. {"sp14_weight", INT_PARAM, &(_osp_sp_weights[13])},
  116. {"sp15_weight", INT_PARAM, &(_osp_sp_weights[14])},
  117. {"sp16_weight", INT_PARAM, &(_osp_sp_weights[15])},
  118. {"device_ip", PARAM_STRING, &_osp_device_ip},
  119. {"device_port", PARAM_STRING, &_osp_device_port},
  120. {"private_key", PARAM_STRING, &_osp_private_key},
  121. {"local_certificate", PARAM_STRING, &_osp_local_certificate},
  122. {"ca_certificates", PARAM_STRING, &_osp_ca_certificate},
  123. {"enable_crypto_hardware_support", INT_PARAM, &_osp_crypto_hw},
  124. {"validate_callid", INT_PARAM, &(_osp_validate_callid)},
  125. {"token_format", INT_PARAM, &_osp_token_format},
  126. {"ssl_lifetime", INT_PARAM, &_osp_ssl_lifetime},
  127. {"persistence", INT_PARAM, &_osp_persistence},
  128. {"retry_delay", INT_PARAM, &_osp_retry_delay},
  129. {"retry_limit", INT_PARAM, &_osp_retry_limit},
  130. {"timeout", INT_PARAM, &_osp_timeout},
  131. {"max_destinations", INT_PARAM, &_osp_max_dests},
  132. {"use_rpid_for_calling_number", INT_PARAM, &_osp_use_rpid},
  133. {"redirection_uri_format", INT_PARAM, &_osp_redir_uri},
  134. {0,0,0}
  135. };
  136. struct module_exports exports = {
  137. "osp",
  138. cmds,
  139. 0, /* RPC methods */
  140. params,
  141. ospInitMod, /* module initialization function */
  142. 0, /* response function*/
  143. ospDestMod, /* destroy function */
  144. 0, /* oncancel function */
  145. ospInitChild, /* per-child init function */
  146. };
  147. /*
  148. * Initialize OSP module
  149. * return 0 success, -1 failure
  150. */
  151. static int ospInitMod(void)
  152. {
  153. LOG(L_DBG, "osp: ospInitMod\n");
  154. if (ospVerifyParameters() != 0) {
  155. /* At least one parameter incorrect -> error */
  156. return -1;
  157. }
  158. if (ospInitTm() < 0) {
  159. return -1;
  160. }
  161. /* everything is fine, initialization done */
  162. return 0;
  163. }
  164. /*
  165. * Destrroy OSP module
  166. */
  167. static void ospDestMod(void)
  168. {
  169. LOG(L_DBG, "osp: ospDestMod\n");
  170. }
  171. /*
  172. * Initializeild process of OSP module
  173. * param rank
  174. * return 0 success, -1 failure
  175. */
  176. static int ospInitChild(
  177. int rank)
  178. {
  179. int code = -1;
  180. LOG(L_DBG, "osp: ospInitChild\n");
  181. code = ospSetupProvider();
  182. LOG(L_DBG, "osp: provider '%i' (%d)\n", _osp_provider, code);
  183. return 0;
  184. }
  185. /*
  186. * Verify parameters for OSP module
  187. * return 0 success, -1 failure
  188. */
  189. static int ospVerifyParameters(void)
  190. {
  191. int i;
  192. int result = 0;
  193. LOG(L_DBG, "osp: ospVerifyParamters\n");
  194. /* Default location for the cert files is in the compile time variable CFG_DIR */
  195. if (_osp_private_key == NULL) {
  196. sprintf(_osp_PRIVATE_KEY, "%spkey.pem", CFG_DIR);
  197. _osp_private_key = (unsigned char*)_osp_PRIVATE_KEY;
  198. }
  199. if (_osp_local_certificate == NULL) {
  200. sprintf(_osp_LOCAL_CERTIFICATE, "%slocalcert.pem", CFG_DIR);
  201. _osp_local_certificate = (unsigned char*)_osp_LOCAL_CERTIFICATE;
  202. }
  203. if (_osp_ca_certificate == NULL) {
  204. sprintf(_osp_CA_CERTIFICATE, "%scacert_0.pem", CFG_DIR);
  205. _osp_ca_certificate = (unsigned char*)_osp_CA_CERTIFICATE;
  206. }
  207. if (_osp_device_ip == NULL) {
  208. _osp_device_ip = "";
  209. }
  210. if (_osp_device_port == NULL) {
  211. _osp_device_port = "";
  212. }
  213. if (_osp_max_dests > OSP_DEF_DESTS || _osp_max_dests < 1) {
  214. _osp_max_dests = OSP_DEF_DESTS;
  215. LOG(L_WARN,
  216. "osp: WARN: max_destinations is out of range, reset to %d\n",
  217. OSP_DEF_DESTS);
  218. }
  219. if (_osp_token_format < 0 || _osp_token_format > 2) {
  220. _osp_token_format = OSP_DEF_TOKEN;
  221. LOG(L_WARN,
  222. "osp: WARN: token_format is out of range, reset to %d\n",
  223. OSP_DEF_TOKEN);
  224. }
  225. _osp_sp_number = 0;
  226. for (i = 0; i < OSP_DEF_SPS; i++) {
  227. if (_osp_sp_uris[i] != NULL) {
  228. if (_osp_sp_number != i) {
  229. _osp_sp_uris[_osp_sp_number] = _osp_sp_uris[i];
  230. _osp_sp_weights[_osp_sp_number] = _osp_sp_weights[i];
  231. _osp_sp_uris[i] = NULL;
  232. _osp_sp_weights[i] = OSP_DEF_WEIGHT;
  233. }
  234. osp_index[_osp_sp_number] = i + 1;
  235. _osp_sp_number++;
  236. }
  237. }
  238. if (_osp_sp_number == 0) {
  239. LOG(L_ERR, "osp: ERROR: at least one service point uri must be configured\n");
  240. result = -1;
  241. }
  242. ospDumpParameters();
  243. return result;
  244. }
  245. /*
  246. * Dump OSP module configuration
  247. */
  248. static void ospDumpParameters(void)
  249. {
  250. int i;
  251. LOG(L_INFO, "osp: module configuration: ");
  252. LOG(L_INFO, " number of service points '%d'", _osp_sp_number);
  253. for (i = 0; i < _osp_sp_number; i++) {
  254. LOG(L_INFO,
  255. " sp%d_uri '%s' sp%d_weight '%ld' ",
  256. osp_index[i], _osp_sp_uris[i], osp_index[i], _osp_sp_weights[i]);
  257. }
  258. LOG(L_INFO, " device_ip '%s' device_port '%s' ", _osp_device_ip, _osp_device_port);
  259. LOG(L_INFO, " private_key '%s' ", _osp_private_key);
  260. LOG(L_INFO, " local_certificate '%s' ", _osp_local_certificate);
  261. LOG(L_INFO, " ca_certificates '%s' ", _osp_ca_certificate);
  262. LOG(L_INFO, " enable_crypto_hardware_support '%d' ", _osp_crypto_hw);
  263. LOG(L_INFO, " token_format '%d' ", _osp_token_format);
  264. LOG(L_INFO, " ssl_lifetime '%d' ", _osp_ssl_lifetime);
  265. LOG(L_INFO, " persistence '%d' ", _osp_persistence);
  266. LOG(L_INFO, " retry_delay '%d' ", _osp_retry_delay);
  267. LOG(L_INFO, " retry_limit '%d' ", _osp_retry_limit);
  268. LOG(L_INFO, " timeout '%d' ", _osp_timeout);
  269. LOG(L_INFO, " validate_call_id '%d' ", _osp_validate_callid);
  270. LOG(L_INFO, " use_rpid_for_calling_number '%d' ", _osp_use_rpid);
  271. LOG(L_INFO, " redirection_uri_format '%d' ", _osp_redir_uri);
  272. LOG(L_INFO, " max_destinations '%d'\n", _osp_max_dests);
  273. }