orig_transaction.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 <string.h>
  36. #include <osp/osp.h>
  37. #include "../../dset.h"
  38. #include "../../usr_avp.h"
  39. #include "../../mem/mem.h"
  40. #include "orig_transaction.h"
  41. #include "destination.h"
  42. #include "osptoolkit.h"
  43. #include "sipheader.h"
  44. #include "usage.h"
  45. extern char* _osp_device_ip;
  46. extern char* _osp_device_port;
  47. extern int _osp_max_dests;
  48. extern OSPTPROVHANDLE _osp_provider;
  49. extern int _osp_redir_uri;
  50. const int OSP_FIRST_ROUTE = 1;
  51. const int OSP_NEXT_ROUTE = 0;
  52. const int OSP_MAIN_ROUTE = 1;
  53. const int OSP_BRANCH_ROUTE = 0;
  54. const str OSP_CALLING_NAME = {"_osp_calling_translated_", 24};
  55. static int ospLoadRoutes(OSPTTRANHANDLE transaction, int destcount, char* source, char* sourcedev, char* origcalled, time_t authtime);
  56. static int ospPrepareDestination(struct sip_msg* msg, int isfirst, int type, int format);
  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. LOG(L_DBG, "osp: ospLoadRoutes\n");
  83. for (count = 0; count < destcount; count++) {
  84. /* This is necessary becuase we will save destinations in reverse order */
  85. dest = ospInitDestination(&dests[count]);
  86. if (dest == NULL) {
  87. result = -1;
  88. break;
  89. }
  90. dest->destinationCount = count + 1;
  91. strncpy(dest->origcalled, origcalled, sizeof(dest->origcalled) - 1);
  92. if (count == 0) {
  93. errorcode = OSPPTransactionGetFirstDestination(
  94. transaction,
  95. sizeof(dest->validafter),
  96. dest->validafter,
  97. dest->validuntil,
  98. &dest->timelimit,
  99. &dest->callidsize,
  100. (void*)dest->callid,
  101. sizeof(dest->called),
  102. dest->called,
  103. sizeof(dest->calling),
  104. dest->calling,
  105. sizeof(dest->host),
  106. dest->host,
  107. sizeof(dest->destdev),
  108. dest->destdev,
  109. &dest->tokensize,
  110. dest->token);
  111. } else {
  112. errorcode = OSPPTransactionGetNextDestination(
  113. transaction,
  114. 0,
  115. sizeof(dest->validafter),
  116. dest->validafter,
  117. dest->validuntil,
  118. &dest->timelimit,
  119. &dest->callidsize,
  120. (void*)dest->callid,
  121. sizeof(dest->called),
  122. dest->called,
  123. sizeof(dest->calling),
  124. dest->calling,
  125. sizeof(dest->host),
  126. dest->host,
  127. sizeof(dest->destdev),
  128. dest->destdev,
  129. &dest->tokensize,
  130. dest->token);
  131. }
  132. if (errorcode != OSPC_ERR_NO_ERROR) {
  133. LOG(L_ERR,
  134. "osp: ERROR: failed to load routes (%d) expected '%d' current '%d'\n",
  135. errorcode,
  136. destcount,
  137. count);
  138. result = -1;
  139. break;
  140. }
  141. errorcode = OSPPTransactionGetDestProtocol(transaction, &protocol);
  142. if (errorcode != OSPC_ERR_NO_ERROR) {
  143. /* This does not mean an ERROR. The OSP server may not support OSP 2.1.1 */
  144. LOG(L_DBG, "osp: cannot get dest protocol (%d)\n", errorcode);
  145. protocol = OSPE_DEST_PROT_SIP;
  146. }
  147. switch (protocol) {
  148. case OSPE_DEST_PROT_H323_LRQ:
  149. case OSPE_DEST_PROT_H323_SETUP:
  150. case OSPE_DEST_PROT_IAX:
  151. dest->supported = 0;
  152. break;
  153. case OSPE_DEST_PROT_SIP:
  154. case OSPE_DEST_PROT_UNDEFINED:
  155. case OSPE_DEST_PROT_UNKNOWN:
  156. default:
  157. dest->supported = 1;
  158. break;
  159. }
  160. errorcode = OSPPTransactionIsDestOSPEnabled(transaction, &enabled);
  161. if (errorcode != OSPC_ERR_NO_ERROR) {
  162. /* This does not mean an ERROR. The OSP server may not support OSP 2.1.1 */
  163. LOG(L_DBG, "osp: cannot get dest OSP version (%d)\n", errorcode);
  164. } else if (enabled == OSPE_OSP_FALSE) {
  165. /* Destination device does not support OSP. Do not send token to it */
  166. dest->token[0] = '\0';
  167. dest->tokensize = 0;
  168. }
  169. errorcode = OSPPTransactionGetDestNetworkId(transaction, dest->networkid);
  170. if (errorcode != OSPC_ERR_NO_ERROR) {
  171. /* This does not mean an ERROR. The OSP server may not support OSP 2.1.1 */
  172. LOG(L_DBG, "osp: cannot get dest network ID (%d)\n", errorcode);
  173. dest->networkid[0] = '\0';
  174. }
  175. strncpy(dest->source, source, sizeof(dest->source) - 1);
  176. strncpy(dest->srcdev, sourcedev, sizeof(dest->srcdev) - 1);
  177. dest->type = OSPC_SOURCE;
  178. dest->transid = ospGetTransactionId(transaction);
  179. dest->authtime = authtime;
  180. LOG(L_INFO,
  181. "osp: get destination '%d': "
  182. "valid after '%s' "
  183. "valid until '%s' "
  184. "time limit '%i' seconds "
  185. "call id '%.*s' "
  186. "calling number '%s' "
  187. "called number '%s' "
  188. "host '%s' "
  189. "supported '%d' "
  190. "network id '%s' "
  191. "token size '%i'\n",
  192. count,
  193. dest->validafter,
  194. dest->validuntil,
  195. dest->timelimit,
  196. dest->callidsize,
  197. dest->callid,
  198. dest->calling,
  199. dest->called,
  200. dest->host,
  201. dest->supported,
  202. dest->networkid,
  203. dest->tokensize);
  204. }
  205. /*
  206. * Save destination in reverse order,
  207. * when we start searching avps the destinations
  208. * will be in order
  209. */
  210. if (result == 0) {
  211. for(count = destcount -1; count >= 0; count--) {
  212. ospSaveOrigDestination(&dests[count]);
  213. }
  214. }
  215. return result;
  216. }
  217. /*
  218. * Request OSP authorization and routeing
  219. * param msg SIP message
  220. * param ignore1
  221. * param ignore2
  222. * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure
  223. */
  224. int ospRequestRouting(
  225. struct sip_msg* msg,
  226. char* ignore1,
  227. char* ignore2)
  228. {
  229. int errorcode;
  230. time_t authtime;
  231. char source[OSP_E164BUF_SIZE];
  232. char sourcedev[OSP_STRBUF_SIZE];
  233. char src[OSP_STRBUF_SIZE];
  234. char destination[OSP_E164BUF_SIZE];
  235. unsigned int callidnumber = 1;
  236. OSPTCALLID* callids[callidnumber];
  237. unsigned int logsize = 0;
  238. char* detaillog = NULL;
  239. const char** preferred = NULL;
  240. unsigned int destcount;
  241. OSPTTRANHANDLE transaction = -1;
  242. int result = MODULE_RETURNCODE_FALSE;
  243. LOG(L_DBG, "osp: ospRequestRouting\n");
  244. authtime = time(NULL);
  245. destcount = _osp_max_dests;
  246. if ((errorcode = OSPPTransactionNew(_osp_provider, &transaction)) != OSPC_ERR_NO_ERROR) {
  247. LOG(L_ERR, "osp: ERROR: failed to create new OSP transaction (%d)\n", errorcode);
  248. } else if ((ospGetRpidUserpart(msg, source, sizeof(source)) != 0) &&
  249. (ospGetFromUserpart(msg, source, sizeof(source)) != 0))
  250. {
  251. LOG(L_ERR, "osp: ERROR: failed to extract calling number\n");
  252. } else if ((ospGetUriUserpart(msg, destination, sizeof(destination)) != 0) &&
  253. (ospGetToUserpart(msg, destination, sizeof(destination)) != 0))
  254. {
  255. LOG(L_ERR, "osp: ERROR: failed to extract called number\n");
  256. } else if (ospGetCallId(msg, &(callids[0])) != 0) {
  257. LOG(L_ERR, "osp: ERROR: failed to extract call id\n");
  258. } else if (ospGetSourceAddress(msg, sourcedev, sizeof(sourcedev)) != 0) {
  259. LOG(L_ERR, "osp: ERROR: failed to extract source address\n");
  260. } else {
  261. ospConvertAddress(sourcedev, src, sizeof(src));
  262. LOG(L_INFO,
  263. "osp: request auth and routing for: "
  264. "source '%s' "
  265. "source_port '%s' "
  266. "source_dev '%s' "
  267. "e164_source '%s' "
  268. "e164_dest '%s' "
  269. "call_id '%.*s' "
  270. "dest_count '%i'\n",
  271. _osp_device_ip,
  272. _osp_device_port,
  273. src, /* sourcedev in "[x.x.x.x]" or host.domain format */
  274. source,
  275. destination,
  276. callids[0]->ospmCallIdLen,
  277. callids[0]->ospmCallIdVal,
  278. destcount
  279. );
  280. /* try to request authorization */
  281. errorcode = OSPPTransactionRequestAuthorisation(
  282. transaction, /* transaction handle */
  283. _osp_device_ip, /* from the configuration file */
  284. src, /* source of call, protocol specific, in OSP format */
  285. source, /* calling number in nodotted e164 notation */
  286. OSPC_E164, /* calling number format */
  287. destination, /* called number */
  288. OSPC_E164, /* called number format */
  289. "", /* optional username string, used if no number */
  290. callidnumber, /* number of call ids, here always 1 */
  291. callids, /* sized-1 array of call ids */
  292. preferred, /* preferred destinations, here always NULL */
  293. &destcount, /* max destinations, after call dest_count */
  294. &logsize, /* size allocated for detaillog (next param) 0=no log */
  295. detaillog); /* memory location for detaillog to be stored */
  296. if ((errorcode == OSPC_ERR_NO_ERROR) &&
  297. (ospLoadRoutes(transaction, destcount, _osp_device_ip, sourcedev, destination, authtime) == 0))
  298. {
  299. LOG(L_INFO,
  300. "osp: there are '%d' OSP routes, call_id '%.*s'\n",
  301. destcount,
  302. callids[0]->ospmCallIdLen,
  303. callids[0]->ospmCallIdVal);
  304. result = MODULE_RETURNCODE_TRUE;
  305. } else {
  306. LOG(L_ERR,
  307. "osp: ERROR: failed to request auth and routing (%i), call_id '%.*s\n",
  308. errorcode,
  309. callids[0]->ospmCallIdLen,
  310. callids[0]->ospmCallIdVal);
  311. switch (errorcode) {
  312. case OSPC_ERR_TRAN_ROUTE_BLOCKED:
  313. result = -403;
  314. break;
  315. case OSPC_ERR_TRAN_ROUTE_NOT_FOUND:
  316. result = -404;
  317. break;
  318. case OSPC_ERR_NO_ERROR:
  319. /* AuthRsp ok but ospLoadRoutes fails */
  320. result = -500;
  321. break;
  322. default:
  323. result = MODULE_RETURNCODE_FALSE;
  324. break;
  325. }
  326. }
  327. }
  328. if (callids[0] != NULL) {
  329. OSPPCallIdDelete(&(callids[0]));
  330. }
  331. if (transaction != -1) {
  332. OSPPTransactionDelete(transaction);
  333. }
  334. return result;
  335. }
  336. /*
  337. * Check if there is a route
  338. * param msg SIP message
  339. * param ignore1
  340. * param ignore2
  341. * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure
  342. */
  343. int ospCheckRoute(
  344. struct sip_msg* msg,
  345. char* ignore1,
  346. char* ignore2)
  347. {
  348. LOG(L_DBG, "osp: ospCheckRoute\n");
  349. if (ospCheckOrigDestination() == 0) {
  350. return MODULE_RETURNCODE_TRUE;
  351. } else {
  352. return MODULE_RETURNCODE_FALSE;
  353. }
  354. }
  355. /*
  356. * Append route specific OSP headers
  357. * This function only works in branch route block.
  358. * This function is only for SER. SER does not support rewrite_uri in BRANCH_ROUTE.
  359. * param msg SIP message
  360. * param ignore1
  361. * param ignore2
  362. * return MODULE_RETURNCODE_TRUE success MODULE_RETURNCODE_FALSE failed
  363. */
  364. int ospAppendHeaders(
  365. struct sip_msg* msg,
  366. char* ignore1,
  367. char* ignore2)
  368. {
  369. osp_dest* dest;
  370. int result = MODULE_RETURNCODE_FALSE;
  371. LOG(L_DBG, "osp: ospAppendHeaders\n");
  372. dest = ospGetLastOrigDestination();
  373. if (dest != NULL) {
  374. ospAddOspHeader(msg, dest->token, dest->tokensize);
  375. result = MODULE_RETURNCODE_TRUE;
  376. } else {
  377. LOG(L_ERR, "osp: ERROR: failed to get last used destination\n");
  378. }
  379. return result;
  380. }
  381. /*
  382. * Build SIP message for destination
  383. * param msg SIP message
  384. * param isfirst Is first destination
  385. * param type Main or branch route block
  386. * param format URI format
  387. * return MODULE_RETURNCODE_TRUE success MODULE_RETURNCODE_FALSE failure
  388. */
  389. static int ospPrepareDestination(
  390. struct sip_msg* msg,
  391. int isfirst,
  392. int type,
  393. int format)
  394. {
  395. str newuri = {NULL, 0};
  396. int result = MODULE_RETURNCODE_FALSE;
  397. LOG(L_DBG, "osp: ospPrepareDestination\n");
  398. osp_dest *dest = ospGetNextOrigDestination();
  399. if (dest != NULL) {
  400. ospRebuildDestionationUri(&newuri, dest->called, dest->host, "", format);
  401. LOG(L_INFO,
  402. "osp: prepare route to URI '%.*s' for call_id '%.*s' transaction_id '%llu'\n",
  403. newuri.len,
  404. newuri.s,
  405. dest->callidsize,
  406. dest->callid,
  407. dest->transid);
  408. if (type == OSP_MAIN_ROUTE) {
  409. if (isfirst == OSP_FIRST_ROUTE) {
  410. rewrite_uri(msg, &newuri);
  411. } else {
  412. append_branch(msg, &newuri, NULL, NULL, Q_UNSPECIFIED,
  413. 0, NULL);
  414. }
  415. result = MODULE_RETURNCODE_TRUE;
  416. } else {
  417. LOG(L_ERR, "osp: ERROR: unsupported route block type\n");
  418. }
  419. } else {
  420. LOG(L_DBG, "osp: there is no more routes\n");
  421. ospReportOrigSetupUsage();
  422. }
  423. if (newuri.len > 0) {
  424. pkg_free(newuri.s);
  425. }
  426. return result;
  427. }
  428. /*
  429. * Prepare OSP first route
  430. * This function prepare the first OSP route
  431. * This function is only for SER. SER does not support rewrite_uri in BRANCH_ROUTE.
  432. * param msg SIP message
  433. * param ignore1
  434. * param ignore2
  435. * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure
  436. */
  437. int ospPrepareFirstRoute(
  438. struct sip_msg* msg,
  439. char* ignore1,
  440. char* ignore2)
  441. {
  442. int result = MODULE_RETURNCODE_TRUE;
  443. LOG(L_DBG, "osp: ospPrepareFirstRoute\n");
  444. result = ospPrepareDestination(msg, OSP_FIRST_ROUTE, OSP_MAIN_ROUTE, 0);
  445. return result;
  446. }
  447. /*
  448. * Prepare OSP next route
  449. * This function prepare the next OSP route
  450. * This function is only for SER. SER does not support rewrite_uri in BRANCH_ROUTE.
  451. * param msg SIP message
  452. * param ignore1
  453. * param ignore2
  454. * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure
  455. */
  456. int ospPrepareNextRoute(
  457. struct sip_msg* msg,
  458. char* ignore1,
  459. char* ignore2)
  460. {
  461. int result = MODULE_RETURNCODE_TRUE;
  462. LOG(L_DBG, "osp: ospPrepareNextRoute\n");
  463. result = ospPrepareDestination(msg, OSP_NEXT_ROUTE, OSP_MAIN_ROUTE, 0);
  464. return result;
  465. }
  466. /*
  467. * Prepare all OSP routes
  468. * This function does not work in branch route block.
  469. * param msg SIP message
  470. * param ignore1
  471. * param ignore2
  472. * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure
  473. */
  474. int ospPrepareAllRoutes(
  475. struct sip_msg* msg,
  476. char* ignore1,
  477. char* ignore2)
  478. {
  479. int result = MODULE_RETURNCODE_TRUE;
  480. LOG(L_DBG, "osp: ospPrepareAllRoutes\n");
  481. for(result = ospPrepareDestination(msg, OSP_FIRST_ROUTE, OSP_MAIN_ROUTE, _osp_redir_uri);
  482. result == MODULE_RETURNCODE_TRUE;
  483. result = ospPrepareDestination(msg, OSP_NEXT_ROUTE, OSP_MAIN_ROUTE, _osp_redir_uri))
  484. {
  485. }
  486. return MODULE_RETURNCODE_TRUE;
  487. }