usage.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 "../../usr_avp.h"
  38. #include "usage.h"
  39. #include "destination.h"
  40. #include "osptoolkit.h"
  41. #include "sipheader.h"
  42. #define OSP_ORIG_COOKIE "osp-o"
  43. #define OSP_TERM_COOKIE "osp-t"
  44. #define OSP_RELEASE_ORIG 0
  45. #define OSP_RELEASE_TERM 1
  46. /* The up case tags for the destinations may corrupt OSP cookies */
  47. #define OSP_COOKIE_TRANSID 't'
  48. #define OSP_COOKIE_TRANSIDUP 'T'
  49. #define OSP_COOKIE_SRCIP 's'
  50. #define OSP_COOKIE_SRCIPUP 'S'
  51. #define OSP_COOKIE_AUTHTIME 'a'
  52. #define OSP_COOKIE_AUTHTIMEUP 'A'
  53. #define OSP_COOKIE_DSTCOUNT 'c'
  54. #define OSP_COOKIE_DSTCOUNTUP 'C'
  55. /* SER uses AVP to add RR pararmters */
  56. const str OSP_ORIGCOOKIE_NAME = {"_osp_orig_cookie_", 17};
  57. const str OSP_TERMCOOKIE_NAME = {"_osp_term_cookie_", 17};
  58. extern char* _osp_device_ip;
  59. extern OSPTPROVHANDLE _osp_provider;
  60. extern str OSP_ORIGDEST_NAME;
  61. static void ospRecordTransaction(struct sip_msg* msg, unsigned long long transid, char* uac, char* from, char* to, time_t authtime, int isorig, unsigned destinationCount);
  62. static int ospBuildUsageFromDestination(OSPTTRANHANDLE transaction, osp_dest* dest, int lastcode);
  63. static int ospReportUsageFromDestination(OSPTTRANHANDLE transaction, osp_dest* dest);
  64. /* SER checks ftag by itself, without release parameter */
  65. static int ospReportUsageFromCookie(struct sip_msg* msg, char* cooky, OSPTCALLID* callid, OSPE_MSG_ROLETYPES type);
  66. /*
  67. * Create OSP cookie and insert it into Record-Route header
  68. * param msg SIP message
  69. * param tansid Transaction ID
  70. * param uac Source IP
  71. * param from
  72. * param to
  73. * param authtime Request authorization time
  74. * param isorig Originate / Terminate
  75. * param destinationCount Destination count
  76. */
  77. static void ospRecordTransaction(
  78. struct sip_msg* msg,
  79. unsigned long long transid,
  80. char* uac,
  81. char* from,
  82. char* to,
  83. time_t authtime,
  84. int isorig,
  85. unsigned destinationCount)
  86. {
  87. const str* name;
  88. str cookie;
  89. char buffer[OSP_STRBUF_SIZE];
  90. LOG(L_DBG, "osp: ospRecordTransaction\n");
  91. cookie.s = buffer;
  92. if (isorig == 1) {
  93. cookie.len = snprintf(
  94. buffer,
  95. sizeof(buffer),
  96. "%c%llu_%c%s_%c%d_%c%d",
  97. OSP_COOKIE_TRANSID,
  98. transid,
  99. OSP_COOKIE_SRCIP,
  100. uac,
  101. OSP_COOKIE_AUTHTIME,
  102. (unsigned int)authtime,
  103. OSP_COOKIE_DSTCOUNT,
  104. destinationCount);
  105. } else {
  106. cookie.len = snprintf(
  107. buffer,
  108. sizeof(buffer),
  109. "%c%llu_%c%s_%c%d",
  110. OSP_COOKIE_TRANSID,
  111. transid,
  112. OSP_COOKIE_SRCIP,
  113. uac,
  114. OSP_COOKIE_AUTHTIME,
  115. (unsigned int)authtime);
  116. }
  117. if (cookie.len < 0) {
  118. LOG(L_ERR, "osp: ERROR: failed to create OSP cookie\n");
  119. return;
  120. }
  121. /* SER uses AVP to add RR parameters */
  122. LOG(L_DBG, "osp: adding RR parameter '%s' for '%s'\n",
  123. buffer,
  124. (isorig == 1) ? "orig" : "term");
  125. name = (isorig == 1) ? &OSP_ORIGCOOKIE_NAME : &OSP_TERMCOOKIE_NAME;
  126. add_avp(AVP_NAME_STR | AVP_VAL_STR, (int_str)*name, (int_str)cookie);
  127. }
  128. /*
  129. * Create OSP originate cookie and insert it into Record-Route header
  130. * param msg SIP message
  131. * param tansid Transaction ID
  132. * param uac Source IP
  133. * param from
  134. * param to
  135. * param authtime Request authorization time
  136. * param destinationCount Destination count
  137. */
  138. void ospRecordOrigTransaction(
  139. struct sip_msg* msg,
  140. unsigned long long transid,
  141. char* uac,
  142. char* from,
  143. char* to,
  144. time_t authtime,
  145. unsigned destinationCount)
  146. {
  147. int isorig = 1;
  148. LOG(L_DBG, "osp: ospRecordOrigTransaction\n");
  149. ospRecordTransaction(msg, transid, uac, from, to, authtime, isorig, destinationCount);
  150. }
  151. /*
  152. * Create OSP terminate cookie and insert it into Record-Route header
  153. * param msg SIP message
  154. * param tansid Transaction ID
  155. * param uac Source IP
  156. * param from
  157. * param to
  158. * param authtime Request authorization time
  159. */
  160. void ospRecordTermTransaction(
  161. struct sip_msg* msg,
  162. unsigned long long transid,
  163. char* uac,
  164. char* from,
  165. char* to,
  166. time_t authtime)
  167. {
  168. int isorig = 0;
  169. unsigned destinationCount = 0; /* N/A */
  170. LOG(L_DBG, "osp: ospRecordTermTransaction\n");
  171. ospRecordTransaction(msg, transid, uac, from, to, authtime, isorig, destinationCount);
  172. }
  173. /*
  174. * Report OSP usage from OSP cookie
  175. * SER checks ftag by itself, without release parameter
  176. * param msg SIP message
  177. * param cookie OSP cookie
  178. * param callid Call ID
  179. * param type Usage type
  180. * return
  181. */
  182. static int ospReportUsageFromCookie(
  183. struct sip_msg* msg,
  184. char* cookie,
  185. OSPTCALLID* callid,
  186. OSPE_MSG_ROLETYPES type)
  187. {
  188. int release;
  189. char* tmp;
  190. char* token;
  191. char tag;
  192. char* value;
  193. unsigned long long transid = 0;
  194. time_t authtime = 0;
  195. unsigned destinationCount = 0;
  196. time_t endtime = time(NULL);
  197. char firstvia[OSP_STRBUF_SIZE];
  198. char from[OSP_STRBUF_SIZE];
  199. char to[OSP_STRBUF_SIZE];
  200. char nexthop[OSP_STRBUF_SIZE];
  201. char* calling;
  202. char* called;
  203. char* originator = NULL;
  204. char* terminator;
  205. char* source;
  206. char srcbuf[OSP_STRBUF_SIZE];
  207. char* destination;
  208. char dstbuf[OSP_STRBUF_SIZE];
  209. char* srcdev;
  210. char devbuf[OSP_STRBUF_SIZE];
  211. OSPTTRANHANDLE transaction = -1;
  212. int errorcode;
  213. LOG(L_DBG, "osp: ospReportUsageFromCookie\n");
  214. LOG(L_DBG, "osp: '%s' type '%d'\n", cookie, type);
  215. if (cookie != NULL) {
  216. for (token = strtok_r(cookie, "_", &tmp);
  217. token;
  218. token = strtok_r(NULL, "_", &tmp))
  219. {
  220. tag = *token;
  221. value= token + 1;
  222. switch (tag) {
  223. case OSP_COOKIE_TRANSID:
  224. case OSP_COOKIE_TRANSIDUP:
  225. transid = atoll(value);
  226. break;
  227. case OSP_COOKIE_AUTHTIME:
  228. case OSP_COOKIE_AUTHTIMEUP:
  229. authtime = atoi(value);
  230. break;
  231. case OSP_COOKIE_SRCIP:
  232. case OSP_COOKIE_SRCIPUP:
  233. originator = value;
  234. break;
  235. case OSP_COOKIE_DSTCOUNT:
  236. case OSP_COOKIE_DSTCOUNTUP:
  237. destinationCount = (unsigned)atoi(value);
  238. break;
  239. default:
  240. LOG(L_ERR, "osp: ERROR: unexpected tag '%c' / value '%s'\n", tag, value);
  241. break;
  242. }
  243. }
  244. }
  245. ospGetSourceAddress(msg, firstvia, sizeof(firstvia));
  246. ospGetFromUserpart(msg, from, sizeof(from));
  247. ospGetToUserpart(msg, to, sizeof(to));
  248. ospGetNextHop(msg, nexthop, sizeof(nexthop));
  249. LOG(L_DBG, "osp: first via '%s' from '%s' to '%s' next hop '%s'\n",
  250. firstvia,
  251. from,
  252. to,
  253. nexthop);
  254. /* SER checks ftag by itself */
  255. errorcode = ospGetDirection(msg);
  256. switch (errorcode) {
  257. case 0:
  258. release = OSP_RELEASE_ORIG;
  259. break;
  260. case 1:
  261. release = OSP_RELEASE_TERM;
  262. break;
  263. default:
  264. /* This approach has a problem of flipping called/calling number */
  265. if (strcmp(firstvia, originator) == 0) {
  266. release = OSP_RELEASE_ORIG;
  267. } else {
  268. release = OSP_RELEASE_TERM;
  269. }
  270. }
  271. if (release == OSP_RELEASE_ORIG) {
  272. LOG(L_DBG,
  273. "osp: orig '%s' released the call, call_id '%.*s' transaction_id '%llu'\n",
  274. firstvia,
  275. callid->ospmCallIdLen,
  276. callid->ospmCallIdVal,
  277. transid);
  278. if (originator == NULL) {
  279. originator = firstvia;
  280. }
  281. calling = from;
  282. called = to;
  283. terminator = nexthop;
  284. } else {
  285. release = OSP_RELEASE_TERM;
  286. LOG(L_DBG,
  287. "osp: term '%s' released the call, call_id '%.*s' transaction_id '%llu'\n",
  288. firstvia,
  289. callid->ospmCallIdLen,
  290. callid->ospmCallIdVal,
  291. transid);
  292. if (originator == NULL) {
  293. originator = nexthop;
  294. }
  295. calling = to;
  296. called = from;
  297. terminator = firstvia;
  298. }
  299. errorcode = OSPPTransactionNew(_osp_provider, &transaction);
  300. LOG(L_DBG, "osp: created transaction handle '%d' (%d)\n", transaction, errorcode);
  301. switch (type) {
  302. case OSPC_DESTINATION:
  303. ospConvertAddress(originator, srcbuf, sizeof(srcbuf));
  304. source = srcbuf;
  305. destination = _osp_device_ip;
  306. srcdev = "";
  307. break;
  308. case OSPC_SOURCE:
  309. case OSPC_OTHER:
  310. case OSPC_UNDEFINED_ROLE:
  311. default:
  312. source = _osp_device_ip;
  313. ospConvertAddress(terminator, dstbuf, sizeof(dstbuf));
  314. destination = dstbuf;
  315. ospConvertAddress(originator, devbuf, sizeof(devbuf));
  316. srcdev = devbuf;
  317. break;
  318. }
  319. errorcode = OSPPTransactionBuildUsageFromScratch(
  320. transaction,
  321. transid,
  322. type,
  323. source,
  324. destination,
  325. srcdev,
  326. "",
  327. calling,
  328. OSPC_E164,
  329. called,
  330. OSPC_E164,
  331. callid->ospmCallIdLen,
  332. callid->ospmCallIdVal,
  333. (enum OSPEFAILREASON)0,
  334. NULL,
  335. NULL);
  336. LOG(L_DBG, "osp: built usage handle '%d' (%d)\n", transaction, errorcode);
  337. if ((errorcode == OSPC_ERR_NO_ERROR) && (destinationCount > 0)) {
  338. errorcode = OSPPTransactionSetDestinationCount(
  339. transaction,
  340. destinationCount);
  341. }
  342. ospReportUsageWrapper(
  343. transaction,
  344. cookie == NULL ? 9016 : 10016,
  345. cookie == NULL ? 0 : endtime - authtime,
  346. authtime,
  347. endtime,
  348. 0,0,
  349. 0,0,
  350. release);
  351. return errorcode;
  352. }
  353. /*
  354. * Report OSP usage
  355. * SER uses AVP to add RR pararmters
  356. * param msg SIP message
  357. * param ignore1
  358. * param ignore2
  359. * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure
  360. */
  361. int ospReportUsage(
  362. struct sip_msg* msg,
  363. char* ignore1,
  364. char* ignore2)
  365. {
  366. int_str cookieval;
  367. struct usr_avp* cookieavp = NULL;
  368. static const int FROMFLAGS = AVP_TRACK_FROM | AVP_CLASS_URI | AVP_NAME_STR | AVP_VAL_STR;
  369. static const int TOFLAGS = AVP_TRACK_TO | AVP_CLASS_URI | AVP_NAME_STR | AVP_VAL_STR;
  370. char buffer[OSP_HEADERBUF_SIZE];
  371. OSPTCALLID* callid = NULL;
  372. int result = MODULE_RETURNCODE_FALSE;
  373. LOG(L_DBG, "osp: ospReportUsage\n");
  374. ospGetCallId(msg, &callid);
  375. if (callid != NULL) {
  376. if ((cookieavp = search_first_avp(FROMFLAGS, (int_str)OSP_ORIGCOOKIE_NAME, &cookieval, NULL)) != 0 ||
  377. (cookieavp = search_first_avp(TOFLAGS, (int_str)OSP_ORIGCOOKIE_NAME, &cookieval, NULL)) != 0)
  378. {
  379. ospCopyStrToBuffer(&cookieval.s, buffer, sizeof(buffer));
  380. LOG(L_DBG, "orig cookie '%s'\n", buffer);
  381. LOG(L_INFO,
  382. "osp: report orig duration for call_id '%.*s'\n",
  383. callid->ospmCallIdLen,
  384. callid->ospmCallIdVal);
  385. ospReportUsageFromCookie(msg, buffer, callid, OSPC_SOURCE);
  386. result = MODULE_RETURNCODE_TRUE;
  387. }
  388. if ((cookieavp = search_first_avp(FROMFLAGS, (int_str)OSP_TERMCOOKIE_NAME, &cookieval, NULL)) != 0 ||
  389. (cookieavp = search_first_avp(TOFLAGS, (int_str)OSP_TERMCOOKIE_NAME, &cookieval, NULL)) != 0)
  390. {
  391. ospCopyStrToBuffer(&cookieval.s, buffer, sizeof(buffer));
  392. LOG(L_DBG, "term cookie '%s'\n", buffer);
  393. LOG(L_INFO,
  394. "osp: report term duration for call_id '%.*s'\n",
  395. callid->ospmCallIdLen,
  396. callid->ospmCallIdVal);
  397. ospReportUsageFromCookie(msg, buffer, callid, OSPC_DESTINATION);
  398. result = MODULE_RETURNCODE_TRUE;
  399. }
  400. if (result == MODULE_RETURNCODE_FALSE) {
  401. LOG(L_DBG, "without orig or term OSP information\n");
  402. LOG(L_INFO, "report other duration for call_id '%.*s'\n",
  403. callid->ospmCallIdLen,
  404. callid->ospmCallIdVal);
  405. ospReportUsageFromCookie(msg, NULL, callid, OSPC_SOURCE);
  406. result = MODULE_RETURNCODE_TRUE;
  407. }
  408. OSPPCallIdDelete(&callid);
  409. }
  410. if (result == MODULE_RETURNCODE_FALSE) {
  411. LOG(L_DBG, "osp: failed to report usage\n");
  412. }
  413. return result;
  414. }
  415. /*
  416. * Build OSP usage from destination
  417. * param transaction OSP transaction handle
  418. * param dest Destination
  419. * param lastcode Destination status
  420. * return 0 success, others failure
  421. */
  422. static int ospBuildUsageFromDestination(
  423. OSPTTRANHANDLE transaction,
  424. osp_dest* dest,
  425. int lastcode)
  426. {
  427. int errorcode;
  428. char addr[OSP_STRBUF_SIZE];
  429. char* source;
  430. char* srcdev;
  431. LOG(L_DBG, "osp: ospBuildUsageFromDestination\n");
  432. if (dest->type == OSPC_SOURCE) {
  433. ospConvertAddress(dest->srcdev, addr, sizeof(addr));
  434. source = dest->source;
  435. srcdev = addr;
  436. } else {
  437. ospConvertAddress(dest->source, addr, sizeof(addr));
  438. source = addr;
  439. srcdev = dest->srcdev;
  440. }
  441. errorcode = OSPPTransactionBuildUsageFromScratch(
  442. transaction,
  443. dest->transid,
  444. dest->type,
  445. source,
  446. dest->host,
  447. srcdev,
  448. dest->destdev,
  449. dest->calling,
  450. OSPC_E164,
  451. dest->origcalled, /* Report original called number */
  452. OSPC_E164,
  453. dest->callidsize,
  454. dest->callid,
  455. (enum OSPEFAILREASON)lastcode,
  456. NULL,
  457. NULL);
  458. return errorcode;
  459. }
  460. /*
  461. * Report OSP usage from destination
  462. * param transaction OSP transaction handle
  463. * param dest Destination
  464. * return 0 success
  465. */
  466. static int ospReportUsageFromDestination(
  467. OSPTTRANHANDLE transaction,
  468. osp_dest* dest)
  469. {
  470. ospReportUsageWrapper(
  471. transaction, /* In - Transaction handle */
  472. dest->lastcode, /* In - Release Code */
  473. 0, /* In - Length of call */
  474. dest->authtime, /* In - Call start time */
  475. 0, /* In - Call end time */
  476. dest->time180, /* In - Call alert time */
  477. dest->time200, /* In - Call connect time */
  478. dest->time180 ? 1 : 0, /* In - Is PDD Info present */
  479. dest->time180 ? dest->time180 - dest->authtime : 0, /* In - Post Dial Delay */
  480. 0);
  481. return 0;
  482. }
  483. /*
  484. * Report originate call setup usage
  485. */
  486. void ospReportOrigSetupUsage(void)
  487. {
  488. osp_dest* dest = NULL;
  489. osp_dest* lastused = NULL;
  490. struct usr_avp* destavp = NULL;
  491. int_str destval;
  492. struct search_state state;
  493. OSPTTRANHANDLE transaction = -1;
  494. int lastcode = 0;
  495. int errorcode;
  496. LOG(L_DBG, "osp: ospReportOrigSetupUsage\n");
  497. errorcode = OSPPTransactionNew(_osp_provider, &transaction);
  498. for (destavp = search_first_avp(AVP_NAME_STR | AVP_VAL_STR, (int_str)OSP_ORIGDEST_NAME, &destval, &state);
  499. destavp != NULL;
  500. destavp = search_next_avp(&state, &destval))
  501. {
  502. /* OSP destination is wrapped in a string */
  503. dest = (osp_dest*)destval.s.s;
  504. if (dest->used == 1) {
  505. if (dest->reported == 1) {
  506. LOG(L_DBG, "osp: orig setup already reported\n");
  507. break;
  508. } else {
  509. dest->reported = 1;
  510. }
  511. LOG(L_DBG, "osp: iterating through used destination\n");
  512. ospDumpDestination(dest);
  513. lastused = dest;
  514. errorcode = ospBuildUsageFromDestination(transaction, dest, lastcode);
  515. lastcode = dest->lastcode;
  516. } else {
  517. LOG(L_DBG, "osp: destination has not been used, breaking out\n");
  518. break;
  519. }
  520. }
  521. if (lastused) {
  522. LOG(L_INFO,
  523. "osp: report orig setup for call_id '%.*s' transaction_id '%llu'\n",
  524. lastused->callidsize,
  525. lastused->callid,
  526. lastused->transid);
  527. errorcode = ospReportUsageFromDestination(transaction, lastused);
  528. } else {
  529. /* If a Toolkit transaction handle was created, but we did not find
  530. * any destinations to report, we need to release the handle. Otherwise,
  531. * the ospReportUsageFromDestination will release it.
  532. */
  533. OSPPTransactionDelete(transaction);
  534. }
  535. }
  536. /*
  537. * Report terminate call setup usage
  538. */
  539. void ospReportTermSetupUsage(void)
  540. {
  541. osp_dest* dest = NULL;
  542. OSPTTRANHANDLE transaction = -1;
  543. int errorcode;
  544. LOG(L_DBG, "osp: ospReportTermSetupUsage\n");
  545. if ((dest = ospGetTermDestination())) {
  546. if (dest->reported == 0) {
  547. dest->reported = 1;
  548. LOG(L_INFO,
  549. "osp: report term setup for call_id '%.*s' transaction_id '%llu'\n",
  550. dest->callidsize,
  551. dest->callid,
  552. dest->transid);
  553. errorcode = OSPPTransactionNew(_osp_provider, &transaction);
  554. errorcode = ospBuildUsageFromDestination(transaction, dest, 0);
  555. errorcode = ospReportUsageFromDestination(transaction, dest);
  556. } else {
  557. LOG(L_DBG, "osp: term setup already reported\n");
  558. }
  559. } else {
  560. LOG(L_ERR, "osp: ERROR: without term setup to report\n");
  561. }
  562. }