chan_skinny.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Implementation of the Skinny protocol
  5. *
  6. * Asterisk is Copyright (C) 1999-2003 Mark Spencer
  7. *
  8. * chan_skinny was developed by Jeremy McNamara & Florian Overkamp
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. *
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <asterisk/lock.h>
  18. #include <asterisk/channel.h>
  19. #include <asterisk/channel_pvt.h>
  20. #include <asterisk/config.h>
  21. #include <asterisk/logger.h>
  22. #include <asterisk/module.h>
  23. #include <asterisk/pbx.h>
  24. #include <asterisk/options.h>
  25. #include <asterisk/lock.h>
  26. #include <asterisk/sched.h>
  27. #include <asterisk/io.h>
  28. #include <asterisk/rtp.h>
  29. #include <asterisk/acl.h>
  30. #include <asterisk/callerid.h>
  31. #include <asterisk/cli.h>
  32. #include <asterisk/say.h>
  33. #include <asterisk/cdr.h>
  34. #include <asterisk/astdb.h>
  35. #include <asterisk/features.h>
  36. #include <asterisk/app.h>
  37. #include <asterisk/musiconhold.h>
  38. #include <asterisk/utils.h>
  39. #include <sys/socket.h>
  40. #include <netinet/in.h>
  41. #include <netinet/tcp.h>
  42. #include <sys/ioctl.h>
  43. #include <net/if.h>
  44. #include <errno.h>
  45. #include <unistd.h>
  46. #include <fcntl.h>
  47. #include <netdb.h>
  48. #include <arpa/inet.h>
  49. #include <sys/signal.h>
  50. #include <signal.h>
  51. #include <asterisk/dsp.h>
  52. #include <ctype.h>
  53. /************************************************************************************/
  54. /* Skinny/Asterisk Protocol Settings */
  55. /************************************************************************************/
  56. static char *desc = "Skinny Client Control Protocol (Skinny)";
  57. static char *tdesc = "Skinny Client Control Protocol (Skinny)";
  58. static char *type = "Skinny";
  59. static char *config = "skinny.conf";
  60. /* Just about everybody seems to support ulaw, so make it a nice default */
  61. static int capability = AST_FORMAT_ULAW;
  62. #define DEFAULT_SKINNY_PORT 2000
  63. #define DEFAULT_SKINNY_BACKLOG 2
  64. #define SKINNY_MAX_PACKET 1000
  65. static int keep_alive = 120;
  66. static char date_format[6] = "D-M-Y";
  67. static char version_id[16] = "P002F202";
  68. /* these should be in an include file, but dunno what to include */
  69. typedef unsigned char UINT8;
  70. typedef unsigned short UINT16;
  71. typedef unsigned int UINT32;
  72. /************************************************************************************/
  73. /* Protocol Messages */
  74. /************************************************************************************/
  75. /* message types */
  76. #define KEEP_ALIVE_MESSAGE 0x0000
  77. /* no additional struct */
  78. #define REGISTER_MESSAGE 0x0001
  79. typedef struct register_message {
  80. char name[16];
  81. int userId;
  82. int instance;
  83. char ip[4];
  84. int type;
  85. int maxStreams;
  86. } register_message;
  87. #define IP_PORT_MESSAGE 0x0002
  88. #define KEYPAD_BUTTON_MESSAGE 0x0003
  89. typedef struct keypad_button_message {
  90. int button;
  91. } keypad_button_message;
  92. #define STIMULUS_MESSAGE 0x0005
  93. typedef struct stimulus_message {
  94. int stimulus;
  95. int stimulusInstance;
  96. } stimulus_message;
  97. #define OFFHOOK_MESSAGE 0x0006
  98. #define ONHOOK_MESSAGE 0x0007
  99. #define CAPABILITIES_RES_MESSAGE 0x0010
  100. typedef struct station_capabilities {
  101. int codec;
  102. int frames;
  103. union {
  104. char res[8];
  105. long rate;
  106. } payloads;
  107. } station_capabilities;
  108. typedef struct capabilities_res_message {
  109. int count;
  110. struct station_capabilities caps[18];
  111. } capabilities_res_message;
  112. #define SPEED_DIAL_STAT_REQ_MESSAGE 0x000A
  113. typedef struct speed_dial_stat_req_message {
  114. int speedDialNumber;
  115. } speed_dial_stat_req_message;
  116. #define LINE_STATE_REQ_MESSAGE 0x000B
  117. typedef struct line_state_req_message {
  118. int lineNumber;
  119. } line_state_req_message;
  120. #define TIME_DATE_REQ_MESSAGE 0x000D
  121. #define VERSION_REQ_MESSAGE 0x000F
  122. #define BUTTON_TEMPLATE_REQ_MESSAGE 0x000E
  123. #define SERVER_REQUEST_MESSAGE 0x0012
  124. #define ALARM_MESSAGE 0x0020
  125. #define OPEN_RECIEVE_CHANNEL_ACK_MESSAGE 0x0022
  126. typedef struct open_recieve_channel_ack_message {
  127. int status;
  128. char ipAddr[4];
  129. int port;
  130. int passThruId;
  131. } open_recieve_channel_ack_message;
  132. #define SOFT_KEY_SET_REQ_MESSAGE 0x0025
  133. #define UNREGISTER_MESSAGE 0x0027
  134. #define SOFT_KEY_TEMPLATE_REQ_MESSAGE 0x0028
  135. #define REGISTER_ACK_MESSAGE 0x0081
  136. typedef struct register_ack_message {
  137. int keepAlive;
  138. char dateTemplate[6];
  139. char res[2];
  140. int secondaryKeepAlive;
  141. char res2[4];
  142. } register_ack_message;
  143. #define START_TONE_MESSAGE 0x0082
  144. typedef struct start_tone_message {
  145. int tone;
  146. } start_tone_message;
  147. #define STOP_TONE_MESSAGE 0x0083
  148. #define SET_RINGER_MESSAGE 0x0085
  149. typedef struct set_ringer_message {
  150. int ringerMode;
  151. } set_ringer_message;
  152. #define SET_LAMP_MESSAGE 0x0086
  153. typedef struct set_lamp_message {
  154. int stimulus;
  155. int stimulusInstance;
  156. int deviceStimulus;
  157. } set_lamp_message;
  158. #define SET_SPEAKER_MESSAGE 0x0088
  159. typedef struct set_speaker_message {
  160. int mode;
  161. } set_speaker_message;
  162. #define START_MEDIA_TRANSMISSION_MESSAGE 0x008A
  163. typedef struct media_qualifier {
  164. int precedence;
  165. int vad;
  166. int packets;
  167. int bitRate;
  168. } media_qualifier;
  169. typedef struct start_media_transmission_message {
  170. int conferenceId;
  171. int passThruPartyId;
  172. char remoteIp[4];
  173. int remotePort;
  174. int packetSize;
  175. int payloadType;
  176. media_qualifier qualifier;
  177. } start_media_transmission_message;
  178. #define STOP_MEDIA_TRANSMISSION_MESSAGE 0x008B
  179. typedef struct stop_media_transmission_message {
  180. int conferenceId;
  181. int passThruPartyId;
  182. } stop_media_transmission_message;
  183. #define CALL_INFO_MESSAGE 0x008F
  184. typedef struct call_info_message {
  185. char callingPartyName[40];
  186. char callingParty[24];
  187. char calledPartyName[40];
  188. char calledParty[24];
  189. int instance;
  190. int reference;
  191. int type;
  192. char originalCalledPartyName[40];
  193. char originalCalledParty[24];
  194. } call_info_message;
  195. #define SPEED_DIAL_STAT_RES_MESSAGE 0x0091
  196. typedef struct speed_dial_stat_res_message {
  197. int speedDialNumber;
  198. char speedDialDirNumber[24];
  199. char speedDialDisplayName[40];
  200. } speed_dial_stat_res_message;
  201. #define LINE_STAT_RES_MESSAGE 0x0092
  202. typedef struct line_stat_res_message {
  203. int linenumber;
  204. char lineDirNumber[24];
  205. char lineDisplayName[42];
  206. int space;
  207. } line_stat_res_message;
  208. #define DEFINETIMEDATE_MESSAGE 0x0094
  209. typedef struct definetimedate_message {
  210. int year; /* since 1900 */
  211. int month;
  212. int dayofweek; /* monday = 1 */
  213. int day;
  214. int hour;
  215. int minute;
  216. int seconds;
  217. int milliseconds;
  218. int timestamp;
  219. } definetimedate_message;
  220. #define DISPLAYTEXT_MESSAGE 0x0099
  221. typedef struct displaytext_message {
  222. char text[40];
  223. } displaytext_message;
  224. #define REGISTER_REJ_MESSAGE 0x009D
  225. typedef struct register_rej_message {
  226. char errMsg[33];
  227. } register_rej_message;
  228. #define CAPABILITIES_REQ_MESSAGE 0x009B
  229. #define SERVER_RES_MESSAGE 0x009E
  230. typedef struct server_identifier {
  231. char serverName[48];
  232. } server_identifier;
  233. typedef struct server_res_message {
  234. server_identifier server[5];
  235. int serverListenPort[5];
  236. int serverIpAddr[5];
  237. } server_res_message;
  238. #define BUTTON_TEMPLATE_RES_MESSAGE 0x0097
  239. static const char *button_definition_hack = {
  240. "\x01\x09\x01\x02\x02\x02\x03\x02\x04\x02\x05\x02\x06\x02\x07\x02"
  241. "\x08\x02\x09\x02\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff"
  242. "\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff"
  243. "\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff"
  244. "\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff"
  245. "\x00\xff\x00\xff"
  246. };
  247. typedef struct buttondefinition {
  248. UINT8 instanceNumber;
  249. UINT8 buttonDefinition;
  250. } button_definition;
  251. typedef struct button_template_res_message {
  252. UINT32 buttonOffset;
  253. UINT32 buttonCount;
  254. UINT32 totalButtonCount;
  255. button_definition definition[42];
  256. } button_template_res_message;
  257. #define VERSION_RES_MESSAGE 0x0098
  258. typedef struct version_res_message {
  259. char version[16];
  260. } version_res_message;
  261. #define KEEP_ALIVE_ACK_MESSAGE 0x0100
  262. #define OPEN_RECIEVE_CHANNEL_MESSAGE 0x0105
  263. typedef struct open_recieve_channel_message {
  264. int conferenceId;
  265. int partyId;
  266. int packets;
  267. int capability;
  268. int echo;
  269. int bitrate;
  270. } open_recieve_channel_message;
  271. #define CLOSE_RECIEVE_CHANNEL_MESSAGE 0x0106
  272. typedef struct close_recieve_channel_message {
  273. int conferenceId;
  274. int partyId;
  275. } close_recieve_channel_message;
  276. #define SOFT_KEY_TEMPLATE_RES_MESSAGE 0x0108
  277. static const char *soft_key_template_hack = {
  278. "\x52\x65\x64\x69\x61\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  279. "\x01\x00\x00\x00\x4e\x65\x77\x43\x61\x6c\6c\\x00\x00\x00\x00\x00"
  280. "\x00\x00\x00\x00\x02\x00\x00\x00\x48\x6f\x6c\x64\x00\x00\x00\x00"
  281. "\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x54\x72\x6e\x73"
  282. "\x66\x65\x72\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00"
  283. "\x43\x46\x77\x64\x41\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05"
  284. "\x00\x00\x00\x43\x46\x77\x64\x20\x42\x75\x73\x79\x00\x00\x00\x00"
  285. "\x00\x00\x00\x06\x00\x00\x00\x43\x46\x77\x64\x4e\x6f\x41\x6e\x73"
  286. "\x77\x65\x72\x00\x00\x00\x00\x07\x00\x00\x00\x3c\x3c\x00\x00\x00"
  287. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x45"
  288. "\x6e\x64\x43\x61\x6c\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x09"
  289. "\x00\x00\x00\x52\x65\x73\x75\x6d\x65\x00\x00\x00\x00\x00\x00\x00"
  290. "\x00\x00\x0a\x00\x00\x00\x41\x6e\x73\x77\x65\x72\x00\x00\x00\x00"
  291. "\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00\x49\x6e\x66\x6f\x00\x00"
  292. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x43\x6f"
  293. "\x6e\x66\x72\x6e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0d\x00\x00"
  294. "\x00\x50\x61\x72\x6b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  295. "\x00\x0e\x00\x00\x00\x4a\x6f\x69\x6e\x00\x00\x00\x00\x00\x00\x00"
  296. "\x00\x00\x00\x00\x0f\x00\x00\x00\x4d\x65\x65\x74\x4d\x65\x00\x00"
  297. "\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x50\x69\x63\x6b"
  298. "\x55\x70\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00"
  299. "\x47\x50\x69\x63\x6b\x55\x70\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  300. "\x12\x00\x00\x00\x52\x6d\x4c\x73\x43\x00\x00\x00\x00\x00\x00\x00"
  301. "\x00\x00\x00\x13\x00\x00\x00\x42\x61\x72\x67\x65\x00\x00\x00\x00"
  302. "\x00\x00\x00\x00\x00\x00\x00\x14\x00\x00\x00\x42\x61\x72\x67\x65"
  303. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x15\x00\x00\x00"
  304. };
  305. typedef struct soft_key_template_definition {
  306. char softKeyLabel[16];
  307. int softKeyEvent;
  308. } soft_key_template_definition;
  309. typedef struct soft_key_template {
  310. int softKeyOffset;
  311. int softKeyCount;
  312. int totalSoftKeyCount;
  313. soft_key_template_definition softKeyTemplateDefinition[32];
  314. } soft_key_template;
  315. #define SOFT_KEY_SET_RES_MESSAGE 0x0109
  316. static const char *soft_key_set_hack = {
  317. "\x01\x02\x05\x03\x09\x0a\x0b\x10\x11\x12\x04\x0e\x0d\x00\x00\x00"
  318. "\x2d\x01\x2e\x01\x31\x01\x2f\x01\x35\x01\x36\x01\x37\x01\x3c\x01"
  319. "\x3d\x01\x3e\x01\x30\x01\x3a\x01\x39\x01\x00\x00\x00\x00\x00\x00"
  320. "\x03\x09\x04\x0e\x0d\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  321. "\x2f\x01\x35\x01\x30\x01\x3a\x01\x39\x01\x3f\x01\x00\x00\x00\x00"
  322. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  323. "\x0a\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  324. "\x36\x01\x2e\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  325. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  326. "\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  327. "\x37\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  328. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  329. "\x01\x09\x05\x10\x11\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  330. "\x2d\x01\x35\x01\x31\x01\x3c\x01\x3d\x01\x3e\x01\x00\x00\x00\x00"
  331. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  332. "\x00\x09\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  333. "\x00\x00\x35\x01\x30\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  334. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  335. "\x08\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  336. "\x34\x01\x35\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  337. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  338. "\x00\x09\x0d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  339. "\x00\x00\x35\x01\x39\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  340. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  341. "\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  342. "\x00\x00\x35\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  343. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  344. "\x01\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  345. "\x2d\x01\x35\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  346. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  347. "\x15\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  348. "\x41\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  349. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  350. };
  351. typedef struct soft_key_set_definition {
  352. UINT8 softKeyTemplateIndex[16];
  353. UINT16 softKeyInfoIndex[16];
  354. } soft_key_set_definition;
  355. typedef struct soft_key_sets {
  356. UINT32 softKeySetOffset;
  357. UINT32 softKeySetCount;
  358. UINT32 totalSoftKeySetCount;
  359. soft_key_set_definition softKeySetDefinition[16];
  360. UINT32 res;
  361. } soft_key_sets;
  362. #define SELECT_SOFT_KEYS_MESSAGE 0x0110
  363. typedef struct select_soft_keys_message {
  364. int instance;
  365. int reference;
  366. int softKeySetIndex;
  367. int validKeyMask;
  368. } select_soft_keys_message;
  369. #define CALL_STATE_MESSAGE 0x0111
  370. typedef struct call_state_message {
  371. int callState;
  372. int lineInstance;
  373. int callReference;
  374. } call_state_message;
  375. #define ACTIVATE_CALL_PLANE_MESSAGE 0x0116
  376. typedef struct activate_call_plane_message {
  377. int lineInstance;
  378. } activate_call_plane_message;
  379. /* packet composition */
  380. typedef struct {
  381. int len;
  382. int res;
  383. int e;
  384. union {
  385. speed_dial_stat_req_message speeddialreq;
  386. register_message reg;
  387. register_ack_message regack;
  388. register_rej_message regrej;
  389. capabilities_res_message caps;
  390. version_res_message version;
  391. button_template_res_message buttontemplate;
  392. displaytext_message displaytext;
  393. definetimedate_message definetimedate;
  394. start_tone_message starttone;
  395. speed_dial_stat_res_message speeddial;
  396. line_state_req_message line;
  397. line_stat_res_message linestat;
  398. soft_key_sets softkeysets;
  399. soft_key_template softkeytemplate;
  400. server_res_message serverres;
  401. set_lamp_message setlamp;
  402. set_ringer_message setringer;
  403. call_state_message callstate;
  404. keypad_button_message keypad;
  405. select_soft_keys_message selectsoftkey;
  406. activate_call_plane_message activatecallplane;
  407. stimulus_message stimulus;
  408. set_speaker_message setspeaker;
  409. call_info_message callinfo;
  410. start_media_transmission_message startmedia;
  411. stop_media_transmission_message stopmedia;
  412. open_recieve_channel_message openrecievechannel;
  413. open_recieve_channel_ack_message openrecievechannelack;
  414. close_recieve_channel_message closerecievechannel;
  415. } data;
  416. } skinny_req;
  417. /************************************************************************************/
  418. /* Asterisk specific globals */
  419. /************************************************************************************/
  420. static int skinnydebug = 1; /* XXX for now, enable debugging default */
  421. /* a hostname, portnumber, socket and such is usefull for VoIP protocols */
  422. static struct sockaddr_in bindaddr;
  423. static char ourhost[256];
  424. static int ourport;
  425. static struct in_addr __ourip;
  426. struct ast_hostent ahp; struct hostent *hp;
  427. static int skinnysock = -1;
  428. static pthread_t tcp_thread;
  429. static pthread_t accept_t;
  430. static char context[AST_MAX_EXTENSION] = "default";
  431. static char language[MAX_LANGUAGE] = "";
  432. static char musicclass[MAX_LANGUAGE] = "";
  433. static char callerid[AST_MAX_EXTENSION] = "";
  434. static char linelabel[AST_MAX_EXTENSION] ="";
  435. static int nat = 0;
  436. static unsigned int cur_callergroup = 0;
  437. static unsigned int cur_pickupgroup = 0;
  438. static int immediate = 0;
  439. static int callwaiting = 0;
  440. static int callreturn = 0;
  441. static int threewaycalling = 0;
  442. /* This is for flashhook transfers */
  443. static int transfer = 0;
  444. static int cancallforward = 0;
  445. /* static int busycount = 3;*/
  446. static char accountcode[20] = "";
  447. static char mailbox[AST_MAX_EXTENSION];
  448. static int amaflags = 0;
  449. static int callnums = 1;
  450. #define SUB_REAL 0
  451. #define SUB_ALT 1
  452. #define MAX_SUBS 2
  453. #define SKINNY_SPEAKERON 1
  454. #define SKINNY_SPEAKEROFF 2
  455. #define SKINNY_OFFHOOK 1
  456. #define SKINNY_ONHOOK 2
  457. #define SKINNY_RINGOUT 3
  458. #define SKINNY_RINGIN 4
  459. #define SKINNY_CONNECTED 5
  460. #define SKINNY_BUSY 6
  461. #define SKINNY_CONGESTION 7
  462. #define SKINNY_HOLD 8
  463. #define SKINNY_CALLWAIT 9
  464. #define SKINNY_TRANSFER 10
  465. #define SKINNY_PARK 11
  466. #define SKINNY_PROGRESS 12
  467. #define SKINNY_INVALID 14
  468. #define SKINNY_SILENCE 0
  469. #define SKINNY_DIALTONE 33
  470. #define SKINNY_BUSYTONE 35
  471. #define SKINNY_ALERT 36
  472. #define SKINNY_REORDER 37
  473. #define SKINNY_CALLWAITTONE 45
  474. #define SKINNY_LAMP_OFF 1
  475. #define SKINNY_LAMP_ON 2
  476. #define SKINNY_LAMP_WINK 3
  477. #define SKINNY_LAMP_FLASH 4
  478. #define SKINNY_LAMP_BLINK 5
  479. #define SKINNY_RING_OFF 1
  480. #define SKINNY_RING_INSIDE 2
  481. #define SKINNY_RING_OUTSIDE 3
  482. #define SKINNY_RING_FEATURE 4
  483. #define TYPE_TRUNK 1
  484. #define TYPE_LINE 2
  485. #define STIMULUS_REDIAL 1
  486. #define STIMULUS_SPEEDDIAL 2
  487. #define STIMULUS_HOLD 3
  488. #define STIMULUS_TRANSFER 4
  489. #define STIMULUS_FORWARDALL 5
  490. #define STIMULUS_FORWARDBUSY 6
  491. #define STIMULUS_FORWARDNOANSWER 7
  492. #define STIMULUS_DISPLAY 8
  493. #define STIMULUS_LINE 9
  494. /* Skinny rtp stream modes. Do we really need this? */
  495. #define SKINNY_CX_SENDONLY 0
  496. #define SKINNY_CX_RECVONLY 1
  497. #define SKINNY_CX_SENDRECV 2
  498. #define SKINNY_CX_CONF 3
  499. #define SKINNY_CX_CONFERENCE 3
  500. #define SKINNY_CX_MUTE 4
  501. #define SKINNY_CX_INACTIVE 4
  502. #if 0
  503. static char *skinny_cxmodes[] = {
  504. "sendonly",
  505. "recvonly",
  506. "sendrecv",
  507. "confrnce",
  508. "inactive"
  509. };
  510. #endif
  511. /* driver scheduler */
  512. static struct sched_context *sched;
  513. static struct io_context *io;
  514. /* usage count and locking */
  515. static int usecnt = 0;
  516. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  517. /* Protect the monitoring thread, so only one process can kill or start it, and not
  518. when it's doing something critical. */
  519. AST_MUTEX_DEFINE_STATIC(monlock);
  520. /* Protect the network socket */
  521. AST_MUTEX_DEFINE_STATIC(netlock);
  522. /* Protect the session list */
  523. AST_MUTEX_DEFINE_STATIC(sessionlock);
  524. /* Protect the device list */
  525. AST_MUTEX_DEFINE_STATIC(devicelock);
  526. /* This is the thread for the monitor which checks for input on the channels
  527. which are not currently in use. */
  528. static pthread_t monitor_thread = AST_PTHREADT_NULL;
  529. /* Wait up to 16 seconds for first digit */
  530. static int firstdigittimeout = 16000;
  531. /* How long to wait for following digits */
  532. static int gendigittimeout = 8000;
  533. /* How long to wait for an extra digit, if there is an ambiguous match */
  534. static int matchdigittimeout = 3000;
  535. struct skinny_subchannel {
  536. ast_mutex_t lock;
  537. unsigned int callid;
  538. struct ast_channel *owner;
  539. struct skinny_line *parent;
  540. struct ast_rtp *rtp;
  541. time_t lastouttime;
  542. int progress;
  543. int ringing;
  544. int lastout;
  545. int cxmode;
  546. int nat;
  547. int outgoing;
  548. int alreadygone;
  549. struct skinny_subchannel *next;
  550. };
  551. struct skinny_line {
  552. ast_mutex_t lock;
  553. char name[80];
  554. char label[42]; /* Label that shows next to the line buttons */
  555. struct skinny_subchannel *sub; /* pointer to our current connection, channel and stuff */
  556. char accountcode[80];
  557. char exten[AST_MAX_EXTENSION]; /* Extention where to start */
  558. char context[AST_MAX_EXTENSION];
  559. char language[MAX_LANGUAGE];
  560. char callerid[AST_MAX_EXTENSION]; /* Caller*ID */
  561. char lastcallerid[AST_MAX_EXTENSION]; /* Last Caller*ID */
  562. char call_forward[AST_MAX_EXTENSION];
  563. char mailbox[AST_MAX_EXTENSION];
  564. char musicclass[MAX_LANGUAGE];
  565. int curtone; /* Current tone being played */
  566. unsigned int callgroup;
  567. unsigned int pickupgroup;
  568. int callwaiting;
  569. int transfer;
  570. int threewaycalling;
  571. int cancallforward;
  572. int callreturn;
  573. int dnd; /* How does this affect callwait? Do we just deny a skinny_request if we're dnd? */
  574. int hascallerid;
  575. int hidecallerid;
  576. int amaflags;
  577. int type;
  578. int instance;
  579. int group;
  580. int needdestroy;
  581. int capability;
  582. int nonCodecCapability;
  583. int onhooktime;
  584. int msgstate; /* voicemail message state */
  585. int immediate;
  586. int hookstate;
  587. int progress;
  588. struct skinny_line *next;
  589. struct skinny_device *parent;
  590. };
  591. static struct skinny_device {
  592. /* A device containing one or more lines */
  593. char name[80];
  594. char id[16];
  595. char version_id[16];
  596. int type;
  597. int registered;
  598. struct sockaddr_in addr;
  599. struct in_addr ourip;
  600. struct skinny_line *lines;
  601. struct ast_ha *ha;
  602. struct skinnysession *session;
  603. struct skinny_device *next;
  604. } *devices = NULL;
  605. static struct skinnysession {
  606. pthread_t t;
  607. ast_mutex_t lock;
  608. struct sockaddr_in sin;
  609. int fd;
  610. char inbuf[SKINNY_MAX_PACKET];
  611. struct skinny_device *device;
  612. struct skinnysession *next;
  613. } *sessions = NULL;
  614. static skinny_req *req_alloc(size_t size)
  615. {
  616. skinny_req *req;
  617. req = malloc(size+12);
  618. if (!req) {
  619. return NULL;
  620. }
  621. memset(req, 0, size+12);
  622. return req;
  623. }
  624. static struct skinny_subchannel *find_subchannel_by_line(struct skinny_line *l)
  625. {
  626. /* XXX Need to figure out how to determine which sub we want */
  627. struct skinny_subchannel *sub = l->sub;
  628. return sub;
  629. }
  630. static struct skinny_subchannel *find_subchannel_by_name(char *dest)
  631. {
  632. struct skinny_line *l;
  633. struct skinny_device *d;
  634. char line[256];
  635. char *at;
  636. char *device;
  637. strncpy(line, dest, sizeof(line) - 1);
  638. at = strchr(line, '@');
  639. if (!at) {
  640. ast_log(LOG_NOTICE, "Device '%s' has no @ (at) sign!\n", dest);
  641. return NULL;
  642. }
  643. *at = '\0';
  644. at++;
  645. device = at;
  646. ast_mutex_lock(&devicelock);
  647. d = devices;
  648. while(d) {
  649. if (!strcasecmp(d->name, device)) {
  650. if (skinnydebug) {
  651. ast_verbose("Found device: %s\n", d->name);
  652. }
  653. /* Found the device */
  654. l = d->lines;
  655. while (l) {
  656. /* Search for the right line */
  657. if (!strcasecmp(l->name, line)) {
  658. ast_mutex_unlock(&devicelock);
  659. return l->sub;
  660. }
  661. l = l->next;
  662. }
  663. }
  664. d = d->next;
  665. }
  666. /* Device not found*/
  667. ast_mutex_unlock(&devicelock);
  668. return NULL;
  669. }
  670. static int transmit_response(struct skinnysession *s, skinny_req *req)
  671. {
  672. int res = 0;
  673. ast_mutex_lock(&s->lock);
  674. #if 0
  675. ast_verbose("writing packet type %d (%d bytes) to socket %d\n", req->e, req->len+8, s->fd);
  676. #endif
  677. res = write(s->fd, req, req->len+8);
  678. if (res != req->len+8) {
  679. ast_log(LOG_WARNING, "Transmit: write only sent %d out of %d bytes: %s\n", res, req->len+8, strerror(errno));
  680. }
  681. ast_mutex_unlock(&s->lock);
  682. return 1;
  683. }
  684. /* XXX Do this right */
  685. static int convert_cap(int capability)
  686. {
  687. return 4; /* ulaw (this is not the same as asterisk's '4' */
  688. }
  689. static void transmit_speaker_mode(struct skinnysession *s, int mode)
  690. {
  691. skinny_req *req;
  692. req = req_alloc(sizeof(struct set_speaker_message));
  693. if (!req) {
  694. ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
  695. return;
  696. }
  697. req->len = sizeof(set_speaker_message)+4;
  698. req->e = SET_SPEAKER_MESSAGE;
  699. req->data.setspeaker.mode = mode;
  700. transmit_response(s, req);
  701. }
  702. static void transmit_callstate(struct skinnysession *s, int instance, int state, unsigned callid)
  703. {
  704. skinny_req *req;
  705. int memsize = sizeof(struct call_state_message);
  706. req = req_alloc(memsize);
  707. if (!req) {
  708. ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
  709. return;
  710. }
  711. if (state == SKINNY_ONHOOK) {
  712. transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
  713. }
  714. req->len = sizeof(call_state_message)+4;
  715. req->e = CALL_STATE_MESSAGE;
  716. req->data.callstate.callState = state;
  717. req->data.callstate.lineInstance = instance;
  718. req->data.callstate.callReference = callid;
  719. transmit_response(s, req);
  720. if (state == SKINNY_OFFHOOK) {
  721. memset(req, 0, memsize);
  722. req->len = sizeof(activate_call_plane_message)+4;
  723. req->e = ACTIVATE_CALL_PLANE_MESSAGE;
  724. req->data.activatecallplane.lineInstance = instance;
  725. transmit_response(s, req);
  726. } else if (state == SKINNY_ONHOOK) {
  727. memset(req, 0, memsize);
  728. req->len = sizeof(activate_call_plane_message)+4;
  729. req->e = ACTIVATE_CALL_PLANE_MESSAGE;
  730. req->data.activatecallplane.lineInstance = 0;
  731. transmit_response(s, req);
  732. memset(req, 0, memsize);
  733. req->len = sizeof(close_recieve_channel_message)+4;
  734. req->e = CLOSE_RECIEVE_CHANNEL_MESSAGE;
  735. req->data.closerecievechannel.conferenceId = 0;
  736. req->data.closerecievechannel.partyId = 0;
  737. transmit_response(s, req);
  738. memset(req, 0, memsize);
  739. req->len = sizeof(stop_media_transmission_message)+4;
  740. req->e = STOP_MEDIA_TRANSMISSION_MESSAGE;
  741. req->data.stopmedia.conferenceId = 0;
  742. req->data.stopmedia.passThruPartyId = 0;
  743. transmit_response(s, req);
  744. }
  745. }
  746. static void transmit_connect(struct skinnysession *s)
  747. {
  748. skinny_req *req;
  749. struct skinny_line *l = s->device->lines;
  750. req = req_alloc(sizeof(struct open_recieve_channel_message));
  751. if (!req) {
  752. ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
  753. return;
  754. }
  755. req->len = sizeof(struct call_info_message);
  756. req->e = OPEN_RECIEVE_CHANNEL_MESSAGE;
  757. req->data.openrecievechannel.conferenceId = 0;
  758. req->data.openrecievechannel.partyId = 0;
  759. req->data.openrecievechannel.packets = 20;
  760. req->data.openrecievechannel.capability = convert_cap(l->capability);
  761. req->data.openrecievechannel.echo = 0;
  762. req->data.openrecievechannel.bitrate = 0;
  763. transmit_response(s, req);
  764. }
  765. static void transmit_tone(struct skinnysession *s, int tone)
  766. {
  767. skinny_req *req;
  768. if (tone > 0)
  769. req = req_alloc(sizeof(struct start_tone_message));
  770. else
  771. req = req_alloc(4);
  772. if (!req) {
  773. ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
  774. return;
  775. }
  776. if (tone > 0) {
  777. req->len = sizeof(start_tone_message)+4;
  778. req->e = START_TONE_MESSAGE;
  779. req->data.starttone.tone = tone;
  780. } else {
  781. req->len = 4;
  782. req->e = STOP_TONE_MESSAGE;
  783. }
  784. transmit_response(s, req);
  785. }
  786. #if 0
  787. /* XXX need to properly deal with softkeys */
  788. static void transmit_selectsoftkeys(struct skinnysession *s, int instance, int callid, int softkey)
  789. {
  790. skinny_req *req;
  791. int memsize = sizeof(struct select_soft_keys_message);
  792. req = req_alloc(memsize);
  793. if (!req) {
  794. ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
  795. return;
  796. }
  797. memset(req, 0, memsize);
  798. req->len = sizeof(select_soft_keys_message)+4;
  799. req->e = SELECT_SOFT_KEYS_MESSAGE;
  800. req->data.selectsoftkey.instance = instance;
  801. req->data.selectsoftkey.reference = callid;
  802. req->data.selectsoftkey.softKeySetIndex = softkey;
  803. transmit_response(s, req);
  804. }
  805. #endif
  806. static void transmit_lamp_indication(struct skinnysession *s, int instance, int indication)
  807. {
  808. skinny_req *req;
  809. req = req_alloc(sizeof(struct set_lamp_message));
  810. if (!req) {
  811. ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
  812. return;
  813. }
  814. req->len = sizeof(set_lamp_message)+4;
  815. req->e = SET_LAMP_MESSAGE;
  816. req->data.setlamp.stimulus = 0x9; /* magic number */
  817. req->data.setlamp.stimulusInstance = instance;
  818. req->data.setlamp.deviceStimulus = indication;
  819. transmit_response(s, req);
  820. }
  821. static void transmit_ringer_mode(struct skinnysession *s, int mode)
  822. {
  823. skinny_req *req;
  824. req = req_alloc(sizeof(struct set_ringer_message));
  825. if (!req) {
  826. ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
  827. return;
  828. }
  829. req->len = sizeof(set_ringer_message)+4;
  830. req->e = SET_RINGER_MESSAGE;
  831. req->data.setringer.ringerMode = mode;
  832. transmit_response(s, req);
  833. }
  834. /* I do not believe skinny can deal with video.
  835. Anyone know differently? */
  836. static struct ast_rtp *skinny_get_vrtp_peer(struct ast_channel *chan)
  837. {
  838. return NULL;
  839. }
  840. static struct ast_rtp *skinny_get_rtp_peer(struct ast_channel *chan)
  841. {
  842. struct skinny_subchannel *sub;
  843. sub = chan->pvt->pvt;
  844. if (sub && sub->rtp)
  845. return sub->rtp;
  846. return NULL;
  847. }
  848. static int skinny_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs)
  849. {
  850. struct skinny_subchannel *sub;
  851. sub = chan->pvt->pvt;
  852. if (sub) {
  853. /* transmit_modify_with_sdp(sub, rtp); @@FIXME@@ if needed */
  854. return 0;
  855. }
  856. return -1;
  857. }
  858. static struct ast_rtp_protocol skinny_rtp = {
  859. get_rtp_info: skinny_get_rtp_peer,
  860. get_vrtp_info: skinny_get_vrtp_peer,
  861. set_rtp_peer: skinny_set_rtp_peer,
  862. };
  863. static int skinny_do_debug(int fd, int argc, char *argv[])
  864. {
  865. if (argc != 2)
  866. return RESULT_SHOWUSAGE;
  867. skinnydebug = 1;
  868. ast_cli(fd, "Skinny Debugging Enabled\n");
  869. return RESULT_SUCCESS;
  870. }
  871. static int skinny_no_debug(int fd, int argc, char *argv[])
  872. {
  873. if (argc != 3)
  874. return RESULT_SHOWUSAGE;
  875. skinnydebug = 0;
  876. ast_cli(fd, "Skinny Debugging Disabled\n");
  877. return RESULT_SUCCESS;
  878. }
  879. static int skinny_show_lines(int fd, int argc, char *argv[])
  880. {
  881. struct skinny_device *d;
  882. struct skinny_line *l;
  883. int haslines = 0;
  884. char iabuf[INET_ADDRSTRLEN];
  885. if (argc != 3)
  886. return RESULT_SHOWUSAGE;
  887. ast_mutex_lock(&devicelock);
  888. d = devices;
  889. while(d) {
  890. l = d->lines;
  891. ast_cli(fd, "Device '%s' at %s\n", d->name, ast_inet_ntoa(iabuf, sizeof(iabuf), d->addr.sin_addr));
  892. while(l) {
  893. ast_cli(fd, " -- '%s@%s in '%s' is %s\n", l->name, d->name, l->context, l->sub->owner ? "active" : "idle");
  894. haslines = 1;
  895. l = l->next;
  896. }
  897. if (!haslines) {
  898. ast_cli(fd, " << No Lines Defined >> ");
  899. }
  900. d = d->next;
  901. }
  902. ast_mutex_unlock(&devicelock);
  903. return RESULT_SUCCESS;
  904. }
  905. static char show_lines_usage[] =
  906. "Usage: skinny show lines\n"
  907. " Lists all lines known to the Skinny subsystem.\n";
  908. static char debug_usage[] =
  909. "Usage: skinny debug\n"
  910. " Enables dumping of Skinny packets for debugging purposes\n";
  911. static char no_debug_usage[] =
  912. "Usage: skinny no debug\n"
  913. " Disables dumping of Skinny packets for debugging purposes\n";
  914. static struct ast_cli_entry cli_show_lines =
  915. { { "skinny", "show", "lines", NULL }, skinny_show_lines, "Show defined Skinny lines per device", show_lines_usage };
  916. static struct ast_cli_entry cli_debug =
  917. { { "skinny", "debug", NULL }, skinny_do_debug, "Enable Skinny debugging", debug_usage };
  918. static struct ast_cli_entry cli_no_debug =
  919. { { "skinny", "no", "debug", NULL }, skinny_no_debug, "Disable Skinny debugging", no_debug_usage };
  920. static struct skinny_device *build_device(char *cat, struct ast_variable *v)
  921. {
  922. struct skinny_device *d;
  923. struct skinny_line *l;
  924. struct skinny_subchannel *sub;
  925. int i=0, y=0;
  926. d = malloc(sizeof(struct skinny_device));
  927. if (d) {
  928. memset(d, 0, sizeof(struct skinny_device));
  929. strncpy(d->name, cat, sizeof(d->name) - 1);
  930. while(v) {
  931. if (!strcasecmp(v->name, "host")) {
  932. if (ast_get_ip(&d->addr, v->value)) {
  933. free(d);
  934. return NULL;
  935. }
  936. } else if (!strcasecmp(v->name, "port")) {
  937. d->addr.sin_port = htons(atoi(v->value));
  938. } else if (!strcasecmp(v->name, "device")) {
  939. strncpy(d->id, v->value, sizeof(d->id)-1);
  940. } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
  941. d->ha = ast_append_ha(v->name, v->value, d->ha);
  942. } else if (!strcasecmp(v->name, "context")) {
  943. strncpy(context, v->value, sizeof(context) - 1);
  944. } else if (!strcasecmp(v->name, "version")) {
  945. strncpy(d->version_id, v->value, sizeof(d->version_id) -1);
  946. } else if (!strcasecmp(v->name, "nat")) {
  947. nat = ast_true(v->value);
  948. } else if (!strcasecmp(v->name, "callerid")) {
  949. if (!strcasecmp(v->value, "asreceived")) {
  950. callerid[0] = '\0';
  951. } else {
  952. strncpy(callerid, v->value, sizeof(callerid) - 1);
  953. }
  954. } else if (!strcasecmp(v->name, "language")) {
  955. strncpy(language, v->value, sizeof(language)-1);
  956. } else if (!strcasecmp(v->name, "accountcode")) {
  957. strncpy(accountcode, v->value, sizeof(accountcode)-1);
  958. } else if (!strcasecmp(v->name, "amaflags")) {
  959. y = ast_cdr_amaflags2int(v->value);
  960. if (y < 0) {
  961. ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
  962. } else {
  963. amaflags = y;
  964. }
  965. } else if (!strcasecmp(v->name, "musiconhold")) {
  966. strncpy(musicclass, v->value, sizeof(musicclass)-1);
  967. } else if (!strcasecmp(v->name, "callgroup")) {
  968. cur_callergroup = ast_get_group(v->value);
  969. } else if (!strcasecmp(v->name, "pickupgroup")) {
  970. cur_pickupgroup = ast_get_group(v->value);
  971. } else if (!strcasecmp(v->name, "immediate")) {
  972. immediate = ast_true(v->value);
  973. } else if (!strcasecmp(v->name, "cancallforward")) {
  974. cancallforward = ast_true(v->value);
  975. } else if (!strcasecmp(v->name, "mailbox")) {
  976. strncpy(mailbox, v->value, sizeof(mailbox) -1);
  977. } else if (!strcasecmp(v->name, "callreturn")) {
  978. callreturn = ast_true(v->value);
  979. } else if (!strcasecmp(v->name, "immediate")) {
  980. immediate = ast_true(v->value);
  981. } else if (!strcasecmp(v->name, "callwaiting")) {
  982. callwaiting = ast_true(v->value);
  983. } else if (!strcasecmp(v->name, "transfer")) {
  984. transfer = ast_true(v->value);
  985. } else if (!strcasecmp(v->name, "threewaycalling")) {
  986. threewaycalling = ast_true(v->value);
  987. } else if (!strcasecmp(v->name, "linelabel")) {
  988. strncpy(linelabel, v->value, sizeof(linelabel)-1);
  989. } else if (!strcasecmp(v->name, "trunk") || !strcasecmp(v->name, "line")) {
  990. l = malloc(sizeof(struct skinny_line));;
  991. if (l) {
  992. memset(l, 0, sizeof(struct skinny_line));
  993. ast_mutex_init(&l->lock);
  994. strncpy(l->name, v->value, sizeof(l->name) - 1);
  995. /* XXX Should we check for uniqueness?? XXX */
  996. strncpy(l->context, context, sizeof(l->context) - 1);
  997. strncpy(l->callerid, callerid, sizeof(l->callerid) - 1);
  998. strncpy(l->label, linelabel, sizeof(l->label) - 1);
  999. strncpy(l->language, language, sizeof(l->language) - 1);
  1000. strncpy(l->musicclass, musicclass, sizeof(l->musicclass)-1);
  1001. strncpy(l->mailbox, mailbox, sizeof(l->mailbox)-1);
  1002. strncpy(l->mailbox, mailbox, sizeof(l->mailbox)-1);
  1003. if (!ast_strlen_zero(mailbox)) {
  1004. ast_verbose(VERBOSE_PREFIX_3 "Setting mailbox '%s' on %s@%s\n", mailbox, d->name, l->name);
  1005. }
  1006. l->msgstate = -1;
  1007. l->capability = capability;
  1008. l->parent = d;
  1009. if (!strcasecmp(v->name, "trunk")) {
  1010. l->type = TYPE_TRUNK;
  1011. } else {
  1012. l->type = TYPE_LINE;
  1013. }
  1014. l->immediate = immediate;
  1015. l->callgroup = cur_callergroup;
  1016. l->pickupgroup = cur_pickupgroup;
  1017. l->callreturn = callreturn;
  1018. l->cancallforward = cancallforward;
  1019. l->callwaiting = callwaiting;
  1020. l->transfer = transfer;
  1021. l->threewaycalling = threewaycalling;
  1022. l->onhooktime = time(NULL);
  1023. l->instance = 1;
  1024. /* ASSUME we're onhook at this point*/
  1025. l->hookstate = SKINNY_ONHOOK;
  1026. for (i = 0; i < MAX_SUBS; i++) {
  1027. sub = malloc(sizeof(struct skinny_subchannel));
  1028. if (sub) {
  1029. ast_verbose(VERBOSE_PREFIX_3 "Allocating Skinny subchannel '%d' on %s@%s\n", i, l->name, d->name);
  1030. memset(sub, 0, sizeof(struct skinny_subchannel));
  1031. ast_mutex_init(&sub->lock);
  1032. sub->parent = l;
  1033. /* Make a call*ID */
  1034. sub->callid = callnums;
  1035. callnums++;
  1036. sub->cxmode = SKINNY_CX_INACTIVE;
  1037. sub->nat = nat;
  1038. sub->next = l->sub;
  1039. l->sub = sub;
  1040. } else {
  1041. /* XXX Should find a way to clean up our memory */
  1042. ast_log(LOG_WARNING, "Out of memory allocating subchannel");
  1043. return NULL;
  1044. }
  1045. }
  1046. l->next = d->lines;
  1047. d->lines = l;
  1048. } else {
  1049. /* XXX Should find a way to clean up our memory */
  1050. ast_log(LOG_WARNING, "Out of memory allocating line");
  1051. return NULL;
  1052. }
  1053. } else {
  1054. ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name, v->lineno);
  1055. }
  1056. v = v->next;
  1057. }
  1058. if (!d->lines) {
  1059. ast_log(LOG_ERROR, "A Skinny device must have at least one line!\n");
  1060. return NULL;
  1061. }
  1062. if (d->addr.sin_addr.s_addr && !ntohs(d->addr.sin_port))
  1063. d->addr.sin_port = htons(DEFAULT_SKINNY_PORT);
  1064. if (d->addr.sin_addr.s_addr) {
  1065. if (ast_ouraddrfor(&d->addr.sin_addr, &d->ourip)) {
  1066. memcpy(&d->ourip, &__ourip, sizeof(d->ourip));
  1067. }
  1068. } else {
  1069. memcpy(&d->ourip, &__ourip, sizeof(d->ourip));
  1070. }
  1071. }
  1072. return d;
  1073. }
  1074. static int has_voicemail(struct skinny_line *l)
  1075. {
  1076. return ast_app_has_voicemail(l->mailbox);
  1077. }
  1078. static int skinny_register(skinny_req *req, struct skinnysession *s)
  1079. {
  1080. struct skinny_device *d;
  1081. ast_mutex_lock(&devicelock);
  1082. d = devices;
  1083. while (d) {
  1084. if (!strcasecmp(req->data.reg.name, d->id)) {
  1085. /* XXX Deal with IP authentication */
  1086. s->device = d;
  1087. d->type = req->data.reg.type;
  1088. if (ast_strlen_zero(d->version_id)) {
  1089. strncpy(d->version_id, version_id, sizeof(d->version_id) - 1);
  1090. }
  1091. d->registered = 1;
  1092. d->session = s;
  1093. break;
  1094. }
  1095. d = d->next;
  1096. }
  1097. ast_mutex_unlock(&devicelock);
  1098. if (!d)
  1099. return 0;
  1100. return 1;
  1101. }
  1102. static void start_rtp(struct skinny_subchannel *sub)
  1103. {
  1104. ast_mutex_lock(&sub->lock);
  1105. /* Allocate the RTP */
  1106. sub->rtp = ast_rtp_new(sched, io, 1, 0);
  1107. if (sub->rtp && sub->owner)
  1108. sub->owner->fds[0] = ast_rtp_fd(sub->rtp);
  1109. if (sub->rtp)
  1110. ast_rtp_setnat(sub->rtp, sub->nat);
  1111. /* Create the RTP connection */
  1112. transmit_connect(sub->parent->parent->session);
  1113. ast_mutex_unlock(&sub->lock);
  1114. }
  1115. static void *skinny_ss(void *data)
  1116. {
  1117. struct ast_channel *chan = data;
  1118. struct skinny_subchannel *sub = chan->pvt->pvt;
  1119. struct skinny_line *l = sub->parent;
  1120. struct skinnysession *s = l->parent->session;
  1121. char exten[AST_MAX_EXTENSION] = "";
  1122. int len = 0;
  1123. int timeout = firstdigittimeout;
  1124. int res;
  1125. int getforward=0;
  1126. if (option_verbose > 2) {
  1127. ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s@%s'\n", l->name, l->parent->name);
  1128. }
  1129. while(len < AST_MAX_EXTENSION-1) {
  1130. res = ast_waitfordigit(chan, timeout);
  1131. timeout = 0;
  1132. if (res < 0) {
  1133. if (skinnydebug) {
  1134. ast_verbose("Skinny(%s@%s): waitfordigit returned < 0\n", l->name, l->parent->name);
  1135. }
  1136. ast_indicate(chan, -1);
  1137. ast_hangup(chan);
  1138. return NULL;
  1139. } else if (res) {
  1140. exten[len++]=res;
  1141. exten[len] = '\0';
  1142. }
  1143. if (!ast_ignore_pattern(chan->context, exten)) {
  1144. transmit_tone(s, SKINNY_SILENCE);
  1145. }
  1146. if (ast_exists_extension(chan, chan->context, exten, 1, l->callerid)) {
  1147. if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, l->callerid)) {
  1148. if (getforward) {
  1149. /* Record this as the forwarding extension */
  1150. strncpy(l->call_forward, exten, sizeof(l->call_forward) - 1);
  1151. if (option_verbose > 2) {
  1152. ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %s\n",
  1153. l->call_forward, chan->name);
  1154. }
  1155. transmit_tone(s, SKINNY_DIALTONE);
  1156. if (res) {
  1157. break;
  1158. }
  1159. usleep(500000);
  1160. ast_indicate(chan, -1);
  1161. sleep(1);
  1162. memset(exten, 0, sizeof(exten));
  1163. transmit_tone(s, SKINNY_DIALTONE);
  1164. len = 0;
  1165. getforward = 0;
  1166. } else {
  1167. strncpy(chan->exten, exten, sizeof(chan->exten)-1);
  1168. if (!ast_strlen_zero(l->callerid)) {
  1169. if (!l->hidecallerid)
  1170. chan->callerid = strdup(l->callerid);
  1171. chan->ani = strdup(l->callerid);
  1172. }
  1173. ast_setstate(chan, AST_STATE_RING);
  1174. res = ast_pbx_run(chan);
  1175. if (res) {
  1176. ast_log(LOG_WARNING, "PBX exited non-zero\n");
  1177. transmit_tone(s, SKINNY_REORDER);
  1178. }
  1179. return NULL;
  1180. }
  1181. } else {
  1182. /* It's a match, but they just typed a digit, and there is an ambiguous match,
  1183. so just set the timeout to matchdigittimeout and wait some more */
  1184. timeout = matchdigittimeout;
  1185. }
  1186. } else if (res == 0) {
  1187. ast_log(LOG_DEBUG, "Not enough digits (and no ambiguous match)...\n");
  1188. transmit_tone(s, SKINNY_REORDER);
  1189. ast_hangup(chan);
  1190. return NULL;
  1191. } else if (l->callwaiting && !strcmp(exten, "*70")) {
  1192. if (option_verbose > 2) {
  1193. ast_verbose(VERBOSE_PREFIX_3 "Disabling call waiting on %s\n", chan->name);
  1194. }
  1195. /* Disable call waiting if enabled */
  1196. l->callwaiting = 0;
  1197. transmit_tone(s, SKINNY_DIALTONE);
  1198. len = 0;
  1199. memset(exten, 0, sizeof(exten));
  1200. timeout = firstdigittimeout;
  1201. } else if (!strcmp(exten,ast_pickup_ext())) {
  1202. /* Scan all channels and see if any there
  1203. * ringing channqels with that have call groups
  1204. * that equal this channels pickup group
  1205. */
  1206. if (ast_pickup_call(chan)) {
  1207. ast_log(LOG_WARNING, "No call pickup possible...\n");
  1208. transmit_tone(s, SKINNY_REORDER);
  1209. }
  1210. ast_hangup(chan);
  1211. return NULL;
  1212. } else if (!l->hidecallerid && !strcmp(exten, "*67")) {
  1213. if (option_verbose > 2) {
  1214. ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name);
  1215. }
  1216. /* Disable Caller*ID if enabled */
  1217. l->hidecallerid = 1;
  1218. if (chan->callerid)
  1219. free(chan->callerid);
  1220. chan->callerid = NULL;
  1221. transmit_tone(s, SKINNY_DIALTONE);
  1222. len = 0;
  1223. memset(exten, 0, sizeof(exten));
  1224. timeout = firstdigittimeout;
  1225. } else if (l->callreturn && !strcmp(exten, "*69")) {
  1226. res = 0;
  1227. if (!ast_strlen_zero(l->lastcallerid)) {
  1228. res = ast_say_digit_str(chan, l->lastcallerid, "", chan->language);
  1229. }
  1230. if (!res) {
  1231. transmit_tone(s, SKINNY_DIALTONE);
  1232. }
  1233. break;
  1234. } else if (!strcmp(exten, "*78")) {
  1235. /* Do not disturb */
  1236. if (option_verbose > 2) {
  1237. ast_verbose(VERBOSE_PREFIX_3 "Enabled DND on channel %s\n", chan->name);
  1238. }
  1239. transmit_tone(s, SKINNY_DIALTONE);
  1240. l->dnd = 1;
  1241. getforward = 0;
  1242. memset(exten, 0, sizeof(exten));
  1243. len = 0;
  1244. } else if (!strcmp(exten, "*79")) {
  1245. /* Do not disturb */
  1246. if (option_verbose > 2) {
  1247. ast_verbose(VERBOSE_PREFIX_3 "Disabled DND on channel %s\n", chan->name);
  1248. }
  1249. transmit_tone(s, SKINNY_DIALTONE);
  1250. l->dnd = 0;
  1251. getforward = 0;
  1252. memset(exten, 0, sizeof(exten));
  1253. len = 0;
  1254. } else if (l->cancallforward && !strcmp(exten, "*72")) {
  1255. transmit_tone(s, SKINNY_DIALTONE);
  1256. getforward = 1;
  1257. memset(exten, 0, sizeof(exten));
  1258. len = 0;
  1259. } else if (l->cancallforward && !strcmp(exten, "*73")) {
  1260. if (option_verbose > 2) {
  1261. ast_verbose(VERBOSE_PREFIX_3 "Cancelling call forwarding on channel %s\n", chan->name);
  1262. }
  1263. transmit_tone(s, SKINNY_DIALTONE);
  1264. memset(l->call_forward, 0, sizeof(l->call_forward));
  1265. getforward = 0;
  1266. memset(exten, 0, sizeof(exten));
  1267. len = 0;
  1268. } else if (!strcmp(exten, ast_parking_ext()) &&
  1269. sub->next->owner &&
  1270. sub->next->owner->bridge) {
  1271. /* This is a three way call, the main call being a real channel,
  1272. and we're parking the first call. */
  1273. ast_masq_park_call(sub->next->owner->bridge, chan, 0, NULL);
  1274. if (option_verbose > 2) {
  1275. ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name);
  1276. }
  1277. break;
  1278. } else if (!ast_strlen_zero(l->lastcallerid) && !strcmp(exten, "*60")) {
  1279. if (option_verbose > 2) {
  1280. ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", l->lastcallerid);
  1281. }
  1282. res = ast_db_put("blacklist", l->lastcallerid, "1");
  1283. if (!res) {
  1284. transmit_tone(s, SKINNY_DIALTONE);
  1285. memset(exten, 0, sizeof(exten));
  1286. len = 0;
  1287. }
  1288. } else if (l->hidecallerid && !strcmp(exten, "*82")) {
  1289. if (option_verbose > 2) {
  1290. ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name);
  1291. }
  1292. /* Enable Caller*ID if enabled */
  1293. l->hidecallerid = 0;
  1294. if (chan->callerid)
  1295. free(chan->callerid);
  1296. if (!ast_strlen_zero(l->callerid))
  1297. chan->callerid = strdup(l->callerid);
  1298. transmit_tone(s, SKINNY_DIALTONE);
  1299. len = 0;
  1300. memset(exten, 0, sizeof(exten));
  1301. timeout = firstdigittimeout;
  1302. } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->callerid) &&
  1303. ((exten[0] != '*') || (!ast_strlen_zero(exten) > 2))) {
  1304. ast_log(LOG_WARNING, "Can't match [%s] from '%s' in context %s\n", exten, chan->callerid ? chan->callerid : "<Unknown Caller>", chan->context);
  1305. transmit_tone(s, SKINNY_REORDER);
  1306. sleep(3); // hang out for 3 seconds to let congestion play
  1307. break;
  1308. }
  1309. if (!timeout)
  1310. timeout = gendigittimeout;
  1311. if (len && !ast_ignore_pattern(chan->context, exten))
  1312. ast_indicate(chan, -1);
  1313. }
  1314. ast_hangup(chan);
  1315. return NULL;
  1316. }
  1317. static int skinny_call(struct ast_channel *ast, char *dest, int timeout)
  1318. {
  1319. int res = 0;
  1320. int tone = 0;
  1321. struct skinny_line *l;
  1322. struct skinny_subchannel *sub;
  1323. struct skinnysession *session;
  1324. sub = ast->pvt->pvt;
  1325. l = sub->parent;
  1326. session = l->parent->session;
  1327. if (!l->parent->registered) {
  1328. ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
  1329. return -1;
  1330. }
  1331. if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
  1332. ast_log(LOG_WARNING, "skinny_call called on %s, neither down nor reserved\n", ast->name);
  1333. return -1;
  1334. }
  1335. if (skinnydebug) {
  1336. ast_verbose(VERBOSE_PREFIX_3 "skinny_call(%s)\n", ast->name);
  1337. }
  1338. if (l->dnd) {
  1339. ast_queue_control(ast, AST_CONTROL_BUSY);
  1340. return -1;
  1341. }
  1342. switch (l->hookstate) {
  1343. case SKINNY_OFFHOOK:
  1344. tone = SKINNY_CALLWAITTONE;
  1345. break;
  1346. case SKINNY_ONHOOK:
  1347. tone = SKINNY_ALERT;
  1348. break;
  1349. default:
  1350. ast_log(LOG_ERROR, "Don't know how to deal with hookstate %d\n", l->hookstate);
  1351. break;
  1352. }
  1353. transmit_lamp_indication(session, l->instance, SKINNY_LAMP_BLINK);
  1354. transmit_ringer_mode(session, SKINNY_RING_INSIDE);
  1355. transmit_tone(session, tone);
  1356. transmit_callstate(session, l->instance, SKINNY_RINGIN, sub->callid);
  1357. /* XXX need to set the prompt */
  1358. /* XXX need to deal with softkeys */
  1359. ast_setstate(ast, AST_STATE_RINGING);
  1360. ast_queue_control(ast, AST_CONTROL_RINGING);
  1361. sub->outgoing = 1;
  1362. if (l->type == TYPE_LINE) {
  1363. if (!sub->rtp) {
  1364. start_rtp(sub);
  1365. } else {
  1366. /* do/should we need to anything if there already is an RTP allocated? */
  1367. }
  1368. } else {
  1369. ast_log(LOG_ERROR, "I don't know how to dial on trunks, yet\n");
  1370. res = -1;
  1371. }
  1372. return res;
  1373. }
  1374. static int skinny_hangup(struct ast_channel *ast)
  1375. {
  1376. struct skinny_subchannel *sub = ast->pvt->pvt;
  1377. struct skinny_line *l = sub->parent;
  1378. struct skinnysession *s = l->parent->session;
  1379. if (skinnydebug) {
  1380. ast_verbose("skinny_hangup(%s) on %s@%s\n", ast->name, l->name, l->parent->name);
  1381. }
  1382. if (!ast->pvt->pvt) {
  1383. ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
  1384. return 0;
  1385. }
  1386. if (l->parent->registered) {
  1387. if ((sub->parent->type = TYPE_LINE) && (sub->parent->hookstate == SKINNY_OFFHOOK)) {
  1388. sub->parent->hookstate = SKINNY_ONHOOK;
  1389. transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
  1390. transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
  1391. } else if ((sub->parent->type = TYPE_LINE) && (sub->parent->hookstate == SKINNY_ONHOOK)) {
  1392. transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
  1393. transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
  1394. transmit_ringer_mode(s, SKINNY_RING_OFF);
  1395. transmit_tone(s, SKINNY_SILENCE);
  1396. }
  1397. }
  1398. ast_mutex_lock(&sub->lock);
  1399. sub->owner = NULL;
  1400. ast->pvt->pvt = NULL;
  1401. sub->alreadygone = 0;
  1402. sub->outgoing = 0;
  1403. if (sub->rtp) {
  1404. ast_rtp_destroy(sub->rtp);
  1405. sub->rtp = NULL;
  1406. }
  1407. ast_mutex_unlock(&sub->lock);
  1408. return 0;
  1409. }
  1410. static int skinny_answer(struct ast_channel *ast)
  1411. {
  1412. int res = 0;
  1413. struct skinny_subchannel *sub = ast->pvt->pvt;
  1414. struct skinny_line *l = sub->parent;
  1415. sub->cxmode = SKINNY_CX_SENDRECV;
  1416. if (!sub->rtp) {
  1417. start_rtp(sub);
  1418. }
  1419. ast_verbose("skinny_answer(%s) on %s@%s-%d\n", ast->name, l->name, l->parent->name, sub->callid);
  1420. if (ast->_state != AST_STATE_UP) {
  1421. ast_setstate(ast, AST_STATE_UP);
  1422. }
  1423. return res;
  1424. }
  1425. static struct ast_frame *skinny_rtp_read(struct skinny_subchannel *sub)
  1426. {
  1427. /* Retrieve audio/etc from channel. Assumes sub->lock is already held. */
  1428. struct ast_frame *f = NULL;
  1429. if (sub->rtp) {
  1430. f = ast_rtp_read(sub->rtp);
  1431. if (sub->owner) {
  1432. /* We already hold the channel lock */
  1433. if (f->frametype == AST_FRAME_VOICE) {
  1434. if (f->subclass != sub->owner->nativeformats) {
  1435. ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
  1436. sub->owner->nativeformats = f->subclass;
  1437. ast_set_read_format(sub->owner, sub->owner->readformat);
  1438. ast_set_write_format(sub->owner, sub->owner->writeformat);
  1439. }
  1440. }
  1441. }
  1442. } else
  1443. ast_log(LOG_WARNING, "sub->rtp is NULL - calling read on a channel that has been hungup?\n");
  1444. return f;
  1445. }
  1446. static struct ast_frame *skinny_read(struct ast_channel *ast)
  1447. {
  1448. struct ast_frame *fr;
  1449. struct skinny_subchannel *sub = ast->pvt->pvt;
  1450. ast_mutex_lock(&sub->lock);
  1451. fr = skinny_rtp_read(sub);
  1452. ast_mutex_unlock(&sub->lock);
  1453. return fr;
  1454. }
  1455. static int skinny_write(struct ast_channel *ast, struct ast_frame *frame)
  1456. {
  1457. struct skinny_subchannel *sub = ast->pvt->pvt;
  1458. int res = 0;
  1459. if (frame->frametype != AST_FRAME_VOICE) {
  1460. if (frame->frametype == AST_FRAME_IMAGE)
  1461. return 0;
  1462. else {
  1463. ast_log(LOG_WARNING, "Can't send %d type frames with skinny_write\n", frame->frametype);
  1464. return 0;
  1465. }
  1466. } else {
  1467. if (!(frame->subclass & ast->nativeformats)) {
  1468. ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
  1469. frame->subclass, ast->nativeformats, ast->readformat, ast->writeformat);
  1470. return -1;
  1471. }
  1472. }
  1473. if (sub) {
  1474. ast_mutex_lock(&sub->lock);
  1475. if (sub->rtp) {
  1476. res = ast_rtp_write(sub->rtp, frame);
  1477. }
  1478. ast_mutex_unlock(&sub->lock);
  1479. }
  1480. return res;
  1481. }
  1482. static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  1483. {
  1484. struct skinny_subchannel *sub = newchan->pvt->pvt;
  1485. ast_log(LOG_NOTICE, "skinny_fixup(%s, %s)\n", oldchan->name, newchan->name);
  1486. if (sub->owner != oldchan) {
  1487. ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, sub->owner);
  1488. return -1;
  1489. }
  1490. sub->owner = newchan;
  1491. return 0;
  1492. }
  1493. static int skinny_senddigit(struct ast_channel *ast, char digit)
  1494. {
  1495. #if 0
  1496. struct skinny_subchannel *sub = ast->pvt->pvt;
  1497. int tmp;
  1498. /* not right */
  1499. sprintf(tmp, "%d", digit);
  1500. transmit_tone(sub->parent->parent->session, digit);
  1501. #endif
  1502. return -1;
  1503. }
  1504. static char *control2str(int ind) {
  1505. switch (ind) {
  1506. case AST_CONTROL_HANGUP:
  1507. return "Other end has hungup";
  1508. case AST_CONTROL_RING:
  1509. return "Local ring";
  1510. case AST_CONTROL_RINGING:
  1511. return "Remote end is ringing";
  1512. case AST_CONTROL_ANSWER:
  1513. return "Remote end has answered";
  1514. case AST_CONTROL_BUSY:
  1515. return "Remote end is busy";
  1516. case AST_CONTROL_TAKEOFFHOOK:
  1517. return "Make it go off hook";
  1518. case AST_CONTROL_OFFHOOK:
  1519. return "Line is off hook";
  1520. case AST_CONTROL_CONGESTION:
  1521. return "Congestion (circuits busy)";
  1522. case AST_CONTROL_FLASH:
  1523. return "Flash hook";
  1524. case AST_CONTROL_WINK:
  1525. return "Wink";
  1526. case AST_CONTROL_OPTION:
  1527. return "Set a low-level option";
  1528. case AST_CONTROL_RADIO_KEY:
  1529. return "Key Radio";
  1530. case AST_CONTROL_RADIO_UNKEY:
  1531. return "Un-Key Radio";
  1532. case -1:
  1533. return "Stop tone";
  1534. }
  1535. return "UNKNOWN";
  1536. }
  1537. static int skinny_indicate(struct ast_channel *ast, int ind)
  1538. {
  1539. struct skinny_subchannel *sub = ast->pvt->pvt;
  1540. struct skinny_line *l = sub->parent;
  1541. struct skinnysession *s = l->parent->session;
  1542. if (skinnydebug) {
  1543. ast_verbose(VERBOSE_PREFIX_3 "Asked to indicate '%s' condition on channel %s\n", control2str(ind), ast->name);
  1544. }
  1545. switch(ind) {
  1546. case AST_CONTROL_RINGING:
  1547. if (ast->_state != AST_STATE_UP) {
  1548. if (!sub->progress) {
  1549. transmit_tone(s, SKINNY_ALERT);
  1550. transmit_callstate(s, l->instance, SKINNY_RINGOUT, sub->callid);
  1551. sub->ringing = 1;
  1552. break;
  1553. }
  1554. }
  1555. return -1;
  1556. case AST_CONTROL_BUSY:
  1557. if (ast->_state != AST_STATE_UP) {
  1558. transmit_tone(s, SKINNY_BUSYTONE);
  1559. transmit_callstate(s, l->instance, SKINNY_BUSY, sub->callid);
  1560. sub->alreadygone = 1;
  1561. ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
  1562. break;
  1563. }
  1564. return -1;
  1565. case AST_CONTROL_CONGESTION:
  1566. if (ast->_state != AST_STATE_UP) {
  1567. transmit_tone(s, SKINNY_REORDER);
  1568. transmit_callstate(s, l->instance, SKINNY_CONGESTION, sub->callid);
  1569. sub->alreadygone = 1;
  1570. ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
  1571. break;
  1572. }
  1573. return -1;
  1574. case AST_CONTROL_PROGRESS:
  1575. if ((ast->_state != AST_STATE_UP) && !sub->progress && !sub->outgoing) {
  1576. transmit_callstate(s, l->instance, SKINNY_PROGRESS, sub->callid);
  1577. sub->progress = 1;
  1578. break;
  1579. }
  1580. return -1;
  1581. case -1:
  1582. transmit_tone(s, SKINNY_SILENCE);
  1583. break;
  1584. case AST_CONTROL_PROCEEDING:
  1585. break;
  1586. default:
  1587. ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
  1588. return -1;
  1589. }
  1590. return 0;
  1591. }
  1592. static struct ast_channel *skinny_new(struct skinny_subchannel *sub, int state)
  1593. {
  1594. struct ast_channel *tmp;
  1595. struct skinny_line *l = sub->parent;
  1596. int fmt;
  1597. l = sub->parent;
  1598. tmp = ast_channel_alloc(1);
  1599. if (tmp) {
  1600. tmp->nativeformats = l->capability;
  1601. if (!tmp->nativeformats)
  1602. tmp->nativeformats = capability;
  1603. fmt = ast_best_codec(tmp->nativeformats);
  1604. snprintf(tmp->name, sizeof(tmp->name), "Skinny/%s@%s-%d", l->name, l->parent->name, sub->callid);
  1605. if (sub->rtp)
  1606. tmp->fds[0] = ast_rtp_fd(sub->rtp);
  1607. tmp->type = type;
  1608. ast_setstate(tmp, state);
  1609. if (state == AST_STATE_RING)
  1610. tmp->rings = 1;
  1611. tmp->writeformat = fmt;
  1612. tmp->pvt->rawwriteformat = fmt;
  1613. tmp->readformat = fmt;
  1614. tmp->pvt->rawreadformat = fmt;
  1615. tmp->pvt->pvt = sub;
  1616. tmp->pvt->call = skinny_call;
  1617. tmp->pvt->hangup = skinny_hangup;
  1618. tmp->pvt->answer = skinny_answer;
  1619. tmp->pvt->read = skinny_read;
  1620. tmp->pvt->write = skinny_write;
  1621. tmp->pvt->indicate = skinny_indicate;
  1622. tmp->pvt->fixup = skinny_fixup;
  1623. tmp->pvt->send_digit = skinny_senddigit;
  1624. /* tmp->pvt->bridge = ast_rtp_bridge; */
  1625. if (!ast_strlen_zero(l->language))
  1626. strncpy(tmp->language, l->language, sizeof(tmp->language)-1);
  1627. if (!ast_strlen_zero(l->accountcode))
  1628. strncpy(tmp->accountcode, l->accountcode, sizeof(tmp->accountcode)-1);
  1629. if (l->amaflags)
  1630. tmp->amaflags = l->amaflags;
  1631. sub->owner = tmp;
  1632. ast_mutex_lock(&usecnt_lock);
  1633. usecnt++;
  1634. ast_mutex_unlock(&usecnt_lock);
  1635. ast_update_use_count();
  1636. tmp->callgroup = l->callgroup;
  1637. tmp->pickupgroup = l->pickupgroup;
  1638. strncpy(tmp->call_forward, l->call_forward, sizeof(tmp->call_forward) - 1);
  1639. strncpy(tmp->context, l->context, sizeof(tmp->context)-1);
  1640. strncpy(tmp->exten,l->exten, sizeof(tmp->exten)-1);
  1641. if (!ast_strlen_zero(l->callerid)) {
  1642. tmp->callerid = strdup(l->callerid);
  1643. }
  1644. tmp->priority = 1;
  1645. if (state != AST_STATE_DOWN) {
  1646. if (ast_pbx_start(tmp)) {
  1647. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
  1648. ast_hangup(tmp);
  1649. tmp = NULL;
  1650. }
  1651. }
  1652. } else {
  1653. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  1654. }
  1655. return tmp;
  1656. }
  1657. static int handle_message(skinny_req *req, struct skinnysession *s)
  1658. {
  1659. struct skinny_subchannel *sub;
  1660. struct ast_channel *c;
  1661. struct ast_frame f = { 0, };
  1662. struct sockaddr_in sin;
  1663. struct sockaddr_in us;
  1664. struct skinny_line *lines;
  1665. char name[16];
  1666. char addr[4];
  1667. char d;
  1668. int digit;
  1669. int res=0;
  1670. int speedDialNum;
  1671. int lineNumber;
  1672. int stimulus;
  1673. int stimulusInstance;
  1674. int status;
  1675. int port;
  1676. int i;
  1677. time_t timer;
  1678. struct tm *cmtime;
  1679. pthread_t t;
  1680. if ( (!s->device) && (req->e != REGISTER_MESSAGE && req->e != ALARM_MESSAGE)) {
  1681. ast_log(LOG_WARNING, "Client sent message #%d without first registering.\n", req->e);
  1682. free(req);
  1683. return 0;
  1684. }
  1685. switch(req->e) {
  1686. case ALARM_MESSAGE:
  1687. /* no response necessary */
  1688. break;
  1689. case REGISTER_MESSAGE:
  1690. if (skinnydebug) {
  1691. ast_verbose("Device %s is attempting to register\n", req->data.reg.name);
  1692. }
  1693. res = skinny_register(req, s);
  1694. if (!res) {
  1695. ast_log(LOG_ERROR, "Rejecting Device %s: Device not found\n", req->data.reg.name);
  1696. memcpy(&name, req->data.reg.name, sizeof(req->data.reg.name));
  1697. memset(req, 0, sizeof(skinny_req));
  1698. req->len = sizeof(register_rej_message)+4;
  1699. req->e = REGISTER_REJ_MESSAGE;
  1700. snprintf(req->data.regrej.errMsg, sizeof(req->data.regrej.errMsg), "No Authority: %s", name);
  1701. transmit_response(s, req);
  1702. break;
  1703. }
  1704. if (option_verbose > 2) {
  1705. ast_verbose(VERBOSE_PREFIX_3 "Device '%s' successfuly registered\n", s->device->name);
  1706. }
  1707. memset(req, 0, SKINNY_MAX_PACKET);
  1708. req->len = sizeof(register_ack_message)+4;
  1709. req->e = REGISTER_ACK_MESSAGE;
  1710. req->data.regack.res[0] = '0';
  1711. req->data.regack.res[1] = '\0';
  1712. req->data.regack.keepAlive = keep_alive;
  1713. strncpy(req->data.regack.dateTemplate, date_format, sizeof(req->data.regack.dateTemplate) - 1);
  1714. req->data.regack.res2[0] = '0';
  1715. req->data.regack.res2[1] = '\0';
  1716. req->data.regack.secondaryKeepAlive = keep_alive;
  1717. transmit_response(s, req);
  1718. if (skinnydebug) {
  1719. ast_verbose("Requesting capabilities\n");
  1720. }
  1721. memset(req, 0, SKINNY_MAX_PACKET);
  1722. req->len = 4;
  1723. req->e = CAPABILITIES_REQ_MESSAGE;
  1724. transmit_response(s, req);
  1725. break;
  1726. case UNREGISTER_MESSAGE:
  1727. /* XXX Acutally unregister the device */
  1728. break;
  1729. case IP_PORT_MESSAGE:
  1730. /* no response necessary */
  1731. break;
  1732. case STIMULUS_MESSAGE:
  1733. stimulus = req->data.stimulus.stimulus;
  1734. stimulusInstance = req->data.stimulus.stimulusInstance;
  1735. switch(stimulus) {
  1736. case STIMULUS_REDIAL:
  1737. /* XXX how we gonna deal with redial ?!?! */
  1738. if (skinnydebug) {
  1739. ast_verbose("Recieved Stimulus: Redial(%d)\n", stimulusInstance);
  1740. }
  1741. break;
  1742. case STIMULUS_SPEEDDIAL:
  1743. if (skinnydebug) {
  1744. ast_verbose("Recieved Stimulus: SpeedDial(%d)\n", stimulusInstance);
  1745. }
  1746. break;
  1747. case STIMULUS_HOLD:
  1748. /* start moh? set RTP to 0.0.0.0? */
  1749. if (skinnydebug) {
  1750. ast_verbose("Recieved Stimulus: Hold(%d)\n", stimulusInstance);
  1751. }
  1752. break;
  1753. case STIMULUS_TRANSFER:
  1754. if (skinnydebug) {
  1755. ast_verbose("Recieved Stimulus: Transfer(%d)\n", stimulusInstance);
  1756. }
  1757. transmit_tone(s, SKINNY_DIALTONE);
  1758. /* figure out how to transfer */
  1759. break;
  1760. case STIMULUS_FORWARDALL:
  1761. case STIMULUS_FORWARDBUSY:
  1762. case STIMULUS_FORWARDNOANSWER:
  1763. /* Gonna be fun, not */
  1764. if (skinnydebug) {
  1765. ast_verbose("Recieved Stimulus: Forward (%d)\n", stimulusInstance);
  1766. }
  1767. break;
  1768. case STIMULUS_DISPLAY:
  1769. /* Not sure what this is */
  1770. if (skinnydebug) {
  1771. ast_verbose("Recieved Stimulus: Display(%d)\n", stimulusInstance);
  1772. }
  1773. break;
  1774. case STIMULUS_LINE:
  1775. if (skinnydebug) {
  1776. ast_verbose("Recieved Stimulus: Line(%d)\n", stimulusInstance);
  1777. }
  1778. sub = find_subchannel_by_line(s->device->lines);
  1779. /* turn the speaker on */
  1780. transmit_speaker_mode(s, 1);
  1781. break;
  1782. default:
  1783. ast_verbose("RECEIVED UNKNOWN STIMULUS: %d(%d)\n", stimulus, stimulusInstance);
  1784. break;
  1785. }
  1786. break;
  1787. case VERSION_REQ_MESSAGE:
  1788. if (skinnydebug) {
  1789. ast_verbose("Version Request\n");
  1790. }
  1791. memset(req, 0, SKINNY_MAX_PACKET);
  1792. req->len = sizeof(version_res_message)+4;
  1793. req->e = VERSION_RES_MESSAGE;
  1794. snprintf(req->data.version.version, sizeof(req->data.version.version), s->device->version_id);
  1795. transmit_response(s, req);
  1796. break;
  1797. case SERVER_REQUEST_MESSAGE:
  1798. if (skinnydebug) {
  1799. ast_verbose("Recieved Server Request\n");
  1800. }
  1801. memset(req, 0, SKINNY_MAX_PACKET);
  1802. req->len = sizeof(server_res_message)+4;
  1803. req->e = SERVER_RES_MESSAGE;
  1804. memcpy(req->data.serverres.server[0].serverName, ourhost,
  1805. sizeof(req->data.serverres.server[0].serverName));
  1806. req->data.serverres.serverListenPort[0] = ourport;
  1807. req->data.serverres.serverIpAddr[0] = __ourip.s_addr;
  1808. transmit_response(s, req);
  1809. break;
  1810. case BUTTON_TEMPLATE_REQ_MESSAGE:
  1811. if (skinnydebug) {
  1812. ast_verbose("Buttontemplate requested\n");
  1813. }
  1814. memset(req, 0, SKINNY_MAX_PACKET);
  1815. req->len = sizeof(button_template_res_message)+4;
  1816. req->e = BUTTON_TEMPLATE_RES_MESSAGE;
  1817. req->data.buttontemplate.buttonOffset = 0;
  1818. req->data.buttontemplate.buttonCount = 10;
  1819. req->data.buttontemplate.totalButtonCount = 10;
  1820. /* XXX Figure out how to do this correctly */
  1821. memcpy(req->data.buttontemplate.definition,
  1822. button_definition_hack,
  1823. sizeof(req->data.buttontemplate.definition));
  1824. transmit_response(s, req);
  1825. break;
  1826. case SOFT_KEY_SET_REQ_MESSAGE:
  1827. if (skinnydebug) {
  1828. ast_verbose("Received SoftKeySetReq\n");
  1829. }
  1830. memset(req, 0, SKINNY_MAX_PACKET);
  1831. req->len = sizeof(soft_key_sets)+4;
  1832. req->e = SOFT_KEY_SET_RES_MESSAGE;
  1833. req->data.softkeysets.softKeySetOffset = 0;
  1834. req->data.softkeysets.softKeySetCount = 11;
  1835. req->data.softkeysets.totalSoftKeySetCount = 11;
  1836. /* XXX Wicked hack XXX */
  1837. memcpy(req->data.softkeysets.softKeySetDefinition,
  1838. soft_key_set_hack,
  1839. sizeof(req->data.softkeysets.softKeySetDefinition));
  1840. transmit_response(s,req);
  1841. break;
  1842. case SOFT_KEY_TEMPLATE_REQ_MESSAGE:
  1843. if (skinnydebug) {
  1844. ast_verbose("Recieved SoftKey Template Request\n");
  1845. }
  1846. memset(req, 0, SKINNY_MAX_PACKET);
  1847. req->len = sizeof(soft_key_template)+4;
  1848. req->e = SOFT_KEY_TEMPLATE_RES_MESSAGE;
  1849. req->data.softkeytemplate.softKeyOffset = 0;
  1850. req->data.softkeytemplate.softKeyCount = 21;
  1851. req->data.softkeytemplate.totalSoftKeyCount = 21;
  1852. /* XXX Another wicked hack XXX */
  1853. memcpy(req->data.softkeytemplate.softKeyTemplateDefinition,
  1854. soft_key_template_hack,
  1855. sizeof(req->data.softkeytemplate.softKeyTemplateDefinition));
  1856. transmit_response(s,req);
  1857. break;
  1858. case TIME_DATE_REQ_MESSAGE:
  1859. if (skinnydebug) {
  1860. ast_verbose("Received Time/Date Request\n");
  1861. }
  1862. memset(req, 0, SKINNY_MAX_PACKET);
  1863. req->len = sizeof(definetimedate_message)+4;
  1864. req->e = DEFINETIMEDATE_MESSAGE;
  1865. timer=time(NULL);
  1866. cmtime = localtime(&timer);
  1867. req->data.definetimedate.year = cmtime->tm_year+1900;
  1868. req->data.definetimedate.month = cmtime->tm_mon+1;
  1869. req->data.definetimedate.dayofweek = cmtime->tm_wday;
  1870. req->data.definetimedate.day = cmtime->tm_mday;
  1871. req->data.definetimedate.hour = cmtime->tm_hour;
  1872. req->data.definetimedate.minute = cmtime->tm_min;
  1873. req->data.definetimedate.seconds = cmtime->tm_sec;
  1874. transmit_response(s, req);
  1875. break;
  1876. case SPEED_DIAL_STAT_REQ_MESSAGE:
  1877. /* Not really sure how Speed Dial's are different than the
  1878. Softkey templates */
  1879. speedDialNum = req->data.speeddialreq.speedDialNumber;
  1880. memset(req, 0, SKINNY_MAX_PACKET);
  1881. req->len = sizeof(speed_dial_stat_res_message)+4;
  1882. req->e = SPEED_DIAL_STAT_RES_MESSAGE;
  1883. #if 0
  1884. /* XXX Do this right XXX */
  1885. req->data.speeddialreq.speedDialNumber = speedDialNum;
  1886. snprintf(req->data.speeddial.speedDialDirNumber, sizeof(req->data.speeddial.speedDialDirNumber), "31337");
  1887. snprintf(req->data.speeddial.speedDialDisplayName, sizeof(req->data.speeddial.speedDialDisplayName),"Asterisk Rules!");
  1888. #endif
  1889. transmit_response(s, req);
  1890. break;
  1891. case LINE_STATE_REQ_MESSAGE:
  1892. lineNumber = req->data.line.lineNumber;
  1893. if (skinnydebug) {
  1894. ast_verbose("Received LineStateReq\n");
  1895. }
  1896. memset(req, 0, SKINNY_MAX_PACKET);
  1897. req->len = sizeof(line_stat_res_message)+4;
  1898. req->e = LINE_STAT_RES_MESSAGE;
  1899. sub = find_subchannel_by_line(s->device->lines);
  1900. if (!sub) {
  1901. ast_log(LOG_NOTICE, "No available lines on: %s\n", s->device->name);
  1902. return 0;
  1903. }
  1904. lines = sub->parent;
  1905. ast_mutex_lock(&devicelock);
  1906. for (i=1; i < lineNumber; i++) {
  1907. lines = lines->next;
  1908. }
  1909. ast_mutex_unlock(&devicelock);
  1910. req->data.linestat.linenumber = lineNumber;
  1911. memcpy(req->data.linestat.lineDirNumber, lines->name,
  1912. sizeof(req->data.linestat.lineDirNumber));
  1913. memcpy(req->data.linestat.lineDisplayName, lines->label,
  1914. sizeof(req->data.linestat.lineDisplayName));
  1915. transmit_response(s,req);
  1916. break;
  1917. case CAPABILITIES_RES_MESSAGE:
  1918. if (skinnydebug) {
  1919. ast_verbose("Received CapabilitiesRes\n");
  1920. }
  1921. /* XXX process the capabilites */
  1922. break;
  1923. case KEEP_ALIVE_MESSAGE:
  1924. memset(req, 0, SKINNY_MAX_PACKET);
  1925. req->len = 4;
  1926. req->e = KEEP_ALIVE_ACK_MESSAGE;
  1927. transmit_response(s, req);
  1928. break;
  1929. case OFFHOOK_MESSAGE:
  1930. transmit_ringer_mode(s,SKINNY_RING_OFF);
  1931. transmit_lamp_indication(s, s->device->lines->instance, SKINNY_LAMP_ON);
  1932. sub = find_subchannel_by_line(s->device->lines);
  1933. if (!sub) {
  1934. ast_log(LOG_NOTICE, "No available lines on: %s\n", s->device->name);
  1935. return 0;
  1936. }
  1937. sub->parent->hookstate = SKINNY_OFFHOOK;
  1938. if (sub->outgoing) {
  1939. transmit_callstate(s, s->device->lines->instance, SKINNY_OFFHOOK, sub->callid);
  1940. transmit_tone(s, SKINNY_SILENCE);
  1941. ast_setstate(sub->owner, AST_STATE_UP);
  1942. /* XXX select the appropriate soft key here */
  1943. } else {
  1944. if (!sub->owner) {
  1945. transmit_callstate(s, s->device->lines->instance, SKINNY_OFFHOOK, sub->callid);
  1946. transmit_tone(s, SKINNY_DIALTONE);
  1947. c = skinny_new(sub, AST_STATE_DOWN);
  1948. if(c) {
  1949. /* start switch */
  1950. if (ast_pthread_create(&t, NULL, skinny_ss, c)) {
  1951. ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
  1952. ast_hangup(c);
  1953. }
  1954. } else {
  1955. ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", sub->parent->name, s->device->name);
  1956. }
  1957. } else {
  1958. ast_log(LOG_DEBUG, "Current sub [%s] already has owner\n", sub->owner->name);
  1959. }
  1960. }
  1961. break;
  1962. case ONHOOK_MESSAGE:
  1963. sub = find_subchannel_by_line(s->device->lines);
  1964. if (sub->parent->hookstate == SKINNY_ONHOOK) {
  1965. /* Somthing else already put us back on hook */
  1966. break;
  1967. }
  1968. sub->cxmode = SKINNY_CX_RECVONLY;
  1969. sub->parent->hookstate = SKINNY_ONHOOK;
  1970. transmit_callstate(s, s->device->lines->instance, sub->parent->hookstate,sub->callid);
  1971. if (skinnydebug) {
  1972. ast_verbose("Skinny %s@%s went on hook\n",sub->parent->name, sub->parent->parent->name);
  1973. }
  1974. if (sub->parent->transfer && (sub->owner && sub->next->owner) && ((!sub->outgoing) || (!sub->next->outgoing))) {
  1975. /* We're allowed to transfer, we have two active calls and */
  1976. /* we made at least one of the calls. Let's try and transfer */
  1977. #if 0
  1978. if ((res = attempt_transfer(p)) < 0) {
  1979. if (p->sub->next->owner) {
  1980. sub->next->alreadygone = 1;
  1981. ast_queue_hangup(sub->next->owner,1);
  1982. }
  1983. } else if (res) {
  1984. ast_log(LOG_WARNING, "Transfer attempt failed\n");
  1985. return -1;
  1986. }
  1987. #endif
  1988. } else {
  1989. /* Hangup the current call */
  1990. /* If there is another active call, skinny_hangup will ring the phone with the other call */
  1991. if (sub->owner) {
  1992. sub->alreadygone = 1;
  1993. ast_queue_hangup(sub->owner);
  1994. } else {
  1995. ast_log(LOG_WARNING, "Skinny(%s@%s-%d) channel already destroyed\n",
  1996. sub->parent->name, sub->parent->parent->name, sub->callid);
  1997. }
  1998. }
  1999. if ((sub->parent->hookstate == SKINNY_ONHOOK) && (!sub->next->rtp)) {
  2000. if (has_voicemail(sub->parent)) {
  2001. transmit_lamp_indication(s, s->device->lines->instance, SKINNY_LAMP_FLASH);
  2002. } else {
  2003. transmit_lamp_indication(s, s->device->lines->instance, SKINNY_LAMP_OFF);
  2004. }
  2005. }
  2006. break;
  2007. case KEYPAD_BUTTON_MESSAGE:
  2008. digit = req->data.keypad.button;
  2009. if (skinnydebug) {
  2010. ast_verbose("Collected digit: [%d]\n", digit);
  2011. }
  2012. f.frametype = AST_FRAME_DTMF;
  2013. if (digit == 14) {
  2014. d = '*';
  2015. } else if (digit == 15) {
  2016. d = '#';
  2017. } else if (digit >=0 && digit <= 9) {
  2018. d = '0' + digit;
  2019. } else {
  2020. /* digit=10-13 (A,B,C,D ?), or
  2021. * digit is bad value
  2022. *
  2023. * probably should not end up here, but set
  2024. * value for backward compatibility, and log
  2025. * a warning.
  2026. */
  2027. d = '0' + digit;
  2028. ast_log(LOG_WARNING, "Unsupported digit %d\n", digit);
  2029. }
  2030. f.subclass = d;
  2031. f.src = "skinny";
  2032. sub = find_subchannel_by_line(s->device->lines);
  2033. if (sub->owner) {
  2034. /* XXX MUST queue this frame to all subs in threeway call if threeway call is active */
  2035. ast_queue_frame(sub->owner, &f);
  2036. if (sub->next->owner) {
  2037. ast_queue_frame(sub->next->owner, &f);
  2038. }
  2039. } else {
  2040. ast_verbose("No owner: %s\n", s->device->lines->name);
  2041. }
  2042. break;
  2043. case OPEN_RECIEVE_CHANNEL_ACK_MESSAGE:
  2044. ast_verbose("Recieved Open Recieve Channel Ack\n");
  2045. status = req->data.openrecievechannelack.status;
  2046. if (status) {
  2047. ast_log(LOG_ERROR, "Open Recieve Channel Failure\n");
  2048. break;
  2049. }
  2050. memcpy(addr, req->data.openrecievechannelack.ipAddr, sizeof(addr));
  2051. port = req->data.openrecievechannelack.port;
  2052. sin.sin_family = AF_INET;
  2053. /* I smell endian problems */
  2054. memcpy(&sin.sin_addr, addr, sizeof(sin.sin_addr));
  2055. sin.sin_port = htons(port);
  2056. sub = find_subchannel_by_line(s->device->lines);
  2057. if (sub->rtp) {
  2058. ast_rtp_set_peer(sub->rtp, &sin);
  2059. ast_rtp_get_us(sub->rtp, &us);
  2060. } else {
  2061. ast_log(LOG_ERROR, "No RTP structure, this is very bad\n");
  2062. break;
  2063. }
  2064. memset(req, 0, SKINNY_MAX_PACKET);
  2065. req->len = sizeof(start_media_transmission_message)+4;
  2066. req->e = START_MEDIA_TRANSMISSION_MESSAGE;
  2067. req->data.startmedia.conferenceId = 0;
  2068. req->data.startmedia.passThruPartyId = 0;
  2069. memcpy(req->data.startmedia.remoteIp, &s->device->ourip, 4); /* Endian? */
  2070. req->data.startmedia.remotePort = ntohs(us.sin_port);
  2071. req->data.startmedia.packetSize = 20;
  2072. req->data.startmedia.payloadType = convert_cap(s->device->lines->capability);
  2073. req->data.startmedia.qualifier.precedence = 127;
  2074. req->data.startmedia.qualifier.vad = 0;
  2075. req->data.startmedia.qualifier.packets = 0;
  2076. req->data.startmedia.qualifier.bitRate = 0;
  2077. transmit_response(s, req);
  2078. break;
  2079. default:
  2080. ast_verbose("RECEIVED UNKNOWN MESSAGE TYPE: %x\n", req->e);
  2081. break;
  2082. }
  2083. free(req);
  2084. return 1;
  2085. }
  2086. static void destroy_session(struct skinnysession *s)
  2087. {
  2088. struct skinnysession *cur, *prev = NULL;
  2089. ast_mutex_lock(&sessionlock);
  2090. cur = sessions;
  2091. while(cur) {
  2092. if (cur == s)
  2093. break;
  2094. prev = cur;
  2095. cur = cur->next;
  2096. }
  2097. if (cur) {
  2098. if (prev)
  2099. prev->next = cur->next;
  2100. else
  2101. sessions = cur->next;
  2102. if (s->fd > -1)
  2103. close(s->fd);
  2104. ast_mutex_destroy(&s->lock);
  2105. free(s);
  2106. } else
  2107. ast_log(LOG_WARNING, "Trying to delete nonexistent session %p?\n", s);
  2108. ast_mutex_unlock(&sessionlock);
  2109. }
  2110. static int get_input(struct skinnysession *s)
  2111. {
  2112. int res;
  2113. int dlen = 0;
  2114. struct pollfd fds[1];
  2115. fds[0].fd = s->fd;
  2116. fds[0].events = POLLIN;
  2117. res = poll(fds, 1, -1);
  2118. if (res < 0) {
  2119. ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
  2120. } else if (res > 0) {
  2121. memset(s->inbuf,0,sizeof(s->inbuf));
  2122. res = read(s->fd, s->inbuf, 4);
  2123. if (res != 4) {
  2124. ast_log(LOG_WARNING, "Skinny Client sent less data than expected.\n");
  2125. return -1;
  2126. }
  2127. dlen = *(int *)s->inbuf;
  2128. if (dlen < 0) {
  2129. ast_log(LOG_WARNING, "Skinny Client sent invalid data.\n");
  2130. return -1;
  2131. }
  2132. if (dlen+8 > sizeof(s->inbuf))
  2133. dlen = sizeof(s->inbuf) - 8;
  2134. res = read(s->fd, s->inbuf+4, dlen+4);
  2135. ast_mutex_unlock(&s->lock);
  2136. if (res != (dlen+4)) {
  2137. ast_log(LOG_WARNING, "Skinny Client sent less data than expected.\n");
  2138. return -1;
  2139. }
  2140. }
  2141. return res;
  2142. }
  2143. static skinny_req *skinny_req_parse(struct skinnysession *s)
  2144. {
  2145. skinny_req *req;
  2146. req = malloc(SKINNY_MAX_PACKET);
  2147. if (!req) {
  2148. ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
  2149. return NULL;
  2150. }
  2151. memset(req, 0, sizeof(skinny_req));
  2152. /* +8 to account for reserved and length fields */
  2153. memcpy(req, s->inbuf, *(int*)(s->inbuf)+8);
  2154. if (req->e < 0) {
  2155. ast_log(LOG_ERROR, "Event Message is NULL from socket %d, This is bad\n", s->fd);
  2156. free(req);
  2157. return NULL;
  2158. }
  2159. return req;
  2160. }
  2161. static void *skinny_session(void *data)
  2162. {
  2163. int res;
  2164. skinny_req *req;
  2165. struct skinnysession *s = data;
  2166. char iabuf[INET_ADDRSTRLEN];
  2167. ast_verbose(VERBOSE_PREFIX_3 "Starting Skinny session from %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
  2168. for (;;) {
  2169. res = 0;
  2170. res = get_input(s);
  2171. if (res < 0)
  2172. break;
  2173. req = skinny_req_parse(s);
  2174. if (!req) {
  2175. return NULL;
  2176. }
  2177. res = handle_message(req, s);
  2178. if (res < 0) {
  2179. destroy_session(s);
  2180. return NULL;
  2181. }
  2182. }
  2183. ast_log(LOG_NOTICE, "Skinny Session returned: %s\n", strerror(errno));
  2184. destroy_session(s);
  2185. return 0;
  2186. }
  2187. static void *accept_thread(void *ignore)
  2188. {
  2189. int as;
  2190. struct sockaddr_in sin;
  2191. int sinlen;
  2192. struct skinnysession *s;
  2193. struct protoent *p;
  2194. int arg = 1;
  2195. pthread_attr_t attr;
  2196. pthread_attr_init(&attr);
  2197. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  2198. for (;;) {
  2199. sinlen = sizeof(sin);
  2200. as = accept(skinnysock, (struct sockaddr *)&sin, &sinlen);
  2201. if (as < 0) {
  2202. ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
  2203. continue;
  2204. }
  2205. p = getprotobyname("tcp");
  2206. if(p) {
  2207. if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
  2208. ast_log(LOG_WARNING, "Failed to set Skinny tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
  2209. }
  2210. }
  2211. s = malloc(sizeof(struct skinnysession));
  2212. if (!s) {
  2213. ast_log(LOG_WARNING, "Failed to allocate Skinny session: %s\n", strerror(errno));
  2214. continue;
  2215. }
  2216. memset(s, 0, sizeof(struct skinnysession));
  2217. memcpy(&s->sin, &sin, sizeof(sin));
  2218. ast_mutex_init(&s->lock);
  2219. s->fd = as;
  2220. ast_mutex_lock(&sessionlock);
  2221. s->next = sessions;
  2222. sessions = s;
  2223. ast_mutex_unlock(&sessionlock);
  2224. if (ast_pthread_create(&tcp_thread, NULL, skinny_session, s)) {
  2225. destroy_session(s);
  2226. }
  2227. }
  2228. if (skinnydebug) {
  2229. ast_verbose("killing accept thread\n");
  2230. }
  2231. close(as);
  2232. return 0;
  2233. }
  2234. static void *do_monitor(void *data)
  2235. {
  2236. int res;
  2237. /* This thread monitors all the interfaces which are not yet in use
  2238. (and thus do not have a separate thread) indefinitely */
  2239. /* From here on out, we die whenever asked */
  2240. for(;;) {
  2241. pthread_testcancel();
  2242. /* Wait for sched or io */
  2243. res = ast_sched_wait(sched);
  2244. if ((res < 0) || (res > 1000)) {
  2245. res = 1000;
  2246. }
  2247. res = ast_io_wait(io, res);
  2248. ast_mutex_lock(&monlock);
  2249. if (res >= 0) {
  2250. ast_sched_runq(sched);
  2251. }
  2252. ast_mutex_unlock(&monlock);
  2253. }
  2254. /* Never reached */
  2255. return NULL;
  2256. }
  2257. static int restart_monitor(void)
  2258. {
  2259. /* If we're supposed to be stopped -- stay stopped */
  2260. if (monitor_thread == AST_PTHREADT_STOP)
  2261. return 0;
  2262. if (ast_mutex_lock(&monlock)) {
  2263. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  2264. return -1;
  2265. }
  2266. if (monitor_thread == pthread_self()) {
  2267. ast_mutex_unlock(&monlock);
  2268. ast_log(LOG_WARNING, "Cannot kill myself\n");
  2269. return -1;
  2270. }
  2271. if (monitor_thread != AST_PTHREADT_NULL) {
  2272. /* Wake up the thread */
  2273. pthread_kill(monitor_thread, SIGURG);
  2274. } else {
  2275. /* Start a new monitor */
  2276. if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
  2277. ast_mutex_unlock(&monlock);
  2278. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  2279. return -1;
  2280. }
  2281. }
  2282. ast_mutex_unlock(&monlock);
  2283. return 0;
  2284. }
  2285. static struct ast_channel *skinny_request(char *type, int format, void *data)
  2286. {
  2287. int oldformat;
  2288. struct skinny_subchannel *sub;
  2289. struct ast_channel *tmpc = NULL;
  2290. char tmp[256];
  2291. char *dest = data;
  2292. oldformat = format;
  2293. format &= capability;
  2294. if (!format) {
  2295. ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
  2296. return NULL;
  2297. }
  2298. strncpy(tmp, dest, sizeof(tmp) - 1);
  2299. if (ast_strlen_zero(tmp)) {
  2300. ast_log(LOG_NOTICE, "Skinny channels require a device\n");
  2301. return NULL;
  2302. }
  2303. sub = find_subchannel_by_name(tmp);
  2304. if (!sub) {
  2305. ast_log(LOG_NOTICE, "No available lines on: %s\n", dest);
  2306. return NULL;
  2307. }
  2308. if (option_verbose > 2) {
  2309. ast_verbose(VERBOSE_PREFIX_3 "skinny_request(%s)\n", tmp);
  2310. ast_verbose(VERBOSE_PREFIX_3 "Skinny cw: %d, dnd: %d, so: %d, sno: %d\n",
  2311. sub->parent->callwaiting, sub->parent->dnd, sub->owner ? 1 : 0, sub->next->owner ? 1: 0);
  2312. }
  2313. tmpc = skinny_new(sub->owner ? sub->next : sub, AST_STATE_DOWN);
  2314. if (!tmpc) {
  2315. ast_log(LOG_WARNING, "Unable to make channel for '%s'\n", tmp);
  2316. }
  2317. restart_monitor();
  2318. /* and finish */
  2319. return tmpc;
  2320. }
  2321. static int reload_config(void)
  2322. {
  2323. int on=1;
  2324. struct ast_config *cfg;
  2325. struct ast_variable *v;
  2326. int format;
  2327. char *cat;
  2328. char iabuf[INET_ADDRSTRLEN];
  2329. struct skinny_device *d;
  2330. int oldport = ntohs(bindaddr.sin_port);
  2331. #if 0
  2332. hp = ast_gethostbyname(ourhost, &ahp);
  2333. if (!hp) {
  2334. ast_log(LOG_WARNING, "Unable to get hostname, Skinny disabled\n");
  2335. return 0;
  2336. }
  2337. #endif
  2338. cfg = ast_load(config);
  2339. /* We *must* have a config file otherwise stop immediately */
  2340. if (!cfg) {
  2341. ast_log(LOG_NOTICE, "Unable to load config %s, Skinny disabled\n", config);
  2342. return 0;
  2343. }
  2344. /* load the general section */
  2345. memset(&bindaddr, 0, sizeof(bindaddr));
  2346. v = ast_variable_browse(cfg, "general");
  2347. while(v) {
  2348. /* Create the interface list */
  2349. if (!strcasecmp(v->name, "bindaddr")) {
  2350. if (!(hp = ast_gethostbyname(v->value, &ahp))) {
  2351. ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
  2352. } else {
  2353. memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
  2354. }
  2355. } else if (!strcasecmp(v->name, "keepAlive")) {
  2356. keep_alive = atoi(v->value);
  2357. } else if (!strcasecmp(v->name, "dateFormat")) {
  2358. strncpy(date_format, v->value, sizeof(date_format) - 1);
  2359. } else if (!strcasecmp(v->name, "allow")) {
  2360. format = ast_getformatbyname(v->value);
  2361. if (format < 1)
  2362. ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
  2363. else
  2364. capability |= format;
  2365. } else if (!strcasecmp(v->name, "disallow")) {
  2366. format = ast_getformatbyname(v->value);
  2367. if (format < 1)
  2368. ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
  2369. else
  2370. capability &= ~format;
  2371. } else if (!strcasecmp(v->name, "port")) {
  2372. if (sscanf(v->value, "%i", &ourport) == 1) {
  2373. bindaddr.sin_port = htons(ourport);
  2374. } else {
  2375. ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
  2376. }
  2377. }
  2378. v = v->next;
  2379. }
  2380. if (ntohl(bindaddr.sin_addr.s_addr)) {
  2381. memcpy(&__ourip, &bindaddr.sin_addr, sizeof(__ourip));
  2382. } else {
  2383. hp = ast_gethostbyname(ourhost, &ahp);
  2384. if (!hp) {
  2385. ast_log(LOG_WARNING, "Unable to get our IP address, Skinny disabled\n");
  2386. ast_destroy(cfg);
  2387. return 0;
  2388. }
  2389. memcpy(&__ourip, hp->h_addr, sizeof(__ourip));
  2390. }
  2391. if (!ntohs(bindaddr.sin_port)) {
  2392. bindaddr.sin_port = ntohs(DEFAULT_SKINNY_PORT);
  2393. }
  2394. bindaddr.sin_family = AF_INET;
  2395. /* load the device sections */
  2396. cat = ast_category_browse(cfg, NULL);
  2397. while(cat) {
  2398. if (strcasecmp(cat, "general")) {
  2399. d = build_device(cat, ast_variable_browse(cfg, cat));
  2400. if (d) {
  2401. if (option_verbose > 2) {
  2402. ast_verbose(VERBOSE_PREFIX_3 "Added device '%s'\n", d->name);
  2403. }
  2404. ast_mutex_lock(&devicelock);
  2405. d->next = devices;
  2406. devices = d;
  2407. ast_mutex_unlock(&devicelock);
  2408. }
  2409. }
  2410. cat = ast_category_browse(cfg, cat);
  2411. }
  2412. ast_mutex_lock(&netlock);
  2413. if ((skinnysock > -1) && (ntohs(bindaddr.sin_port) != oldport)) {
  2414. close(skinnysock);
  2415. skinnysock = -1;
  2416. }
  2417. if (skinnysock < 0) {
  2418. skinnysock = socket(AF_INET, SOCK_STREAM, 0);
  2419. if(setsockopt(skinnysock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
  2420. ast_log(LOG_ERROR, "Set Socket Options failed: errno %d, %s", errno, strerror(errno));
  2421. ast_destroy(cfg);
  2422. return 0;
  2423. }
  2424. if (skinnysock < 0) {
  2425. ast_log(LOG_WARNING, "Unable to create Skinny socket: %s\n", strerror(errno));
  2426. } else {
  2427. if (bind(skinnysock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
  2428. ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
  2429. ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr), ntohs(bindaddr.sin_port),
  2430. strerror(errno));
  2431. close(skinnysock);
  2432. skinnysock = -1;
  2433. ast_destroy(cfg);
  2434. return 0;
  2435. }
  2436. if (listen(skinnysock,DEFAULT_SKINNY_BACKLOG)) {
  2437. ast_log(LOG_WARNING, "Failed to start listening to %s:%d: %s\n",
  2438. ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr), ntohs(bindaddr.sin_port),
  2439. strerror(errno));
  2440. close(skinnysock);
  2441. skinnysock = -1;
  2442. ast_destroy(cfg);
  2443. return 0;
  2444. }
  2445. if (option_verbose > 1)
  2446. ast_verbose(VERBOSE_PREFIX_2 "Skinny listening on %s:%d\n",
  2447. ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr), ntohs(bindaddr.sin_port));
  2448. ast_pthread_create(&accept_t,NULL, accept_thread, NULL);
  2449. }
  2450. }
  2451. ast_mutex_unlock(&netlock);
  2452. /* and unload the configuration when were done */
  2453. ast_destroy(cfg);
  2454. return 0;
  2455. }
  2456. #if 0
  2457. void delete_devices(void)
  2458. {
  2459. struct skinny_device *d, *dlast;
  2460. struct skinny_line *l, *llast;
  2461. struct skinny_subchannel *sub, *slast;
  2462. ast_mutex_lock(&devicelock);
  2463. /* Delete all devices */
  2464. for (d=devices;d;) {
  2465. /* Delete all lines for this device */
  2466. for (l=d->lines;l;) {
  2467. /* Delete all subchannels for this line */
  2468. for (sub=l->sub;sub;) {
  2469. slast = sub;
  2470. sub = sub->next;
  2471. ast_mutex_destroy(&slast->lock);
  2472. free(slast);
  2473. }
  2474. llast = l;
  2475. l = l->next;
  2476. ast_mutex_destroy(&llast->lock);
  2477. free(llast);
  2478. }
  2479. dlast = d;
  2480. d = d->next;
  2481. free(dlast);
  2482. }
  2483. devices=NULL;
  2484. ast_mutex_unlock(&devicelock);
  2485. }
  2486. #endif
  2487. int reload(void)
  2488. {
  2489. #if 0
  2490. // XXX Causes Seg
  2491. delete_devices();
  2492. reload_config();
  2493. restart_monitor();
  2494. #endif
  2495. return 0;
  2496. }
  2497. int load_module()
  2498. {
  2499. int res = 0;
  2500. /* load and parse config */
  2501. res = reload_config();
  2502. /* Announce our presence to Asterisk */
  2503. if (!res) {
  2504. /* Make sure we can register our skinny channel type */
  2505. if (ast_channel_register(type, tdesc, capability, skinny_request)) {
  2506. ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
  2507. return -1;
  2508. }
  2509. }
  2510. skinny_rtp.type = type;
  2511. ast_rtp_proto_register(&skinny_rtp);
  2512. ast_cli_register(&cli_show_lines);
  2513. ast_cli_register(&cli_debug);
  2514. ast_cli_register(&cli_no_debug);
  2515. sched = sched_context_create();
  2516. if (!sched) {
  2517. ast_log(LOG_WARNING, "Unable to create schedule context\n");
  2518. }
  2519. io = io_context_create();
  2520. if (!io) {
  2521. ast_log(LOG_WARNING, "Unable to create I/O context\n");
  2522. }
  2523. /* And start the monitor for the first time */
  2524. restart_monitor();
  2525. return res;
  2526. }
  2527. int unload_module()
  2528. {
  2529. #if 0
  2530. struct skinny_session *session, s;
  2531. struct skinny_subchannel *sub;
  2532. struct skinny_line *line = session;
  2533. /* close all IP connections */
  2534. if (!ast_mutex_lock(&devicelock)) {
  2535. /* Terminate tcp listener thread */
  2536. } else {
  2537. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  2538. return -1;
  2539. }
  2540. if (!ast_mutex_lock(&monlock)) {
  2541. if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
  2542. pthread_cancel(monitor_thread);
  2543. pthread_kill(monitor_thread, SIGURG);
  2544. pthread_join(monitor_thread, NULL);
  2545. }
  2546. monitor_thread = AST_PTHREADT_STOP;
  2547. ast_mutex_unlock(&monlock);
  2548. } else {
  2549. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  2550. return -1;
  2551. }
  2552. if (!ast_mutex_lock(&iflock)) {
  2553. /* Destroy all the interfaces and free their memory */
  2554. p = iflist;
  2555. while(p) {
  2556. pl = p;
  2557. p = p->next;
  2558. /* Free associated memory */
  2559. ast_mutex_destroy(&pl->lock);
  2560. free(pl);
  2561. }
  2562. iflist = NULL;
  2563. ast_mutex_unlock(&iflock);
  2564. } else {
  2565. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  2566. return -1;
  2567. }
  2568. ast_rtp_proto_register(&skinny_rtp);
  2569. ast_cli_register(&cli_show_lines);
  2570. ast_cli_register(&cli_debug);
  2571. ast_cli_register(&cli_no_debug);
  2572. return 0;
  2573. #endif
  2574. return -1;
  2575. }
  2576. int usecount()
  2577. {
  2578. int res;
  2579. ast_mutex_lock(&usecnt_lock);
  2580. res = usecnt;
  2581. ast_mutex_unlock(&usecnt_lock);
  2582. return res;
  2583. }
  2584. char *key()
  2585. {
  2586. return ASTERISK_GPL_KEY;
  2587. }
  2588. char *description()
  2589. {
  2590. return desc;
  2591. }