sig_ss7.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010 Digium, Inc.
  5. *
  6. * Richard Mudgett <rmudgett@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief SS7 signaling module.
  21. *
  22. * \author Matthew Fredrickson <creslin@digium.com>
  23. * \author Richard Mudgett <rmudgett@digium.com>
  24. *
  25. * See Also:
  26. * \arg \ref AstCREDITS
  27. */
  28. #include "asterisk.h"
  29. #if defined(HAVE_SS7)
  30. #include <signal.h>
  31. #include "asterisk/pbx.h"
  32. #include "asterisk/causes.h"
  33. #include "asterisk/musiconhold.h"
  34. #include "asterisk/cli.h"
  35. #include "asterisk/transcap.h"
  36. #include "sig_ss7.h"
  37. /* ------------------------------------------------------------------- */
  38. static const char *sig_ss7_call_level2str(enum sig_ss7_call_level level)
  39. {
  40. switch (level) {
  41. case SIG_SS7_CALL_LEVEL_IDLE:
  42. return "Idle";
  43. case SIG_SS7_CALL_LEVEL_ALLOCATED:
  44. return "Allocated";
  45. case SIG_SS7_CALL_LEVEL_CONTINUITY:
  46. return "Continuity";
  47. case SIG_SS7_CALL_LEVEL_SETUP:
  48. return "Setup";
  49. case SIG_SS7_CALL_LEVEL_PROCEEDING:
  50. return "Proceeding";
  51. case SIG_SS7_CALL_LEVEL_ALERTING:
  52. return "Alerting";
  53. case SIG_SS7_CALL_LEVEL_CONNECT:
  54. return "Connect";
  55. case SIG_SS7_CALL_LEVEL_GLARE:
  56. return "Glare";
  57. }
  58. return "Unknown";
  59. }
  60. #define SIG_SS7_DEADLOCK_AVOIDANCE(p) \
  61. do { \
  62. sig_ss7_unlock_private(p); \
  63. usleep(1); \
  64. sig_ss7_lock_private(p); \
  65. } while (0)
  66. static void sig_ss7_unlock_private(struct sig_ss7_chan *p)
  67. {
  68. if (p->calls->unlock_private) {
  69. p->calls->unlock_private(p->chan_pvt);
  70. }
  71. }
  72. static void sig_ss7_lock_private(struct sig_ss7_chan *p)
  73. {
  74. if (p->calls->lock_private) {
  75. p->calls->lock_private(p->chan_pvt);
  76. }
  77. }
  78. void sig_ss7_set_alarm(struct sig_ss7_chan *p, int in_alarm)
  79. {
  80. p->inalarm = in_alarm;
  81. if (p->calls->set_alarm) {
  82. p->calls->set_alarm(p->chan_pvt, in_alarm);
  83. }
  84. }
  85. static void sig_ss7_set_dialing(struct sig_ss7_chan *p, int is_dialing)
  86. {
  87. if (p->calls->set_dialing) {
  88. p->calls->set_dialing(p->chan_pvt, is_dialing);
  89. }
  90. }
  91. static void sig_ss7_set_digital(struct sig_ss7_chan *p, int is_digital)
  92. {
  93. if (p->calls->set_digital) {
  94. p->calls->set_digital(p->chan_pvt, is_digital);
  95. }
  96. }
  97. static void sig_ss7_set_outgoing(struct sig_ss7_chan *p, int is_outgoing)
  98. {
  99. p->outgoing = is_outgoing;
  100. if (p->calls->set_outgoing) {
  101. p->calls->set_outgoing(p->chan_pvt, is_outgoing);
  102. }
  103. }
  104. static void sig_ss7_set_inservice(struct sig_ss7_chan *p, int is_inservice)
  105. {
  106. if (p->calls->set_inservice) {
  107. p->calls->set_inservice(p->chan_pvt, is_inservice);
  108. }
  109. }
  110. static void sig_ss7_set_locallyblocked(struct sig_ss7_chan *p, int is_blocked)
  111. {
  112. p->locallyblocked = is_blocked;
  113. if (p->calls->set_locallyblocked) {
  114. p->calls->set_locallyblocked(p->chan_pvt, is_blocked);
  115. }
  116. }
  117. static void sig_ss7_set_remotelyblocked(struct sig_ss7_chan *p, int is_blocked)
  118. {
  119. p->remotelyblocked = is_blocked;
  120. if (p->calls->set_remotelyblocked) {
  121. p->calls->set_remotelyblocked(p->chan_pvt, is_blocked);
  122. }
  123. }
  124. /*!
  125. * \internal
  126. * \brief Set the caller id information in the parent module.
  127. * \since 1.8
  128. *
  129. * \param p sig_ss7 channel structure.
  130. *
  131. * \return Nothing
  132. */
  133. static void sig_ss7_set_caller_id(struct sig_ss7_chan *p)
  134. {
  135. struct ast_party_caller caller;
  136. if (p->calls->set_callerid) {
  137. ast_party_caller_init(&caller);
  138. caller.id.name.str = p->cid_name;
  139. caller.id.name.presentation = p->callingpres;
  140. caller.id.name.valid = 1;
  141. caller.id.number.str = p->cid_num;
  142. caller.id.number.plan = p->cid_ton;
  143. caller.id.number.presentation = p->callingpres;
  144. caller.id.number.valid = 1;
  145. if (!ast_strlen_zero(p->cid_subaddr)) {
  146. caller.id.subaddress.valid = 1;
  147. //caller.id.subaddress.type = 0;/* nsap */
  148. //caller.id.subaddress.odd_even_indicator = 0;
  149. caller.id.subaddress.str = p->cid_subaddr;
  150. }
  151. caller.ani.number.str = p->cid_ani;
  152. //caller.ani.number.plan = p->xxx;
  153. //caller.ani.number.presentation = p->xxx;
  154. caller.ani.number.valid = 1;
  155. caller.ani2 = p->cid_ani2;
  156. p->calls->set_callerid(p->chan_pvt, &caller);
  157. }
  158. }
  159. /*!
  160. * \internal
  161. * \brief Set the Dialed Number Identifier.
  162. * \since 1.8
  163. *
  164. * \param p sig_ss7 channel structure.
  165. * \param dnid Dialed Number Identifier string.
  166. *
  167. * \return Nothing
  168. */
  169. static void sig_ss7_set_dnid(struct sig_ss7_chan *p, const char *dnid)
  170. {
  171. if (p->calls->set_dnid) {
  172. p->calls->set_dnid(p->chan_pvt, dnid);
  173. }
  174. }
  175. static int sig_ss7_play_tone(struct sig_ss7_chan *p, enum sig_ss7_tone tone)
  176. {
  177. int res;
  178. if (p->calls->play_tone) {
  179. res = p->calls->play_tone(p->chan_pvt, tone);
  180. } else {
  181. res = -1;
  182. }
  183. return res;
  184. }
  185. static int sig_ss7_set_echocanceller(struct sig_ss7_chan *p, int enable)
  186. {
  187. if (p->calls->set_echocanceller) {
  188. return p->calls->set_echocanceller(p->chan_pvt, enable);
  189. }
  190. return -1;
  191. }
  192. static void sig_ss7_loopback(struct sig_ss7_chan *p, int enable)
  193. {
  194. if (p->loopedback != enable) {
  195. p->loopedback = enable;
  196. if (p->calls->set_loopback) {
  197. p->calls->set_loopback(p->chan_pvt, enable);
  198. }
  199. }
  200. }
  201. static struct ast_channel *sig_ss7_new_ast_channel(struct sig_ss7_chan *p, int state, int ulaw, int transfercapability, char *exten, const struct ast_channel *requestor)
  202. {
  203. struct ast_channel *ast;
  204. if (p->calls->new_ast_channel) {
  205. ast = p->calls->new_ast_channel(p->chan_pvt, state, ulaw, exten, requestor);
  206. } else {
  207. return NULL;
  208. }
  209. if (!ast) {
  210. return NULL;
  211. }
  212. if (!p->owner) {
  213. p->owner = ast;
  214. }
  215. p->alreadyhungup = 0;
  216. ast->transfercapability = transfercapability;
  217. pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY",
  218. ast_transfercapability2str(transfercapability));
  219. if (transfercapability & AST_TRANS_CAP_DIGITAL) {
  220. sig_ss7_set_digital(p, 1);
  221. }
  222. return ast;
  223. }
  224. static void sig_ss7_handle_link_exception(struct sig_ss7_linkset *linkset, int which)
  225. {
  226. if (linkset->calls->handle_link_exception) {
  227. linkset->calls->handle_link_exception(linkset, which);
  228. }
  229. }
  230. /*!
  231. * \internal
  232. * \brief Obtain the sig_ss7 owner channel lock if the owner exists.
  233. * \since 1.8
  234. *
  235. * \param ss7 SS7 linkset control structure.
  236. * \param chanpos Channel position in the span.
  237. *
  238. * \note Assumes the ss7->lock is already obtained.
  239. * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
  240. *
  241. * \return Nothing
  242. */
  243. static void sig_ss7_lock_owner(struct sig_ss7_linkset *ss7, int chanpos)
  244. {
  245. for (;;) {
  246. if (!ss7->pvts[chanpos]->owner) {
  247. /* There is no owner lock to get. */
  248. break;
  249. }
  250. if (!ast_channel_trylock(ss7->pvts[chanpos]->owner)) {
  251. /* We got the lock */
  252. break;
  253. }
  254. /* We must unlock the SS7 to avoid the possibility of a deadlock */
  255. ast_mutex_unlock(&ss7->lock);
  256. SIG_SS7_DEADLOCK_AVOIDANCE(ss7->pvts[chanpos]);
  257. ast_mutex_lock(&ss7->lock);
  258. }
  259. }
  260. /*!
  261. * \internal
  262. * \brief Queue the given frame onto the owner channel.
  263. * \since 1.8
  264. *
  265. * \param ss7 SS7 linkset control structure.
  266. * \param chanpos Channel position in the span.
  267. * \param frame Frame to queue onto the owner channel.
  268. *
  269. * \note Assumes the ss7->lock is already obtained.
  270. * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
  271. *
  272. * \return Nothing
  273. */
  274. static void sig_ss7_queue_frame(struct sig_ss7_linkset *ss7, int chanpos, struct ast_frame *frame)
  275. {
  276. sig_ss7_lock_owner(ss7, chanpos);
  277. if (ss7->pvts[chanpos]->owner) {
  278. ast_queue_frame(ss7->pvts[chanpos]->owner, frame);
  279. ast_channel_unlock(ss7->pvts[chanpos]->owner);
  280. }
  281. }
  282. /*!
  283. * \internal
  284. * \brief Queue a control frame of the specified subclass onto the owner channel.
  285. * \since 1.8
  286. *
  287. * \param ss7 SS7 linkset control structure.
  288. * \param chanpos Channel position in the span.
  289. * \param subclass Control frame subclass to queue onto the owner channel.
  290. *
  291. * \note Assumes the ss7->lock is already obtained.
  292. * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
  293. *
  294. * \return Nothing
  295. */
  296. static void sig_ss7_queue_control(struct sig_ss7_linkset *ss7, int chanpos, int subclass)
  297. {
  298. struct ast_frame f = {AST_FRAME_CONTROL, };
  299. struct sig_ss7_chan *p = ss7->pvts[chanpos];
  300. if (p->calls->queue_control) {
  301. p->calls->queue_control(p->chan_pvt, subclass);
  302. }
  303. f.subclass.integer = subclass;
  304. sig_ss7_queue_frame(ss7, chanpos, &f);
  305. }
  306. /*!
  307. * \internal
  308. * \brief Find the channel position by CIC/DPC.
  309. *
  310. * \param linkset SS7 linkset control structure.
  311. * \param cic Circuit Identification Code
  312. * \param dpc Destination Point Code
  313. *
  314. * \retval chanpos on success.
  315. * \retval -1 on error.
  316. */
  317. static int ss7_find_cic(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc)
  318. {
  319. int i;
  320. int winner = -1;
  321. for (i = 0; i < linkset->numchans; i++) {
  322. if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && linkset->pvts[i]->cic == cic)) {
  323. winner = i;
  324. break;
  325. }
  326. }
  327. return winner;
  328. }
  329. /*!
  330. * \internal
  331. * \brief Find the channel position by CIC/DPC and gripe if not found.
  332. *
  333. * \param linkset SS7 linkset control structure.
  334. * \param cic Circuit Identification Code
  335. * \param dpc Destination Point Code
  336. * \param msg_name Message type name that failed.
  337. *
  338. * \retval chanpos on success.
  339. * \retval -1 on error.
  340. */
  341. static int ss7_find_cic_gripe(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc, const char *msg_name)
  342. {
  343. int chanpos;
  344. chanpos = ss7_find_cic(linkset, cic, dpc);
  345. if (chanpos < 0) {
  346. ast_log(LOG_WARNING, "Linkset %d: SS7 %s requested unconfigured CIC/DPC %d/%d.\n",
  347. linkset->span, msg_name, cic, dpc);
  348. return -1;
  349. }
  350. return chanpos;
  351. }
  352. static void ss7_handle_cqm(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
  353. {
  354. unsigned char status[32];
  355. struct sig_ss7_chan *p = NULL;
  356. int i, offset;
  357. for (i = 0; i < linkset->numchans; i++) {
  358. if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
  359. p = linkset->pvts[i];
  360. offset = p->cic - startcic;
  361. status[offset] = 0;
  362. if (p->locallyblocked)
  363. status[offset] |= (1 << 0) | (1 << 4);
  364. if (p->remotelyblocked)
  365. status[offset] |= (1 << 1) | (1 << 5);
  366. if (p->ss7call) {
  367. if (p->outgoing)
  368. status[offset] |= (1 << 3);
  369. else
  370. status[offset] |= (1 << 2);
  371. } else
  372. status[offset] |= 0x3 << 2;
  373. }
  374. }
  375. if (p)
  376. isup_cqr(linkset->ss7, startcic, endcic, dpc, status);
  377. else
  378. ast_log(LOG_WARNING, "Could not find any equipped circuits within CQM CICs\n");
  379. }
  380. static inline void ss7_hangup_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
  381. {
  382. int i;
  383. for (i = 0; i < linkset->numchans; i++) {
  384. if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
  385. sig_ss7_lock_private(linkset->pvts[i]);
  386. if (linkset->pvts[i]->owner)
  387. linkset->pvts[i]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  388. sig_ss7_unlock_private(linkset->pvts[i]);
  389. }
  390. }
  391. }
  392. static inline void ss7_block_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc, unsigned char state[], int block)
  393. {
  394. int i;
  395. /* XXX the use of state here seems questionable about matching up with the linkset channels */
  396. for (i = 0; i < linkset->numchans; i++) {
  397. if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
  398. if (state) {
  399. if (state[i])
  400. sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
  401. } else
  402. sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
  403. }
  404. }
  405. }
  406. static void ss7_inservice(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
  407. {
  408. int i;
  409. for (i = 0; i < linkset->numchans; i++) {
  410. if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic))))
  411. sig_ss7_set_inservice(linkset->pvts[i], 1);
  412. }
  413. }
  414. static void ss7_reset_linkset(struct sig_ss7_linkset *linkset)
  415. {
  416. int i, startcic = -1, endcic, dpc;
  417. if (linkset->numchans <= 0)
  418. return;
  419. startcic = linkset->pvts[0]->cic;
  420. /* DB: CIC's DPC fix */
  421. dpc = linkset->pvts[0]->dpc;
  422. for (i = 0; i < linkset->numchans; i++) {
  423. if (linkset->pvts[i+1] && linkset->pvts[i+1]->dpc == dpc && ((linkset->pvts[i+1]->cic - linkset->pvts[i]->cic) == 1) && (linkset->pvts[i]->cic - startcic < 31)) {
  424. continue;
  425. } else {
  426. endcic = linkset->pvts[i]->cic;
  427. ast_verbose("Resetting CICs %d to %d\n", startcic, endcic);
  428. isup_grs(linkset->ss7, startcic, endcic, dpc);
  429. /* DB: CIC's DPC fix */
  430. if (linkset->pvts[i+1]) {
  431. startcic = linkset->pvts[i+1]->cic;
  432. dpc = linkset->pvts[i+1]->dpc;
  433. }
  434. }
  435. }
  436. }
  437. /* This function is assumed to be called with the private channel lock and linkset lock held */
  438. static void ss7_start_call(struct sig_ss7_chan *p, struct sig_ss7_linkset *linkset)
  439. {
  440. struct ss7 *ss7 = linkset->ss7;
  441. int law;
  442. struct ast_channel *c;
  443. char tmp[256];
  444. if (!(linkset->flags & LINKSET_FLAG_EXPLICITACM)) {
  445. p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
  446. isup_acm(ss7, p->ss7call);
  447. } else {
  448. p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
  449. }
  450. if (linkset->type == SS7_ITU) {
  451. law = SIG_SS7_ALAW;
  452. } else {
  453. law = SIG_SS7_ULAW;
  454. }
  455. /*
  456. * Release the SS7 lock while we create the channel so other
  457. * threads can send messages. We must also release the private
  458. * lock to prevent deadlock while creating the channel.
  459. */
  460. ast_mutex_unlock(&linkset->lock);
  461. sig_ss7_unlock_private(p);
  462. c = sig_ss7_new_ast_channel(p, AST_STATE_RING, law, 0, p->exten, NULL);
  463. if (!c) {
  464. ast_log(LOG_WARNING, "Unable to start PBX on CIC %d\n", p->cic);
  465. ast_mutex_lock(&linkset->lock);
  466. sig_ss7_lock_private(p);
  467. isup_rel(linkset->ss7, p->ss7call, -1);
  468. p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
  469. p->alreadyhungup = 1;
  470. return;
  471. }
  472. /* Hold the channel and private lock while we setup the channel. */
  473. ast_channel_lock(c);
  474. sig_ss7_lock_private(p);
  475. sig_ss7_set_echocanceller(p, 1);
  476. /*
  477. * It is reasonably safe to set the following
  478. * channel variables while the channel private
  479. * structure is locked. The PBX has not been
  480. * started yet and it is unlikely that any other task
  481. * will do anything with the channel we have just
  482. * created.
  483. *
  484. * We only reference these variables in the context of the ss7_linkset function
  485. * when receiving either and IAM or a COT message.
  486. */
  487. if (!ast_strlen_zero(p->charge_number)) {
  488. pbx_builtin_setvar_helper(c, "SS7_CHARGE_NUMBER", p->charge_number);
  489. /* Clear this after we set it */
  490. p->charge_number[0] = 0;
  491. }
  492. if (!ast_strlen_zero(p->gen_add_number)) {
  493. pbx_builtin_setvar_helper(c, "SS7_GENERIC_ADDRESS", p->gen_add_number);
  494. /* Clear this after we set it */
  495. p->gen_add_number[0] = 0;
  496. }
  497. if (!ast_strlen_zero(p->jip_number)) {
  498. pbx_builtin_setvar_helper(c, "SS7_JIP", p->jip_number);
  499. /* Clear this after we set it */
  500. p->jip_number[0] = 0;
  501. }
  502. if (!ast_strlen_zero(p->gen_dig_number)) {
  503. pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGITS", p->gen_dig_number);
  504. /* Clear this after we set it */
  505. p->gen_dig_number[0] = 0;
  506. }
  507. if (!ast_strlen_zero(p->orig_called_num)) {
  508. pbx_builtin_setvar_helper(c, "SS7_ORIG_CALLED_NUM", p->orig_called_num);
  509. /* Clear this after we set it */
  510. p->orig_called_num[0] = 0;
  511. }
  512. snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_type);
  513. pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGTYPE", tmp);
  514. /* Clear this after we set it */
  515. p->gen_dig_type = 0;
  516. snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_scheme);
  517. pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGSCHEME", tmp);
  518. /* Clear this after we set it */
  519. p->gen_dig_scheme = 0;
  520. if (!ast_strlen_zero(p->lspi_ident)) {
  521. pbx_builtin_setvar_helper(c, "SS7_LSPI_IDENT", p->lspi_ident);
  522. /* Clear this after we set it */
  523. p->lspi_ident[0] = 0;
  524. }
  525. snprintf(tmp, sizeof(tmp), "%d", p->call_ref_ident);
  526. pbx_builtin_setvar_helper(c, "SS7_CALLREF_IDENT", tmp);
  527. /* Clear this after we set it */
  528. p->call_ref_ident = 0;
  529. snprintf(tmp, sizeof(tmp), "%d", p->call_ref_pc);
  530. pbx_builtin_setvar_helper(c, "SS7_CALLREF_PC", tmp);
  531. /* Clear this after we set it */
  532. p->call_ref_pc = 0;
  533. snprintf(tmp, sizeof(tmp), "%d", p->calling_party_cat);
  534. pbx_builtin_setvar_helper(c, "SS7_CALLING_PARTY_CATEGORY", tmp);
  535. /* Clear this after we set it */
  536. p->calling_party_cat = 0;
  537. if (!ast_strlen_zero(p->redirecting_num)) {
  538. pbx_builtin_setvar_helper(c, "SS7_REDIRECTING_NUMBER", p->redirecting_num);
  539. /* Clear this after we set it */
  540. p->redirecting_num[0] = 0;
  541. }
  542. if (!ast_strlen_zero(p->generic_name)) {
  543. pbx_builtin_setvar_helper(c, "SS7_GENERIC_NAME", p->generic_name);
  544. /* Clear this after we set it */
  545. p->generic_name[0] = 0;
  546. }
  547. sig_ss7_unlock_private(p);
  548. ast_channel_unlock(c);
  549. if (ast_pbx_start(c)) {
  550. ast_log(LOG_WARNING, "Unable to start PBX on %s (CIC %d)\n", c->name, p->cic);
  551. ast_hangup(c);
  552. } else {
  553. ast_verb(3, "Accepting call to '%s' on CIC %d\n", p->exten, p->cic);
  554. }
  555. /* Must return with linkset and private lock. */
  556. ast_mutex_lock(&linkset->lock);
  557. sig_ss7_lock_private(p);
  558. }
  559. static void ss7_apply_plan_to_number(char *buf, size_t size, const struct sig_ss7_linkset *ss7, const char *number, const unsigned nai)
  560. {
  561. if (ast_strlen_zero(number)) { /* make sure a number exists so prefix isn't placed on an empty string */
  562. if (size) {
  563. *buf = '\0';
  564. }
  565. return;
  566. }
  567. switch (nai) {
  568. case SS7_NAI_INTERNATIONAL:
  569. snprintf(buf, size, "%s%s", ss7->internationalprefix, number);
  570. break;
  571. case SS7_NAI_NATIONAL:
  572. snprintf(buf, size, "%s%s", ss7->nationalprefix, number);
  573. break;
  574. case SS7_NAI_SUBSCRIBER:
  575. snprintf(buf, size, "%s%s", ss7->subscriberprefix, number);
  576. break;
  577. case SS7_NAI_UNKNOWN:
  578. snprintf(buf, size, "%s%s", ss7->unknownprefix, number);
  579. break;
  580. default:
  581. snprintf(buf, size, "%s", number);
  582. break;
  583. }
  584. }
  585. static int ss7_pres_scr2cid_pres(char presentation_ind, char screening_ind)
  586. {
  587. return ((presentation_ind & 0x3) << 5) | (screening_ind & 0x3);
  588. }
  589. /* This is a thread per linkset that handles all received events from libss7. */
  590. void *ss7_linkset(void *data)
  591. {
  592. int res, i;
  593. struct timeval *next = NULL, tv;
  594. struct sig_ss7_linkset *linkset = (struct sig_ss7_linkset *) data;
  595. struct ss7 *ss7 = linkset->ss7;
  596. ss7_event *e = NULL;
  597. struct sig_ss7_chan *p;
  598. int chanpos;
  599. struct pollfd pollers[SIG_SS7_NUM_DCHANS];
  600. int nextms = 0;
  601. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  602. ss7_set_debug(ss7, SIG_SS7_DEBUG_DEFAULT);
  603. ast_mutex_lock(&linkset->lock);
  604. ss7_start(ss7);
  605. ast_mutex_unlock(&linkset->lock);
  606. for (;;) {
  607. ast_mutex_lock(&linkset->lock);
  608. if ((next = ss7_schedule_next(ss7))) {
  609. tv = ast_tvnow();
  610. tv.tv_sec = next->tv_sec - tv.tv_sec;
  611. tv.tv_usec = next->tv_usec - tv.tv_usec;
  612. if (tv.tv_usec < 0) {
  613. tv.tv_usec += 1000000;
  614. tv.tv_sec -= 1;
  615. }
  616. if (tv.tv_sec < 0) {
  617. tv.tv_sec = 0;
  618. tv.tv_usec = 0;
  619. }
  620. nextms = tv.tv_sec * 1000;
  621. nextms += tv.tv_usec / 1000;
  622. }
  623. for (i = 0; i < linkset->numsigchans; i++) {
  624. pollers[i].fd = linkset->fds[i];
  625. pollers[i].events = ss7_pollflags(ss7, linkset->fds[i]);
  626. pollers[i].revents = 0;
  627. }
  628. ast_mutex_unlock(&linkset->lock);
  629. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  630. pthread_testcancel();
  631. res = poll(pollers, linkset->numsigchans, nextms);
  632. pthread_testcancel();
  633. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  634. if ((res < 0) && (errno != EINTR)) {
  635. ast_log(LOG_ERROR, "poll(%s)\n", strerror(errno));
  636. } else if (!res) {
  637. ast_mutex_lock(&linkset->lock);
  638. ss7_schedule_run(ss7);
  639. ast_mutex_unlock(&linkset->lock);
  640. continue;
  641. }
  642. ast_mutex_lock(&linkset->lock);
  643. for (i = 0; i < linkset->numsigchans; i++) {
  644. if (pollers[i].revents & POLLPRI) {
  645. sig_ss7_handle_link_exception(linkset, i);
  646. }
  647. if (pollers[i].revents & POLLIN) {
  648. res = ss7_read(ss7, pollers[i].fd);
  649. }
  650. if (pollers[i].revents & POLLOUT) {
  651. res = ss7_write(ss7, pollers[i].fd);
  652. if (res < 0) {
  653. ast_debug(1, "Error in write %s\n", strerror(errno));
  654. }
  655. }
  656. }
  657. while ((e = ss7_check_event(ss7))) {
  658. if (linkset->debug) {
  659. ast_verbose("Linkset %d: Processing event: %s\n",
  660. linkset->span, ss7_event2str(e->e));
  661. }
  662. switch (e->e) {
  663. case SS7_EVENT_UP:
  664. if (linkset->state != LINKSET_STATE_UP) {
  665. ast_verbose("--- SS7 Up ---\n");
  666. ss7_reset_linkset(linkset);
  667. }
  668. linkset->state = LINKSET_STATE_UP;
  669. break;
  670. case SS7_EVENT_DOWN:
  671. ast_verbose("--- SS7 Down ---\n");
  672. linkset->state = LINKSET_STATE_DOWN;
  673. for (i = 0; i < linkset->numchans; i++) {
  674. p = linkset->pvts[i];
  675. if (p) {
  676. sig_ss7_set_alarm(p, 1);
  677. }
  678. }
  679. break;
  680. case MTP2_LINK_UP:
  681. ast_verbose("MTP2 link up (SLC %d)\n", e->gen.data);
  682. break;
  683. case MTP2_LINK_DOWN:
  684. ast_log(LOG_WARNING, "MTP2 link down (SLC %d)\n", e->gen.data);
  685. break;
  686. case ISUP_EVENT_CPG:
  687. chanpos = ss7_find_cic_gripe(linkset, e->cpg.cic, e->cpg.opc, "CPG");
  688. if (chanpos < 0) {
  689. break;
  690. }
  691. p = linkset->pvts[chanpos];
  692. sig_ss7_lock_private(p);
  693. switch (e->cpg.event) {
  694. case CPG_EVENT_ALERTING:
  695. if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
  696. p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
  697. }
  698. sig_ss7_lock_owner(linkset, chanpos);
  699. if (p->owner) {
  700. ast_setstate(p->owner, AST_STATE_RINGING);
  701. ast_channel_unlock(p->owner);
  702. }
  703. sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
  704. break;
  705. case CPG_EVENT_PROGRESS:
  706. case CPG_EVENT_INBANDINFO:
  707. {
  708. ast_debug(1, "Queuing frame PROGRESS on CIC %d\n", p->cic);
  709. sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROGRESS);
  710. p->progress = 1;
  711. sig_ss7_set_dialing(p, 0);
  712. #if 0 /* This code no longer seems to be necessary so I did not convert it. */
  713. if (p->dsp && p->dsp_features) {
  714. ast_dsp_set_features(p->dsp, p->dsp_features);
  715. p->dsp_features = 0;
  716. }
  717. #endif
  718. }
  719. break;
  720. default:
  721. ast_debug(1, "Do not handle CPG with event type 0x%x\n", e->cpg.event);
  722. break;
  723. }
  724. sig_ss7_unlock_private(p);
  725. break;
  726. case ISUP_EVENT_RSC:
  727. ast_verbose("Resetting CIC %d\n", e->rsc.cic);
  728. chanpos = ss7_find_cic_gripe(linkset, e->rsc.cic, e->rsc.opc, "RSC");
  729. if (chanpos < 0) {
  730. break;
  731. }
  732. p = linkset->pvts[chanpos];
  733. sig_ss7_lock_private(p);
  734. sig_ss7_set_inservice(p, 1);
  735. sig_ss7_set_remotelyblocked(p, 0);
  736. isup_set_call_dpc(e->rsc.call, p->dpc);
  737. sig_ss7_lock_owner(linkset, chanpos);
  738. p->ss7call = NULL;
  739. if (p->owner) {
  740. ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
  741. ast_channel_unlock(p->owner);
  742. }
  743. sig_ss7_unlock_private(p);
  744. isup_rlc(ss7, e->rsc.call);
  745. break;
  746. case ISUP_EVENT_GRS:
  747. ast_debug(1, "Got Reset for CICs %d to %d: Acknowledging\n", e->grs.startcic, e->grs.endcic);
  748. chanpos = ss7_find_cic_gripe(linkset, e->grs.startcic, e->grs.opc, "GRS");
  749. if (chanpos < 0) {
  750. break;
  751. }
  752. p = linkset->pvts[chanpos];
  753. isup_gra(ss7, e->grs.startcic, e->grs.endcic, e->grs.opc);
  754. ss7_block_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc, NULL, 0);
  755. ss7_hangup_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc);
  756. break;
  757. case ISUP_EVENT_CQM:
  758. ast_debug(1, "Got Circuit group query message from CICs %d to %d\n", e->cqm.startcic, e->cqm.endcic);
  759. ss7_handle_cqm(linkset, e->cqm.startcic, e->cqm.endcic, e->cqm.opc);
  760. break;
  761. case ISUP_EVENT_GRA:
  762. ast_verbose("Got reset acknowledgement from CIC %d to %d.\n", e->gra.startcic, e->gra.endcic);
  763. ss7_inservice(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc);
  764. ss7_block_cics(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc, e->gra.status, 1);
  765. break;
  766. case ISUP_EVENT_IAM:
  767. ast_debug(1, "Got IAM for CIC %d and called number %s, calling number %s\n", e->iam.cic, e->iam.called_party_num, e->iam.calling_party_num);
  768. chanpos = ss7_find_cic_gripe(linkset, e->iam.cic, e->iam.opc, "IAM");
  769. if (chanpos < 0) {
  770. isup_rel(ss7, e->iam.call, -1);
  771. break;
  772. }
  773. p = linkset->pvts[chanpos];
  774. sig_ss7_lock_private(p);
  775. sig_ss7_lock_owner(linkset, chanpos);
  776. if (p->call_level != SIG_SS7_CALL_LEVEL_IDLE) {
  777. /*
  778. * Detected glare/dual-seizure
  779. *
  780. * Always abort both calls since we can't implement the dual
  781. * seizure procedures due to our channel assignment architecture
  782. * and the fact that we cannot tell libss7 to discard its call
  783. * structure to ignore the incoming IAM.
  784. */
  785. ast_debug(1,
  786. "Linkset %d: SS7 IAM glare on CIC/DPC %d/%d. Dropping both calls.\n",
  787. linkset->span, e->iam.cic, e->iam.opc);
  788. if (p->call_level == SIG_SS7_CALL_LEVEL_ALLOCATED) {
  789. /*
  790. * We have not sent our IAM yet and we never will at this point.
  791. */
  792. p->alreadyhungup = 1;
  793. isup_rel(ss7, e->iam.call, -1);
  794. }
  795. p->call_level = SIG_SS7_CALL_LEVEL_GLARE;
  796. if (p->owner) {
  797. p->owner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
  798. ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
  799. ast_channel_unlock(p->owner);
  800. }
  801. sig_ss7_unlock_private(p);
  802. break;
  803. }
  804. /*
  805. * The channel should not have an owner at this point since we
  806. * are in the process of creating an owner for it.
  807. */
  808. ast_assert(!p->owner);
  809. /* Mark channel as in use so no outgoing call will steal it. */
  810. p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
  811. p->ss7call = e->iam.call;
  812. isup_set_call_dpc(p->ss7call, p->dpc);
  813. if ((p->use_callerid) && (!ast_strlen_zero(e->iam.calling_party_num))) {
  814. ss7_apply_plan_to_number(p->cid_num, sizeof(p->cid_num), linkset, e->iam.calling_party_num, e->iam.calling_nai);
  815. p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
  816. } else
  817. p->cid_num[0] = 0;
  818. /* Set DNID */
  819. if (!ast_strlen_zero(e->iam.called_party_num)) {
  820. ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset,
  821. e->iam.called_party_num, e->iam.called_nai);
  822. } else {
  823. p->exten[0] = '\0';
  824. }
  825. sig_ss7_set_dnid(p, p->exten);
  826. if (p->immediate) {
  827. p->exten[0] = 's';
  828. p->exten[1] = '\0';
  829. } else if (!ast_strlen_zero(e->iam.called_party_num)) {
  830. char *st;
  831. ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset, e->iam.called_party_num, e->iam.called_nai);
  832. st = strchr(p->exten, '#');
  833. if (st) {
  834. *st = '\0';
  835. }
  836. } else {
  837. p->exten[0] = '\0';
  838. }
  839. p->cid_ani[0] = '\0';
  840. if ((p->use_callerid) && (!ast_strlen_zero(e->iam.generic_name)))
  841. ast_copy_string(p->cid_name, e->iam.generic_name, sizeof(p->cid_name));
  842. else
  843. p->cid_name[0] = '\0';
  844. p->cid_ani2 = e->iam.oli_ani2;
  845. p->cid_ton = 0;
  846. ast_copy_string(p->charge_number, e->iam.charge_number, sizeof(p->charge_number));
  847. ast_copy_string(p->gen_add_number, e->iam.gen_add_number, sizeof(p->gen_add_number));
  848. p->gen_add_type = e->iam.gen_add_type;
  849. p->gen_add_nai = e->iam.gen_add_nai;
  850. p->gen_add_pres_ind = e->iam.gen_add_pres_ind;
  851. p->gen_add_num_plan = e->iam.gen_add_num_plan;
  852. ast_copy_string(p->gen_dig_number, e->iam.gen_dig_number, sizeof(p->gen_dig_number));
  853. p->gen_dig_type = e->iam.gen_dig_type;
  854. p->gen_dig_scheme = e->iam.gen_dig_scheme;
  855. ast_copy_string(p->jip_number, e->iam.jip_number, sizeof(p->jip_number));
  856. ast_copy_string(p->orig_called_num, e->iam.orig_called_num, sizeof(p->orig_called_num));
  857. ast_copy_string(p->redirecting_num, e->iam.redirecting_num, sizeof(p->redirecting_num));
  858. ast_copy_string(p->generic_name, e->iam.generic_name, sizeof(p->generic_name));
  859. p->calling_party_cat = e->iam.calling_party_cat;
  860. sig_ss7_set_caller_id(p);
  861. if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
  862. if (e->iam.cot_check_required) {
  863. p->call_level = SIG_SS7_CALL_LEVEL_CONTINUITY;
  864. sig_ss7_loopback(p, 1);
  865. } else {
  866. ss7_start_call(p, linkset);
  867. }
  868. } else {
  869. ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
  870. p->alreadyhungup = 1;
  871. isup_rel(ss7, e->iam.call, AST_CAUSE_UNALLOCATED);
  872. }
  873. sig_ss7_unlock_private(p);
  874. break;
  875. case ISUP_EVENT_COT:
  876. chanpos = ss7_find_cic_gripe(linkset, e->cot.cic, e->cot.opc, "COT");
  877. if (chanpos < 0) {
  878. isup_rel(ss7, e->cot.call, -1);
  879. break;
  880. }
  881. p = linkset->pvts[chanpos];
  882. sig_ss7_lock_private(p);
  883. if (p->loopedback) {
  884. sig_ss7_loopback(p, 0);
  885. ss7_start_call(p, linkset);
  886. }
  887. sig_ss7_unlock_private(p);
  888. break;
  889. case ISUP_EVENT_CCR:
  890. ast_debug(1, "Got CCR request on CIC %d\n", e->ccr.cic);
  891. chanpos = ss7_find_cic_gripe(linkset, e->ccr.cic, e->ccr.opc, "CCR");
  892. if (chanpos < 0) {
  893. break;
  894. }
  895. p = linkset->pvts[chanpos];
  896. sig_ss7_lock_private(p);
  897. sig_ss7_loopback(p, 1);
  898. sig_ss7_unlock_private(p);
  899. isup_lpa(linkset->ss7, e->ccr.cic, p->dpc);
  900. break;
  901. case ISUP_EVENT_CVT:
  902. ast_debug(1, "Got CVT request on CIC %d\n", e->cvt.cic);
  903. chanpos = ss7_find_cic_gripe(linkset, e->cvt.cic, e->cvt.opc, "CVT");
  904. if (chanpos < 0) {
  905. break;
  906. }
  907. p = linkset->pvts[chanpos];
  908. sig_ss7_lock_private(p);
  909. sig_ss7_loopback(p, 1);
  910. sig_ss7_unlock_private(p);
  911. isup_cvr(linkset->ss7, e->cvt.cic, p->dpc);
  912. break;
  913. case ISUP_EVENT_REL:
  914. chanpos = ss7_find_cic_gripe(linkset, e->rel.cic, e->rel.opc, "REL");
  915. if (chanpos < 0) {
  916. /* Continue hanging up the call anyway. */
  917. isup_rlc(ss7, e->rel.call);
  918. break;
  919. }
  920. p = linkset->pvts[chanpos];
  921. sig_ss7_lock_private(p);
  922. sig_ss7_lock_owner(linkset, chanpos);
  923. if (p->owner) {
  924. p->owner->hangupcause = e->rel.cause;
  925. ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
  926. ast_channel_unlock(p->owner);
  927. }
  928. /* End the loopback if we have one */
  929. sig_ss7_loopback(p, 0);
  930. isup_rlc(ss7, e->rel.call);
  931. p->ss7call = NULL;
  932. sig_ss7_unlock_private(p);
  933. break;
  934. case ISUP_EVENT_ACM:
  935. chanpos = ss7_find_cic_gripe(linkset, e->acm.cic, e->acm.opc, "ACM");
  936. if (chanpos < 0) {
  937. isup_rel(ss7, e->acm.call, -1);
  938. break;
  939. }
  940. {
  941. p = linkset->pvts[chanpos];
  942. ast_debug(1, "Queueing frame from SS7_EVENT_ACM on CIC %d\n", p->cic);
  943. if (e->acm.call_ref_ident > 0) {
  944. p->rlt = 1; /* Setting it but not using it here*/
  945. }
  946. sig_ss7_lock_private(p);
  947. sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROCEEDING);
  948. if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING) {
  949. p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
  950. }
  951. sig_ss7_set_dialing(p, 0);
  952. /* Send alerting if subscriber is free */
  953. if (e->acm.called_party_status_ind == 1) {
  954. if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
  955. p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
  956. }
  957. sig_ss7_lock_owner(linkset, chanpos);
  958. if (p->owner) {
  959. ast_setstate(p->owner, AST_STATE_RINGING);
  960. ast_channel_unlock(p->owner);
  961. }
  962. sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
  963. }
  964. sig_ss7_unlock_private(p);
  965. }
  966. break;
  967. case ISUP_EVENT_CGB:
  968. chanpos = ss7_find_cic_gripe(linkset, e->cgb.startcic, e->cgb.opc, "CGB");
  969. if (chanpos < 0) {
  970. break;
  971. }
  972. p = linkset->pvts[chanpos];
  973. ss7_block_cics(linkset, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, 1);
  974. isup_cgba(linkset->ss7, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, e->cgb.type);
  975. break;
  976. case ISUP_EVENT_CGU:
  977. chanpos = ss7_find_cic_gripe(linkset, e->cgu.startcic, e->cgu.opc, "CGU");
  978. if (chanpos < 0) {
  979. break;
  980. }
  981. p = linkset->pvts[chanpos];
  982. ss7_block_cics(linkset, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, 0);
  983. isup_cgua(linkset->ss7, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, e->cgu.type);
  984. break;
  985. case ISUP_EVENT_UCIC:
  986. chanpos = ss7_find_cic_gripe(linkset, e->ucic.cic, e->ucic.opc, "UCIC");
  987. if (chanpos < 0) {
  988. break;
  989. }
  990. p = linkset->pvts[chanpos];
  991. ast_debug(1, "Unequiped Circuit Id Code on CIC %d\n", e->ucic.cic);
  992. sig_ss7_lock_private(p);
  993. sig_ss7_set_remotelyblocked(p, 1);
  994. sig_ss7_set_inservice(p, 0);
  995. sig_ss7_unlock_private(p);/* doesn't require a SS7 acknowledgement */
  996. break;
  997. case ISUP_EVENT_BLO:
  998. chanpos = ss7_find_cic_gripe(linkset, e->blo.cic, e->blo.opc, "BLO");
  999. if (chanpos < 0) {
  1000. break;
  1001. }
  1002. p = linkset->pvts[chanpos];
  1003. ast_debug(1, "Blocking CIC %d\n", e->blo.cic);
  1004. sig_ss7_lock_private(p);
  1005. sig_ss7_set_remotelyblocked(p, 1);
  1006. sig_ss7_unlock_private(p);
  1007. isup_bla(linkset->ss7, e->blo.cic, p->dpc);
  1008. break;
  1009. case ISUP_EVENT_BLA:
  1010. chanpos = ss7_find_cic_gripe(linkset, e->bla.cic, e->bla.opc, "BLA");
  1011. if (chanpos < 0) {
  1012. break;
  1013. }
  1014. ast_debug(1, "Blocking CIC %d\n", e->bla.cic);
  1015. p = linkset->pvts[chanpos];
  1016. sig_ss7_lock_private(p);
  1017. sig_ss7_set_locallyblocked(p, 1);
  1018. sig_ss7_unlock_private(p);
  1019. break;
  1020. case ISUP_EVENT_UBL:
  1021. chanpos = ss7_find_cic_gripe(linkset, e->ubl.cic, e->ubl.opc, "UBL");
  1022. if (chanpos < 0) {
  1023. break;
  1024. }
  1025. p = linkset->pvts[chanpos];
  1026. ast_debug(1, "Unblocking CIC %d\n", e->ubl.cic);
  1027. sig_ss7_lock_private(p);
  1028. sig_ss7_set_remotelyblocked(p, 0);
  1029. sig_ss7_unlock_private(p);
  1030. isup_uba(linkset->ss7, e->ubl.cic, p->dpc);
  1031. break;
  1032. case ISUP_EVENT_UBA:
  1033. chanpos = ss7_find_cic_gripe(linkset, e->uba.cic, e->uba.opc, "UBA");
  1034. if (chanpos < 0) {
  1035. break;
  1036. }
  1037. p = linkset->pvts[chanpos];
  1038. ast_debug(1, "Unblocking CIC %d\n", e->uba.cic);
  1039. sig_ss7_lock_private(p);
  1040. sig_ss7_set_locallyblocked(p, 0);
  1041. sig_ss7_unlock_private(p);
  1042. break;
  1043. case ISUP_EVENT_CON:
  1044. case ISUP_EVENT_ANM:
  1045. if (e->e == ISUP_EVENT_CON) {
  1046. chanpos = ss7_find_cic_gripe(linkset, e->con.cic, e->con.opc, "CON");
  1047. if (chanpos < 0) {
  1048. isup_rel(ss7, e->con.call, -1);
  1049. break;
  1050. }
  1051. } else {
  1052. chanpos = ss7_find_cic_gripe(linkset, e->anm.cic, e->anm.opc, "ANM");
  1053. if (chanpos < 0) {
  1054. isup_rel(ss7, e->anm.call, -1);
  1055. break;
  1056. }
  1057. }
  1058. {
  1059. p = linkset->pvts[chanpos];
  1060. sig_ss7_lock_private(p);
  1061. if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
  1062. p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
  1063. }
  1064. sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_ANSWER);
  1065. #if 0 /* This code no longer seems to be necessary so I did not convert it. */
  1066. if (p->dsp && p->dsp_features) {
  1067. ast_dsp_set_features(p->dsp, p->dsp_features);
  1068. p->dsp_features = 0;
  1069. }
  1070. #endif
  1071. sig_ss7_set_echocanceller(p, 1);
  1072. sig_ss7_unlock_private(p);
  1073. }
  1074. break;
  1075. case ISUP_EVENT_RLC:
  1076. /* XXX Call ptr should be passed up from libss7! */
  1077. chanpos = ss7_find_cic_gripe(linkset, e->rlc.cic, e->rlc.opc, "RLC");
  1078. if (chanpos < 0) {
  1079. break;
  1080. }
  1081. {
  1082. p = linkset->pvts[chanpos];
  1083. sig_ss7_lock_private(p);
  1084. if (p->alreadyhungup) {
  1085. if (!p->owner) {
  1086. p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
  1087. }
  1088. p->ss7call = NULL;
  1089. }
  1090. sig_ss7_unlock_private(p);
  1091. }
  1092. break;
  1093. case ISUP_EVENT_FAA:
  1094. /*!
  1095. * \todo The handling of the SS7 FAA message is not good and I
  1096. * don't know enough to handle it correctly.
  1097. */
  1098. chanpos = ss7_find_cic_gripe(linkset, e->faa.cic, e->faa.opc, "FAA");
  1099. if (chanpos < 0) {
  1100. isup_rel(linkset->ss7, e->faa.call, -1);
  1101. break;
  1102. }
  1103. {
  1104. /* XXX FAR and FAA used for something dealing with transfers? */
  1105. p = linkset->pvts[chanpos];
  1106. ast_debug(1, "FAA received on CIC %d\n", e->faa.cic);
  1107. sig_ss7_lock_private(p);
  1108. if (p->alreadyhungup){
  1109. if (!p->owner) {
  1110. p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
  1111. }
  1112. /* XXX We seem to be leaking the isup call structure here. */
  1113. p->ss7call = NULL;
  1114. ast_log(LOG_NOTICE, "Received FAA and we haven't sent FAR. Ignoring.\n");
  1115. }
  1116. sig_ss7_unlock_private(p);
  1117. }
  1118. break;
  1119. default:
  1120. ast_debug(1, "Unknown event %s\n", ss7_event2str(e->e));
  1121. break;
  1122. }
  1123. }
  1124. ast_mutex_unlock(&linkset->lock);
  1125. }
  1126. return 0;
  1127. }
  1128. static inline void ss7_rel(struct sig_ss7_linkset *ss7)
  1129. {
  1130. ast_mutex_unlock(&ss7->lock);
  1131. }
  1132. static void ss7_grab(struct sig_ss7_chan *pvt, struct sig_ss7_linkset *ss7)
  1133. {
  1134. int res;
  1135. /* Grab the lock first */
  1136. do {
  1137. res = ast_mutex_trylock(&ss7->lock);
  1138. if (res) {
  1139. SIG_SS7_DEADLOCK_AVOIDANCE(pvt);
  1140. }
  1141. } while (res);
  1142. /* Then break the poll */
  1143. if (ss7->master != AST_PTHREADT_NULL)
  1144. pthread_kill(ss7->master, SIGURG);
  1145. }
  1146. /*!
  1147. * \brief Notify the SS7 layer that the link is in alarm.
  1148. * \since 1.8
  1149. *
  1150. * \param linkset Controlling linkset for the channel.
  1151. * \param which Link index of the signaling channel.
  1152. *
  1153. * \return Nothing
  1154. */
  1155. void sig_ss7_link_alarm(struct sig_ss7_linkset *linkset, int which)
  1156. {
  1157. linkset->linkstate[which] |= (LINKSTATE_DOWN | LINKSTATE_INALARM);
  1158. linkset->linkstate[which] &= ~LINKSTATE_UP;
  1159. ss7_link_alarm(linkset->ss7, linkset->fds[which]);
  1160. }
  1161. /*!
  1162. * \brief Notify the SS7 layer that the link is no longer in alarm.
  1163. * \since 1.8
  1164. *
  1165. * \param linkset Controlling linkset for the channel.
  1166. * \param which Link index of the signaling channel.
  1167. *
  1168. * \return Nothing
  1169. */
  1170. void sig_ss7_link_noalarm(struct sig_ss7_linkset *linkset, int which)
  1171. {
  1172. linkset->linkstate[which] &= ~(LINKSTATE_INALARM | LINKSTATE_DOWN);
  1173. linkset->linkstate[which] |= LINKSTATE_STARTING;
  1174. ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
  1175. }
  1176. /*!
  1177. * \brief Setup and add a SS7 link channel.
  1178. * \since 1.8
  1179. *
  1180. * \param linkset Controlling linkset for the channel.
  1181. * \param which Link index of the signaling channel.
  1182. * \param ss7type Switch type of the linkset
  1183. * \param transport Signaling transport of channel.
  1184. * \param inalarm Non-zero if the channel is in alarm.
  1185. * \param networkindicator User configuration parameter.
  1186. * \param pointcode User configuration parameter.
  1187. * \param adjpointcode User configuration parameter.
  1188. *
  1189. * \retval 0 on success.
  1190. * \retval -1 on error.
  1191. */
  1192. int sig_ss7_add_sigchan(struct sig_ss7_linkset *linkset, int which, int ss7type, int transport, int inalarm, int networkindicator, int pointcode, int adjpointcode)
  1193. {
  1194. if (!linkset->ss7) {
  1195. linkset->type = ss7type;
  1196. linkset->ss7 = ss7_new(ss7type);
  1197. if (!linkset->ss7) {
  1198. ast_log(LOG_ERROR, "Can't create new SS7!\n");
  1199. return -1;
  1200. }
  1201. }
  1202. ss7_set_network_ind(linkset->ss7, networkindicator);
  1203. ss7_set_pc(linkset->ss7, pointcode);
  1204. if (ss7_add_link(linkset->ss7, transport, linkset->fds[which])) {
  1205. ast_log(LOG_WARNING, "Could not add SS7 link!\n");
  1206. }
  1207. if (inalarm) {
  1208. linkset->linkstate[which] = LINKSTATE_DOWN | LINKSTATE_INALARM;
  1209. ss7_link_alarm(linkset->ss7, linkset->fds[which]);
  1210. } else {
  1211. linkset->linkstate[which] = LINKSTATE_DOWN;
  1212. ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
  1213. }
  1214. ss7_set_adjpc(linkset->ss7, linkset->fds[which], adjpointcode);
  1215. return 0;
  1216. }
  1217. /*!
  1218. * \internal
  1219. * \brief Determine if a private channel structure is available.
  1220. *
  1221. * \param pvt Channel to determine if available.
  1222. *
  1223. * \return TRUE if the channel is available.
  1224. */
  1225. static int sig_ss7_is_chan_available(struct sig_ss7_chan *pvt)
  1226. {
  1227. if (!pvt->inalarm && !pvt->owner && !pvt->ss7call
  1228. && pvt->call_level == SIG_SS7_CALL_LEVEL_IDLE
  1229. && !pvt->locallyblocked && !pvt->remotelyblocked) {
  1230. return 1;
  1231. }
  1232. return 0;
  1233. }
  1234. /*!
  1235. * \brief Determine if the specified channel is available for an outgoing call.
  1236. * \since 1.8
  1237. *
  1238. * \param p Signaling private structure pointer.
  1239. *
  1240. * \retval TRUE if the channel is available.
  1241. */
  1242. int sig_ss7_available(struct sig_ss7_chan *p)
  1243. {
  1244. int available;
  1245. if (!p->ss7) {
  1246. /* Something is wrong here. A SS7 channel without the ss7 pointer? */
  1247. return 0;
  1248. }
  1249. /* Only have to deal with the linkset lock. */
  1250. ast_mutex_lock(&p->ss7->lock);
  1251. available = sig_ss7_is_chan_available(p);
  1252. if (available) {
  1253. p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
  1254. }
  1255. ast_mutex_unlock(&p->ss7->lock);
  1256. return available;
  1257. }
  1258. static unsigned char cid_pres2ss7pres(int cid_pres)
  1259. {
  1260. return (cid_pres >> 5) & 0x03;
  1261. }
  1262. static unsigned char cid_pres2ss7screen(int cid_pres)
  1263. {
  1264. return cid_pres & 0x03;
  1265. }
  1266. /*!
  1267. * \brief Dial out using the specified SS7 channel.
  1268. * \since 1.8
  1269. *
  1270. * \param p Signaling private structure pointer.
  1271. * \param ast Asterisk channel structure pointer.
  1272. * \param rdest Dialstring.
  1273. *
  1274. * \retval 0 on success.
  1275. * \retval -1 on error.
  1276. */
  1277. int sig_ss7_call(struct sig_ss7_chan *p, struct ast_channel *ast, char *rdest)
  1278. {
  1279. char ss7_called_nai;
  1280. int called_nai_strip;
  1281. char ss7_calling_nai;
  1282. int calling_nai_strip;
  1283. const char *charge_str = NULL;
  1284. const char *gen_address = NULL;
  1285. const char *gen_digits = NULL;
  1286. const char *gen_dig_type = NULL;
  1287. const char *gen_dig_scheme = NULL;
  1288. const char *gen_name = NULL;
  1289. const char *jip_digits = NULL;
  1290. const char *lspi_ident = NULL;
  1291. const char *rlt_flag = NULL;
  1292. const char *call_ref_id = NULL;
  1293. const char *call_ref_pc = NULL;
  1294. const char *send_far = NULL;
  1295. char *c;
  1296. char *l;
  1297. char dest[256];
  1298. ast_copy_string(dest, rdest, sizeof(dest));
  1299. c = strchr(dest, '/');
  1300. if (c) {
  1301. c++;
  1302. } else {
  1303. c = "";
  1304. }
  1305. if (strlen(c) < p->stripmsd) {
  1306. ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
  1307. return -1;
  1308. }
  1309. if (!p->hidecallerid) {
  1310. l = ast->connected.id.number.valid ? ast->connected.id.number.str : NULL;
  1311. } else {
  1312. l = NULL;
  1313. }
  1314. ss7_grab(p, p->ss7);
  1315. if (p->call_level != SIG_SS7_CALL_LEVEL_ALLOCATED) {
  1316. /* Call collision before sending IAM. Abort call. */
  1317. ss7_rel(p->ss7);
  1318. return -1;
  1319. }
  1320. p->ss7call = isup_new_call(p->ss7->ss7);
  1321. if (!p->ss7call) {
  1322. ss7_rel(p->ss7);
  1323. ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
  1324. return -1;
  1325. }
  1326. called_nai_strip = 0;
  1327. ss7_called_nai = p->ss7->called_nai;
  1328. if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
  1329. if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
  1330. called_nai_strip = strlen(p->ss7->internationalprefix);
  1331. ss7_called_nai = SS7_NAI_INTERNATIONAL;
  1332. } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
  1333. called_nai_strip = strlen(p->ss7->nationalprefix);
  1334. ss7_called_nai = SS7_NAI_NATIONAL;
  1335. } else {
  1336. ss7_called_nai = SS7_NAI_SUBSCRIBER;
  1337. }
  1338. }
  1339. isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
  1340. calling_nai_strip = 0;
  1341. ss7_calling_nai = p->ss7->calling_nai;
  1342. if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
  1343. if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
  1344. calling_nai_strip = strlen(p->ss7->internationalprefix);
  1345. ss7_calling_nai = SS7_NAI_INTERNATIONAL;
  1346. } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
  1347. calling_nai_strip = strlen(p->ss7->nationalprefix);
  1348. ss7_calling_nai = SS7_NAI_NATIONAL;
  1349. } else {
  1350. ss7_calling_nai = SS7_NAI_SUBSCRIBER;
  1351. }
  1352. }
  1353. isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
  1354. p->use_callingpres ? cid_pres2ss7pres(ast->connected.id.number.presentation) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
  1355. p->use_callingpres ? cid_pres2ss7screen(ast->connected.id.number.presentation) : SS7_SCREENING_USER_PROVIDED);
  1356. isup_set_oli(p->ss7call, ast->connected.ani2);
  1357. isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
  1358. /* Set the charge number if it is set */
  1359. charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
  1360. if (charge_str)
  1361. isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
  1362. gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
  1363. if (gen_address)
  1364. isup_set_gen_address(p->ss7call, gen_address, p->gen_add_nai,p->gen_add_pres_ind, p->gen_add_num_plan,p->gen_add_type); /* need to add some types here for NAI,PRES,TYPE */
  1365. gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
  1366. gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
  1367. gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
  1368. if (gen_digits)
  1369. isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
  1370. gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
  1371. if (gen_name)
  1372. isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
  1373. jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
  1374. if (jip_digits)
  1375. isup_set_jip_digits(p->ss7call, jip_digits);
  1376. lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
  1377. if (lspi_ident)
  1378. isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
  1379. rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
  1380. if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 )) {
  1381. isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00); /* Setting for Nortel DMS-250/500 */
  1382. }
  1383. call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
  1384. call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
  1385. if (call_ref_id && call_ref_pc) {
  1386. isup_set_callref(p->ss7call, atoi(call_ref_id),
  1387. call_ref_pc ? atoi(call_ref_pc) : 0);
  1388. }
  1389. send_far = pbx_builtin_getvar_helper(ast, "SS7_SEND_FAR");
  1390. if ((send_far) && ((strncmp("NO", send_far, strlen(send_far))) != 0 ))
  1391. (isup_far(p->ss7->ss7, p->ss7call));
  1392. p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
  1393. isup_iam(p->ss7->ss7, p->ss7call);
  1394. sig_ss7_set_dialing(p, 1);
  1395. ast_setstate(ast, AST_STATE_DIALING);
  1396. ss7_rel(p->ss7);
  1397. return 0;
  1398. }
  1399. /*!
  1400. * \brief SS7 hangup channel.
  1401. * \since 1.8
  1402. *
  1403. * \param p Signaling private structure pointer.
  1404. * \param ast Asterisk channel structure pointer.
  1405. *
  1406. * \retval 0 on success.
  1407. * \retval -1 on error.
  1408. */
  1409. int sig_ss7_hangup(struct sig_ss7_chan *p, struct ast_channel *ast)
  1410. {
  1411. int res = 0;
  1412. if (!ast->tech_pvt) {
  1413. ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
  1414. return 0;
  1415. }
  1416. p->owner = NULL;
  1417. sig_ss7_set_dialing(p, 0);
  1418. sig_ss7_set_outgoing(p, 0);
  1419. p->progress = 0;
  1420. p->rlt = 0;
  1421. p->exten[0] = '\0';
  1422. /* Perform low level hangup if no owner left */
  1423. ss7_grab(p, p->ss7);
  1424. p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
  1425. if (p->ss7call) {
  1426. if (!p->alreadyhungup) {
  1427. const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
  1428. int icause = ast->hangupcause ? ast->hangupcause : -1;
  1429. if (cause) {
  1430. if (atoi(cause)) {
  1431. icause = atoi(cause);
  1432. }
  1433. }
  1434. isup_rel(p->ss7->ss7, p->ss7call, icause);
  1435. p->alreadyhungup = 1;
  1436. }
  1437. }
  1438. ss7_rel(p->ss7);
  1439. return res;
  1440. }
  1441. /*!
  1442. * \brief SS7 answer channel.
  1443. * \since 1.8
  1444. *
  1445. * \param p Signaling private structure pointer.
  1446. * \param ast Asterisk channel structure pointer.
  1447. *
  1448. * \retval 0 on success.
  1449. * \retval -1 on error.
  1450. */
  1451. int sig_ss7_answer(struct sig_ss7_chan *p, struct ast_channel *ast)
  1452. {
  1453. int res;
  1454. ss7_grab(p, p->ss7);
  1455. if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
  1456. p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
  1457. }
  1458. res = isup_anm(p->ss7->ss7, p->ss7call);
  1459. ss7_rel(p->ss7);
  1460. return res;
  1461. }
  1462. /*!
  1463. * \brief Fix up a channel: If a channel is consumed, this is called. Basically update any ->owner links.
  1464. * \since 1.8
  1465. *
  1466. * \param oldchan Old channel pointer to replace.
  1467. * \param newchan New channel pointer to set.
  1468. * \param pchan Signaling private structure pointer.
  1469. *
  1470. * \return Nothing
  1471. */
  1472. void sig_ss7_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_ss7_chan *pchan)
  1473. {
  1474. if (pchan->owner == oldchan) {
  1475. pchan->owner = newchan;
  1476. }
  1477. }
  1478. /*!
  1479. * \brief SS7 answer channel.
  1480. * \since 1.8
  1481. *
  1482. * \param p Signaling private structure pointer.
  1483. * \param chan Asterisk channel structure pointer.
  1484. * \param condition AST control frame subtype.
  1485. * \param data AST control frame payload contents.
  1486. * \param datalen Length of payload contents.
  1487. *
  1488. * \retval 0 on success.
  1489. * \retval -1 on error or indication condition not handled.
  1490. */
  1491. int sig_ss7_indicate(struct sig_ss7_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
  1492. {
  1493. int res = -1;
  1494. switch (condition) {
  1495. case AST_CONTROL_BUSY:
  1496. res = sig_ss7_play_tone(p, SIG_SS7_TONE_BUSY);
  1497. break;
  1498. case AST_CONTROL_RINGING:
  1499. ss7_grab(p, p->ss7);
  1500. if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
  1501. p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
  1502. if ((isup_far(p->ss7->ss7, p->ss7call)) != -1) {
  1503. p->rlt = 1;
  1504. }
  1505. /* No need to send CPG if call will be RELEASE */
  1506. if (p->rlt != 1) {
  1507. isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_ALERTING);
  1508. }
  1509. }
  1510. ss7_rel(p->ss7);
  1511. res = sig_ss7_play_tone(p, SIG_SS7_TONE_RINGTONE);
  1512. if (chan->_state != AST_STATE_UP && chan->_state != AST_STATE_RING) {
  1513. ast_setstate(chan, AST_STATE_RINGING);
  1514. }
  1515. break;
  1516. case AST_CONTROL_PROCEEDING:
  1517. ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
  1518. ss7_grab(p, p->ss7);
  1519. /* This IF sends the FAR for an answered ALEG call */
  1520. if (chan->_state == AST_STATE_UP && (p->rlt != 1)){
  1521. if ((isup_far(p->ss7->ss7, p->ss7call)) != -1) {
  1522. p->rlt = 1;
  1523. }
  1524. }
  1525. if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING && !p->outgoing) {
  1526. p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
  1527. isup_acm(p->ss7->ss7, p->ss7call);
  1528. }
  1529. ss7_rel(p->ss7);
  1530. /* don't continue in ast_indicate */
  1531. res = 0;
  1532. break;
  1533. case AST_CONTROL_PROGRESS:
  1534. ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
  1535. ss7_grab(p, p->ss7);
  1536. if (!p->progress && p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
  1537. p->progress = 1;/* No need to send inband-information progress again. */
  1538. isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_INBANDINFO);
  1539. ss7_rel(p->ss7);
  1540. /* enable echo canceler here on SS7 calls */
  1541. sig_ss7_set_echocanceller(p, 1);
  1542. } else {
  1543. ss7_rel(p->ss7);
  1544. }
  1545. /* don't continue in ast_indicate */
  1546. res = 0;
  1547. break;
  1548. case AST_CONTROL_INCOMPLETE:
  1549. /* If the channel is connected, wait for additional input */
  1550. if (p->call_level == SIG_SS7_CALL_LEVEL_CONNECT) {
  1551. res = 0;
  1552. break;
  1553. }
  1554. chan->hangupcause = AST_CAUSE_INVALID_NUMBER_FORMAT;
  1555. break;
  1556. case AST_CONTROL_CONGESTION:
  1557. chan->hangupcause = AST_CAUSE_CONGESTION;
  1558. break;
  1559. case AST_CONTROL_HOLD:
  1560. ast_moh_start(chan, data, p->mohinterpret);
  1561. break;
  1562. case AST_CONTROL_UNHOLD:
  1563. ast_moh_stop(chan);
  1564. break;
  1565. case AST_CONTROL_SRCUPDATE:
  1566. res = 0;
  1567. break;
  1568. case -1:
  1569. res = sig_ss7_play_tone(p, -1);
  1570. break;
  1571. }
  1572. return res;
  1573. }
  1574. /*!
  1575. * \brief SS7 channel request.
  1576. * \since 1.8
  1577. *
  1578. * \param p Signaling private structure pointer.
  1579. * \param law Companding law preferred
  1580. * \param requestor Asterisk channel requesting a channel to dial (Can be NULL)
  1581. * \param transfercapability
  1582. *
  1583. * \retval ast_channel on success.
  1584. * \retval NULL on error.
  1585. */
  1586. struct ast_channel *sig_ss7_request(struct sig_ss7_chan *p, enum sig_ss7_law law, const struct ast_channel *requestor, int transfercapability)
  1587. {
  1588. struct ast_channel *ast;
  1589. sig_ss7_set_outgoing(p, 1);
  1590. ast = sig_ss7_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability, p->exten, requestor);
  1591. if (!ast) {
  1592. sig_ss7_set_outgoing(p, 0);
  1593. /* Release the allocated channel. Only have to deal with the linkset lock. */
  1594. ast_mutex_lock(&p->ss7->lock);
  1595. p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
  1596. ast_mutex_unlock(&p->ss7->lock);
  1597. }
  1598. return ast;
  1599. }
  1600. /*!
  1601. * \brief Delete the sig_ss7 private channel structure.
  1602. * \since 1.8
  1603. *
  1604. * \param doomed sig_ss7 private channel structure to delete.
  1605. *
  1606. * \return Nothing
  1607. */
  1608. void sig_ss7_chan_delete(struct sig_ss7_chan *doomed)
  1609. {
  1610. ast_free(doomed);
  1611. }
  1612. #define SIG_SS7_SC_HEADER "%-4s %4s %-4s %-3s %-3s %-10s %-4s %s\n"
  1613. #define SIG_SS7_SC_LINE "%4d %4d %-4s %-3s %-3s %-10s %-4s %s"
  1614. void sig_ss7_cli_show_channels_header(int fd)
  1615. {
  1616. ast_cli(fd, SIG_SS7_SC_HEADER, "link", "", "Chan", "Lcl", "Rem", "Call", "SS7", "Channel");
  1617. ast_cli(fd, SIG_SS7_SC_HEADER, "set", "Chan", "Idle", "Blk", "Blk", "Level", "Call", "Name");
  1618. }
  1619. void sig_ss7_cli_show_channels(int fd, struct sig_ss7_linkset *linkset)
  1620. {
  1621. char line[256];
  1622. int idx;
  1623. struct sig_ss7_chan *pvt;
  1624. ast_mutex_lock(&linkset->lock);
  1625. for (idx = 0; idx < linkset->numchans; ++idx) {
  1626. if (!linkset->pvts[idx]) {
  1627. continue;
  1628. }
  1629. pvt = linkset->pvts[idx];
  1630. sig_ss7_lock_private(pvt);
  1631. sig_ss7_lock_owner(linkset, idx);
  1632. snprintf(line, sizeof(line), SIG_SS7_SC_LINE,
  1633. linkset->span,
  1634. pvt->channel,
  1635. sig_ss7_is_chan_available(pvt) ? "Yes" : "No",
  1636. pvt->locallyblocked ? "Yes" : "No",
  1637. pvt->remotelyblocked ? "Yes" : "No",
  1638. sig_ss7_call_level2str(pvt->call_level),
  1639. pvt->ss7call ? "Yes" : "No",
  1640. pvt->owner ? pvt->owner->name : "");
  1641. if (pvt->owner) {
  1642. ast_channel_unlock(pvt->owner);
  1643. }
  1644. sig_ss7_unlock_private(pvt);
  1645. ast_mutex_unlock(&linkset->lock);
  1646. ast_cli(fd, "%s\n", line);
  1647. ast_mutex_lock(&linkset->lock);
  1648. }
  1649. ast_mutex_unlock(&linkset->lock);
  1650. }
  1651. /*!
  1652. * \brief Create a new sig_ss7 private channel structure.
  1653. * \since 1.8
  1654. *
  1655. * \param pvt_data Upper layer private data structure.
  1656. * \param callback Callbacks to the upper layer.
  1657. * \param ss7 Controlling linkset for the channel.
  1658. *
  1659. * \retval sig_ss7_chan on success.
  1660. * \retval NULL on error.
  1661. */
  1662. struct sig_ss7_chan *sig_ss7_chan_new(void *pvt_data, struct sig_ss7_callback *callback, struct sig_ss7_linkset *ss7)
  1663. {
  1664. struct sig_ss7_chan *pvt;
  1665. pvt = ast_calloc(1, sizeof(*pvt));
  1666. if (!pvt) {
  1667. return pvt;
  1668. }
  1669. pvt->calls = callback;
  1670. pvt->chan_pvt = pvt_data;
  1671. pvt->ss7 = ss7;
  1672. return pvt;
  1673. }
  1674. /*!
  1675. * \brief Initialize the SS7 linkset control.
  1676. * \since 1.8
  1677. *
  1678. * \param ss7 SS7 linkset control structure.
  1679. *
  1680. * \return Nothing
  1681. */
  1682. void sig_ss7_init_linkset(struct sig_ss7_linkset *ss7)
  1683. {
  1684. int idx;
  1685. memset(ss7, 0, sizeof(*ss7));
  1686. ast_mutex_init(&ss7->lock);
  1687. ss7->master = AST_PTHREADT_NULL;
  1688. for (idx = 0; idx < ARRAY_LEN(ss7->fds); ++idx) {
  1689. ss7->fds[idx] = -1;
  1690. }
  1691. }
  1692. /* ------------------------------------------------------------------- */
  1693. #endif /* defined(HAVE_SS7) */
  1694. /* end sig_ss7.c */