parser.c 38 KB

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