iax2-parser.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Implementation of Inter-Asterisk eXchange Protocol, v 2
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include <sys/socket.h>
  30. #include <netinet/in.h>
  31. #include <arpa/inet.h>
  32. #include "asterisk/frame.h"
  33. #include "asterisk/utils.h"
  34. #include "asterisk/unaligned.h"
  35. #include "asterisk/config.h"
  36. #include "asterisk/lock.h"
  37. #include "asterisk/threadstorage.h"
  38. #include "iax2.h"
  39. #include "iax2-parser.h"
  40. #include "iax2-provision.h"
  41. static int frames = 0;
  42. static int iframes = 0;
  43. static int oframes = 0;
  44. #if !defined(LOW_MEMORY)
  45. static void frame_cache_cleanup(void *data);
  46. /*! \brief A per-thread cache of iax_frame structures */
  47. AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
  48. /*! \brief This is just so iax_frames, a list head struct for holding a list of
  49. * iax_frame structures, is defined. */
  50. AST_LIST_HEAD_NOLOCK(iax_frame_list, iax_frame);
  51. struct iax_frames {
  52. struct iax_frame_list list;
  53. size_t size;
  54. };
  55. #define FRAME_CACHE_MAX_SIZE 20
  56. #endif
  57. static void internaloutput(const char *str)
  58. {
  59. fputs(str, stdout);
  60. }
  61. static void internalerror(const char *str)
  62. {
  63. fprintf(stderr, "WARNING: %s", str);
  64. }
  65. static void (*outputf)(const char *str) = internaloutput;
  66. static void (*errorf)(const char *str) = internalerror;
  67. static void dump_addr(char *output, int maxlen, void *value, int len)
  68. {
  69. struct sockaddr_in sin;
  70. if (len == (int)sizeof(sin)) {
  71. memcpy(&sin, value, len);
  72. snprintf(output, maxlen, "IPV4 %s:%d", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
  73. } else {
  74. ast_copy_string(output, "Invalid Address", maxlen);
  75. }
  76. }
  77. static void dump_string_hex(char *output, int maxlen, void *value, int len)
  78. {
  79. int i = 0;
  80. while (len-- && (i + 1) * 4 < maxlen) {
  81. sprintf(output + (4 * i), "\\x%2.2x", *((unsigned char *)value + i));
  82. i++;
  83. }
  84. }
  85. static void dump_string(char *output, int maxlen, void *value, int len)
  86. {
  87. maxlen--;
  88. if (maxlen > len)
  89. maxlen = len;
  90. strncpy(output, value, maxlen);
  91. output[maxlen] = '\0';
  92. }
  93. static void dump_prefs(char *output, int maxlen, void *value, int len)
  94. {
  95. struct ast_codec_pref pref;
  96. int total_len = 0;
  97. maxlen--;
  98. total_len = maxlen;
  99. if (maxlen > len)
  100. maxlen = len;
  101. strncpy(output, value, maxlen);
  102. output[maxlen] = '\0';
  103. ast_codec_pref_convert(&pref, output, total_len, 0);
  104. memset(output,0,total_len);
  105. ast_codec_pref_string(&pref, output, total_len);
  106. }
  107. static void dump_int(char *output, int maxlen, void *value, int len)
  108. {
  109. if (len == (int)sizeof(unsigned int))
  110. snprintf(output, maxlen, "%lu", (unsigned long)ntohl(get_unaligned_uint32(value)));
  111. else
  112. ast_copy_string(output, "Invalid INT", maxlen);
  113. }
  114. static void dump_short(char *output, int maxlen, void *value, int len)
  115. {
  116. if (len == (int)sizeof(unsigned short))
  117. snprintf(output, maxlen, "%d", ntohs(get_unaligned_uint16(value)));
  118. else
  119. ast_copy_string(output, "Invalid SHORT", maxlen);
  120. }
  121. static void dump_byte(char *output, int maxlen, void *value, int len)
  122. {
  123. if (len == (int)sizeof(unsigned char))
  124. snprintf(output, maxlen, "%d", *((unsigned char *)value));
  125. else
  126. ast_copy_string(output, "Invalid BYTE", maxlen);
  127. }
  128. static void dump_datetime(char *output, int maxlen, void *value, int len)
  129. {
  130. struct ast_tm tm;
  131. unsigned long val = (unsigned long) ntohl(get_unaligned_uint32(value));
  132. if (len == (int)sizeof(unsigned int)) {
  133. tm.tm_sec = (val & 0x1f) << 1;
  134. tm.tm_min = (val >> 5) & 0x3f;
  135. tm.tm_hour = (val >> 11) & 0x1f;
  136. tm.tm_mday = (val >> 16) & 0x1f;
  137. tm.tm_mon = ((val >> 21) & 0x0f) - 1;
  138. tm.tm_year = ((val >> 25) & 0x7f) + 100;
  139. ast_strftime(output, maxlen, "%Y-%m-%d %T", &tm);
  140. } else
  141. ast_copy_string(output, "Invalid DATETIME format!", maxlen);
  142. }
  143. static void dump_ipaddr(char *output, int maxlen, void *value, int len)
  144. {
  145. struct sockaddr_in sin;
  146. if (len == (int)sizeof(unsigned int)) {
  147. memcpy(&sin.sin_addr, value, len);
  148. snprintf(output, maxlen, "%s", ast_inet_ntoa(sin.sin_addr));
  149. } else
  150. ast_copy_string(output, "Invalid IPADDR", maxlen);
  151. }
  152. static void dump_prov_flags(char *output, int maxlen, void *value, int len)
  153. {
  154. char buf[256] = "";
  155. if (len == (int)sizeof(unsigned int))
  156. snprintf(output, maxlen, "%lu (%s)", (unsigned long)ntohl(get_unaligned_uint32(value)),
  157. iax_provflags2str(buf, sizeof(buf), ntohl(get_unaligned_uint32(value))));
  158. else
  159. ast_copy_string(output, "Invalid INT", maxlen);
  160. }
  161. static void dump_samprate(char *output, int maxlen, void *value, int len)
  162. {
  163. char tmp[256]="";
  164. int sr;
  165. if (len == (int)sizeof(unsigned short)) {
  166. sr = ntohs(*((unsigned short *)value));
  167. if (sr & IAX_RATE_8KHZ)
  168. strcat(tmp, ",8khz");
  169. if (sr & IAX_RATE_11KHZ)
  170. strcat(tmp, ",11.025khz");
  171. if (sr & IAX_RATE_16KHZ)
  172. strcat(tmp, ",16khz");
  173. if (sr & IAX_RATE_22KHZ)
  174. strcat(tmp, ",22.05khz");
  175. if (sr & IAX_RATE_44KHZ)
  176. strcat(tmp, ",44.1khz");
  177. if (sr & IAX_RATE_48KHZ)
  178. strcat(tmp, ",48khz");
  179. if (strlen(tmp))
  180. ast_copy_string(output, &tmp[1], maxlen);
  181. else
  182. ast_copy_string(output, "None Specified!\n", maxlen);
  183. } else
  184. ast_copy_string(output, "Invalid SHORT", maxlen);
  185. }
  186. static void dump_versioned_codec(char *output, int maxlen, void *value, int len)
  187. {
  188. char *version = (char *) value;
  189. if (version[0] == 0) {
  190. if (len == (int) (sizeof(iax2_format) + sizeof(char))) {
  191. iax2_format codec = ntohll(get_unaligned_uint64(value + 1));
  192. ast_copy_string(output, iax2_getformatname(codec), maxlen);
  193. } else {
  194. ast_copy_string(output, "Invalid length!", maxlen);
  195. }
  196. } else {
  197. ast_copy_string(output, "Unknown version!", maxlen);
  198. }
  199. }
  200. static void dump_prov_ies(char *output, int maxlen, unsigned char *iedata, int len);
  201. static void dump_prov(char *output, int maxlen, void *value, int len)
  202. {
  203. dump_prov_ies(output, maxlen, value, len);
  204. }
  205. static struct iax2_ie {
  206. int ie;
  207. char *name;
  208. void (*dump)(char *output, int maxlen, void *value, int len);
  209. } infoelts[] = {
  210. { IAX_IE_CALLED_NUMBER, "CALLED NUMBER", dump_string },
  211. { IAX_IE_CALLING_NUMBER, "CALLING NUMBER", dump_string },
  212. { IAX_IE_CALLING_ANI, "ANI", dump_string },
  213. { IAX_IE_CALLING_NAME, "CALLING NAME", dump_string },
  214. { IAX_IE_CALLED_CONTEXT, "CALLED CONTEXT", dump_string },
  215. { IAX_IE_USERNAME, "USERNAME", dump_string },
  216. { IAX_IE_PASSWORD, "PASSWORD", dump_string },
  217. { IAX_IE_CAPABILITY, "CAPABILITY", dump_int },
  218. { IAX_IE_CAPABILITY2, "CAPABILITY2", dump_versioned_codec },
  219. { IAX_IE_FORMAT, "FORMAT", dump_int },
  220. { IAX_IE_FORMAT2, "FORMAT2", dump_versioned_codec },
  221. { IAX_IE_LANGUAGE, "LANGUAGE", dump_string },
  222. { IAX_IE_VERSION, "VERSION", dump_short },
  223. { IAX_IE_ADSICPE, "ADSICPE", dump_short },
  224. { IAX_IE_DNID, "DNID", dump_string },
  225. { IAX_IE_AUTHMETHODS, "AUTHMETHODS", dump_short },
  226. { IAX_IE_CHALLENGE, "CHALLENGE", dump_string_hex },
  227. { IAX_IE_MD5_RESULT, "MD5 RESULT", dump_string },
  228. { IAX_IE_RSA_RESULT, "RSA RESULT", dump_string },
  229. { IAX_IE_APPARENT_ADDR, "APPARENT ADDRESS", dump_addr },
  230. { IAX_IE_REFRESH, "REFRESH", dump_short },
  231. { IAX_IE_DPSTATUS, "DIALPLAN STATUS", dump_short },
  232. { IAX_IE_CALLNO, "CALL NUMBER", dump_short },
  233. { IAX_IE_CAUSE, "CAUSE", dump_string },
  234. { IAX_IE_IAX_UNKNOWN, "UNKNOWN IAX CMD", dump_byte },
  235. { IAX_IE_MSGCOUNT, "MESSAGE COUNT", dump_short },
  236. { IAX_IE_AUTOANSWER, "AUTO ANSWER REQ" },
  237. { IAX_IE_TRANSFERID, "TRANSFER ID", dump_int },
  238. { IAX_IE_RDNIS, "REFERRING DNIS", dump_string },
  239. { IAX_IE_PROVISIONING, "PROVISIONING", dump_prov },
  240. { IAX_IE_AESPROVISIONING, "AES PROVISIONG" },
  241. { IAX_IE_DATETIME, "DATE TIME", dump_datetime },
  242. { IAX_IE_DEVICETYPE, "DEVICE TYPE", dump_string },
  243. { IAX_IE_SERVICEIDENT, "SERVICE IDENT", dump_string },
  244. { IAX_IE_FIRMWAREVER, "FIRMWARE VER", dump_short },
  245. { IAX_IE_FWBLOCKDESC, "FW BLOCK DESC", dump_int },
  246. { IAX_IE_FWBLOCKDATA, "FW BLOCK DATA" },
  247. { IAX_IE_PROVVER, "PROVISIONG VER", dump_int },
  248. { IAX_IE_CALLINGPRES, "CALLING PRESNTN", dump_byte },
  249. { IAX_IE_CALLINGTON, "CALLING TYPEOFNUM", dump_byte },
  250. { IAX_IE_CALLINGTNS, "CALLING TRANSITNET", dump_short },
  251. { IAX_IE_SAMPLINGRATE, "SAMPLINGRATE", dump_samprate },
  252. { IAX_IE_CAUSECODE, "CAUSE CODE", dump_byte },
  253. { IAX_IE_ENCRYPTION, "ENCRYPTION", dump_short },
  254. { IAX_IE_ENCKEY, "ENCRYPTION KEY" },
  255. { IAX_IE_CODEC_PREFS, "CODEC_PREFS", dump_prefs },
  256. { IAX_IE_RR_JITTER, "RR_JITTER", dump_int },
  257. { IAX_IE_RR_LOSS, "RR_LOSS", dump_int },
  258. { IAX_IE_RR_PKTS, "RR_PKTS", dump_int },
  259. { IAX_IE_RR_DELAY, "RR_DELAY", dump_short },
  260. { IAX_IE_RR_DROPPED, "RR_DROPPED", dump_int },
  261. { IAX_IE_RR_OOO, "RR_OUTOFORDER", dump_int },
  262. { IAX_IE_VARIABLE, "VARIABLE", dump_string },
  263. { IAX_IE_OSPTOKEN, "OSPTOKEN" },
  264. { IAX_IE_CALLTOKEN, "CALLTOKEN" },
  265. };
  266. static const struct iax2_ie prov_ies[] = {
  267. { PROV_IE_USEDHCP, "USEDHCP" },
  268. { PROV_IE_IPADDR, "IPADDR", dump_ipaddr },
  269. { PROV_IE_SUBNET, "SUBNET", dump_ipaddr },
  270. { PROV_IE_GATEWAY, "GATEWAY", dump_ipaddr },
  271. { PROV_IE_PORTNO, "BINDPORT", dump_short },
  272. { PROV_IE_USER, "USERNAME", dump_string },
  273. { PROV_IE_PASS, "PASSWORD", dump_string },
  274. { PROV_IE_LANG, "LANGUAGE", dump_string },
  275. { PROV_IE_TOS, "TYPEOFSERVICE", dump_byte },
  276. { PROV_IE_FLAGS, "FLAGS", dump_prov_flags },
  277. { PROV_IE_FORMAT, "FORMAT", dump_int },
  278. { PROV_IE_AESKEY, "AESKEY" },
  279. { PROV_IE_SERVERIP, "SERVERIP", dump_ipaddr },
  280. { PROV_IE_SERVERPORT, "SERVERPORT", dump_short },
  281. { PROV_IE_NEWAESKEY, "NEWAESKEY" },
  282. { PROV_IE_PROVVER, "PROV VERSION", dump_int },
  283. { PROV_IE_ALTSERVER, "ALTSERVERIP", dump_ipaddr },
  284. };
  285. const char *iax_ie2str(int ie)
  286. {
  287. int x;
  288. for (x = 0; x < ARRAY_LEN(infoelts); x++) {
  289. if (infoelts[x].ie == ie)
  290. return infoelts[x].name;
  291. }
  292. return "Unknown IE";
  293. }
  294. static void dump_prov_ies(char *output, int maxlen, unsigned char *iedata, int len)
  295. {
  296. int ielen;
  297. int ie;
  298. int x;
  299. int found;
  300. char interp[80];
  301. char tmp[256];
  302. if (len < 2)
  303. return;
  304. strcpy(output, "\n");
  305. maxlen -= strlen(output); output += strlen(output);
  306. while(len > 2) {
  307. ie = iedata[0];
  308. ielen = iedata[1];
  309. if (ielen + 2> len) {
  310. snprintf(tmp, (int)sizeof(tmp), "Total Prov IE length of %d bytes exceeds remaining prov frame length of %d bytes\n", ielen + 2, len);
  311. ast_copy_string(output, tmp, maxlen);
  312. maxlen -= strlen(output);
  313. output += strlen(output);
  314. return;
  315. }
  316. found = 0;
  317. for (x=0;x<(int)sizeof(prov_ies) / (int)sizeof(prov_ies[0]); x++) {
  318. if (prov_ies[x].ie == ie) {
  319. if (prov_ies[x].dump) {
  320. prov_ies[x].dump(interp, (int)sizeof(interp), iedata + 2, ielen);
  321. snprintf(tmp, (int)sizeof(tmp), " %-15.15s : %s\n", prov_ies[x].name, interp);
  322. ast_copy_string(output, tmp, maxlen);
  323. maxlen -= strlen(output); output += strlen(output);
  324. } else {
  325. if (ielen)
  326. snprintf(interp, (int)sizeof(interp), "%d bytes", ielen);
  327. else
  328. strcpy(interp, "Present");
  329. snprintf(tmp, (int)sizeof(tmp), " %-15.15s : %s\n", prov_ies[x].name, interp);
  330. ast_copy_string(output, tmp, maxlen);
  331. maxlen -= strlen(output); output += strlen(output);
  332. }
  333. found++;
  334. }
  335. }
  336. if (!found) {
  337. snprintf(tmp, (int)sizeof(tmp), " Unknown Prov IE %03d : Present\n", ie);
  338. ast_copy_string(output, tmp, maxlen);
  339. maxlen -= strlen(output); output += strlen(output);
  340. }
  341. iedata += (2 + ielen);
  342. len -= (2 + ielen);
  343. }
  344. }
  345. static void dump_ies(unsigned char *iedata, int len)
  346. {
  347. int ielen;
  348. int ie;
  349. int x;
  350. int found;
  351. char interp[1024];
  352. char tmp[1024];
  353. if (len < 2)
  354. return;
  355. while(len > 2) {
  356. ie = iedata[0];
  357. ielen = iedata[1];
  358. if (ielen + 2> len) {
  359. snprintf(tmp, (int)sizeof(tmp), "Total IE length of %d bytes exceeds remaining frame length of %d bytes\n", ielen + 2, len);
  360. outputf(tmp);
  361. return;
  362. }
  363. found = 0;
  364. for (x = 0; x < ARRAY_LEN(infoelts); x++) {
  365. if (infoelts[x].ie == ie) {
  366. if (infoelts[x].dump) {
  367. infoelts[x].dump(interp, (int)sizeof(interp), iedata + 2, ielen);
  368. snprintf(tmp, (int)sizeof(tmp), " %-15.15s : %s\n", infoelts[x].name, interp);
  369. outputf(tmp);
  370. } else {
  371. if (ielen)
  372. snprintf(interp, (int)sizeof(interp), "%d bytes", ielen);
  373. else
  374. strcpy(interp, "Present");
  375. snprintf(tmp, (int)sizeof(tmp), " %-15.15s : %s\n", infoelts[x].name, interp);
  376. outputf(tmp);
  377. }
  378. found++;
  379. }
  380. }
  381. if (!found) {
  382. snprintf(tmp, (int)sizeof(tmp), " Unknown IE %03d : Present\n", ie);
  383. outputf(tmp);
  384. }
  385. iedata += (2 + ielen);
  386. len -= (2 + ielen);
  387. }
  388. outputf("\n");
  389. }
  390. void iax_frame_subclass2str(enum iax_frame_subclass subclass, char *str, size_t len)
  391. {
  392. const char *cmd = "Unknown";
  393. /* if an error occurs here during compile, that means a new iax frame subclass
  394. * has been added to the iax_frame_subclass enum. Add the new subclass to the
  395. * switch case and make sure to update it with a new string representation. */
  396. switch (subclass) {
  397. case IAX_COMMAND_NEW:
  398. cmd = "NEW ";
  399. break;
  400. case IAX_COMMAND_PING:
  401. cmd = "PING ";
  402. break;
  403. case IAX_COMMAND_PONG:
  404. cmd = "PONG ";
  405. break;
  406. case IAX_COMMAND_ACK:
  407. cmd = "ACK ";
  408. break;
  409. case IAX_COMMAND_HANGUP:
  410. cmd = "HANGUP ";
  411. break;
  412. case IAX_COMMAND_REJECT:
  413. cmd = "REJECT ";
  414. break;
  415. case IAX_COMMAND_ACCEPT:
  416. cmd = "ACCEPT ";
  417. break;
  418. case IAX_COMMAND_AUTHREQ:
  419. cmd = "AUTHREQ";
  420. break;
  421. case IAX_COMMAND_AUTHREP:
  422. cmd = "AUTHREP";
  423. break;
  424. case IAX_COMMAND_INVAL:
  425. cmd = "INVAL ";
  426. break;
  427. case IAX_COMMAND_LAGRQ:
  428. cmd = "LAGRQ ";
  429. break;
  430. case IAX_COMMAND_LAGRP:
  431. cmd = "LAGRP ";
  432. break;
  433. case IAX_COMMAND_REGREQ:
  434. cmd = "REGREQ ";
  435. break;
  436. case IAX_COMMAND_REGAUTH:
  437. cmd = "REGAUTH";
  438. break;
  439. case IAX_COMMAND_REGACK:
  440. cmd = "REGACK ";
  441. break;
  442. case IAX_COMMAND_REGREJ:
  443. cmd = "REGREJ ";
  444. break;
  445. case IAX_COMMAND_REGREL:
  446. cmd = "REGREL ";
  447. break;
  448. case IAX_COMMAND_VNAK:
  449. cmd = "VNAK ";
  450. break;
  451. case IAX_COMMAND_DPREQ:
  452. cmd = "DPREQ ";
  453. break;
  454. case IAX_COMMAND_DPREP:
  455. cmd = "DPREP ";
  456. break;
  457. case IAX_COMMAND_DIAL:
  458. cmd = "DIAL ";
  459. break;
  460. case IAX_COMMAND_TXREQ:
  461. cmd = "TXREQ ";
  462. break;
  463. case IAX_COMMAND_TXCNT:
  464. cmd = "TXCNT ";
  465. break;
  466. case IAX_COMMAND_TXACC:
  467. cmd = "TXACC ";
  468. break;
  469. case IAX_COMMAND_TXREADY:
  470. cmd = "TXREADY";
  471. break;
  472. case IAX_COMMAND_TXREL:
  473. cmd = "TXREL ";
  474. break;
  475. case IAX_COMMAND_TXREJ:
  476. cmd = "TXREJ ";
  477. break;
  478. case IAX_COMMAND_QUELCH:
  479. cmd = "QUELCH ";
  480. break;
  481. case IAX_COMMAND_UNQUELCH:
  482. cmd = "UNQULCH";
  483. break;
  484. case IAX_COMMAND_POKE:
  485. cmd = "POKE ";
  486. break;
  487. case IAX_COMMAND_PAGE:
  488. cmd = "PAGE ";
  489. break;
  490. case IAX_COMMAND_MWI:
  491. cmd = "MWI ";
  492. break;
  493. case IAX_COMMAND_UNSUPPORT:
  494. cmd = "UNSPRTD";
  495. break;
  496. case IAX_COMMAND_TRANSFER:
  497. cmd = "TRANSFR";
  498. break;
  499. case IAX_COMMAND_PROVISION:
  500. cmd = "PROVISN";
  501. break;
  502. case IAX_COMMAND_FWDOWNL:
  503. cmd = "FWDWNLD";
  504. break;
  505. case IAX_COMMAND_FWDATA:
  506. cmd = "FWDATA ";
  507. break;
  508. case IAX_COMMAND_TXMEDIA:
  509. cmd = "TXMEDIA";
  510. break;
  511. case IAX_COMMAND_RTKEY:
  512. cmd = "RTKEY ";
  513. break;
  514. case IAX_COMMAND_CALLTOKEN:
  515. cmd = "CTOKEN ";
  516. break;
  517. }
  518. ast_copy_string(str, cmd, len);
  519. }
  520. void iax_showframe(struct iax_frame *f, struct ast_iax2_full_hdr *fhi, int rx, struct sockaddr_in *sin, int datalen)
  521. {
  522. const char *framelist[] = {
  523. "(0?)",
  524. "DTMF_E ",
  525. "VOICE ",
  526. "VIDEO ",
  527. "CONTROL",
  528. "NULL ",
  529. "IAX ",
  530. "TEXT ",
  531. "IMAGE ",
  532. "HTML ",
  533. "CNG ",
  534. "MODEM ",
  535. "DTMF_B ",
  536. };
  537. const char *cmds[] = {
  538. "(0?)",
  539. "HANGUP ",
  540. "RING ",
  541. "RINGING",
  542. "ANSWER ",
  543. "BUSY ",
  544. "TKOFFHK",
  545. "OFFHOOK",
  546. "CONGSTN",
  547. "FLASH ",
  548. "WINK ",
  549. "OPTION ",
  550. "RDKEY ",
  551. "RDUNKEY",
  552. "PROGRES",
  553. "PROCDNG",
  554. "HOLD ",
  555. "UNHOLD ",
  556. "VIDUPDT",
  557. "T38 ",
  558. "SRCUPDT",
  559. "TXFER ",
  560. "CNLINE ",
  561. "REDIR ",
  562. };
  563. struct ast_iax2_full_hdr *fh;
  564. char retries[20];
  565. char class2[20];
  566. char subclass2[20];
  567. const char *class;
  568. const char *subclass;
  569. char *dir;
  570. char tmp[512];
  571. switch(rx) {
  572. case 0:
  573. dir = "Tx";
  574. break;
  575. case 2:
  576. dir = "TE";
  577. break;
  578. case 3:
  579. dir = "RD";
  580. break;
  581. default:
  582. dir = "Rx";
  583. break;
  584. }
  585. if (f) {
  586. fh = f->data;
  587. snprintf(retries, sizeof(retries), "%03d", f->retries);
  588. } else {
  589. fh = fhi;
  590. if (ntohs(fh->dcallno) & IAX_FLAG_RETRANS)
  591. strcpy(retries, "Yes");
  592. else
  593. strcpy(retries, " No");
  594. }
  595. if (!(ntohs(fh->scallno) & IAX_FLAG_FULL)) {
  596. /* Don't mess with mini-frames */
  597. return;
  598. }
  599. if (fh->type >= ARRAY_LEN(framelist)) {
  600. snprintf(class2, sizeof(class2), "(%d?)", fh->type);
  601. class = class2;
  602. } else {
  603. class = framelist[(int)fh->type];
  604. }
  605. if (fh->type == AST_FRAME_DTMF_BEGIN || fh->type == AST_FRAME_DTMF_END) {
  606. sprintf(subclass2, "%c", fh->csub);
  607. subclass = subclass2;
  608. } else if (fh->type == AST_FRAME_IAX) {
  609. iax_frame_subclass2str((int)fh->csub, subclass2, sizeof(subclass2));
  610. subclass = subclass2;
  611. } else if (fh->type == AST_FRAME_CONTROL) {
  612. if (fh->csub >= ARRAY_LEN(cmds)) {
  613. snprintf(subclass2, sizeof(subclass2), "(%d?)", fh->csub);
  614. subclass = subclass2;
  615. } else {
  616. subclass = cmds[(int)fh->csub];
  617. }
  618. } else {
  619. snprintf(subclass2, sizeof(subclass2), "%d", fh->csub);
  620. subclass = subclass2;
  621. }
  622. snprintf(tmp, sizeof(tmp),
  623. "%s-Frame Retry[%s] -- OSeqno: %3.3d ISeqno: %3.3d Type: %s Subclass: %s\n",
  624. dir,
  625. retries, fh->oseqno, fh->iseqno, class, subclass);
  626. outputf(tmp);
  627. snprintf(tmp, sizeof(tmp),
  628. " Timestamp: %05lums SCall: %5.5d DCall: %5.5d [%s:%d]\n",
  629. (unsigned long)ntohl(fh->ts),
  630. ntohs(fh->scallno) & ~IAX_FLAG_FULL, ntohs(fh->dcallno) & ~IAX_FLAG_RETRANS,
  631. ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
  632. outputf(tmp);
  633. if (fh->type == AST_FRAME_IAX)
  634. dump_ies(fh->iedata, datalen);
  635. }
  636. int iax_ie_append_raw(struct iax_ie_data *ied, unsigned char ie, const void *data, int datalen)
  637. {
  638. char tmp[256];
  639. if (datalen > ((int)sizeof(ied->buf) - ied->pos)) {
  640. snprintf(tmp, (int)sizeof(tmp), "Out of space for ie '%s' (%d), need %d have %d\n", iax_ie2str(ie), ie, datalen, (int)sizeof(ied->buf) - ied->pos);
  641. errorf(tmp);
  642. return -1;
  643. }
  644. ied->buf[ied->pos++] = ie;
  645. ied->buf[ied->pos++] = datalen;
  646. memcpy(ied->buf + ied->pos, data, datalen);
  647. ied->pos += datalen;
  648. return 0;
  649. }
  650. int iax_ie_append_addr(struct iax_ie_data *ied, unsigned char ie, const struct sockaddr_in *sin)
  651. {
  652. return iax_ie_append_raw(ied, ie, sin, (int)sizeof(struct sockaddr_in));
  653. }
  654. int iax_ie_append_versioned_uint64(struct iax_ie_data *ied, unsigned char ie, unsigned char version, uint64_t value)
  655. {
  656. struct _local {
  657. unsigned char version;
  658. uint64_t value;
  659. } __attribute__((packed)) newval = { version, };
  660. put_unaligned_uint64(&newval.value, htonll(value));
  661. return iax_ie_append_raw(ied, ie, &newval, (int) sizeof(newval));
  662. }
  663. int iax_ie_append_int(struct iax_ie_data *ied, unsigned char ie, unsigned int value)
  664. {
  665. unsigned int newval;
  666. newval = htonl(value);
  667. return iax_ie_append_raw(ied, ie, &newval, (int)sizeof(newval));
  668. }
  669. int iax_ie_append_short(struct iax_ie_data *ied, unsigned char ie, unsigned short value)
  670. {
  671. unsigned short newval;
  672. newval = htons(value);
  673. return iax_ie_append_raw(ied, ie, &newval, (int)sizeof(newval));
  674. }
  675. int iax_ie_append_str(struct iax_ie_data *ied, unsigned char ie, const char *str)
  676. {
  677. return iax_ie_append_raw(ied, ie, str, strlen(str));
  678. }
  679. int iax_ie_append_byte(struct iax_ie_data *ied, unsigned char ie, unsigned char dat)
  680. {
  681. return iax_ie_append_raw(ied, ie, &dat, 1);
  682. }
  683. int iax_ie_append(struct iax_ie_data *ied, unsigned char ie)
  684. {
  685. return iax_ie_append_raw(ied, ie, NULL, 0);
  686. }
  687. void iax_set_output(void (*func)(const char *))
  688. {
  689. outputf = func;
  690. }
  691. void iax_set_error(void (*func)(const char *))
  692. {
  693. errorf = func;
  694. }
  695. int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
  696. {
  697. /* Parse data into information elements */
  698. int len;
  699. int ie;
  700. char tmp[256], *tmp2;
  701. struct ast_variable *var, *var2, *prev;
  702. unsigned int count;
  703. memset(ies, 0, (int)sizeof(struct iax_ies));
  704. ies->msgcount = -1;
  705. ies->firmwarever = -1;
  706. ies->calling_ton = -1;
  707. ies->calling_tns = -1;
  708. ies->calling_pres = -1;
  709. ies->samprate = IAX_RATE_8KHZ;
  710. while(datalen >= 2) {
  711. ie = data[0];
  712. len = data[1];
  713. if (len > datalen - 2) {
  714. errorf("Information element length exceeds message size\n");
  715. return -1;
  716. }
  717. switch(ie) {
  718. case IAX_IE_CALLED_NUMBER:
  719. ies->called_number = (char *)data + 2;
  720. break;
  721. case IAX_IE_CALLING_NUMBER:
  722. ies->calling_number = (char *)data + 2;
  723. break;
  724. case IAX_IE_CALLING_ANI:
  725. ies->calling_ani = (char *)data + 2;
  726. break;
  727. case IAX_IE_CALLING_NAME:
  728. ies->calling_name = (char *)data + 2;
  729. break;
  730. case IAX_IE_CALLED_CONTEXT:
  731. ies->called_context = (char *)data + 2;
  732. break;
  733. case IAX_IE_USERNAME:
  734. ies->username = (char *)data + 2;
  735. break;
  736. case IAX_IE_PASSWORD:
  737. ies->password = (char *)data + 2;
  738. break;
  739. case IAX_IE_CODEC_PREFS:
  740. ies->codec_prefs = (char *)data + 2;
  741. break;
  742. case IAX_IE_CAPABILITY:
  743. if (len != (int)sizeof(unsigned int)) {
  744. snprintf(tmp, (int)sizeof(tmp), "Expecting capability to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  745. errorf(tmp);
  746. } else if (ies->capability == 0) { /* Don't overwrite capability2, if specified */
  747. ies->capability = ntohl(get_unaligned_uint32(data + 2));
  748. }
  749. break;
  750. case IAX_IE_CAPABILITY2:
  751. {
  752. int version = data[2];
  753. if (version == 0) {
  754. if (len != (int)sizeof(char) + sizeof(iax2_format)) {
  755. snprintf(tmp, (int)sizeof(tmp), "Expecting capability to be %d bytes long but was %d\n", (int) (sizeof(iax2_format) + sizeof(char)), len);
  756. errorf(tmp);
  757. } else {
  758. ies->capability = (iax2_format) ntohll(get_unaligned_uint64(data + 3));
  759. }
  760. } /* else unknown version */
  761. }
  762. break;
  763. case IAX_IE_FORMAT:
  764. if (len != (int)sizeof(unsigned int)) {
  765. snprintf(tmp, (int)sizeof(tmp), "Expecting format to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  766. errorf(tmp);
  767. } else if (ies->format == 0) { /* Don't overwrite format2, if specified */
  768. ies->format = ntohl(get_unaligned_uint32(data + 2));
  769. }
  770. break;
  771. case IAX_IE_FORMAT2:
  772. {
  773. int version = data[2];
  774. if (version == 0) {
  775. if (len != (int)sizeof(char) + sizeof(iax2_format)) {
  776. snprintf(tmp, (int)sizeof(tmp), "Expecting format to be %d bytes long but was %d\n", (int) (sizeof(iax2_format) + sizeof(char)), len);
  777. errorf(tmp);
  778. } else {
  779. ies->format = (iax2_format) ntohll(get_unaligned_uint64(data + 3));
  780. }
  781. } /* else unknown version */
  782. }
  783. break;
  784. case IAX_IE_LANGUAGE:
  785. ies->language = (char *)data + 2;
  786. break;
  787. case IAX_IE_VERSION:
  788. if (len != (int)sizeof(unsigned short)) {
  789. snprintf(tmp, (int)sizeof(tmp), "Expecting version to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  790. errorf(tmp);
  791. } else
  792. ies->version = ntohs(get_unaligned_uint16(data + 2));
  793. break;
  794. case IAX_IE_ADSICPE:
  795. if (len != (int)sizeof(unsigned short)) {
  796. snprintf(tmp, (int)sizeof(tmp), "Expecting adsicpe to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  797. errorf(tmp);
  798. } else
  799. ies->adsicpe = ntohs(get_unaligned_uint16(data + 2));
  800. break;
  801. case IAX_IE_SAMPLINGRATE:
  802. if (len != (int)sizeof(unsigned short)) {
  803. snprintf(tmp, (int)sizeof(tmp), "Expecting samplingrate to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  804. errorf(tmp);
  805. } else
  806. ies->samprate = ntohs(get_unaligned_uint16(data + 2));
  807. break;
  808. case IAX_IE_DNID:
  809. ies->dnid = (char *)data + 2;
  810. break;
  811. case IAX_IE_RDNIS:
  812. ies->rdnis = (char *)data + 2;
  813. break;
  814. case IAX_IE_AUTHMETHODS:
  815. if (len != (int)sizeof(unsigned short)) {
  816. snprintf(tmp, (int)sizeof(tmp), "Expecting authmethods to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  817. errorf(tmp);
  818. } else
  819. ies->authmethods = ntohs(get_unaligned_uint16(data + 2));
  820. break;
  821. case IAX_IE_ENCRYPTION:
  822. if (len != (int)sizeof(unsigned short)) {
  823. snprintf(tmp, (int)sizeof(tmp), "Expecting encryption to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  824. errorf(tmp);
  825. } else
  826. ies->encmethods = ntohs(get_unaligned_uint16(data + 2));
  827. break;
  828. case IAX_IE_CHALLENGE:
  829. ies->challenge = (char *)data + 2;
  830. break;
  831. case IAX_IE_MD5_RESULT:
  832. ies->md5_result = (char *)data + 2;
  833. break;
  834. case IAX_IE_RSA_RESULT:
  835. ies->rsa_result = (char *)data + 2;
  836. break;
  837. case IAX_IE_APPARENT_ADDR:
  838. ies->apparent_addr = ((struct sockaddr_in *)(data + 2));
  839. break;
  840. case IAX_IE_REFRESH:
  841. if (len != (int)sizeof(unsigned short)) {
  842. snprintf(tmp, (int)sizeof(tmp), "Expecting refresh to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  843. errorf(tmp);
  844. } else
  845. ies->refresh = ntohs(get_unaligned_uint16(data + 2));
  846. break;
  847. case IAX_IE_DPSTATUS:
  848. if (len != (int)sizeof(unsigned short)) {
  849. snprintf(tmp, (int)sizeof(tmp), "Expecting dpstatus to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  850. errorf(tmp);
  851. } else
  852. ies->dpstatus = ntohs(get_unaligned_uint16(data + 2));
  853. break;
  854. case IAX_IE_CALLNO:
  855. if (len != (int)sizeof(unsigned short)) {
  856. snprintf(tmp, (int)sizeof(tmp), "Expecting callno to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  857. errorf(tmp);
  858. } else
  859. ies->callno = ntohs(get_unaligned_uint16(data + 2));
  860. break;
  861. case IAX_IE_CAUSE:
  862. ies->cause = (char *)data + 2;
  863. break;
  864. case IAX_IE_CAUSECODE:
  865. if (len != 1) {
  866. snprintf(tmp, (int)sizeof(tmp), "Expecting causecode to be single byte but was %d\n", len);
  867. errorf(tmp);
  868. } else {
  869. ies->causecode = data[2];
  870. }
  871. break;
  872. case IAX_IE_IAX_UNKNOWN:
  873. if (len == 1)
  874. ies->iax_unknown = data[2];
  875. else {
  876. snprintf(tmp, (int)sizeof(tmp), "Expected single byte Unknown command, but was %d long\n", len);
  877. errorf(tmp);
  878. }
  879. break;
  880. case IAX_IE_MSGCOUNT:
  881. if (len != (int)sizeof(unsigned short)) {
  882. snprintf(tmp, (int)sizeof(tmp), "Expecting msgcount to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  883. errorf(tmp);
  884. } else
  885. ies->msgcount = ntohs(get_unaligned_uint16(data + 2));
  886. break;
  887. case IAX_IE_AUTOANSWER:
  888. ies->autoanswer = 1;
  889. break;
  890. case IAX_IE_MUSICONHOLD:
  891. ies->musiconhold = 1;
  892. break;
  893. case IAX_IE_TRANSFERID:
  894. if (len != (int)sizeof(unsigned int)) {
  895. snprintf(tmp, (int)sizeof(tmp), "Expecting transferid to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  896. errorf(tmp);
  897. } else
  898. ies->transferid = ntohl(get_unaligned_uint32(data + 2));
  899. break;
  900. case IAX_IE_DATETIME:
  901. if (len != (int)sizeof(unsigned int)) {
  902. snprintf(tmp, (int)sizeof(tmp), "Expecting date/time to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  903. errorf(tmp);
  904. } else
  905. ies->datetime = ntohl(get_unaligned_uint32(data + 2));
  906. break;
  907. case IAX_IE_FIRMWAREVER:
  908. if (len != (int)sizeof(unsigned short)) {
  909. snprintf(tmp, (int)sizeof(tmp), "Expecting firmwarever to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  910. errorf(tmp);
  911. } else
  912. ies->firmwarever = ntohs(get_unaligned_uint16(data + 2));
  913. break;
  914. case IAX_IE_DEVICETYPE:
  915. ies->devicetype = (char *)data + 2;
  916. break;
  917. case IAX_IE_SERVICEIDENT:
  918. ies->serviceident = (char *)data + 2;
  919. break;
  920. case IAX_IE_FWBLOCKDESC:
  921. if (len != (int)sizeof(unsigned int)) {
  922. snprintf(tmp, (int)sizeof(tmp), "Expected block desc to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  923. errorf(tmp);
  924. } else
  925. ies->fwdesc = ntohl(get_unaligned_uint32(data + 2));
  926. break;
  927. case IAX_IE_FWBLOCKDATA:
  928. ies->fwdata = data + 2;
  929. ies->fwdatalen = len;
  930. break;
  931. case IAX_IE_ENCKEY:
  932. ies->enckey = data + 2;
  933. ies->enckeylen = len;
  934. break;
  935. case IAX_IE_PROVVER:
  936. if (len != (int)sizeof(unsigned int)) {
  937. snprintf(tmp, (int)sizeof(tmp), "Expected provisioning version to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  938. errorf(tmp);
  939. } else {
  940. ies->provverpres = 1;
  941. ies->provver = ntohl(get_unaligned_uint32(data + 2));
  942. }
  943. break;
  944. case IAX_IE_CALLINGPRES:
  945. if (len == 1)
  946. ies->calling_pres = data[2];
  947. else {
  948. snprintf(tmp, (int)sizeof(tmp), "Expected single byte callingpres, but was %d long\n", len);
  949. errorf(tmp);
  950. }
  951. break;
  952. case IAX_IE_CALLINGTON:
  953. if (len == 1)
  954. ies->calling_ton = data[2];
  955. else {
  956. snprintf(tmp, (int)sizeof(tmp), "Expected single byte callington, but was %d long\n", len);
  957. errorf(tmp);
  958. }
  959. break;
  960. case IAX_IE_CALLINGTNS:
  961. if (len != (int)sizeof(unsigned short)) {
  962. snprintf(tmp, (int)sizeof(tmp), "Expecting callingtns to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  963. errorf(tmp);
  964. } else
  965. ies->calling_tns = ntohs(get_unaligned_uint16(data + 2));
  966. break;
  967. case IAX_IE_RR_JITTER:
  968. if (len != (int)sizeof(unsigned int)) {
  969. snprintf(tmp, (int)sizeof(tmp), "Expected jitter rr to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  970. errorf(tmp);
  971. } else {
  972. ies->rr_jitter = ntohl(get_unaligned_uint32(data + 2));
  973. }
  974. break;
  975. case IAX_IE_RR_LOSS:
  976. if (len != (int)sizeof(unsigned int)) {
  977. snprintf(tmp, (int)sizeof(tmp), "Expected loss rr to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  978. errorf(tmp);
  979. } else {
  980. ies->rr_loss = ntohl(get_unaligned_uint32(data + 2));
  981. }
  982. break;
  983. case IAX_IE_RR_PKTS:
  984. if (len != (int)sizeof(unsigned int)) {
  985. snprintf(tmp, (int)sizeof(tmp), "Expected packets rr to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  986. errorf(tmp);
  987. } else {
  988. ies->rr_pkts = ntohl(get_unaligned_uint32(data + 2));
  989. }
  990. break;
  991. case IAX_IE_RR_DELAY:
  992. if (len != (int)sizeof(unsigned short)) {
  993. snprintf(tmp, (int)sizeof(tmp), "Expected loss rr to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  994. errorf(tmp);
  995. } else {
  996. ies->rr_delay = ntohs(get_unaligned_uint16(data + 2));
  997. }
  998. break;
  999. case IAX_IE_RR_DROPPED:
  1000. if (len != (int)sizeof(unsigned int)) {
  1001. snprintf(tmp, (int)sizeof(tmp), "Expected packets rr to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  1002. errorf(tmp);
  1003. } else {
  1004. ies->rr_dropped = ntohl(get_unaligned_uint32(data + 2));
  1005. }
  1006. break;
  1007. case IAX_IE_RR_OOO:
  1008. if (len != (int)sizeof(unsigned int)) {
  1009. snprintf(tmp, (int)sizeof(tmp), "Expected packets rr to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  1010. errorf(tmp);
  1011. } else {
  1012. ies->rr_ooo = ntohl(get_unaligned_uint32(data + 2));
  1013. }
  1014. break;
  1015. case IAX_IE_VARIABLE:
  1016. ast_copy_string(tmp, (char *)data + 2, len + 1);
  1017. tmp2 = strchr(tmp, '=');
  1018. if (tmp2)
  1019. *tmp2++ = '\0';
  1020. else
  1021. tmp2 = "";
  1022. {
  1023. struct ast_str *str = ast_str_create(16);
  1024. /* Existing variable or new variable? */
  1025. for (var2 = ies->vars, prev = NULL; var2; prev = var2, var2 = var2->next) {
  1026. if (strcmp(tmp, var2->name) == 0) {
  1027. ast_str_set(&str, 0, "%s%s", var2->value, tmp2);
  1028. var = ast_variable_new(tmp, ast_str_buffer(str), var2->file);
  1029. var->next = var2->next;
  1030. if (prev) {
  1031. prev->next = var;
  1032. } else {
  1033. ies->vars = var;
  1034. }
  1035. snprintf(tmp, sizeof(tmp), "Assigned (%p)%s to (%p)%s\n", var->name, var->name, var->value, var->value);
  1036. outputf(tmp);
  1037. ast_free(var2);
  1038. break;
  1039. }
  1040. }
  1041. ast_free(str);
  1042. }
  1043. if (!var2) {
  1044. var = ast_variable_new(tmp, tmp2, "");
  1045. snprintf(tmp, sizeof(tmp), "Assigned (%p)%s to (%p)%s\n", var->name, var->name, var->value, var->value);
  1046. outputf(tmp);
  1047. var->next = ies->vars;
  1048. ies->vars = var;
  1049. }
  1050. break;
  1051. case IAX_IE_OSPTOKEN:
  1052. if ((count = data[2]) < IAX_MAX_OSPBLOCK_NUM) {
  1053. ies->osptokenblock[count] = (char *)data + 2 + 1;
  1054. ies->ospblocklength[count] = len - 1;
  1055. } else {
  1056. snprintf(tmp, (int)sizeof(tmp), "Expected OSP token block index to be 0~%d but was %d\n", IAX_MAX_OSPBLOCK_NUM - 1, count);
  1057. errorf(tmp);
  1058. }
  1059. break;
  1060. case IAX_IE_CALLTOKEN:
  1061. if (len) {
  1062. ies->calltokendata = (unsigned char *) data + 2;
  1063. }
  1064. ies->calltoken = 1;
  1065. break;
  1066. default:
  1067. snprintf(tmp, (int)sizeof(tmp), "Ignoring unknown information element '%s' (%d) of length %d\n", iax_ie2str(ie), ie, len);
  1068. outputf(tmp);
  1069. }
  1070. /* Overwrite information element with 0, to null terminate previous portion */
  1071. data[0] = 0;
  1072. datalen -= (len + 2);
  1073. data += (len + 2);
  1074. }
  1075. /* Null-terminate last field */
  1076. *data = '\0';
  1077. if (datalen) {
  1078. errorf("Invalid information element contents, strange boundary\n");
  1079. return -1;
  1080. }
  1081. return 0;
  1082. }
  1083. void iax_frame_wrap(struct iax_frame *fr, struct ast_frame *f)
  1084. {
  1085. fr->af.frametype = f->frametype;
  1086. ast_format_copy(&fr->af.subclass.format, &f->subclass.format);
  1087. fr->af.mallocd = 0; /* Our frame is static relative to the container */
  1088. fr->af.datalen = f->datalen;
  1089. fr->af.samples = f->samples;
  1090. fr->af.offset = AST_FRIENDLY_OFFSET;
  1091. fr->af.src = f->src;
  1092. fr->af.delivery.tv_sec = 0;
  1093. fr->af.delivery.tv_usec = 0;
  1094. fr->af.data.ptr = fr->afdata;
  1095. fr->af.len = f->len;
  1096. if (fr->af.datalen) {
  1097. size_t copy_len = fr->af.datalen;
  1098. if (copy_len > fr->afdatalen) {
  1099. ast_log(LOG_ERROR, "Losing frame data because destination buffer size '%d' bytes not big enough for '%d' bytes in the frame\n",
  1100. (int) fr->afdatalen, (int) fr->af.datalen);
  1101. copy_len = fr->afdatalen;
  1102. }
  1103. #if __BYTE_ORDER == __LITTLE_ENDIAN
  1104. /* We need to byte-swap slinear samples from network byte order */
  1105. if ((fr->af.frametype == AST_FRAME_VOICE) && (fr->af.subclass.format.id == AST_FORMAT_SLINEAR)) {
  1106. /* 2 bytes / sample for SLINEAR */
  1107. ast_swapcopy_samples(fr->af.data.ptr, f->data.ptr, copy_len / 2);
  1108. } else
  1109. #endif
  1110. memcpy(fr->af.data.ptr, f->data.ptr, copy_len);
  1111. }
  1112. }
  1113. struct iax_frame *iax_frame_new(int direction, int datalen, unsigned int cacheable)
  1114. {
  1115. struct iax_frame *fr = NULL;
  1116. #if !defined(LOW_MEMORY)
  1117. struct iax_frames *iax_frames = NULL;
  1118. struct iax_frame *smallest = NULL;
  1119. /* Attempt to get a frame from this thread's cache */
  1120. if ((iax_frames = ast_threadstorage_get(&frame_cache, sizeof(*iax_frames)))) {
  1121. smallest = AST_LIST_FIRST(&iax_frames->list);
  1122. AST_LIST_TRAVERSE_SAFE_BEGIN(&iax_frames->list, fr, list) {
  1123. if (fr->afdatalen >= datalen) {
  1124. size_t afdatalen = fr->afdatalen;
  1125. AST_LIST_REMOVE_CURRENT(list);
  1126. iax_frames->size--;
  1127. memset(fr, 0, sizeof(*fr));
  1128. fr->afdatalen = afdatalen;
  1129. break;
  1130. } else if (smallest->afdatalen > fr->afdatalen) {
  1131. smallest = fr;
  1132. }
  1133. }
  1134. AST_LIST_TRAVERSE_SAFE_END;
  1135. }
  1136. if (!fr) {
  1137. if (iax_frames && iax_frames->size >= FRAME_CACHE_MAX_SIZE && smallest) {
  1138. /* Make useless cache into something more useful */
  1139. AST_LIST_REMOVE(&iax_frames->list, smallest, list);
  1140. if (!(fr = ast_realloc(smallest, sizeof(*fr) + datalen))) {
  1141. AST_LIST_INSERT_TAIL(&iax_frames->list, smallest, list);
  1142. return NULL;
  1143. }
  1144. } else if (!(fr = ast_calloc_cache(1, sizeof(*fr) + datalen)))
  1145. return NULL;
  1146. fr->afdatalen = datalen;
  1147. }
  1148. #else
  1149. if (!(fr = ast_calloc(1, sizeof(*fr) + datalen)))
  1150. return NULL;
  1151. fr->afdatalen = datalen;
  1152. #endif
  1153. fr->direction = direction;
  1154. fr->retrans = -1;
  1155. fr->cacheable = cacheable;
  1156. if (fr->direction == DIRECTION_INGRESS)
  1157. ast_atomic_fetchadd_int(&iframes, 1);
  1158. else
  1159. ast_atomic_fetchadd_int(&oframes, 1);
  1160. ast_atomic_fetchadd_int(&frames, 1);
  1161. return fr;
  1162. }
  1163. void iax_frame_free(struct iax_frame *fr)
  1164. {
  1165. #if !defined(LOW_MEMORY)
  1166. struct iax_frames *iax_frames = NULL;
  1167. #endif
  1168. /* Note: does not remove from scheduler! */
  1169. if (fr->direction == DIRECTION_INGRESS)
  1170. ast_atomic_fetchadd_int(&iframes, -1);
  1171. else if (fr->direction == DIRECTION_OUTGRESS)
  1172. ast_atomic_fetchadd_int(&oframes, -1);
  1173. else {
  1174. errorf("Attempt to double free frame detected\n");
  1175. return;
  1176. }
  1177. ast_atomic_fetchadd_int(&frames, -1);
  1178. #if !defined(LOW_MEMORY)
  1179. if (!fr->cacheable || !(iax_frames = ast_threadstorage_get(&frame_cache, sizeof(*iax_frames)))) {
  1180. ast_free(fr);
  1181. return;
  1182. }
  1183. if (iax_frames->size < FRAME_CACHE_MAX_SIZE) {
  1184. fr->direction = 0;
  1185. /* Pseudo-sort: keep smaller frames at the top of the list. This should
  1186. * increase the chance that we pick the smallest applicable frame for use. */
  1187. if (AST_LIST_FIRST(&iax_frames->list) && AST_LIST_FIRST(&iax_frames->list)->afdatalen < fr->afdatalen) {
  1188. AST_LIST_INSERT_TAIL(&iax_frames->list, fr, list);
  1189. } else {
  1190. AST_LIST_INSERT_HEAD(&iax_frames->list, fr, list);
  1191. }
  1192. iax_frames->size++;
  1193. return;
  1194. }
  1195. #endif
  1196. ast_free(fr);
  1197. }
  1198. #if !defined(LOW_MEMORY)
  1199. static void frame_cache_cleanup(void *data)
  1200. {
  1201. struct iax_frames *framelist = data;
  1202. struct iax_frame *current;
  1203. while ((current = AST_LIST_REMOVE_HEAD(&framelist->list, list)))
  1204. ast_free(current);
  1205. ast_free(framelist);
  1206. }
  1207. #endif
  1208. int iax_get_frames(void) { return frames; }
  1209. int iax_get_iframes(void) { return iframes; }
  1210. int iax_get_oframes(void) { return oframes; }