chan_skinny.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820
  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+8 > sizeof(s->inbuf))
  2129. dlen = sizeof(s->inbuf) - 8;
  2130. res = read(s->fd, s->inbuf+4, dlen+4);
  2131. ast_mutex_unlock(&s->lock);
  2132. if (res != (dlen+4)) {
  2133. ast_log(LOG_WARNING, "Skinny Client sent less data than expected.\n");
  2134. return -1;
  2135. }
  2136. }
  2137. return res;
  2138. }
  2139. static skinny_req *skinny_req_parse(struct skinnysession *s)
  2140. {
  2141. skinny_req *req;
  2142. req = malloc(SKINNY_MAX_PACKET);
  2143. if (!req) {
  2144. ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
  2145. return NULL;
  2146. }
  2147. memset(req, 0, sizeof(skinny_req));
  2148. /* +8 to account for reserved and length fields */
  2149. memcpy(req, s->inbuf, *(int*)(s->inbuf)+8);
  2150. if (req->e < 0) {
  2151. ast_log(LOG_ERROR, "Event Message is NULL from socket %d, This is bad\n", s->fd);
  2152. free(req);
  2153. return NULL;
  2154. }
  2155. return req;
  2156. }
  2157. static void *skinny_session(void *data)
  2158. {
  2159. int res;
  2160. skinny_req *req;
  2161. struct skinnysession *s = data;
  2162. char iabuf[INET_ADDRSTRLEN];
  2163. ast_verbose(VERBOSE_PREFIX_3 "Starting Skinny session from %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
  2164. for (;;) {
  2165. res = 0;
  2166. res = get_input(s);
  2167. if (res < 0)
  2168. break;
  2169. req = skinny_req_parse(s);
  2170. if (!req) {
  2171. return NULL;
  2172. }
  2173. res = handle_message(req, s);
  2174. if (res < 0) {
  2175. destroy_session(s);
  2176. return NULL;
  2177. }
  2178. }
  2179. ast_log(LOG_NOTICE, "Skinny Session returned: %s\n", strerror(errno));
  2180. destroy_session(s);
  2181. return 0;
  2182. }
  2183. static void *accept_thread(void *ignore)
  2184. {
  2185. int as;
  2186. struct sockaddr_in sin;
  2187. int sinlen;
  2188. struct skinnysession *s;
  2189. struct protoent *p;
  2190. int arg = 1;
  2191. pthread_attr_t attr;
  2192. pthread_attr_init(&attr);
  2193. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  2194. for (;;) {
  2195. sinlen = sizeof(sin);
  2196. as = accept(skinnysock, (struct sockaddr *)&sin, &sinlen);
  2197. if (as < 0) {
  2198. ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
  2199. continue;
  2200. }
  2201. p = getprotobyname("tcp");
  2202. if(p) {
  2203. if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
  2204. ast_log(LOG_WARNING, "Failed to set Skinny tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
  2205. }
  2206. }
  2207. s = malloc(sizeof(struct skinnysession));
  2208. if (!s) {
  2209. ast_log(LOG_WARNING, "Failed to allocate Skinny session: %s\n", strerror(errno));
  2210. continue;
  2211. }
  2212. memset(s, 0, sizeof(struct skinnysession));
  2213. memcpy(&s->sin, &sin, sizeof(sin));
  2214. ast_mutex_init(&s->lock);
  2215. s->fd = as;
  2216. ast_mutex_lock(&sessionlock);
  2217. s->next = sessions;
  2218. sessions = s;
  2219. ast_mutex_unlock(&sessionlock);
  2220. if (ast_pthread_create(&tcp_thread, NULL, skinny_session, s)) {
  2221. destroy_session(s);
  2222. }
  2223. }
  2224. if (skinnydebug) {
  2225. ast_verbose("killing accept thread\n");
  2226. }
  2227. close(as);
  2228. return 0;
  2229. }
  2230. static void *do_monitor(void *data)
  2231. {
  2232. int res;
  2233. /* This thread monitors all the interfaces which are not yet in use
  2234. (and thus do not have a separate thread) indefinitely */
  2235. /* From here on out, we die whenever asked */
  2236. for(;;) {
  2237. pthread_testcancel();
  2238. /* Wait for sched or io */
  2239. res = ast_sched_wait(sched);
  2240. if ((res < 0) || (res > 1000)) {
  2241. res = 1000;
  2242. }
  2243. res = ast_io_wait(io, res);
  2244. ast_mutex_lock(&monlock);
  2245. if (res >= 0) {
  2246. ast_sched_runq(sched);
  2247. }
  2248. ast_mutex_unlock(&monlock);
  2249. }
  2250. /* Never reached */
  2251. return NULL;
  2252. }
  2253. static int restart_monitor(void)
  2254. {
  2255. /* If we're supposed to be stopped -- stay stopped */
  2256. if (monitor_thread == AST_PTHREADT_STOP)
  2257. return 0;
  2258. if (ast_mutex_lock(&monlock)) {
  2259. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  2260. return -1;
  2261. }
  2262. if (monitor_thread == pthread_self()) {
  2263. ast_mutex_unlock(&monlock);
  2264. ast_log(LOG_WARNING, "Cannot kill myself\n");
  2265. return -1;
  2266. }
  2267. if (monitor_thread != AST_PTHREADT_NULL) {
  2268. /* Wake up the thread */
  2269. pthread_kill(monitor_thread, SIGURG);
  2270. } else {
  2271. /* Start a new monitor */
  2272. if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
  2273. ast_mutex_unlock(&monlock);
  2274. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  2275. return -1;
  2276. }
  2277. }
  2278. ast_mutex_unlock(&monlock);
  2279. return 0;
  2280. }
  2281. static struct ast_channel *skinny_request(char *type, int format, void *data)
  2282. {
  2283. int oldformat;
  2284. struct skinny_subchannel *sub;
  2285. struct ast_channel *tmpc = NULL;
  2286. char tmp[256];
  2287. char *dest = data;
  2288. oldformat = format;
  2289. format &= capability;
  2290. if (!format) {
  2291. ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
  2292. return NULL;
  2293. }
  2294. strncpy(tmp, dest, sizeof(tmp) - 1);
  2295. if (ast_strlen_zero(tmp)) {
  2296. ast_log(LOG_NOTICE, "Skinny channels require a device\n");
  2297. return NULL;
  2298. }
  2299. sub = find_subchannel_by_name(tmp);
  2300. if (!sub) {
  2301. ast_log(LOG_NOTICE, "No available lines on: %s\n", dest);
  2302. return NULL;
  2303. }
  2304. if (option_verbose > 2) {
  2305. ast_verbose(VERBOSE_PREFIX_3 "skinny_request(%s)\n", tmp);
  2306. ast_verbose(VERBOSE_PREFIX_3 "Skinny cw: %d, dnd: %d, so: %d, sno: %d\n",
  2307. sub->parent->callwaiting, sub->parent->dnd, sub->owner ? 1 : 0, sub->next->owner ? 1: 0);
  2308. }
  2309. tmpc = skinny_new(sub->owner ? sub->next : sub, AST_STATE_DOWN);
  2310. if (!tmpc) {
  2311. ast_log(LOG_WARNING, "Unable to make channel for '%s'\n", tmp);
  2312. }
  2313. restart_monitor();
  2314. /* and finish */
  2315. return tmpc;
  2316. }
  2317. static int reload_config(void)
  2318. {
  2319. int on=1;
  2320. struct ast_config *cfg;
  2321. struct ast_variable *v;
  2322. int format;
  2323. char *cat;
  2324. char iabuf[INET_ADDRSTRLEN];
  2325. struct skinny_device *d;
  2326. int oldport = ntohs(bindaddr.sin_port);
  2327. #if 0
  2328. hp = ast_gethostbyname(ourhost, &ahp);
  2329. if (!hp) {
  2330. ast_log(LOG_WARNING, "Unable to get hostname, Skinny disabled\n");
  2331. return 0;
  2332. }
  2333. #endif
  2334. cfg = ast_load(config);
  2335. /* We *must* have a config file otherwise stop immediately */
  2336. if (!cfg) {
  2337. ast_log(LOG_NOTICE, "Unable to load config %s, Skinny disabled\n", config);
  2338. return 0;
  2339. }
  2340. /* load the general section */
  2341. memset(&bindaddr, 0, sizeof(bindaddr));
  2342. v = ast_variable_browse(cfg, "general");
  2343. while(v) {
  2344. /* Create the interface list */
  2345. if (!strcasecmp(v->name, "bindaddr")) {
  2346. if (!(hp = ast_gethostbyname(v->value, &ahp))) {
  2347. ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
  2348. } else {
  2349. memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
  2350. }
  2351. } else if (!strcasecmp(v->name, "keepAlive")) {
  2352. keep_alive = atoi(v->value);
  2353. } else if (!strcasecmp(v->name, "dateFormat")) {
  2354. strncpy(date_format, v->value, sizeof(date_format) - 1);
  2355. } else if (!strcasecmp(v->name, "allow")) {
  2356. format = ast_getformatbyname(v->value);
  2357. if (format < 1)
  2358. ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
  2359. else
  2360. capability |= format;
  2361. } else if (!strcasecmp(v->name, "disallow")) {
  2362. format = ast_getformatbyname(v->value);
  2363. if (format < 1)
  2364. ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
  2365. else
  2366. capability &= ~format;
  2367. } else if (!strcasecmp(v->name, "port")) {
  2368. if (sscanf(v->value, "%i", &ourport) == 1) {
  2369. bindaddr.sin_port = htons(ourport);
  2370. } else {
  2371. ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
  2372. }
  2373. }
  2374. v = v->next;
  2375. }
  2376. if (ntohl(bindaddr.sin_addr.s_addr)) {
  2377. memcpy(&__ourip, &bindaddr.sin_addr, sizeof(__ourip));
  2378. } else {
  2379. hp = ast_gethostbyname(ourhost, &ahp);
  2380. if (!hp) {
  2381. ast_log(LOG_WARNING, "Unable to get our IP address, Skinny disabled\n");
  2382. ast_destroy(cfg);
  2383. return 0;
  2384. }
  2385. memcpy(&__ourip, hp->h_addr, sizeof(__ourip));
  2386. }
  2387. if (!ntohs(bindaddr.sin_port)) {
  2388. bindaddr.sin_port = ntohs(DEFAULT_SKINNY_PORT);
  2389. }
  2390. bindaddr.sin_family = AF_INET;
  2391. /* load the device sections */
  2392. cat = ast_category_browse(cfg, NULL);
  2393. while(cat) {
  2394. if (strcasecmp(cat, "general")) {
  2395. d = build_device(cat, ast_variable_browse(cfg, cat));
  2396. if (d) {
  2397. if (option_verbose > 2) {
  2398. ast_verbose(VERBOSE_PREFIX_3 "Added device '%s'\n", d->name);
  2399. }
  2400. ast_mutex_lock(&devicelock);
  2401. d->next = devices;
  2402. devices = d;
  2403. ast_mutex_unlock(&devicelock);
  2404. }
  2405. }
  2406. cat = ast_category_browse(cfg, cat);
  2407. }
  2408. ast_mutex_lock(&netlock);
  2409. if ((skinnysock > -1) && (ntohs(bindaddr.sin_port) != oldport)) {
  2410. close(skinnysock);
  2411. skinnysock = -1;
  2412. }
  2413. if (skinnysock < 0) {
  2414. skinnysock = socket(AF_INET, SOCK_STREAM, 0);
  2415. if(setsockopt(skinnysock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
  2416. ast_log(LOG_ERROR, "Set Socket Options failed: errno %d, %s", errno, strerror(errno));
  2417. ast_destroy(cfg);
  2418. return 0;
  2419. }
  2420. if (skinnysock < 0) {
  2421. ast_log(LOG_WARNING, "Unable to create Skinny socket: %s\n", strerror(errno));
  2422. } else {
  2423. if (bind(skinnysock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
  2424. ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
  2425. ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr), ntohs(bindaddr.sin_port),
  2426. strerror(errno));
  2427. close(skinnysock);
  2428. skinnysock = -1;
  2429. ast_destroy(cfg);
  2430. return 0;
  2431. }
  2432. if (listen(skinnysock,DEFAULT_SKINNY_BACKLOG)) {
  2433. ast_log(LOG_WARNING, "Failed to start listening to %s:%d: %s\n",
  2434. ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr), ntohs(bindaddr.sin_port),
  2435. strerror(errno));
  2436. close(skinnysock);
  2437. skinnysock = -1;
  2438. ast_destroy(cfg);
  2439. return 0;
  2440. }
  2441. if (option_verbose > 1)
  2442. ast_verbose(VERBOSE_PREFIX_2 "Skinny listening on %s:%d\n",
  2443. ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr), ntohs(bindaddr.sin_port));
  2444. ast_pthread_create(&accept_t,NULL, accept_thread, NULL);
  2445. }
  2446. }
  2447. ast_mutex_unlock(&netlock);
  2448. /* and unload the configuration when were done */
  2449. ast_destroy(cfg);
  2450. return 0;
  2451. }
  2452. #if 0
  2453. void delete_devices(void)
  2454. {
  2455. struct skinny_device *d, *dlast;
  2456. struct skinny_line *l, *llast;
  2457. struct skinny_subchannel *sub, *slast;
  2458. ast_mutex_lock(&devicelock);
  2459. /* Delete all devices */
  2460. for (d=devices;d;) {
  2461. /* Delete all lines for this device */
  2462. for (l=d->lines;l;) {
  2463. /* Delete all subchannels for this line */
  2464. for (sub=l->sub;sub;) {
  2465. slast = sub;
  2466. sub = sub->next;
  2467. ast_mutex_destroy(&slast->lock);
  2468. free(slast);
  2469. }
  2470. llast = l;
  2471. l = l->next;
  2472. ast_mutex_destroy(&llast->lock);
  2473. free(llast);
  2474. }
  2475. dlast = d;
  2476. d = d->next;
  2477. free(dlast);
  2478. }
  2479. devices=NULL;
  2480. ast_mutex_unlock(&devicelock);
  2481. }
  2482. #endif
  2483. int reload(void)
  2484. {
  2485. #if 0
  2486. // XXX Causes Seg
  2487. delete_devices();
  2488. reload_config();
  2489. restart_monitor();
  2490. #endif
  2491. return 0;
  2492. }
  2493. int load_module()
  2494. {
  2495. int res = 0;
  2496. /* load and parse config */
  2497. res = reload_config();
  2498. /* Announce our presence to Asterisk */
  2499. if (!res) {
  2500. /* Make sure we can register our skinny channel type */
  2501. if (ast_channel_register(type, tdesc, capability, skinny_request)) {
  2502. ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
  2503. return -1;
  2504. }
  2505. }
  2506. skinny_rtp.type = type;
  2507. ast_rtp_proto_register(&skinny_rtp);
  2508. ast_cli_register(&cli_show_lines);
  2509. ast_cli_register(&cli_debug);
  2510. ast_cli_register(&cli_no_debug);
  2511. sched = sched_context_create();
  2512. if (!sched) {
  2513. ast_log(LOG_WARNING, "Unable to create schedule context\n");
  2514. }
  2515. io = io_context_create();
  2516. if (!io) {
  2517. ast_log(LOG_WARNING, "Unable to create I/O context\n");
  2518. }
  2519. /* And start the monitor for the first time */
  2520. restart_monitor();
  2521. return res;
  2522. }
  2523. int unload_module()
  2524. {
  2525. #if 0
  2526. struct skinny_session *session, s;
  2527. struct skinny_subchannel *sub;
  2528. struct skinny_line *line = session;
  2529. /* close all IP connections */
  2530. if (!ast_mutex_lock(&devicelock)) {
  2531. /* Terminate tcp listener thread */
  2532. } else {
  2533. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  2534. return -1;
  2535. }
  2536. if (!ast_mutex_lock(&monlock)) {
  2537. if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
  2538. pthread_cancel(monitor_thread);
  2539. pthread_kill(monitor_thread, SIGURG);
  2540. pthread_join(monitor_thread, NULL);
  2541. }
  2542. monitor_thread = AST_PTHREADT_STOP;
  2543. ast_mutex_unlock(&monlock);
  2544. } else {
  2545. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  2546. return -1;
  2547. }
  2548. if (!ast_mutex_lock(&iflock)) {
  2549. /* Destroy all the interfaces and free their memory */
  2550. p = iflist;
  2551. while(p) {
  2552. pl = p;
  2553. p = p->next;
  2554. /* Free associated memory */
  2555. ast_mutex_destroy(&pl->lock);
  2556. free(pl);
  2557. }
  2558. iflist = NULL;
  2559. ast_mutex_unlock(&iflock);
  2560. } else {
  2561. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  2562. return -1;
  2563. }
  2564. ast_rtp_proto_register(&skinny_rtp);
  2565. ast_cli_register(&cli_show_lines);
  2566. ast_cli_register(&cli_debug);
  2567. ast_cli_register(&cli_no_debug);
  2568. return 0;
  2569. #endif
  2570. return -1;
  2571. }
  2572. int usecount()
  2573. {
  2574. int res;
  2575. ast_mutex_lock(&usecnt_lock);
  2576. res = usecnt;
  2577. ast_mutex_unlock(&usecnt_lock);
  2578. return res;
  2579. }
  2580. char *key()
  2581. {
  2582. return ASTERISK_GPL_KEY;
  2583. }
  2584. char *description()
  2585. {
  2586. return desc;
  2587. }