orig_transaction.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. #include <string.h>
  31. #include <osp/osp.h>
  32. #include "../../dset.h"
  33. #include "../../usr_avp.h"
  34. #include "../../mem/mem.h"
  35. #include "../../modules/siputils/siputils.h"
  36. #include "orig_transaction.h"
  37. #include "destination.h"
  38. #include "osptoolkit.h"
  39. #include "sipheader.h"
  40. #include "usage.h"
  41. extern char* _osp_device_ip;
  42. extern char* _osp_device_port;
  43. extern int _osp_max_dests;
  44. extern int _osp_redir_uri;
  45. extern int_str _osp_snid_avpname;
  46. extern unsigned short _osp_snid_avptype;
  47. extern OSPTPROVHANDLE _osp_provider;
  48. extern siputils_api_t osp_siputils;
  49. const int OSP_FIRST_ROUTE = 1;
  50. const int OSP_NEXT_ROUTE = 0;
  51. const int OSP_MAIN_ROUTE = 1;
  52. const int OSP_BRANCH_ROUTE = 0;
  53. const str OSP_CALLING_NAME = {"_osp_calling_translated_", 24};
  54. static int ospLoadRoutes(OSPTTRANHANDLE transaction, int destcount, char* source, char* sourcedev, char* origcalled, time_t authtime);
  55. static int ospPrepareDestination(struct sip_msg* msg, int isfirst, int type, int format);
  56. static int ospSetRpid(struct sip_msg* msg, osp_dest* dest);
  57. /*
  58. * Get routes from AuthRsp
  59. * param transaction Transaction handle
  60. * param destcount Expected destination count
  61. * param source Source IP
  62. * param sourcedev Source device IP
  63. * param origcalled Original called number
  64. * param authtime Request authorization time
  65. * return 0 success, -1 failure
  66. */
  67. static int ospLoadRoutes(
  68. OSPTTRANHANDLE transaction,
  69. int destcount,
  70. char* source,
  71. char* sourcedev,
  72. char* origcalled,
  73. time_t authtime)
  74. {
  75. int count;
  76. int errorcode;
  77. osp_dest* dest;
  78. osp_dest dests[OSP_DEF_DESTS];
  79. OSPE_DEST_PROT protocol;
  80. OSPE_DEST_OSP_ENABLED enabled;
  81. int result = 0;
  82. for (count = 0; count < destcount; count++) {
  83. /* This is necessary because we will save destinations in reverse order */
  84. dest = ospInitDestination(&dests[count]);
  85. if (dest == NULL) {
  86. result = -1;
  87. break;
  88. }
  89. dest->destinationCount = count + 1;
  90. strncpy(dest->origcalled, origcalled, sizeof(dest->origcalled) - 1);
  91. if (count == 0) {
  92. errorcode = OSPPTransactionGetFirstDestination(
  93. transaction,
  94. sizeof(dest->validafter),
  95. dest->validafter,
  96. dest->validuntil,
  97. &dest->timelimit,
  98. &dest->callidsize,
  99. (void*)dest->callid,
  100. sizeof(dest->called),
  101. dest->called,
  102. sizeof(dest->calling),
  103. dest->calling,
  104. sizeof(dest->host),
  105. dest->host,
  106. sizeof(dest->destdev),
  107. dest->destdev,
  108. &dest->tokensize,
  109. dest->token);
  110. } else {
  111. errorcode = OSPPTransactionGetNextDestination(
  112. transaction,
  113. 0,
  114. sizeof(dest->validafter),
  115. dest->validafter,
  116. dest->validuntil,
  117. &dest->timelimit,
  118. &dest->callidsize,
  119. (void*)dest->callid,
  120. sizeof(dest->called),
  121. dest->called,
  122. sizeof(dest->calling),
  123. dest->calling,
  124. sizeof(dest->host),
  125. dest->host,
  126. sizeof(dest->destdev),
  127. dest->destdev,
  128. &dest->tokensize,
  129. dest->token);
  130. }
  131. if (errorcode != OSPC_ERR_NO_ERROR) {
  132. LM_ERR("failed to load routes (%d) expected '%d' current '%d'\n",
  133. errorcode,
  134. destcount,
  135. count);
  136. result = -1;
  137. break;
  138. }
  139. errorcode = OSPPTransactionGetDestProtocol(transaction, &protocol);
  140. if (errorcode != OSPC_ERR_NO_ERROR) {
  141. /* This does not mean an ERROR. The OSP server may not support OSP 2.1.1 */
  142. LM_DBG("cannot get dest protocol (%d)\n", errorcode);
  143. protocol = OSPE_DEST_PROT_SIP;
  144. }
  145. switch (protocol) {
  146. case OSPE_DEST_PROT_H323_LRQ:
  147. case OSPE_DEST_PROT_H323_SETUP:
  148. case OSPE_DEST_PROT_IAX:
  149. dest->supported = 0;
  150. break;
  151. case OSPE_DEST_PROT_SIP:
  152. case OSPE_DEST_PROT_UNDEFINED:
  153. case OSPE_DEST_PROT_UNKNOWN:
  154. default:
  155. dest->supported = 1;
  156. break;
  157. }
  158. errorcode = OSPPTransactionIsDestOSPEnabled(transaction, &enabled);
  159. if (errorcode != OSPC_ERR_NO_ERROR) {
  160. /* This does not mean an ERROR. The OSP server may not support OSP 2.1.1 */
  161. LM_DBG("cannot get dest OSP version (%d)\n", errorcode);
  162. } else if (enabled == OSPE_OSP_FALSE) {
  163. /* Destination device does not support OSP. Do not send token to it */
  164. dest->token[0] = '\0';
  165. dest->tokensize = 0;
  166. }
  167. errorcode = OSPPTransactionGetDestNetworkId(transaction, dest->networkid);
  168. if (errorcode != OSPC_ERR_NO_ERROR) {
  169. /* This does not mean an ERROR. The OSP server may not support OSP 2.1.1 */
  170. LM_DBG("cannot get dest network ID (%d)\n", errorcode);
  171. dest->networkid[0] = '\0';
  172. }
  173. strncpy(dest->source, source, sizeof(dest->source) - 1);
  174. strncpy(dest->srcdev, sourcedev, sizeof(dest->srcdev) - 1);
  175. dest->type = OSPC_SOURCE;
  176. dest->transid = ospGetTransactionId(transaction);
  177. dest->authtime = authtime;
  178. LM_INFO("get destination '%d': "
  179. "valid after '%s' "
  180. "valid until '%s' "
  181. "time limit '%d' seconds "
  182. "call id '%.*s' "
  183. "calling '%s' "
  184. "called '%s' "
  185. "host '%s' "
  186. "supported '%d' "
  187. "network id '%s' "
  188. "token size '%d'\n",
  189. count,
  190. dest->validafter,
  191. dest->validuntil,
  192. dest->timelimit,
  193. dest->callidsize,
  194. dest->callid,
  195. dest->calling,
  196. dest->called,
  197. dest->host,
  198. dest->supported,
  199. dest->networkid,
  200. dest->tokensize);
  201. }
  202. /*
  203. * Save destination in reverse order,
  204. * when we start searching avps the destinations
  205. * will be in order
  206. */
  207. if (result == 0) {
  208. for(count = destcount -1; count >= 0; count--) {
  209. if (ospSaveOrigDestination(&dests[count]) == -1) {
  210. LM_ERR("failed to save originate destination\n");
  211. /* Report terminate CDR */
  212. ospRecordEvent(0, 500);
  213. result = -1;
  214. break;
  215. }
  216. }
  217. }
  218. return result;
  219. }
  220. /*
  221. * Request OSP authorization and routeing
  222. * param msg SIP message
  223. * param ignore1
  224. * param ignore2
  225. * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure, MODULE_RETURNCODE_ERROR error
  226. */
  227. int ospRequestRouting(
  228. struct sip_msg* msg,
  229. char* ignore1,
  230. char* ignore2)
  231. {
  232. int errorcode;
  233. time_t authtime;
  234. char called[OSP_E164BUF_SIZE];
  235. char calling[OSP_E164BUF_SIZE];
  236. char sourcedev[OSP_STRBUF_SIZE];
  237. char deviceinfo[OSP_STRBUF_SIZE];
  238. struct usr_avp* snidavp = NULL;
  239. int_str snidval;
  240. char snid[OSP_STRBUF_SIZE];
  241. unsigned int callidnumber = 1;
  242. OSPTCALLID* callids[callidnumber];
  243. unsigned int logsize = 0;
  244. char* detaillog = NULL;
  245. const char** preferred = NULL;
  246. unsigned int destcount;
  247. OSPTTRANHANDLE transaction = -1;
  248. int result = MODULE_RETURNCODE_FALSE;
  249. authtime = time(NULL);
  250. destcount = _osp_max_dests;
  251. if ((errorcode = OSPPTransactionNew(_osp_provider, &transaction)) != OSPC_ERR_NO_ERROR) {
  252. LM_ERR("failed to create new OSP transaction (%d)\n", errorcode);
  253. } else if ((ospGetRpidUserpart(msg, calling, sizeof(calling)) != 0) &&
  254. (ospGetFromUserpart(msg, calling, sizeof(calling)) != 0))
  255. {
  256. LM_ERR("failed to extract calling number\n");
  257. } else if ((ospGetUriUserpart(msg, called, sizeof(called)) != 0) &&
  258. (ospGetToUserpart(msg, called, sizeof(called)) != 0))
  259. {
  260. LM_ERR("failed to extract called number\n");
  261. } else if (ospGetCallId(msg, &(callids[0])) != 0) {
  262. LM_ERR("failed to extract call id\n");
  263. } else if (ospGetSourceAddress(msg, sourcedev, sizeof(sourcedev)) != 0) {
  264. LM_ERR("failed to extract source deivce address\n");
  265. } else {
  266. ospConvertAddress(sourcedev, deviceinfo, sizeof(deviceinfo));
  267. if ((_osp_snid_avpname.n != 0) &&
  268. ((snidavp = search_first_avp(_osp_snid_avptype, _osp_snid_avpname, &snidval, 0)) != NULL) &&
  269. (snidavp->flags & AVP_VAL_STR) && (snidval.s.s && snidval.s.len))
  270. {
  271. snprintf(snid, sizeof(snid), "%.*s", snidval.s.len, snidval.s.s);
  272. OSPPTransactionSetNetworkIds(transaction, snid, "");
  273. } else {
  274. snid[0] = '\0';
  275. }
  276. LM_INFO("request auth and routing for: "
  277. "source_ip '%s' "
  278. "source_port '%s' "
  279. "source_dev '%s' "
  280. "source_networkid '%s' "
  281. "calling '%s' "
  282. "called '%s' "
  283. "call_id '%.*s' "
  284. "dest_count '%d'\n",
  285. _osp_device_ip,
  286. _osp_device_port,
  287. deviceinfo, /* in "[x.x.x.x]" or host.domain format */
  288. snid,
  289. calling,
  290. called,
  291. callids[0]->ospmCallIdLen,
  292. callids[0]->ospmCallIdVal,
  293. destcount
  294. );
  295. /* try to request authorization */
  296. errorcode = OSPPTransactionRequestAuthorisation(
  297. transaction, /* transaction handle */
  298. _osp_device_ip, /* from the configuration file */
  299. deviceinfo, /* source device of call, protocol specific, in OSP format */
  300. calling, /* calling number in nodotted e164 notation */
  301. OSPC_E164, /* calling number format */
  302. called, /* called number */
  303. OSPC_E164, /* called number format */
  304. "", /* optional username string, used if no number */
  305. callidnumber, /* number of call ids, here always 1 */
  306. callids, /* sized-1 array of call ids */
  307. preferred, /* preferred destinations, here always NULL */
  308. &destcount, /* max destinations, after call dest_count */
  309. &logsize, /* size allocated for detaillog (next param) 0=no log */
  310. detaillog); /* memory location for detaillog to be stored */
  311. if ((errorcode == OSPC_ERR_NO_ERROR) &&
  312. (ospLoadRoutes(transaction, destcount, _osp_device_ip, sourcedev, called, authtime) == 0))
  313. {
  314. LM_INFO("there are '%d' OSP routes, call_id '%.*s'\n",
  315. destcount,
  316. callids[0]->ospmCallIdLen,
  317. callids[0]->ospmCallIdVal);
  318. result = MODULE_RETURNCODE_TRUE;
  319. } else {
  320. LM_ERR("failed to request auth and routing (%d), call_id '%.*s'\n",
  321. errorcode,
  322. callids[0]->ospmCallIdLen,
  323. callids[0]->ospmCallIdVal);
  324. switch (errorcode) {
  325. case OSPC_ERR_TRAN_ROUTE_BLOCKED:
  326. result = -403;
  327. break;
  328. case OSPC_ERR_TRAN_ROUTE_NOT_FOUND:
  329. result = -404;
  330. break;
  331. case OSPC_ERR_NO_ERROR:
  332. /* AuthRsp ok but ospLoadRoutes fails */
  333. result = MODULE_RETURNCODE_ERROR;
  334. break;
  335. default:
  336. result = MODULE_RETURNCODE_FALSE;
  337. break;
  338. }
  339. }
  340. }
  341. if (callids[0] != NULL) {
  342. OSPPCallIdDelete(&(callids[0]));
  343. }
  344. if (transaction != -1) {
  345. OSPPTransactionDelete(transaction);
  346. }
  347. return result;
  348. }
  349. /*
  350. * Check if there is a route
  351. * param msg SIP message
  352. * param ignore1
  353. * param ignore2
  354. * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure
  355. */
  356. int ospCheckRoute(
  357. struct sip_msg* msg,
  358. char* ignore1,
  359. char* ignore2)
  360. {
  361. if (ospCheckOrigDestination() == 0) {
  362. return MODULE_RETURNCODE_TRUE;
  363. } else {
  364. return MODULE_RETURNCODE_FALSE;
  365. }
  366. }
  367. /*
  368. * Create RPID AVP
  369. * param msg SIP message
  370. * param dest Destination structure
  371. * return 0 success, 1 calling number same, -1 failure
  372. */
  373. static int ospSetRpid(
  374. struct sip_msg* msg,
  375. osp_dest* dest)
  376. {
  377. str rpid;
  378. char calling[OSP_STRBUF_SIZE];
  379. char source[OSP_STRBUF_SIZE];
  380. char buffer[OSP_STRBUF_SIZE];
  381. int result = -1;
  382. if ((ospGetRpidUserpart(msg, calling, sizeof(calling)) != 0) &&
  383. (ospGetFromUserpart(msg, calling, sizeof(calling)) !=0))
  384. {
  385. LM_ERR("failed to extract calling number\n");
  386. return result;
  387. }
  388. if (strcmp(calling, dest->calling) == 0) {
  389. /* Do nothing for this case */
  390. result = 1;
  391. } else if ((osp_siputils.rpid_avp.s.s == NULL)
  392. || (osp_siputils.rpid_avp.s.len == 0)) {
  393. LM_WARN("rpid_avp is not foune, cannot set rpid avp\n");
  394. result = -1;
  395. } else {
  396. if (dest->source[0] == '[') {
  397. /* Strip "[]" */
  398. memset(source, 0, sizeof(source));
  399. strncpy(source, &dest->source[1], sizeof(source) - 1);
  400. source[strlen(source) - 1] = '\0';
  401. }
  402. snprintf(
  403. buffer,
  404. sizeof(buffer),
  405. "\"%s\" <sip:%s@%s>",
  406. dest->calling,
  407. dest->calling,
  408. source);
  409. rpid.s = buffer;
  410. rpid.len = strlen(buffer);
  411. add_avp(osp_siputils.rpid_avp_type | AVP_VAL_STR,
  412. (int_str)osp_siputils.rpid_avp, (int_str)rpid);
  413. result = 0;
  414. }
  415. return result;
  416. }
  417. /*
  418. * Check if the calling number is translated.
  419. * This function checks the avp set by ospPrepareDestination.
  420. * param msg SIP message
  421. * param ignore1
  422. * param ignore2
  423. * return MODULE_RETURNCODE_TRUE calling number translated MODULE_RETURNCODE_FALSE without transaltion
  424. */
  425. int ospCheckTranslation(
  426. struct sip_msg* msg,
  427. char* ignore1,
  428. char* ignore2)
  429. {
  430. int_str callingval;
  431. int result = MODULE_RETURNCODE_FALSE;
  432. if (search_first_avp(AVP_NAME_STR, (int_str)OSP_CALLING_NAME, &callingval, 0) != NULL) {
  433. if (callingval.n == 0) {
  434. LM_DBG("the calling number does not been translated\n");
  435. } else {
  436. LM_DBG("the calling number is translated\n");
  437. result = MODULE_RETURNCODE_TRUE;
  438. }
  439. } else {
  440. LM_ERR("there is not calling translation avp\n");
  441. }
  442. return result;
  443. }
  444. /*
  445. * Build SIP message for destination
  446. * param msg SIP message
  447. * param isfirst Is first destination
  448. * param type Main or branch route block
  449. * param format URI format
  450. * return MODULE_RETURNCODE_TRUE success MODULE_RETURNCODE_FALSE failure
  451. */
  452. static int ospPrepareDestination(
  453. struct sip_msg* msg,
  454. int isfirst,
  455. int type,
  456. int format)
  457. {
  458. int_str val;
  459. int res;
  460. str newuri = {NULL, 0};
  461. int result = MODULE_RETURNCODE_FALSE;
  462. osp_dest* dest = ospGetNextOrigDestination();
  463. if (dest != NULL) {
  464. ospRebuildDestionationUri(&newuri, dest->called, dest->host, "", format);
  465. LM_INFO("prepare route to URI '%.*s' for call_id '%.*s' transaction_id '%llu'\n",
  466. newuri.len,
  467. newuri.s,
  468. dest->callidsize,
  469. dest->callid,
  470. dest->transid);
  471. if (type == OSP_MAIN_ROUTE) {
  472. if (isfirst == OSP_FIRST_ROUTE) {
  473. rewrite_uri(msg, &newuri);
  474. } else {
  475. km_append_branch(msg, &newuri, NULL, NULL, Q_UNSPECIFIED, 0, NULL);
  476. }
  477. /* Do not add route specific OSP information */
  478. result = MODULE_RETURNCODE_TRUE;
  479. } else if (type == OSP_BRANCH_ROUTE) {
  480. /* For branch route, add route specific OSP information */
  481. /* Update the Request-Line */
  482. rewrite_uri(msg, &newuri);
  483. /* Add OSP token header */
  484. ospAddOspHeader(msg, dest->token, dest->tokensize);
  485. /* Add branch-specific OSP Cookie */
  486. ospRecordOrigTransaction(msg, dest->transid, dest->srcdev, dest->calling, dest->called, dest->authtime, dest->destinationCount);
  487. /* Add rpid avp for calling number translation */
  488. res = ospSetRpid(msg, dest);
  489. switch (res) {
  490. case 0:
  491. /* Calling number is translated */
  492. val.n = 1;
  493. add_avp(AVP_NAME_STR, (int_str)OSP_CALLING_NAME, val);
  494. break;
  495. default:
  496. LM_DBG("cannot set rpid avp\n");
  497. /* Just like without calling translation */
  498. case 1:
  499. /* Calling number does not been translated */
  500. val.n = 0;
  501. add_avp(AVP_NAME_STR, (int_str)OSP_CALLING_NAME, val);
  502. break;
  503. }
  504. result = MODULE_RETURNCODE_TRUE;
  505. } else {
  506. LM_ERR("unsupported route block type\n");
  507. }
  508. } else {
  509. LM_DBG("there is no more routes\n");
  510. ospReportOrigSetupUsage();
  511. }
  512. if (newuri.len > 0) {
  513. pkg_free(newuri.s);
  514. }
  515. return result;
  516. }
  517. /*
  518. * Prepare OSP route
  519. * This function only works in branch route block.
  520. * This function is only for Kamailio.
  521. * param msg SIP message
  522. * param ignore1
  523. * param ignore2
  524. * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure
  525. */
  526. int ospPrepareRoute(
  527. struct sip_msg* msg,
  528. char* ignore1,
  529. char* ignore2)
  530. {
  531. int result = MODULE_RETURNCODE_TRUE;
  532. /* The first parameter will be ignored */
  533. result = ospPrepareDestination(msg, OSP_FIRST_ROUTE, OSP_BRANCH_ROUTE, 0);
  534. return result;
  535. }
  536. /*
  537. * Prepare all OSP routes
  538. * This function does not work in branch route block.
  539. * param msg SIP message
  540. * param ignore1
  541. * param ignore2
  542. * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure
  543. */
  544. int ospPrepareAllRoutes(
  545. struct sip_msg* msg,
  546. char* ignore1,
  547. char* ignore2)
  548. {
  549. int result = MODULE_RETURNCODE_TRUE;
  550. for(result = ospPrepareDestination(msg, OSP_FIRST_ROUTE, OSP_MAIN_ROUTE, _osp_redir_uri);
  551. result == MODULE_RETURNCODE_TRUE;
  552. result = ospPrepareDestination(msg, OSP_NEXT_ROUTE, OSP_MAIN_ROUTE, _osp_redir_uri))
  553. {
  554. }
  555. return MODULE_RETURNCODE_TRUE;
  556. }