dundi-parser.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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 Distributed Universal Number Discovery (DUNDi)
  21. *
  22. */
  23. /*** MODULEINFO
  24. <support_level>extended</support_level>
  25. ***/
  26. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  28. #include <sys/socket.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31. #include "asterisk/frame.h"
  32. #include "asterisk/utils.h"
  33. #include "asterisk/dundi.h"
  34. #include "dundi-parser.h"
  35. static void internaloutput(const char *str)
  36. {
  37. fputs(str, stdout);
  38. }
  39. static void internalerror(const char *str)
  40. {
  41. fprintf(stderr, "WARNING: %s", str);
  42. }
  43. static void (*outputf)(const char *str) = internaloutput;
  44. static void (*errorf)(const char *str) = internalerror;
  45. char *dundi_eid_to_str_short(char *s, int maxlen, dundi_eid *eid)
  46. {
  47. int x;
  48. char *os = s;
  49. if (maxlen < 13) {
  50. if (s && (maxlen > 0))
  51. *s = '\0';
  52. } else {
  53. for (x=0;x<6;x++) {
  54. sprintf(s, "%02hhX", (unsigned char)eid->eid[x]);
  55. s += 2;
  56. }
  57. }
  58. return os;
  59. }
  60. int dundi_str_short_to_eid(dundi_eid *eid, const char *s)
  61. {
  62. unsigned int eid_int[6];
  63. int x;
  64. if (sscanf(s, "%2x%2x%2x%2x%2x%2x", &eid_int[0], &eid_int[1], &eid_int[2],
  65. &eid_int[3], &eid_int[4], &eid_int[5]) != 6)
  66. return -1;
  67. for (x = 0; x < 6; x++)
  68. eid->eid[x] = eid_int[x];
  69. return 0;
  70. }
  71. int dundi_eid_zero(dundi_eid *eid)
  72. {
  73. int x;
  74. for (x = 0; x < ARRAY_LEN(eid->eid); x++)
  75. if (eid->eid[x]) return 0;
  76. return 1;
  77. }
  78. static void dump_string(char *output, int maxlen, void *value, int len)
  79. {
  80. if (maxlen > len + 1)
  81. maxlen = len + 1;
  82. snprintf(output, maxlen, "%s", (char *) value);
  83. }
  84. static void dump_cbypass(char *output, int maxlen, void *value, int len)
  85. {
  86. snprintf(output, maxlen, "Bypass Caches");
  87. }
  88. static void dump_eid(char *output, int maxlen, void *value, int len)
  89. {
  90. if (len == 6)
  91. ast_eid_to_str(output, maxlen, (dundi_eid *)value);
  92. else
  93. snprintf(output, maxlen, "Invalid EID len %d", len);
  94. }
  95. char *dundi_hint2str(char *buf, int bufsiz, int flags)
  96. {
  97. strcpy(buf, "");
  98. buf[bufsiz-1] = '\0';
  99. if (flags & DUNDI_HINT_TTL_EXPIRED) {
  100. strncat(buf, "TTLEXPIRED|", bufsiz - strlen(buf) - 1);
  101. }
  102. if (flags & DUNDI_HINT_DONT_ASK) {
  103. strncat(buf, "DONTASK|", bufsiz - strlen(buf) - 1);
  104. }
  105. if (flags & DUNDI_HINT_UNAFFECTED) {
  106. strncat(buf, "UNAFFECTED|", bufsiz - strlen(buf) - 1);
  107. }
  108. /* Get rid of trailing | */
  109. if (ast_strlen_zero(buf))
  110. strcpy(buf, "NONE|");
  111. buf[strlen(buf)-1] = '\0';
  112. return buf;
  113. }
  114. static void dump_hint(char *output, int maxlen, void *value, int len)
  115. {
  116. char tmp2[256];
  117. char tmp3[256];
  118. int datalen;
  119. struct dundi_hint *hint;
  120. if (len < sizeof(*hint)) {
  121. snprintf(output, maxlen, "<invalid contents>");
  122. return;
  123. }
  124. hint = (struct dundi_hint *) value;;
  125. datalen = len - offsetof(struct dundi_hint, data);
  126. if (datalen > sizeof(tmp3) - 1)
  127. datalen = sizeof(tmp3) - 1;
  128. memcpy(tmp3, hint->data, datalen);
  129. tmp3[datalen] = '\0';
  130. dundi_hint2str(tmp2, sizeof(tmp2), ntohs(hint->flags));
  131. if (ast_strlen_zero(tmp3))
  132. snprintf(output, maxlen, "[%s]", tmp2);
  133. else
  134. snprintf(output, maxlen, "[%s] %s", tmp2, tmp3);
  135. }
  136. static void dump_cause(char *output, int maxlen, void *value, int len)
  137. {
  138. static const char * const causes[] = {
  139. "SUCCESS",
  140. "GENERAL",
  141. "DYNAMIC",
  142. "NOAUTH" ,
  143. };
  144. char tmp2[256];
  145. struct dundi_cause *cause;
  146. int datalen;
  147. int causecode;
  148. if (len < sizeof(*cause)) {
  149. snprintf(output, maxlen, "<invalid contents>");
  150. return;
  151. }
  152. cause = (struct dundi_cause*) value;
  153. causecode = cause->causecode;
  154. datalen = len - offsetof(struct dundi_cause, desc);
  155. if (datalen > sizeof(tmp2) - 1)
  156. datalen = sizeof(tmp2) - 1;
  157. memcpy(tmp2, cause->desc, datalen);
  158. tmp2[datalen] = '\0';
  159. if (causecode < ARRAY_LEN(causes)) {
  160. if (ast_strlen_zero(tmp2))
  161. snprintf(output, maxlen, "%s", causes[causecode]);
  162. else
  163. snprintf(output, maxlen, "%s: %s", causes[causecode], tmp2);
  164. } else {
  165. if (ast_strlen_zero(tmp2))
  166. snprintf(output, maxlen, "%d", causecode);
  167. else
  168. snprintf(output, maxlen, "%d: %s", causecode, tmp2);
  169. }
  170. }
  171. static void dump_int(char *output, int maxlen, void *value, int len)
  172. {
  173. if (len == (int)sizeof(unsigned int))
  174. snprintf(output, maxlen, "%lu", (unsigned long)ntohl(*((unsigned int *)value)));
  175. else
  176. ast_copy_string(output, "Invalid INT", maxlen);
  177. }
  178. static void dump_short(char *output, int maxlen, void *value, int len)
  179. {
  180. if (len == (int)sizeof(unsigned short))
  181. snprintf(output, maxlen, "%d", ntohs(*((unsigned short *)value)));
  182. else
  183. ast_copy_string(output, "Invalid SHORT", maxlen);
  184. }
  185. static void dump_byte(char *output, int maxlen, void *value, int len)
  186. {
  187. if (len == (int)sizeof(unsigned char))
  188. snprintf(output, maxlen, "%d", *((unsigned char *)value));
  189. else
  190. ast_copy_string(output, "Invalid BYTE", maxlen);
  191. }
  192. static char *proto2str(int proto, char *buf, int bufsiz)
  193. {
  194. switch(proto) {
  195. case DUNDI_PROTO_NONE:
  196. strncpy(buf, "None", bufsiz - 1);
  197. break;
  198. case DUNDI_PROTO_IAX:
  199. strncpy(buf, "IAX", bufsiz - 1);
  200. break;
  201. case DUNDI_PROTO_SIP:
  202. strncpy(buf, "SIP", bufsiz - 1);
  203. break;
  204. case DUNDI_PROTO_H323:
  205. strncpy(buf, "H.323", bufsiz - 1);
  206. break;
  207. default:
  208. snprintf(buf, bufsiz, "Unknown Proto(%d)", proto);
  209. }
  210. buf[bufsiz-1] = '\0';
  211. return buf;
  212. }
  213. char *dundi_flags2str(char *buf, int bufsiz, int flags)
  214. {
  215. strcpy(buf, "");
  216. buf[bufsiz-1] = '\0';
  217. if (flags & DUNDI_FLAG_EXISTS) {
  218. strncat(buf, "EXISTS|", bufsiz - strlen(buf) - 1);
  219. }
  220. if (flags & DUNDI_FLAG_MATCHMORE) {
  221. strncat(buf, "MATCHMORE|", bufsiz - strlen(buf) - 1);
  222. }
  223. if (flags & DUNDI_FLAG_CANMATCH) {
  224. strncat(buf, "CANMATCH|", bufsiz - strlen(buf) - 1);
  225. }
  226. if (flags & DUNDI_FLAG_IGNOREPAT) {
  227. strncat(buf, "IGNOREPAT|", bufsiz - strlen(buf) - 1);
  228. }
  229. if (flags & DUNDI_FLAG_RESIDENTIAL) {
  230. strncat(buf, "RESIDENCE|", bufsiz - strlen(buf) - 1);
  231. }
  232. if (flags & DUNDI_FLAG_COMMERCIAL) {
  233. strncat(buf, "COMMERCIAL|", bufsiz - strlen(buf) - 1);
  234. }
  235. if (flags & DUNDI_FLAG_MOBILE) {
  236. strncat(buf, "MOBILE", bufsiz - strlen(buf) - 1);
  237. }
  238. if (flags & DUNDI_FLAG_NOUNSOLICITED) {
  239. strncat(buf, "NOUNSLCTD|", bufsiz - strlen(buf) - 1);
  240. }
  241. if (flags & DUNDI_FLAG_NOCOMUNSOLICIT) {
  242. strncat(buf, "NOCOMUNSLTD|", bufsiz - strlen(buf) - 1);
  243. }
  244. /* Get rid of trailing | */
  245. if (ast_strlen_zero(buf))
  246. strcpy(buf, "NONE|");
  247. buf[strlen(buf)-1] = '\0';
  248. return buf;
  249. }
  250. static void dump_answer(char *output, int maxlen, void *value, int len)
  251. {
  252. struct dundi_answer *answer;
  253. char proto[40];
  254. char flags[40];
  255. char eid_str[40];
  256. char tmp[512]="";
  257. int datalen;
  258. if (len < sizeof(*answer)) {
  259. snprintf(output, maxlen, "Invalid Answer");
  260. return;
  261. }
  262. answer = (struct dundi_answer *)(value);
  263. datalen = len - offsetof(struct dundi_answer, data);
  264. if (datalen > sizeof(tmp) - 1)
  265. datalen = sizeof(tmp) - 1;
  266. memcpy(tmp, answer->data, datalen);
  267. tmp[datalen] = '\0';
  268. ast_eid_to_str(eid_str, sizeof(eid_str), &answer->eid);
  269. snprintf(output, maxlen, "[%s] %d <%s/%s> from [%s]",
  270. dundi_flags2str(flags, sizeof(flags), ntohs(answer->flags)),
  271. ntohs(answer->weight),
  272. proto2str(answer->protocol, proto, sizeof(proto)),
  273. tmp, eid_str);
  274. }
  275. static void dump_encrypted(char *output, int maxlen, void *value, int len)
  276. {
  277. char iv[33];
  278. int x;
  279. if ((len > 16) && !(len % 16)) {
  280. /* Build up IV */
  281. for (x=0;x<16;x++) {
  282. snprintf(iv + (x << 1), 3, "%02hhx", ((unsigned char *)value)[x]);
  283. }
  284. snprintf(output, maxlen, "[IV %s] %d encrypted blocks\n", iv, len / 16);
  285. } else
  286. snprintf(output, maxlen, "Invalid Encrypted Datalen %d", len);
  287. }
  288. static void dump_raw(char *output, int maxlen, void *value, int len)
  289. {
  290. int x;
  291. unsigned char *u = value;
  292. output[maxlen - 1] = '\0';
  293. strcpy(output, "[ ");
  294. for (x=0;x<len;x++) {
  295. snprintf(output + strlen(output), maxlen - strlen(output) - 1, "%02hhx ", u[x]);
  296. }
  297. strncat(output + strlen(output), "]", maxlen - strlen(output) - 1);
  298. }
  299. static struct dundi_ie {
  300. int ie;
  301. char *name;
  302. void (*dump)(char *output, int maxlen, void *value, int len);
  303. } infoelts[] = {
  304. { DUNDI_IE_EID, "ENTITY IDENT", dump_eid },
  305. { DUNDI_IE_CALLED_CONTEXT, "CALLED CONTEXT", dump_string },
  306. { DUNDI_IE_CALLED_NUMBER, "CALLED NUMBER", dump_string },
  307. { DUNDI_IE_EID_DIRECT, "DIRECT EID", dump_eid },
  308. { DUNDI_IE_ANSWER, "ANSWER", dump_answer },
  309. { DUNDI_IE_TTL, "TTL", dump_short },
  310. { DUNDI_IE_VERSION, "VERSION", dump_short },
  311. { DUNDI_IE_EXPIRATION, "EXPIRATION", dump_short },
  312. { DUNDI_IE_UNKNOWN, "UKWN DUNDI CMD", dump_byte },
  313. { DUNDI_IE_CAUSE, "CAUSE", dump_cause },
  314. { DUNDI_IE_REQEID, "REQUEST EID", dump_eid },
  315. { DUNDI_IE_ENCDATA, "ENCDATA", dump_encrypted },
  316. { DUNDI_IE_SHAREDKEY, "SHAREDKEY", dump_raw },
  317. { DUNDI_IE_SIGNATURE, "SIGNATURE", dump_raw },
  318. { DUNDI_IE_KEYCRC32, "KEYCRC32", dump_int },
  319. { DUNDI_IE_HINT, "HINT", dump_hint },
  320. { DUNDI_IE_DEPARTMENT, "DEPARTMENT", dump_string },
  321. { DUNDI_IE_ORGANIZATION, "ORGANIZTN", dump_string },
  322. { DUNDI_IE_LOCALITY, "LOCALITY", dump_string },
  323. { DUNDI_IE_STATE_PROV, "STATEPROV", dump_string },
  324. { DUNDI_IE_COUNTRY, "COUNTRY", dump_string },
  325. { DUNDI_IE_EMAIL, "EMAIL", dump_string },
  326. { DUNDI_IE_PHONE, "PHONE", dump_string },
  327. { DUNDI_IE_IPADDR, "ADDRESS", dump_string },
  328. { DUNDI_IE_CACHEBYPASS, "CBYPASS", dump_cbypass },
  329. };
  330. const char *dundi_ie2str(int ie)
  331. {
  332. int x;
  333. for (x = 0; x < ARRAY_LEN(infoelts); x++) {
  334. if (infoelts[x].ie == ie)
  335. return infoelts[x].name;
  336. }
  337. return "Unknown IE";
  338. }
  339. static void dump_ies(unsigned char *iedata, int spaces, int len)
  340. {
  341. int ielen;
  342. int ie;
  343. int x;
  344. int found;
  345. char interp[1024];
  346. char tmp[1051];
  347. if (len < 2)
  348. return;
  349. while(len >= 2) {
  350. ie = iedata[0];
  351. ielen = iedata[1];
  352. /* Encrypted data is the remainder */
  353. if (ie == DUNDI_IE_ENCDATA)
  354. ielen = len - 2;
  355. if (ielen + 2> len) {
  356. snprintf(tmp, (int)sizeof(tmp), "Total IE length of %d bytes exceeds remaining frame length of %d bytes\n", ielen + 2, len);
  357. outputf(tmp);
  358. return;
  359. }
  360. found = 0;
  361. for (x = 0; x < ARRAY_LEN(infoelts); x++) {
  362. if (infoelts[x].ie == ie) {
  363. if (infoelts[x].dump) {
  364. infoelts[x].dump(interp, (int)sizeof(interp), iedata + 2, ielen);
  365. snprintf(tmp, (int)sizeof(tmp), " %s%-15.15s : %s\n", (spaces ? " " : "" ), infoelts[x].name, interp);
  366. outputf(tmp);
  367. } else {
  368. if (ielen)
  369. snprintf(interp, (int)sizeof(interp), "%d bytes", ielen);
  370. else
  371. strcpy(interp, "Present");
  372. snprintf(tmp, (int)sizeof(tmp), " %s%-15.15s : %s\n", (spaces ? " " : "" ), infoelts[x].name, interp);
  373. outputf(tmp);
  374. }
  375. found++;
  376. }
  377. }
  378. if (!found) {
  379. snprintf(tmp, (int)sizeof(tmp), " %sUnknown IE %03d : Present\n", (spaces ? " " : "" ), ie);
  380. outputf(tmp);
  381. }
  382. iedata += (2 + ielen);
  383. len -= (2 + ielen);
  384. }
  385. outputf("\n");
  386. }
  387. void dundi_showframe(struct dundi_hdr *fhi, int rx, struct sockaddr_in *sin, int datalen)
  388. {
  389. char *pref[] = {
  390. "Tx",
  391. "Rx",
  392. " ETx",
  393. " Erx" };
  394. char *commands[] = {
  395. "ACK ",
  396. "DPDISCOVER ",
  397. "DPRESPONSE ",
  398. "EIDQUERY ",
  399. "EIDRESPONSE ",
  400. "PRECACHERQ ",
  401. "PRECACHERP ",
  402. "INVALID ",
  403. "UNKNOWN CMD ",
  404. "NULL ",
  405. "REGREQ ",
  406. "REGRESPONSE ",
  407. "CANCEL ",
  408. "ENCRYPT ",
  409. "ENCREJ " };
  410. char class2[20];
  411. char *class;
  412. char subclass2[20];
  413. char *subclass;
  414. char tmp[256];
  415. if ((fhi->cmdresp & 0x3f) >= ARRAY_LEN(commands)) {
  416. snprintf(class2, sizeof(class2), "(%d?)", fhi->cmdresp & 0x3f);
  417. class = class2;
  418. } else {
  419. class = commands[fhi->cmdresp & 0x3f];
  420. }
  421. snprintf(subclass2, sizeof(subclass2), "%02hhx", (unsigned char)fhi->cmdflags);
  422. subclass = subclass2;
  423. snprintf(tmp, sizeof(tmp),
  424. "%s-Frame -- OSeqno: %3.3d ISeqno: %3.3d Type: %s (%s)\n",
  425. pref[rx],
  426. fhi->oseqno, fhi->iseqno, class, fhi->cmdresp & 0x40 ? "Response" : "Command");
  427. outputf(tmp);
  428. snprintf(tmp, (int)sizeof(tmp),
  429. "%s Flags: %s STrans: %5.5d DTrans: %5.5d [%s:%d]%s\n", (rx > 1) ? " " : "",
  430. subclass, ntohs(fhi->strans) & ~DUNDI_FLAG_RESERVED, ntohs(fhi->dtrans) & ~DUNDI_FLAG_RETRANS,
  431. ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port),
  432. fhi->cmdresp & 0x80 ? " (Final)" : "");
  433. outputf(tmp);
  434. dump_ies(fhi->ies, rx > 1, datalen);
  435. }
  436. int dundi_ie_append_raw(struct dundi_ie_data *ied, unsigned char ie, void *data, int datalen)
  437. {
  438. char tmp[256];
  439. if (datalen > ((int)sizeof(ied->buf) - ied->pos)) {
  440. snprintf(tmp, (int)sizeof(tmp), "Out of space for ie '%s' (%d), need %d have %d\n", dundi_ie2str(ie), ie, datalen, (int)sizeof(ied->buf) - ied->pos);
  441. errorf(tmp);
  442. return -1;
  443. }
  444. ied->buf[ied->pos++] = ie;
  445. ied->buf[ied->pos++] = datalen;
  446. memcpy(ied->buf + ied->pos, data, datalen);
  447. ied->pos += datalen;
  448. return 0;
  449. }
  450. int dundi_ie_append_cause(struct dundi_ie_data *ied, unsigned char ie, unsigned char cause, char *data)
  451. {
  452. char tmp[256];
  453. int datalen = data ? strlen(data) + 1 : 1;
  454. if (datalen > ((int)sizeof(ied->buf) - ied->pos)) {
  455. snprintf(tmp, (int)sizeof(tmp), "Out of space for ie '%s' (%d), need %d have %d\n", dundi_ie2str(ie), ie, datalen, (int)sizeof(ied->buf) - ied->pos);
  456. errorf(tmp);
  457. return -1;
  458. }
  459. ied->buf[ied->pos++] = ie;
  460. ied->buf[ied->pos++] = datalen;
  461. ied->buf[ied->pos++] = cause;
  462. if (data) {
  463. memcpy(ied->buf + ied->pos, data, datalen-1);
  464. ied->pos += datalen-1;
  465. }
  466. return 0;
  467. }
  468. int dundi_ie_append_hint(struct dundi_ie_data *ied, unsigned char ie, unsigned short flags, char *data)
  469. {
  470. char tmp[256];
  471. int datalen = data ? strlen(data) + 2 : 2;
  472. if (datalen > ((int)sizeof(ied->buf) - ied->pos)) {
  473. snprintf(tmp, (int)sizeof(tmp), "Out of space for ie '%s' (%d), need %d have %d\n", dundi_ie2str(ie), ie, datalen, (int)sizeof(ied->buf) - ied->pos);
  474. errorf(tmp);
  475. return -1;
  476. }
  477. ied->buf[ied->pos++] = ie;
  478. ied->buf[ied->pos++] = datalen;
  479. flags = htons(flags);
  480. memcpy(ied->buf + ied->pos, &flags, sizeof(flags));
  481. ied->pos += 2;
  482. if (data) {
  483. memcpy(ied->buf + ied->pos, data, datalen-2);
  484. ied->pos += datalen-2;
  485. }
  486. return 0;
  487. }
  488. int dundi_ie_append_encdata(struct dundi_ie_data *ied, unsigned char ie, unsigned char *iv, void *data, int datalen)
  489. {
  490. char tmp[256];
  491. datalen += 16;
  492. if (datalen > ((int)sizeof(ied->buf) - ied->pos)) {
  493. snprintf(tmp, (int)sizeof(tmp), "Out of space for ie '%s' (%d), need %d have %d\n", dundi_ie2str(ie), ie, datalen, (int)sizeof(ied->buf) - ied->pos);
  494. errorf(tmp);
  495. return -1;
  496. }
  497. ied->buf[ied->pos++] = ie;
  498. ied->buf[ied->pos++] = datalen;
  499. memcpy(ied->buf + ied->pos, iv, 16);
  500. ied->pos += 16;
  501. if (data) {
  502. memcpy(ied->buf + ied->pos, data, datalen-16);
  503. ied->pos += datalen-16;
  504. }
  505. return 0;
  506. }
  507. int dundi_ie_append_answer(struct dundi_ie_data *ied, unsigned char ie, dundi_eid *eid, unsigned char protocol, unsigned short flags, unsigned short weight, char *data)
  508. {
  509. char tmp[256];
  510. int datalen = data ? strlen(data) + 11 : 11;
  511. int x;
  512. unsigned short myw;
  513. if (datalen > ((int)sizeof(ied->buf) - ied->pos)) {
  514. snprintf(tmp, (int)sizeof(tmp), "Out of space for ie '%s' (%d), need %d have %d\n", dundi_ie2str(ie), ie, datalen, (int)sizeof(ied->buf) - ied->pos);
  515. errorf(tmp);
  516. return -1;
  517. }
  518. ied->buf[ied->pos++] = ie;
  519. ied->buf[ied->pos++] = datalen;
  520. for (x=0;x<6;x++)
  521. ied->buf[ied->pos++] = eid->eid[x];
  522. ied->buf[ied->pos++] = protocol;
  523. myw = htons(flags);
  524. memcpy(ied->buf + ied->pos, &myw, 2);
  525. ied->pos += 2;
  526. myw = htons(weight);
  527. memcpy(ied->buf + ied->pos, &myw, 2);
  528. ied->pos += 2;
  529. memcpy(ied->buf + ied->pos, data, datalen-11);
  530. ied->pos += datalen-11;
  531. return 0;
  532. }
  533. int dundi_ie_append_addr(struct dundi_ie_data *ied, unsigned char ie, struct sockaddr_in *sin)
  534. {
  535. return dundi_ie_append_raw(ied, ie, sin, (int)sizeof(struct sockaddr_in));
  536. }
  537. int dundi_ie_append_int(struct dundi_ie_data *ied, unsigned char ie, unsigned int value)
  538. {
  539. unsigned int newval;
  540. newval = htonl(value);
  541. return dundi_ie_append_raw(ied, ie, &newval, (int)sizeof(newval));
  542. }
  543. int dundi_ie_append_short(struct dundi_ie_data *ied, unsigned char ie, unsigned short value)
  544. {
  545. unsigned short newval;
  546. newval = htons(value);
  547. return dundi_ie_append_raw(ied, ie, &newval, (int)sizeof(newval));
  548. }
  549. int dundi_ie_append_str(struct dundi_ie_data *ied, unsigned char ie, char *str)
  550. {
  551. return dundi_ie_append_raw(ied, ie, str, strlen(str));
  552. }
  553. int dundi_ie_append_eid(struct dundi_ie_data *ied, unsigned char ie, dundi_eid *eid)
  554. {
  555. return dundi_ie_append_raw(ied, ie, (unsigned char *)eid, sizeof(dundi_eid));
  556. }
  557. int dundi_ie_append_byte(struct dundi_ie_data *ied, unsigned char ie, unsigned char dat)
  558. {
  559. return dundi_ie_append_raw(ied, ie, &dat, 1);
  560. }
  561. int dundi_ie_append(struct dundi_ie_data *ied, unsigned char ie)
  562. {
  563. return dundi_ie_append_raw(ied, ie, NULL, 0);
  564. }
  565. void dundi_set_output(void (*func)(const char *))
  566. {
  567. outputf = func;
  568. }
  569. void dundi_set_error(void (*func)(const char *))
  570. {
  571. errorf = func;
  572. }
  573. int dundi_parse_ies(struct dundi_ies *ies, unsigned char *data, int datalen)
  574. {
  575. /* Parse data into information elements */
  576. int len;
  577. int ie;
  578. char tmp[256];
  579. memset(ies, 0, (int)sizeof(struct dundi_ies));
  580. ies->ttl = -1;
  581. ies->expiration = -1;
  582. ies->unknowncmd = -1;
  583. ies->cause = -1;
  584. while(datalen >= 2) {
  585. ie = data[0];
  586. len = data[1];
  587. if (len > datalen - 2) {
  588. errorf("Information element length exceeds message size\n");
  589. return -1;
  590. }
  591. switch(ie) {
  592. case DUNDI_IE_EID:
  593. case DUNDI_IE_EID_DIRECT:
  594. if (len != (int)sizeof(dundi_eid)) {
  595. errorf("Improper entity identifer, expecting 6 bytes!\n");
  596. } else if (ies->eidcount < DUNDI_MAX_STACK) {
  597. ies->eids[ies->eidcount] = (dundi_eid *)(data + 2);
  598. ies->eid_direct[ies->eidcount] = (ie == DUNDI_IE_EID_DIRECT);
  599. ies->eidcount++;
  600. } else
  601. errorf("Too many entities in stack!\n");
  602. break;
  603. case DUNDI_IE_REQEID:
  604. if (len != (int)sizeof(dundi_eid)) {
  605. errorf("Improper requested entity identifer, expecting 6 bytes!\n");
  606. } else
  607. ies->reqeid = (dundi_eid *)(data + 2);
  608. break;
  609. case DUNDI_IE_CALLED_CONTEXT:
  610. ies->called_context = (char *)data + 2;
  611. break;
  612. case DUNDI_IE_CALLED_NUMBER:
  613. ies->called_number = (char *)data + 2;
  614. break;
  615. case DUNDI_IE_ANSWER:
  616. if (len < sizeof(struct dundi_answer)) {
  617. snprintf(tmp, (int)sizeof(tmp), "Answer expected to be >=%d bytes long but was %d\n", (int)sizeof(struct dundi_answer), len);
  618. errorf(tmp);
  619. } else {
  620. if (ies->anscount < DUNDI_MAX_ANSWERS)
  621. ies->answers[ies->anscount++]= (struct dundi_answer *)(data + 2);
  622. else
  623. errorf("Ignoring extra answers!\n");
  624. }
  625. break;
  626. case DUNDI_IE_TTL:
  627. if (len != (int)sizeof(unsigned short)) {
  628. snprintf(tmp, (int)sizeof(tmp), "Expecting ttl to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  629. errorf(tmp);
  630. } else
  631. ies->ttl = ntohs(*((unsigned short *)(data + 2)));
  632. break;
  633. case DUNDI_IE_VERSION:
  634. if (len != (int)sizeof(unsigned short)) {
  635. snprintf(tmp, (int)sizeof(tmp), "Expecting version to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  636. errorf(tmp);
  637. } else
  638. ies->version = ntohs(*((unsigned short *)(data + 2)));
  639. break;
  640. case DUNDI_IE_EXPIRATION:
  641. if (len != (int)sizeof(unsigned short)) {
  642. snprintf(tmp, (int)sizeof(tmp), "Expecting expiration to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
  643. errorf(tmp);
  644. } else
  645. ies->expiration = ntohs(*((unsigned short *)(data + 2)));
  646. break;
  647. case DUNDI_IE_KEYCRC32:
  648. if (len != (int)sizeof(unsigned int)) {
  649. snprintf(tmp, (int)sizeof(tmp), "Expecting expiration to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
  650. errorf(tmp);
  651. } else
  652. ies->keycrc32 = ntohl(*((unsigned int *)(data + 2)));
  653. break;
  654. case DUNDI_IE_UNKNOWN:
  655. if (len == 1)
  656. ies->unknowncmd = data[2];
  657. else {
  658. snprintf(tmp, (int)sizeof(tmp), "Expected single byte Unknown command, but was %d long\n", len);
  659. errorf(tmp);
  660. }
  661. break;
  662. case DUNDI_IE_CAUSE:
  663. if (len >= 1) {
  664. ies->cause = data[2];
  665. ies->causestr = (char *)data + 3;
  666. } else {
  667. snprintf(tmp, (int)sizeof(tmp), "Expected at least one byte cause, but was %d long\n", len);
  668. errorf(tmp);
  669. }
  670. break;
  671. case DUNDI_IE_HINT:
  672. if (len >= 2) {
  673. ies->hint = (struct dundi_hint *)(data + 2);
  674. } else {
  675. snprintf(tmp, (int)sizeof(tmp), "Expected at least two byte hint, but was %d long\n", len);
  676. errorf(tmp);
  677. }
  678. break;
  679. case DUNDI_IE_DEPARTMENT:
  680. ies->q_dept = (char *)data + 2;
  681. break;
  682. case DUNDI_IE_ORGANIZATION:
  683. ies->q_org = (char *)data + 2;
  684. break;
  685. case DUNDI_IE_LOCALITY:
  686. ies->q_locality = (char *)data + 2;
  687. break;
  688. case DUNDI_IE_STATE_PROV:
  689. ies->q_stateprov = (char *)data + 2;
  690. break;
  691. case DUNDI_IE_COUNTRY:
  692. ies->q_country = (char *)data + 2;
  693. break;
  694. case DUNDI_IE_EMAIL:
  695. ies->q_email = (char *)data + 2;
  696. break;
  697. case DUNDI_IE_PHONE:
  698. ies->q_phone = (char *)data + 2;
  699. break;
  700. case DUNDI_IE_IPADDR:
  701. ies->q_ipaddr = (char *)data + 2;
  702. break;
  703. case DUNDI_IE_ENCDATA:
  704. /* Recalculate len as the remainder of the message, regardless of
  705. theoretical length */
  706. len = datalen - 2;
  707. if ((len > 16) && !(len % 16)) {
  708. ies->encblock = (struct dundi_encblock *)(data + 2);
  709. ies->enclen = len - 16;
  710. } else {
  711. snprintf(tmp, (int)sizeof(tmp), "Invalid encrypted data length %d\n", len);
  712. errorf(tmp);
  713. }
  714. break;
  715. case DUNDI_IE_SHAREDKEY:
  716. if (len == 128) {
  717. ies->encsharedkey = (unsigned char *)(data + 2);
  718. } else {
  719. snprintf(tmp, (int)sizeof(tmp), "Invalid encrypted shared key length %d\n", len);
  720. errorf(tmp);
  721. }
  722. break;
  723. case DUNDI_IE_SIGNATURE:
  724. if (len == 128) {
  725. ies->encsig = (unsigned char *)(data + 2);
  726. } else {
  727. snprintf(tmp, (int)sizeof(tmp), "Invalid encrypted signature length %d\n", len);
  728. errorf(tmp);
  729. }
  730. break;
  731. case DUNDI_IE_CACHEBYPASS:
  732. ies->cbypass = 1;
  733. break;
  734. default:
  735. snprintf(tmp, (int)sizeof(tmp), "Ignoring unknown information element '%s' (%d) of length %d\n", dundi_ie2str(ie), ie, len);
  736. outputf(tmp);
  737. }
  738. /* Overwrite information element with 0, to null terminate previous portion */
  739. data[0] = 0;
  740. datalen -= (len + 2);
  741. data += (len + 2);
  742. }
  743. /* Null-terminate last field */
  744. *data = '\0';
  745. if (datalen) {
  746. errorf("Invalid information element contents, strange boundary\n");
  747. return -1;
  748. }
  749. return 0;
  750. }