2 * Asterisk -- A telephony toolkit for Linux.
4 * Implementation of Inter-Asterisk eXchange
6 * Copyright (C) 2003, Digium
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <sys/types.h>
15 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <asterisk/frame.h>
19 #include <arpa/inet.h>
24 #include "iax2-parser.h"
27 static int frames = 0;
28 static int iframes = 0;
29 static int oframes = 0;
31 static void internaloutput(const char *str)
36 static void internalerror(const char *str)
38 fprintf(stderr, "WARNING: %s", str);
41 static void (*outputf)(const char *str) = internaloutput;
42 static void (*errorf)(const char *str) = internalerror;
44 static void dump_addr(char *output, int maxlen, void *value, int len)
46 struct sockaddr_in sin;
47 if (len == sizeof(sin)) {
48 memcpy(&sin, value, len);
49 snprintf(output, maxlen, "IPV4 %s:%d", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
51 snprintf(output, maxlen, "Invalid Address");
55 static void dump_string(char *output, int maxlen, void *value, int len)
60 strncpy(output,value, maxlen);
61 output[maxlen] = '\0';
64 static void dump_int(char *output, int maxlen, void *value, int len)
66 if (len == sizeof(unsigned int))
67 snprintf(output, maxlen, "%d", ntohl(*((unsigned int *)value)));
69 snprintf(output, maxlen, "Invalid INT");
72 static void dump_short(char *output, int maxlen, void *value, int len)
74 if (len == sizeof(unsigned short))
75 snprintf(output, maxlen, "%d", ntohs(*((unsigned short *)value)));
77 snprintf(output, maxlen, "Invalid SHORT");
80 static void dump_byte(char *output, int maxlen, void *value, int len)
82 if (len == sizeof(unsigned char))
83 snprintf(output, maxlen, "%d", ntohs(*((unsigned char *)value)));
85 snprintf(output, maxlen, "Invalid BYTE");
88 static struct iax2_ie {
91 void (*dump)(char *output, int maxlen, void *value, int len);
93 { IAX_IE_CALLED_NUMBER, "CALLED NUMBER", dump_string },
94 { IAX_IE_CALLING_NUMBER, "CALLING NUMBER", dump_string },
95 { IAX_IE_CALLING_ANI, "ANI", dump_string },
96 { IAX_IE_CALLING_NAME, "CALLING NAME", dump_string },
97 { IAX_IE_CALLED_CONTEXT, "CALLED CONTEXT", dump_string },
98 { IAX_IE_USERNAME, "USERNAME", dump_string },
99 { IAX_IE_PASSWORD, "PASSWORD", dump_string },
100 { IAX_IE_CAPABILITY, "CAPABILITY", dump_int },
101 { IAX_IE_FORMAT, "FORMAT", dump_int },
102 { IAX_IE_LANGUAGE, "LANGUAGE", dump_string },
103 { IAX_IE_VERSION, "VERSION", dump_short },
104 { IAX_IE_ADSICPE, "ADSICPE", dump_short },
105 { IAX_IE_DNID, "DNID", dump_string },
106 { IAX_IE_AUTHMETHODS, "AUTHMETHODS", dump_short },
107 { IAX_IE_CHALLENGE, "CHALLENGE", dump_string },
108 { IAX_IE_MD5_RESULT, "MD5 RESULT", dump_string },
109 { IAX_IE_RSA_RESULT, "RSA RESULT", dump_string },
110 { IAX_IE_APPARENT_ADDR, "APPARENT ADDRESS", dump_addr },
111 { IAX_IE_REFRESH, "REFRESH", dump_short },
112 { IAX_IE_DPSTATUS, "DIALPLAN STATUS", dump_short },
113 { IAX_IE_CALLNO, "CALL NUMBER", dump_short },
114 { IAX_IE_CAUSE, "CAUSE", dump_string },
115 { IAX_IE_IAX_UNKNOWN, "UNKNOWN IAX CMD", dump_byte },
116 { IAX_IE_MSGCOUNT, "MESSAGE COUNT", dump_short },
117 { IAX_IE_AUTOANSWER, "AUTO ANSWER REQ" },
118 { IAX_IE_TRANSFERID, "TRANSFER ID", dump_int },
119 { IAX_IE_RDNIS, "REFERRING DNIS", dump_string },
122 const char *iax_ie2str(int ie)
125 for (x=0;x<sizeof(ies) / sizeof(ies[0]); x++) {
132 static void dump_ies(unsigned char *iedata, int len)
145 if (ielen + 2> len) {
146 snprintf(tmp, sizeof(tmp), "Total IE length of %d bytes exceeds remaining frame length of %d bytes\n", ielen + 2, len);
151 for (x=0;x<sizeof(ies) / sizeof(ies[0]); x++) {
152 if (ies[x].ie == ie) {
154 ies[x].dump(interp, sizeof(interp), iedata + 2, ielen);
155 snprintf(tmp, sizeof(tmp), " %-15.15s : %s\n", ies[x].name, interp);
158 snprintf(tmp, sizeof(tmp), " %-15.15s : Present\n", ies[x].name);
165 snprintf(tmp, sizeof(tmp), " Unknown IE %03d : Present\n", ie);
168 iedata += (2 + ielen);
174 void iax_showframe(struct iax_frame *f, struct ast_iax2_full_hdr *fhi, int rx, struct sockaddr_in *sin, int datalen)
232 struct ast_iax2_full_hdr *fh;
241 snprintf(retries, sizeof(retries), "%03d", f->retries);
244 if (ntohs(fh->dcallno) & IAX_FLAG_RETRANS)
245 strcpy(retries, "Yes");
247 strcpy(retries, "No");
249 if (!(ntohs(fh->scallno) & IAX_FLAG_FULL)) {
250 /* Don't mess with mini-frames */
253 if (fh->type > sizeof(frames)/sizeof(char *)) {
254 snprintf(class2, sizeof(class2), "(%d?)", fh->type);
257 class = frames[(int)fh->type];
259 if (fh->type == AST_FRAME_DTMF) {
260 sprintf(subclass2, "%c", fh->csub);
261 subclass = subclass2;
262 } else if (fh->type == AST_FRAME_IAX) {
263 if (fh->csub >= sizeof(iaxs)/sizeof(iaxs[0])) {
264 snprintf(subclass2, sizeof(subclass2), "(%d?)", fh->csub);
265 subclass = subclass2;
267 subclass = iaxs[(int)fh->csub];
269 } else if (fh->type == AST_FRAME_CONTROL) {
270 if (fh->csub > sizeof(cmds)/sizeof(char *)) {
271 snprintf(subclass2, sizeof(subclass2), "(%d?)", fh->csub);
272 subclass = subclass2;
274 subclass = cmds[(int)fh->csub];
277 snprintf(subclass2, sizeof(subclass2), "%d", fh->csub);
278 subclass = subclass2;
280 snprintf(tmp, sizeof(tmp),
281 "%s-Frame Retry[%s] -- OSeqno: %3.3d ISeqno: %3.3d Type: %s Subclass: %s\n",
283 retries, fh->oseqno, fh->iseqno, class, subclass);
285 snprintf(tmp, sizeof(tmp),
286 " Timestamp: %05dms SCall: %5.5d DCall: %5.5d [%s:%d]\n",
288 ntohs(fh->scallno) & ~IAX_FLAG_FULL, ntohs(fh->dcallno) & ~IAX_FLAG_RETRANS,
289 inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
291 if (fh->type == AST_FRAME_IAX)
292 dump_ies(fh->iedata, datalen);
295 int iax_ie_append_raw(struct iax_ie_data *ied, unsigned char ie, void *data, int datalen)
298 if (datalen > (sizeof(ied->buf) - ied->pos)) {
299 snprintf(tmp, sizeof(tmp), "Out of space for ie '%s' (%d), need %d have %d\n", iax_ie2str(ie), ie, datalen, sizeof(ied->buf) - ied->pos);
303 ied->buf[ied->pos++] = ie;
304 ied->buf[ied->pos++] = datalen;
305 memcpy(ied->buf + ied->pos, data, datalen);
310 int iax_ie_append_addr(struct iax_ie_data *ied, unsigned char ie, struct sockaddr_in *sin)
312 return iax_ie_append_raw(ied, ie, sin, sizeof(struct sockaddr_in));
315 int iax_ie_append_int(struct iax_ie_data *ied, unsigned char ie, unsigned int value)
318 newval = htonl(value);
319 return iax_ie_append_raw(ied, ie, &newval, sizeof(newval));
322 int iax_ie_append_short(struct iax_ie_data *ied, unsigned char ie, unsigned short value)
324 unsigned short newval;
325 newval = htons(value);
326 return iax_ie_append_raw(ied, ie, &newval, sizeof(newval));
329 int iax_ie_append_str(struct iax_ie_data *ied, unsigned char ie, unsigned char *str)
331 return iax_ie_append_raw(ied, ie, str, strlen(str));
334 int iax_ie_append_byte(struct iax_ie_data *ied, unsigned char ie, unsigned char dat)
336 return iax_ie_append_raw(ied, ie, &dat, 1);
339 int iax_ie_append(struct iax_ie_data *ied, unsigned char ie)
341 return iax_ie_append_raw(ied, ie, NULL, 0);
344 void iax_set_output(void (*func)(const char *))
349 void iax_set_error(void (*func)(const char *))
354 int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
356 /* Parse data into information elements */
360 memset(ies, 0, sizeof(struct iax_ies));
362 while(datalen >= 2) {
365 if (len > datalen - 2) {
366 errorf("Information element length exceeds message size\n");
370 case IAX_IE_CALLED_NUMBER:
371 ies->called_number = data + 2;
373 case IAX_IE_CALLING_NUMBER:
374 ies->calling_number = data + 2;
376 case IAX_IE_CALLING_ANI:
377 ies->calling_ani = data + 2;
379 case IAX_IE_CALLING_NAME:
380 ies->calling_name = data + 2;
382 case IAX_IE_CALLED_CONTEXT:
383 ies->called_context = data + 2;
385 case IAX_IE_USERNAME:
386 ies->username = data + 2;
388 case IAX_IE_PASSWORD:
389 ies->password = data + 2;
391 case IAX_IE_CAPABILITY:
392 if (len != sizeof(unsigned int)) {
393 snprintf(tmp, sizeof(tmp), "Expecting capability to be %d bytes long but was %d\n", sizeof(unsigned int), len);
396 ies->capability = ntohl(*((unsigned int *)(data + 2)));
399 if (len != sizeof(unsigned int)) {
400 snprintf(tmp, sizeof(tmp), "Expecting format to be %d bytes long but was %d\n", sizeof(unsigned int), len);
403 ies->format = ntohl(*((unsigned int *)(data + 2)));
405 case IAX_IE_LANGUAGE:
406 ies->language = data + 2;
409 if (len != sizeof(unsigned short)) {
410 snprintf(tmp, sizeof(tmp), "Expecting version to be %d bytes long but was %d\n", sizeof(unsigned short), len);
413 ies->version = ntohs(*((unsigned short *)(data + 2)));
416 if (len != sizeof(unsigned short)) {
417 snprintf(tmp, sizeof(tmp), "Expecting adsicpe to be %d bytes long but was %d\n", sizeof(unsigned short), len);
420 ies->adsicpe = ntohs(*((unsigned short *)(data + 2)));
423 ies->dnid = data + 2;
426 ies->rdnis = data + 2;
428 case IAX_IE_AUTHMETHODS:
429 if (len != sizeof(unsigned short)) {
430 snprintf(tmp, sizeof(tmp), "Expecting authmethods to be %d bytes long but was %d\n", sizeof(unsigned short), len);
433 ies->authmethods = ntohs(*((unsigned short *)(data + 2)));
435 case IAX_IE_CHALLENGE:
436 ies->challenge = data + 2;
438 case IAX_IE_MD5_RESULT:
439 ies->md5_result = data + 2;
441 case IAX_IE_RSA_RESULT:
442 ies->rsa_result = data + 2;
444 case IAX_IE_APPARENT_ADDR:
445 ies->apparent_addr = ((struct sockaddr_in *)(data + 2));
448 if (len != sizeof(unsigned short)) {
449 snprintf(tmp, sizeof(tmp), "Expecting refresh to be %d bytes long but was %d\n", sizeof(unsigned short), len);
452 ies->refresh = ntohs(*((unsigned short *)(data + 2)));
454 case IAX_IE_DPSTATUS:
455 if (len != sizeof(unsigned short)) {
456 snprintf(tmp, sizeof(tmp), "Expecting dpstatus to be %d bytes long but was %d\n", sizeof(unsigned short), len);
459 ies->dpstatus = ntohs(*((unsigned short *)(data + 2)));
462 if (len != sizeof(unsigned short)) {
463 snprintf(tmp, sizeof(tmp), "Expecting callno to be %d bytes long but was %d\n", sizeof(unsigned short), len);
466 ies->callno = ntohs(*((unsigned short *)(data + 2)));
469 ies->cause = data + 2;
471 case IAX_IE_IAX_UNKNOWN:
473 ies->iax_unknown = data[2];
475 snprintf(tmp, sizeof(tmp), "Expected single byte Unknown command, but was %d long\n", len);
479 case IAX_IE_MSGCOUNT:
480 if (len != sizeof(unsigned short)) {
481 snprintf(tmp, sizeof(tmp), "Expecting msgcount to be %d bytes long but was %d\n", sizeof(unsigned short), len);
484 ies->msgcount = ntohs(*((unsigned short *)(data + 2)));
486 case IAX_IE_AUTOANSWER:
489 case IAX_IE_MUSICONHOLD:
490 ies->musiconhold = 1;
492 case IAX_IE_TRANSFERID:
493 if (len != sizeof(unsigned int)) {
494 snprintf(tmp, sizeof(tmp), "Expecting transferid to be %d bytes long but was %d\n", sizeof(unsigned int), len);
497 ies->transferid = ntohl(*((unsigned int *)(data + 2)));
500 snprintf(tmp, sizeof(tmp), "Ignoring unknown information element '%s' (%d) of length %d\n", iax_ie2str(ie), ie, len);
503 /* Overwrite information element with 0, to null terminate previous portion */
505 datalen -= (len + 2);
508 /* Null-terminate last field */
511 errorf("Invalid information element contents, strange boundary\n");
517 void iax_frame_wrap(struct iax_frame *fr, struct ast_frame *f)
519 fr->af.frametype = f->frametype;
520 fr->af.subclass = f->subclass;
521 fr->af.mallocd = 0; /* Our frame is static relative to the container */
522 fr->af.datalen = f->datalen;
523 fr->af.samples = f->samples;
524 fr->af.offset = AST_FRIENDLY_OFFSET;
526 fr->af.data = fr->afdata;
528 memcpy(fr->af.data, f->data, fr->af.datalen);
531 struct iax_frame *iax_frame_new(int direction, int datalen)
533 struct iax_frame *fr;
534 fr = malloc(sizeof(struct iax_frame) + datalen);
536 fr->direction = direction;
539 if (fr->direction == DIRECTION_INGRESS)
547 void iax_frame_free(struct iax_frame *fr)
549 /* Note: does not remove from scheduler! */
550 if (fr->direction == DIRECTION_INGRESS)
552 else if (fr->direction == DIRECTION_OUTGRESS)
555 errorf("Attempt to double free frame detected\n");
563 int iax_get_frames(void) { return frames; }
564 int iax_get_iframes(void) { return iframes; }
565 int iax_get_oframes(void) { return oframes; }