parser.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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%2.2x", (unsigned)*((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[1024];
  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 || !(iax_frames = ast_threadstorage_get(&frame_cache, sizeof(*iax_frames)))) {
  1222. ast_free(fr);
  1223. return;
  1224. }
  1225. if (iax_frames->size < FRAME_CACHE_MAX_SIZE) {
  1226. fr->direction = 0;
  1227. /* Pseudo-sort: keep smaller frames at the top of the list. This should
  1228. * increase the chance that we pick the smallest applicable frame for use. */
  1229. if (AST_LIST_FIRST(&iax_frames->list) && AST_LIST_FIRST(&iax_frames->list)->afdatalen < fr->afdatalen) {
  1230. AST_LIST_INSERT_TAIL(&iax_frames->list, fr, list);
  1231. } else {
  1232. AST_LIST_INSERT_HEAD(&iax_frames->list, fr, list);
  1233. }
  1234. iax_frames->size++;
  1235. return;
  1236. }
  1237. #endif
  1238. ast_free(fr);
  1239. }
  1240. #if !defined(LOW_MEMORY)
  1241. static void frame_cache_cleanup(void *data)
  1242. {
  1243. struct iax_frames *framelist = data;
  1244. struct iax_frame *current;
  1245. while ((current = AST_LIST_REMOVE_HEAD(&framelist->list, list)))
  1246. ast_free(current);
  1247. ast_free(framelist);
  1248. }
  1249. #endif
  1250. int iax_get_frames(void) { return frames; }
  1251. int iax_get_iframes(void) { return iframes; }
  1252. int iax_get_oframes(void) { return oframes; }