iax2-parser.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Implementation of Inter-Asterisk eXchange
  5. *
  6. * Copyright (C) 2003, Digium
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <string.h>
  16. #include <netinet/in.h>
  17. #include <asterisk/frame.h>
  18. #include <asterisk/utils.h>
  19. #include <arpa/inet.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include "iax2.h"
  24. #include "iax2-parser.h"
  25. #include "iax2-provision.h"
  26. static int frames = 0;
  27. static int iframes = 0;
  28. static int oframes = 0;
  29. static void internaloutput(const char *str)
  30. {
  31. fputs(str, stdout);
  32. }
  33. static void internalerror(const char *str)
  34. {
  35. fprintf(stderr, "WARNING: %s", str);
  36. }
  37. static void (*outputf)(const char *str) = internaloutput;
  38. static void (*errorf)(const char *str) = internalerror;
  39. static void dump_addr(char *output, int maxlen, void *value, int len)
  40. {
  41. struct sockaddr_in sin;
  42. char iabuf[INET_ADDRSTRLEN];
  43. if (len == (int)sizeof(sin)) {
  44. memcpy(&sin, value, len);
  45. snprintf(output, maxlen, "IPV4 %s:%d", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port));
  46. } else {
  47. snprintf(output, maxlen, "Invalid Address");
  48. }
  49. }
  50. static void dump_string(char *output, int maxlen, void *value, int len)
  51. {
  52. maxlen--;
  53. if (maxlen > len)
  54. maxlen = len;
  55. strncpy(output,value, maxlen);
  56. output[maxlen] = '\0';
  57. }
  58. static void dump_int(char *output, int maxlen, void *value, int len)
  59. {
  60. if (len == (int)sizeof(unsigned int))
  61. snprintf(output, maxlen, "%lu", (unsigned long)ntohl(*((unsigned int *)value)));
  62. else
  63. snprintf(output, maxlen, "Invalid INT");
  64. }
  65. static void dump_short(char *output, int maxlen, void *value, int len)
  66. {
  67. if (len == (int)sizeof(unsigned short))
  68. snprintf(output, maxlen, "%d", ntohs(*((unsigned short *)value)));
  69. else
  70. snprintf(output, maxlen, "Invalid SHORT");
  71. }
  72. static void dump_byte(char *output, int maxlen, void *value, int len)
  73. {
  74. if (len == (int)sizeof(unsigned char))
  75. snprintf(output, maxlen, "%d", *((unsigned char *)value));
  76. else
  77. snprintf(output, maxlen, "Invalid BYTE");
  78. }
  79. static void dump_ipaddr(char *output, int maxlen, void *value, int len)
  80. {
  81. struct sockaddr_in sin;
  82. char iabuf[INET_ADDRSTRLEN];
  83. if (len == (int)sizeof(unsigned int)) {
  84. memcpy(&sin.sin_addr, value, len);
  85. ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr);
  86. snprintf(output, maxlen, "%s", iabuf);
  87. } else
  88. snprintf(output, maxlen, "Invalid IPADDR");
  89. }
  90. static void dump_prov_flags(char *output, int maxlen, void *value, int len)
  91. {
  92. char buf[256] = "";
  93. if (len == (int)sizeof(unsigned int))
  94. snprintf(output, maxlen, "%lu (%s)", (unsigned long)ntohl(*((unsigned int *)value)),
  95. iax_provflags2str(buf, sizeof(buf), ntohl(*((unsigned int *)value))));
  96. else
  97. snprintf(output, maxlen, "Invalid INT");
  98. }
  99. static void dump_prov_ies(char *output, int maxlen, unsigned char *iedata, int len);
  100. static void dump_prov(char *output, int maxlen, void *value, int len)
  101. {
  102. dump_prov_ies(output, maxlen, value, len);
  103. }
  104. static struct iax2_ie {
  105. int ie;
  106. char *name;
  107. void (*dump)(char *output, int maxlen, void *value, int len);
  108. } ies[] = {
  109. { IAX_IE_CALLED_NUMBER, "CALLED NUMBER", dump_string },
  110. { IAX_IE_CALLING_NUMBER, "CALLING NUMBER", dump_string },
  111. { IAX_IE_CALLING_ANI, "ANI", dump_string },
  112. { IAX_IE_CALLING_NAME, "CALLING NAME", dump_string },
  113. { IAX_IE_CALLED_CONTEXT, "CALLED CONTEXT", dump_string },
  114. { IAX_IE_USERNAME, "USERNAME", dump_string },
  115. { IAX_IE_PASSWORD, "PASSWORD", dump_string },
  116. { IAX_IE_CAPABILITY, "CAPABILITY", dump_int },
  117. { IAX_IE_FORMAT, "FORMAT", dump_int },
  118. { IAX_IE_LANGUAGE, "LANGUAGE", dump_string },
  119. { IAX_IE_VERSION, "VERSION", dump_short },
  120. { IAX_IE_ADSICPE, "ADSICPE", dump_short },
  121. { IAX_IE_DNID, "DNID", dump_string },
  122. { IAX_IE_AUTHMETHODS, "AUTHMETHODS", dump_short },
  123. { IAX_IE_CHALLENGE, "CHALLENGE", dump_string },
  124. { IAX_IE_MD5_RESULT, "MD5 RESULT", dump_string },
  125. { IAX_IE_RSA_RESULT, "RSA RESULT", dump_string },
  126. { IAX_IE_APPARENT_ADDR, "APPARENT ADDRESS", dump_addr },
  127. { IAX_IE_REFRESH, "REFRESH", dump_short },
  128. { IAX_IE_DPSTATUS, "DIALPLAN STATUS", dump_short },
  129. { IAX_IE_CALLNO, "CALL NUMBER", dump_short },
  130. { IAX_IE_CAUSE, "CAUSE", dump_string },
  131. { IAX_IE_IAX_UNKNOWN, "UNKNOWN IAX CMD", dump_byte },
  132. { IAX_IE_MSGCOUNT, "MESSAGE COUNT", dump_short },
  133. { IAX_IE_AUTOANSWER, "AUTO ANSWER REQ" },
  134. { IAX_IE_TRANSFERID, "TRANSFER ID", dump_int },
  135. { IAX_IE_RDNIS, "REFERRING DNIS", dump_string },
  136. { IAX_IE_PROVISIONING, "PROVISIONING", dump_prov },
  137. { IAX_IE_AESPROVISIONING, "AES PROVISIONG" },
  138. { IAX_IE_DATETIME, "DATE TIME", dump_int },
  139. { IAX_IE_DEVICETYPE, "DEVICE TYPE", dump_string },
  140. { IAX_IE_SERVICEIDENT, "SERVICE IDENT", dump_string },
  141. { IAX_IE_FIRMWAREVER, "FIRMWARE VER", dump_short },
  142. { IAX_IE_FWBLOCKDESC, "FW BLOCK DESC", dump_int },
  143. { IAX_IE_FWBLOCKDATA, "FW BLOCK DATA" },
  144. { IAX_IE_PROVVER, "PROVISIONG VER", dump_int },
  145. };
  146. static struct iax2_ie prov_ies[] = {
  147. { PROV_IE_USEDHCP, "USEDHCP" },
  148. { PROV_IE_IPADDR, "IPADDR", dump_ipaddr },
  149. { PROV_IE_SUBNET, "SUBNET", dump_ipaddr },
  150. { PROV_IE_GATEWAY, "GATEWAY", dump_ipaddr },
  151. { PROV_IE_PORTNO, "BINDPORT", dump_short },
  152. { PROV_IE_USER, "USERNAME", dump_string },
  153. { PROV_IE_PASS, "PASSWORD", dump_string },
  154. { PROV_IE_LANG, "LANGUAGE", dump_string },
  155. { PROV_IE_TOS, "TYPEOFSERVICE", dump_byte },
  156. { PROV_IE_FLAGS, "FLAGS", dump_prov_flags },
  157. { PROV_IE_FORMAT, "FORMAT", dump_int },
  158. { PROV_IE_AESKEY, "AESKEY" },
  159. { PROV_IE_SERVERIP, "SERVERIP", dump_ipaddr },
  160. { PROV_IE_SERVERPORT, "SERVERPORT", dump_short },
  161. { PROV_IE_NEWAESKEY, "NEWAESKEY" },
  162. { PROV_IE_PROVVER, "PROV VERSION", dump_int },
  163. { PROV_IE_ALTSERVER, "ALTSERVERIP", dump_ipaddr },
  164. };
  165. const char *iax_ie2str(int ie)
  166. {
  167. int x;
  168. for (x=0;x<(int)sizeof(ies) / (int)sizeof(ies[0]); x++) {
  169. if (ies[x].ie == ie)
  170. return ies[x].name;
  171. }
  172. return "Unknown IE";
  173. }
  174. static void dump_prov_ies(char *output, int maxlen, unsigned char *iedata, int len)
  175. {
  176. int ielen;
  177. int ie;
  178. int x;
  179. int found;
  180. char interp[80];
  181. char tmp[256];
  182. if (len < 2)
  183. return;
  184. strcpy(output, "\n");
  185. maxlen -= strlen(output); output += strlen(output);
  186. while(len > 2) {
  187. ie = iedata[0];
  188. ielen = iedata[1];
  189. if (ielen + 2> len) {
  190. snprintf(tmp, (int)sizeof(tmp), "Total Prov IE length of %d bytes exceeds remaining prov frame length of %d bytes\n", ielen + 2, len);
  191. strncpy(output, tmp, maxlen - 1);
  192. maxlen -= strlen(output); output += strlen(output);
  193. return;
  194. }
  195. found = 0;
  196. for (x=0;x<(int)sizeof(prov_ies) / (int)sizeof(prov_ies[0]); x++) {
  197. if (prov_ies[x].ie == ie) {
  198. if (prov_ies[x].dump) {
  199. prov_ies[x].dump(interp, (int)sizeof(interp), iedata + 2, ielen);
  200. snprintf(tmp, (int)sizeof(tmp), " %-15.15s : %s\n", prov_ies[x].name, interp);
  201. strncpy(output, tmp, maxlen - 1);
  202. maxlen -= strlen(output); output += strlen(output);
  203. } else {
  204. if (ielen)
  205. snprintf(interp, (int)sizeof(interp), "%d bytes", ielen);
  206. else
  207. strcpy(interp, "Present");
  208. snprintf(tmp, (int)sizeof(tmp), " %-15.15s : %s\n", prov_ies[x].name, interp);
  209. strncpy(output, tmp, maxlen - 1);
  210. maxlen -= strlen(output); output += strlen(output);
  211. }
  212. found++;
  213. }
  214. }
  215. if (!found) {
  216. snprintf(tmp, (int)sizeof(tmp), " Unknown Prov IE %03d : Present\n", ie);
  217. strncpy(output, tmp, maxlen - 1);
  218. maxlen -= strlen(output); output += strlen(output);
  219. }
  220. iedata += (2 + ielen);
  221. len -= (2 + ielen);
  222. }
  223. }
  224. static void dump_ies(unsigned char *iedata, int len)
  225. {
  226. int ielen;
  227. int ie;
  228. int x;
  229. int found;
  230. char interp[1024];
  231. char tmp[1024];
  232. if (len < 2)
  233. return;
  234. while(len > 2) {
  235. ie = iedata[0];
  236. ielen = iedata[1];
  237. if (ielen + 2> len) {
  238. snprintf(tmp, (int)sizeof(tmp), "Total IE length of %d bytes exceeds remaining frame length of %d bytes\n", ielen + 2, len);
  239. outputf(tmp);
  240. return;
  241. }
  242. found = 0;
  243. for (x=0;x<(int)sizeof(ies) / (int)sizeof(ies[0]); x++) {
  244. if (ies[x].ie == ie) {
  245. if (ies[x].dump) {
  246. ies[x].dump(interp, (int)sizeof(interp), iedata + 2, ielen);
  247. snprintf(tmp, (int)sizeof(tmp), " %-15.15s : %s\n", ies[x].name, interp);
  248. outputf(tmp);
  249. } else {
  250. if (ielen)
  251. snprintf(interp, (int)sizeof(interp), "%d bytes", ielen);
  252. else
  253. strcpy(interp, "Present");
  254. snprintf(tmp, (int)sizeof(tmp), " %-15.15s : %s\n", ies[x].name, interp);
  255. outputf(tmp);
  256. }
  257. found++;
  258. }
  259. }
  260. if (!found) {
  261. snprintf(tmp, (int)sizeof(tmp), " Unknown IE %03d : Present\n", ie);
  262. outputf(tmp);
  263. }
  264. iedata += (2 + ielen);
  265. len -= (2 + ielen);
  266. }
  267. outputf("\n");
  268. }
  269. void iax_showframe(struct iax_frame *f, struct ast_iax2_full_hdr *fhi, int rx, struct sockaddr_in *sin, int datalen)
  270. {
  271. char *frames[] = {
  272. "(0?)",
  273. "DTMF ",
  274. "VOICE ",
  275. "VIDEO ",
  276. "CONTROL",
  277. "NULL ",
  278. "IAX ",
  279. "TEXT ",
  280. "IMAGE ",
  281. "HTML ",
  282. "CNG " };
  283. char *iaxs[] = {
  284. "(0?)",
  285. "NEW ",
  286. "PING ",
  287. "PONG ",
  288. "ACK ",
  289. "HANGUP ",
  290. "REJECT ",
  291. "ACCEPT ",
  292. "AUTHREQ",
  293. "AUTHREP",
  294. "INVAL ",
  295. "LAGRQ ",
  296. "LAGRP ",
  297. "REGREQ ",
  298. "REGAUTH",
  299. "REGACK ",
  300. "REGREJ ",
  301. "REGREL ",
  302. "VNAK ",
  303. "DPREQ ",
  304. "DPREP ",
  305. "DIAL ",
  306. "TXREQ ",
  307. "TXCNT ",
  308. "TXACC ",
  309. "TXREADY",
  310. "TXREL ",
  311. "TXREJ ",
  312. "QUELCH ",
  313. "UNQULCH",
  314. "POKE",
  315. "PAGE",
  316. "MWI",
  317. "UNSUPPORTED",
  318. "TRANSFER",
  319. "PROVISION",
  320. "FWDOWNLD",
  321. "FWDATA"
  322. };
  323. char *cmds[] = {
  324. "(0?)",
  325. "HANGUP ",
  326. "RING ",
  327. "RINGING",
  328. "ANSWER ",
  329. "BUSY ",
  330. "TKOFFHK ",
  331. "OFFHOOK" };
  332. struct ast_iax2_full_hdr *fh;
  333. char retries[20];
  334. char class2[20];
  335. char subclass2[20];
  336. char *class;
  337. char *subclass;
  338. char tmp[512];
  339. char iabuf[INET_ADDRSTRLEN];
  340. if (f) {
  341. fh = f->data;
  342. snprintf(retries, (int)sizeof(retries), "%03d", f->retries);
  343. } else {
  344. fh = fhi;
  345. if (ntohs(fh->dcallno) & IAX_FLAG_RETRANS)
  346. strcpy(retries, "Yes");
  347. else
  348. strcpy(retries, " No");
  349. }
  350. if (!(ntohs(fh->scallno) & IAX_FLAG_FULL)) {
  351. /* Don't mess with mini-frames */
  352. return;
  353. }
  354. if (fh->type >= (int)sizeof(frames)/(int)sizeof(frames[0])) {
  355. snprintf(class2, (int)sizeof(class2), "(%d?)", fh->type);
  356. class = class2;
  357. } else {
  358. class = frames[(int)fh->type];
  359. }
  360. if (fh->type == AST_FRAME_DTMF) {
  361. sprintf(subclass2, "%c", fh->csub);
  362. subclass = subclass2;
  363. } else if (fh->type == AST_FRAME_IAX) {
  364. if (fh->csub >= (int)sizeof(iaxs)/(int)sizeof(iaxs[0])) {
  365. snprintf(subclass2, (int)sizeof(subclass2), "(%d?)", fh->csub);
  366. subclass = subclass2;
  367. } else {
  368. subclass = iaxs[(int)fh->csub];
  369. }
  370. } else if (fh->type == AST_FRAME_CONTROL) {
  371. if (fh->csub > (int)sizeof(cmds)/(int)sizeof(char *)) {
  372. snprintf(subclass2, (int)sizeof(subclass2), "(%d?)", fh->csub);
  373. subclass = subclass2;
  374. } else {
  375. subclass = cmds[(int)fh->csub];
  376. }
  377. } else {
  378. snprintf(subclass2, (int)sizeof(subclass2), "%d", fh->csub);
  379. subclass = subclass2;
  380. }
  381. snprintf(tmp, (int)sizeof(tmp),
  382. "%s-Frame Retry[%s] -- OSeqno: %3.3d ISeqno: %3.3d Type: %s Subclass: %s\n",
  383. (rx ? "Rx" : "Tx"),
  384. retries, fh->oseqno, fh->iseqno, class, subclass);
  385. outputf(tmp);
  386. snprintf(tmp, (int)sizeof(tmp),
  387. " Timestamp: %05lums SCall: %5.5d DCall: %5.5d [%s:%d]\n",
  388. (unsigned long)ntohl(fh->ts),
  389. ntohs(fh->scallno) & ~IAX_FLAG_FULL, ntohs(fh->dcallno) & ~IAX_FLAG_RETRANS,
  390. ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port));
  391. outputf(tmp);
  392. if (fh->type == AST_FRAME_IAX)
  393. dump_ies(fh->iedata, datalen);
  394. }
  395. int iax_ie_append_raw(struct iax_ie_data *ied, unsigned char ie, void *data, int datalen)
  396. {
  397. char tmp[256];
  398. if (datalen > ((int)sizeof(ied->buf) - ied->pos)) {
  399. 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);
  400. errorf(tmp);
  401. return -1;
  402. }
  403. ied->buf[ied->pos++] = ie;
  404. ied->buf[ied->pos++] = datalen;
  405. memcpy(ied->buf + ied->pos, data, datalen);
  406. ied->pos += datalen;
  407. return 0;
  408. }
  409. int iax_ie_append_addr(struct iax_ie_data *ied, unsigned char ie, struct sockaddr_in *sin)
  410. {
  411. return iax_ie_append_raw(ied, ie, sin, (int)sizeof(struct sockaddr_in));
  412. }
  413. int iax_ie_append_int(struct iax_ie_data *ied, unsigned char ie, unsigned int value)
  414. {
  415. unsigned int newval;
  416. newval = htonl(value);
  417. return iax_ie_append_raw(ied, ie, &newval, (int)sizeof(newval));
  418. }
  419. int iax_ie_append_short(struct iax_ie_data *ied, unsigned char ie, unsigned short value)
  420. {
  421. unsigned short newval;
  422. newval = htons(value);
  423. return iax_ie_append_raw(ied, ie, &newval, (int)sizeof(newval));
  424. }
  425. int iax_ie_append_str(struct iax_ie_data *ied, unsigned char ie, unsigned char *str)
  426. {
  427. return iax_ie_append_raw(ied, ie, str, strlen(str));
  428. }
  429. int iax_ie_append_byte(struct iax_ie_data *ied, unsigned char ie, unsigned char dat)
  430. {
  431. return iax_ie_append_raw(ied, ie, &dat, 1);
  432. }
  433. int iax_ie_append(struct iax_ie_data *ied, unsigned char ie)
  434. {
  435. return iax_ie_append_raw(ied, ie, NULL, 0);
  436. }
  437. void iax_set_output(void (*func)(const char *))
  438. {
  439. outputf = func;
  440. }
  441. void iax_set_error(void (*func)(const char *))
  442. {
  443. errorf = func;
  444. }
  445. int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
  446. {
  447. /* Parse data into information elements */
  448. int len;
  449. int ie;
  450. char tmp[256];
  451. memset(ies, 0, (int)sizeof(struct iax_ies));
  452. ies->msgcount = -1;
  453. ies->firmwarever = -1;
  454. while(datalen >= 2) {
  455. ie = data[0];
  456. len = data[1];
  457. if (len > datalen - 2) {
  458. errorf("Information element length exceeds message size\n");
  459. return -1;
  460. }
  461. switch(ie) {
  462. case IAX_IE_CALLED_NUMBER:
  463. ies->called_number = data + 2;
  464. break;
  465. case IAX_IE_CALLING_NUMBER:
  466. ies->calling_number = data + 2;
  467. break;
  468. case IAX_IE_CALLING_ANI:
  469. ies->calling_ani = data + 2;
  470. break;
  471. case IAX_IE_CALLING_NAME:
  472. ies->calling_name = data + 2;
  473. break;
  474. case IAX_IE_CALLED_CONTEXT:
  475. ies->called_context = data + 2;
  476. break;
  477. case IAX_IE_USERNAME:
  478. ies->username = data + 2;
  479. break;
  480. case IAX_IE_PASSWORD:
  481. ies->password = data + 2;
  482. break;
  483. case IAX_IE_CAPABILITY:
  484. if (len != (int)sizeof(unsigned int)) {
  485. snprintf(tmp, (int)sizeof(tmp), "Expecting capability to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  486. errorf(tmp);
  487. } else
  488. ies->capability = ntohl(*((unsigned int *)(data + 2)));
  489. break;
  490. case IAX_IE_FORMAT:
  491. if (len != (int)sizeof(unsigned int)) {
  492. snprintf(tmp, (int)sizeof(tmp), "Expecting format to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  493. errorf(tmp);
  494. } else
  495. ies->format = ntohl(*((unsigned int *)(data + 2)));
  496. break;
  497. case IAX_IE_LANGUAGE:
  498. ies->language = data + 2;
  499. break;
  500. case IAX_IE_VERSION:
  501. if (len != (int)sizeof(unsigned short)) {
  502. snprintf(tmp, (int)sizeof(tmp), "Expecting version to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  503. errorf(tmp);
  504. } else
  505. ies->version = ntohs(*((unsigned short *)(data + 2)));
  506. break;
  507. case IAX_IE_ADSICPE:
  508. if (len != (int)sizeof(unsigned short)) {
  509. snprintf(tmp, (int)sizeof(tmp), "Expecting adsicpe to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  510. errorf(tmp);
  511. } else
  512. ies->adsicpe = ntohs(*((unsigned short *)(data + 2)));
  513. break;
  514. case IAX_IE_DNID:
  515. ies->dnid = data + 2;
  516. break;
  517. case IAX_IE_RDNIS:
  518. ies->rdnis = data + 2;
  519. break;
  520. case IAX_IE_AUTHMETHODS:
  521. if (len != (int)sizeof(unsigned short)) {
  522. snprintf(tmp, (int)sizeof(tmp), "Expecting authmethods to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  523. errorf(tmp);
  524. } else
  525. ies->authmethods = ntohs(*((unsigned short *)(data + 2)));
  526. break;
  527. case IAX_IE_CHALLENGE:
  528. ies->challenge = data + 2;
  529. break;
  530. case IAX_IE_MD5_RESULT:
  531. ies->md5_result = data + 2;
  532. break;
  533. case IAX_IE_RSA_RESULT:
  534. ies->rsa_result = data + 2;
  535. break;
  536. case IAX_IE_APPARENT_ADDR:
  537. ies->apparent_addr = ((struct sockaddr_in *)(data + 2));
  538. break;
  539. case IAX_IE_REFRESH:
  540. if (len != (int)sizeof(unsigned short)) {
  541. snprintf(tmp, (int)sizeof(tmp), "Expecting refresh to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  542. errorf(tmp);
  543. } else
  544. ies->refresh = ntohs(*((unsigned short *)(data + 2)));
  545. break;
  546. case IAX_IE_DPSTATUS:
  547. if (len != (int)sizeof(unsigned short)) {
  548. snprintf(tmp, (int)sizeof(tmp), "Expecting dpstatus to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  549. errorf(tmp);
  550. } else
  551. ies->dpstatus = ntohs(*((unsigned short *)(data + 2)));
  552. break;
  553. case IAX_IE_CALLNO:
  554. if (len != (int)sizeof(unsigned short)) {
  555. snprintf(tmp, (int)sizeof(tmp), "Expecting callno to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  556. errorf(tmp);
  557. } else
  558. ies->callno = ntohs(*((unsigned short *)(data + 2)));
  559. break;
  560. case IAX_IE_CAUSE:
  561. ies->cause = data + 2;
  562. break;
  563. case IAX_IE_IAX_UNKNOWN:
  564. if (len == 1)
  565. ies->iax_unknown = data[2];
  566. else {
  567. snprintf(tmp, (int)sizeof(tmp), "Expected single byte Unknown command, but was %d long\n", len);
  568. errorf(tmp);
  569. }
  570. break;
  571. case IAX_IE_MSGCOUNT:
  572. if (len != (int)sizeof(unsigned short)) {
  573. snprintf(tmp, (int)sizeof(tmp), "Expecting msgcount to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  574. errorf(tmp);
  575. } else
  576. ies->msgcount = ntohs(*((unsigned short *)(data + 2)));
  577. break;
  578. case IAX_IE_AUTOANSWER:
  579. ies->autoanswer = 1;
  580. break;
  581. case IAX_IE_MUSICONHOLD:
  582. ies->musiconhold = 1;
  583. break;
  584. case IAX_IE_TRANSFERID:
  585. if (len != (int)sizeof(unsigned int)) {
  586. snprintf(tmp, (int)sizeof(tmp), "Expecting transferid to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  587. errorf(tmp);
  588. } else
  589. ies->transferid = ntohl(*((unsigned int *)(data + 2)));
  590. break;
  591. case IAX_IE_DATETIME:
  592. if (len != (int)sizeof(unsigned int)) {
  593. snprintf(tmp, (int)sizeof(tmp), "Expecting date/time to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  594. errorf(tmp);
  595. } else
  596. ies->datetime = ntohl(*((unsigned int *)(data + 2)));
  597. break;
  598. case IAX_IE_FIRMWAREVER:
  599. if (len != (int)sizeof(unsigned short)) {
  600. snprintf(tmp, (int)sizeof(tmp), "Expecting firmwarever to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  601. errorf(tmp);
  602. } else
  603. ies->firmwarever = ntohs(*((unsigned short *)(data + 2)));
  604. break;
  605. case IAX_IE_DEVICETYPE:
  606. ies->devicetype = data + 2;
  607. break;
  608. case IAX_IE_SERVICEIDENT:
  609. ies->serviceident = data + 2;
  610. break;
  611. case IAX_IE_FWBLOCKDESC:
  612. if (len != (int)sizeof(unsigned int)) {
  613. snprintf(tmp, (int)sizeof(tmp), "Expected block desc to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  614. errorf(tmp);
  615. } else
  616. ies->fwdesc = ntohl(*((unsigned int *)(data + 2)));
  617. break;
  618. case IAX_IE_FWBLOCKDATA:
  619. ies->fwdata = data + 2;
  620. ies->fwdatalen = len;
  621. break;
  622. case IAX_IE_PROVVER:
  623. if (len != (int)sizeof(unsigned int)) {
  624. snprintf(tmp, (int)sizeof(tmp), "Expected provisioning version to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  625. errorf(tmp);
  626. } else {
  627. ies->provverpres = 1;
  628. ies->provver = ntohl(*((unsigned int *)(data + 2)));
  629. }
  630. break;
  631. default:
  632. snprintf(tmp, (int)sizeof(tmp), "Ignoring unknown information element '%s' (%d) of length %d\n", iax_ie2str(ie), ie, len);
  633. outputf(tmp);
  634. }
  635. /* Overwrite information element with 0, to null terminate previous portion */
  636. data[0] = 0;
  637. datalen -= (len + 2);
  638. data += (len + 2);
  639. }
  640. /* Null-terminate last field */
  641. *data = '\0';
  642. if (datalen) {
  643. errorf("Invalid information element contents, strange boundary\n");
  644. return -1;
  645. }
  646. return 0;
  647. }
  648. void iax_frame_wrap(struct iax_frame *fr, struct ast_frame *f)
  649. {
  650. fr->af.frametype = f->frametype;
  651. fr->af.subclass = f->subclass;
  652. fr->af.mallocd = 0; /* Our frame is static relative to the container */
  653. fr->af.datalen = f->datalen;
  654. fr->af.samples = f->samples;
  655. fr->af.offset = AST_FRIENDLY_OFFSET;
  656. fr->af.src = f->src;
  657. fr->af.delivery.tv_sec = 0;
  658. fr->af.delivery.tv_usec = 0;
  659. fr->af.data = fr->afdata;
  660. if (fr->af.datalen) {
  661. #if __BYTE_ORDER == __LITTLE_ENDIAN
  662. /* We need to byte-swap slinear samples from network byte order */
  663. if (fr->af.subclass == AST_FORMAT_SLINEAR) {
  664. ast_memcpy_byteswap(fr->af.data, f->data, fr->af.samples);
  665. } else
  666. #endif
  667. memcpy(fr->af.data, f->data, fr->af.datalen);
  668. }
  669. }
  670. struct iax_frame *iax_frame_new(int direction, int datalen)
  671. {
  672. struct iax_frame *fr;
  673. fr = malloc((int)sizeof(struct iax_frame) + datalen);
  674. if (fr) {
  675. fr->direction = direction;
  676. fr->retrans = -1;
  677. frames++;
  678. if (fr->direction == DIRECTION_INGRESS)
  679. iframes++;
  680. else
  681. oframes++;
  682. }
  683. return fr;
  684. }
  685. void iax_frame_free(struct iax_frame *fr)
  686. {
  687. /* Note: does not remove from scheduler! */
  688. if (fr->direction == DIRECTION_INGRESS)
  689. iframes--;
  690. else if (fr->direction == DIRECTION_OUTGRESS)
  691. oframes--;
  692. else {
  693. errorf("Attempt to double free frame detected\n");
  694. return;
  695. }
  696. fr->direction = 0;
  697. free(fr);
  698. frames--;
  699. }
  700. int iax_get_frames(void) { return frames; }
  701. int iax_get_iframes(void) { return iframes; }
  702. int iax_get_oframes(void) { return oframes; }