1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273 |
- #include <stdio.h>
- #include <string.h>
- #include <asterisk/lock.h>
- #include <asterisk/channel.h>
- #include <asterisk/channel_pvt.h>
- #include <asterisk/config.h>
- #include <asterisk/logger.h>
- #include <asterisk/module.h>
- #include <asterisk/utils.h>
- #include <asterisk/pbx.h>
- #include <asterisk/options.h>
- #include <sys/socket.h>
- #include <errno.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <arpa/inet.h>
- #include <linux/if_packet.h>
- #include <linux/if_ether.h>
- #ifndef OLD_SANGOMA_API
- #include <linux/if_wanpipe.h>
- #include <linux/wanpipe.h>
- #endif
- #include <sys/signal.h>
- #include "adtranvofr.h"
- #define G723_MAX_BUF 2048
- #define FR_API_MESS 16
- static char *desc = "Adtran Voice over Frame Relay";
- static char *type = "AdtranVoFR";
- static char *tdesc = "Voice over Frame Relay/Adtran style";
- static char *config = "adtranvofr.conf";
- static char context[AST_MAX_EXTENSION] = "default";
- static char language[MAX_LANGUAGE] = "";
- static int usecnt =0;
- AST_MUTEX_DEFINE_STATIC(usecnt_lock);
- AST_MUTEX_DEFINE_STATIC(iflock);
- AST_MUTEX_DEFINE_STATIC(monlock);
- static pthread_t monitor_thread = AST_PTHREADT_NULL;
- static int restart_monitor(void);
-
- static struct vofr_pvt {
- int s;
- #ifdef OLD_SANGOMA_API
- struct sockaddr_pkt sa;
- #else
- struct wan_sockaddr_ll sa;
- #endif
- struct ast_channel *owner;
- int outgoing;
- struct vofr_pvt *next;
- struct vofr_hdr *hdr;
- struct vofr_hdr *ohdr;
- u_int8_t dlcih;
- u_int8_t dlcil;
- u_int8_t cid;
- char buf[G723_MAX_BUF];
- char obuf[G723_MAX_BUF];
- char context[AST_MAX_EXTENSION];
- char language[MAX_LANGUAGE];
- int ringgothangup;
- } *iflist = NULL;
- #ifdef VOFRDUMPER
- static char *set(int val)
- {
- return (val ? "Set " : "Unset");
- }
- static char *controlstr(int control)
- {
- switch(control) {
- case VOFR_CONTROL_ADTRAN:
- return "Adtran Proprietary";
- case VOFR_CONTROL_VOICE:
- return "Voice";
- case VOFR_CONTROL_RFC1490:
- return "RFC 1490";
- }
- return "Unknown";
- }
- static char *dtypestr(int control)
- {
- switch(control) {
- case VOFR_TYPE_SIGNAL:
- return "Signal Frame";
- case VOFR_TYPE_VOICE:
- return "Voice Frame";
- case VOFR_TYPE_ANSWER:
- return "Answer Tone";
- case VOFR_TYPE_FAX:
- return "FAX";
- case VOFR_TYPE_DTMF:
- return "DTMF Digit";
- }
- return "Unknown";
- }
- static char *vflagsstr(int flags)
- {
- static char buf[80] = "";
- buf[0] = '\0';
- if (!flags)
- return "(None)";
- if (flags & VOFR_ROUTE_LOCAL)
- strncat(buf, "Local ", sizeof(buf) - strlen(buf) - 1);
- if (flags & VOFR_ROUTE_VOICE)
- strncat(buf, "Voice ", sizeof(buf) - strlen(buf) - 1);
- if (flags & VOFR_ROUTE_DTE)
- strncat(buf, "DTE ", sizeof(buf) - strlen(buf) - 1);
- else if (flags & VOFR_ROUTE_DTE1)
- strncat(buf, "DTE1 ", sizeof(buf) - strlen(buf) - 1);
- else if (flags & VOFR_ROUTE_DTE2)
- strncat(buf, "DTE2 ", sizeof(buf) - strlen(buf) - 1);
- return buf;
- }
- static char *remidstr(int remid)
- {
- switch(remid) {
- case VOFR_CARD_TYPE_UNSPEC:
- return "Unspecified";
- case VOFR_CARD_TYPE_FXS:
- return "FXS";
- case VOFR_CARD_TYPE_FXO:
- return "FXO";
- case VOFR_CARD_TYPE_ENM:
- return "E&M";
- case VOFR_CARD_TYPE_VCOM:
- return "Atlas/VCOM";
- }
- return "Unknown";
- }
- static char *modulationstr(int modulation)
- {
- switch(modulation) {
- case VOFR_MODULATION_SINGLE:
- return "Single Frequency";
- case VOFR_MODULATION_V21:
- return "V.21";
- case VOFR_MODULATION_V27ter_2:
- return "V.27 (2400bps)";
- case VOFR_MODULATION_V27ter_4:
- return "V.27 (4800bps)";
- case VOFR_MODULATION_V29_7:
- return "V.29 (7200bps)";
- case VOFR_MODULATION_V29_9:
- return "V.29 (9600bps)";
- case VOFR_MODULATION_V33_12:
- return "V.33 (12000bps)";
- case VOFR_MODULATION_V33_14:
- return "V.33 (14400BPS)";
- }
- return "Unknown";
- }
- static char *signalstr(int signal)
- {
- switch(signal) {
- case VOFR_SIGNAL_ON_HOOK:
- return "On Hook";
- case VOFR_SIGNAL_OFF_HOOK:
- return "Off Hook";
- case VOFR_SIGNAL_RING:
- return "Ring";
- case VOFR_SIGNAL_SWITCHED_DIAL:
- return "Switched Dial";
- case VOFR_SIGNAL_BUSY:
- return "Busy";
- case VOFR_SIGNAL_TRUNK_BUSY:
- return "Trunk Busy";
- }
- return "Unknown";
- }
- static char *vofr_digitstr(int val)
- {
- static char num[5];
- if (val < 10) {
- snprintf(num, sizeof(num), "%d", val);
- return num;
- }
- switch(val) {
- case 10:
- return "*";
- case 11:
- return "#";
- }
- return "Unknown";
- }
- static void vofr_dump_packet(struct vofr_hdr *vh, int len)
- {
- printf("VoFR Packet Dump\n");
- printf("================\n");
- printf("EI: %s ", set(vh->control & VOFR_MASK_EI));
- printf("LI: %s\n", set(vh->control & VOFR_MASK_LI));
- printf("Control: %s (0x%02x)\n",
- controlstr(vh->control & VOFR_MASK_CONTROL), vh->control & VOFR_MASK_CONTROL);
- printf("Data Type: %s (0x%02x)\n", dtypestr(vh->dtype), vh->dtype);
- if (vh->dtype == VOFR_TYPE_SIGNAL) {
- printf(" \\--Signal: %s (0x%02x)\n", signalstr(vh->data[0]), vh->data[0]);
- }
- if (vh->dtype == VOFR_TYPE_DTMF) {
- printf(" \\--Digit: %s (0x%02x)\n", vofr_digitstr(vh->data[0]), vh->data[0]);
- }
- printf("Connect Tag: 0x%02x\n", vh->ctag);
- printf("Voice Rt Flags: %s\n", vflagsstr(vh->vflags));
- printf("DLCI X-Ref: %d\n", (vh->dlcih << 8) | (vh->dlcil));
- printf("Channel ID: %d\n", vh->cid);
- printf("Remote ID: %s (0x%02x)\n", remidstr(vh->remid), vh->remid);
- printf("Modulation: %s (0x%02x)\n", modulationstr(vh->mod), vh->mod);
- printf("\n");
- fflush(stdout);
- }
- #endif
- static struct ast_frame *vofr_read(struct ast_channel *ast);
- static int vofr_xmit(struct vofr_pvt *p, char *data, int len)
- {
- int res;
- #ifdef OLD_SANGOMA_API
- res=sendto(p->s, data, len, 0, (struct sockaddr *)&p->sa, sizeof(struct sockaddr_pkt));
- #else
- res=sendto(p->s, data, len, 0, (struct sockaddr *)&p->sa, sizeof(struct wan_sockaddr_ll));
- #endif
- if (res != len) {
- ast_log(LOG_WARNING, "vofr_xmit returned %d\n", res);
- }
- return res;
- }
- static int vofr_digit(struct ast_channel *ast, char digit)
- {
-
- struct vofr_pvt *p;
- struct vofr_hdr *vh;
- p = ast->pvt->pvt;
- vh = p->ohdr;
- vh->control = VOFR_CONTROL_VOICE;
- vh->dtype = VOFR_TYPE_DTMF;
- vh->vflags = VOFR_ROUTE_NONE;
- vh->dlcih = p->dlcih;
- vh->dlcil = p->dlcil;
- vh->cid = p->cid;
- vh->remid = VOFR_CARD_TYPE_ASTERISK;
- vh->mod = VOFR_MODULATION_SINGLE;
- if ((digit >= '0') && (digit <= '9'))
- vh->data[0] = digit - '0';
- else if (digit == '*')
- vh->data[0] = 10;
- else if (digit == '#')
- vh->data[0] = 11;
- else {
- ast_log(LOG_WARNING, "%s: tried to dial a non digit '%c'\n", ast->name, digit);
- return -1;
- }
- vh->data[1] = 0x14;
- vh->data[2] = 0x1f;
- vh->data[3] = 0x70;
-
- vofr_xmit(p, p->obuf, VOFR_HDR_SIZE + 4 + FR_API_MESS);
- usleep(30000);
-
- vh->control = VOFR_CONTROL_VOICE;
- vh->dtype = VOFR_TYPE_VOICE;
- vh->vflags = VOFR_ROUTE_NONE;
- vh->dlcih = p->dlcih;
- vh->dlcil = p->dlcil;
- vh->cid = p->cid;
- vh->remid = VOFR_CARD_TYPE_ASTERISK;
- vh->mod = VOFR_MODULATION_SINGLE;
- vofr_xmit(p, p->obuf, VOFR_HDR_SIZE + FR_API_MESS);
- return 0;
- }
- static int vofr_xmit_signal(struct vofr_pvt *p, int signal, int pad)
- {
-
- struct vofr_hdr *vh = p->ohdr;
- int res;
- vh->control = VOFR_CONTROL_VOICE;
- vh->dtype = VOFR_TYPE_SIGNAL;
- vh->vflags = VOFR_ROUTE_NONE;
- vh->dlcih = p->dlcih;
- vh->dlcil = p->dlcil;
- vh->cid = p->cid;
- vh->remid = VOFR_CARD_TYPE_ASTERISK;
- vh->mod = VOFR_MODULATION_SINGLE;
- vh->data[0] = signal;
- if (FR_API_MESS)
- memset(p->obuf, 0, FR_API_MESS);
- res = vofr_xmit(p, p->obuf, VOFR_HDR_SIZE + pad + 1 + FR_API_MESS);
- return res;
- }
- static int vofr_call(struct ast_channel *ast, char *dest, int timeout)
- {
- int res;
- int otimeout;
- struct ast_frame *f;
- struct vofr_pvt *p;
- p = ast->pvt->pvt;
- if ((ast->state != AST_STATE_DOWN) && (ast->state != AST_STATE_RESERVED)) {
- ast_log(LOG_WARNING, "vofr_call called on %s, neither down nor reserved\n", ast->name);
- return -1;
- }
-
- vofr_xmit_signal(p, VOFR_SIGNAL_OFFHOOK, 0);
-
- otimeout = 1000;
- while(otimeout) {
- otimeout = ast_waitfor(ast, 1000);
- if (otimeout < 1) {
- ast_log(LOG_WARNING, "Unable to take line '%s' off hook\n", ast->name);
-
- return -1;
- }
- f = vofr_read(ast);
- if (!f)
- return -1;
- if ((f->frametype == AST_FRAME_CONTROL) &&
- (f->subclass == AST_CONTROL_OFFHOOK))
-
- break;
- }
- if (!otimeout) {
- ast_log(LOG_WARNING, "Unable to take line off hook\n");
- return -1;
- }
-
- while(*dest) {
- ast->state = AST_STATE_DIALING;
- vofr_digit(ast, *dest);
-
- usleep(100000);
- dest++;
- }
- if (timeout) {
-
- otimeout = 1000;
- while(otimeout) {
- otimeout = ast_waitfor(ast, 1000);
- if (otimeout < 1) {
- ast_log(LOG_WARNING, "No acknowledgement for ringing\n");
-
- return -1;
- }
- f = vofr_read(ast);
- if (!f)
- return -1;
-
- if (f->frametype == AST_FRAME_CONTROL) {
- if (f->subclass == AST_CONTROL_RINGING) {
- ast->state = AST_STATE_RINGING;
-
- break;
- }
- if (f->subclass == AST_CONTROL_BUSY)
-
- return -1;
- }
- ast_frfree(f);
- }
- }
- otimeout = timeout;
- while(timeout) {
-
- res = ast_waitfor(ast, timeout);
- if (res < 0)
-
- return -1;
- else
- timeout = res;
- if (res) {
-
- f = vofr_read(ast);
- if (!f)
- return -1;
- if ((f->frametype == AST_FRAME_CONTROL) &&
- (f->subclass == AST_CONTROL_ANSWER))
-
- return otimeout - res;
-
- }
- }
- return 0;
- }
- static int send_hangup(struct vofr_pvt *p)
- {
-
- return vofr_xmit_signal(p, 0x80, 0);
- }
- static int vofr_hangup(struct ast_channel *ast)
- {
- int res;
- if (option_debug)
- ast_log(LOG_DEBUG, "vofr_hangup(%s)\n", ast->name);
- if (!ast->pvt->pvt) {
- ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
- return 0;
- }
- res = send_hangup(ast->pvt->pvt);
- if (res < 0) {
- ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
- return -1;
- }
- ast->state = AST_STATE_DOWN;
- ((struct vofr_pvt *)(ast->pvt->pvt))->owner = NULL;
- ((struct vofr_pvt *)(ast->pvt->pvt))->ringgothangup = 0;
- ast_mutex_lock(&usecnt_lock);
- usecnt--;
- if (usecnt < 0)
- ast_log(LOG_WARNING, "Usecnt < 0???\n");
- ast_mutex_unlock(&usecnt_lock);
- ast_update_use_count();
- if (option_verbose > 2)
- ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
- ast->pvt->pvt = NULL;
- ast->state = AST_STATE_DOWN;
- restart_monitor();
- return 0;
- }
- static int vofr_answer(struct ast_channel *ast)
- {
- int res;
- int cnt = 1000;
- char buf[2048];
- struct vofr_hdr *vh;
- ast->rings = 0;
- if (option_debug)
- ast_log(LOG_DEBUG, "vofr_answer(%s)\n", ast->name);
- res = vofr_xmit_signal(ast->pvt->pvt, VOFR_SIGNAL_OFFHOOK, 0);
- if (res < 0)
- ast_log(LOG_WARNING, "Unable to anaswer line %s\n", ast->name);
- ast->state = AST_STATE_UP;
- while(cnt > 0) {
- cnt = ast_waitfor(ast, cnt);
- if (cnt > 0) {
- res = read(ast->fds[0], buf, sizeof(buf));
- #ifdef VOFRDUMPER
- vofr_dump_packet((void *)(buf +FR_API_MESS), res - FR_API_MESS);
- #endif
- res -= FR_API_MESS;
- if (res < 0)
- ast_log(LOG_WARNING, "Warning: read failed (%s) on %s\n", strerror(errno), ast->name);
- else {
-
- vh = (struct vofr_hdr *)(buf + FR_API_MESS);
- switch(vh->dtype) {
- case VOFR_TYPE_SIGNAL:
- switch(vh->data[0]) {
- case VOFR_SIGNAL_UNKNOWN:
- switch(vh->data[1]) {
- case 0x1:
- if (option_debug)
- ast_log(LOG_DEBUG, "Answered '%s'\n", ast->name);
- else if (option_verbose > 2)
- ast_verbose( VERBOSE_PREFIX_3 "Answered '%s'\n", ast->name);
- ast->state = AST_STATE_UP;
- return 0;
- break;
- default:
- ast_log(LOG_WARNING, "Unexpected 'unknown' frame type %d\n", vh->data[1]);
- }
- break;
- case VOFR_SIGNAL_ON_HOOK:
-
- break;
- default:
- ast_log(LOG_WARNING, "Unexpected signal type %d\n", vh->data[0]);
- }
- break;
- default:
- ast_log(LOG_WARNING, "Unexpected data type %d\n", vh->dtype);
- }
- }
- }
- }
- ast_log(LOG_WARNING, "Did not get acknowledged answer\n");
- return -1;
- }
- static char vofr_2digit(char c)
- {
- if (c == 11)
- return '#';
- else if (c == 10)
- return '*';
- else if ((c < 10) && (c >= 0))
- return '0' + c;
- else
- return '?';
- }
- static struct ast_frame *vofr_read(struct ast_channel *ast)
- {
- int res;
- char tone;
- int timeout,x;
- struct vofr_pvt *p = ast->pvt->pvt;
- short *swapping;
- struct ast_frame *fr = (struct ast_frame *)(p->buf);
- struct vofr_hdr *vh = (struct vofr_hdr *)(p->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET - sizeof(struct vofr_hdr));
-
- CHECK_BLOCKING(ast);
- retry:
- res = read(p->s, ((char *)vh) - FR_API_MESS,
- G723_MAX_BUF - AST_FRIENDLY_OFFSET - sizeof(struct ast_frame) + sizeof(struct vofr_hdr) + FR_API_MESS);
- if (res < 0) {
-
- if (errno == EAGAIN) {
- fd_set fds;
- FD_ZERO(&fds);
- FD_SET(p->s, &fds);
- ast_select(p->s + 1, &fds, NULL, NULL, NULL);
- goto retry;
- }
- ast->blocking = 0;
- ast_log(LOG_WARNING, "Read error on %s: %s (%d)\n", ast->name, strerror(errno));
- return NULL;
- }
- ast->blocking = 0;
-
- #ifdef VOFRDUMPER
- vofr_dump_packet((void *)(vh), res);
- #endif
- res -= FR_API_MESS;
- if (res < sizeof(struct vofr_hdr)) {
- ast_log(LOG_WARNING, "Nonsense frame on %s\n", ast->name);
- return NULL;
- }
-
- fr->datalen = 0;
- fr->timelen = 0;
- fr->data = NULL;
- fr->src = type;
- fr->offset = 0;
- fr->mallocd=0;
- fr->delivery.tv_sec = 0;
- fr->delivery.tv_usec = 0;
-
-
- switch(vh->dtype) {
- case VOFR_TYPE_SIGNAL:
- switch(vh->data[0]) {
- case VOFR_SIGNAL_ON_HOOK:
-
- if ((ast->state == AST_STATE_UP) || (p->ringgothangup)) {
- return NULL;
- } else {
- fr->frametype = AST_FRAME_NULL;
- fr->subclass = 0;
- p->ringgothangup=1;
- }
- break;
- case VOFR_SIGNAL_RING:
- ast->rings++;
- p->ringgothangup = 0;
- break;
- case VOFR_SIGNAL_UNKNOWN:
- switch(vh->data[1]) {
- case 0x1:
-
- switch(ast->state) {
- case AST_STATE_RINGING:
- fr->frametype = AST_FRAME_CONTROL;
- fr->subclass = AST_CONTROL_ANSWER;
- ast->state = AST_STATE_UP;
- break;
- case AST_STATE_DOWN:
- case AST_STATE_UP:
- fr->frametype = AST_FRAME_NULL;
- fr->subclass = 0;
- break;
- }
- break;
- case 0x2:
-
- fr->frametype = AST_FRAME_CONTROL;
- fr->subclass = AST_CONTROL_OFFHOOK;
- ast->state = AST_STATE_OFFHOOK;
- break;
- case 0x3:
-
- fr->frametype = AST_FRAME_CONTROL;
- fr->subclass = AST_CONTROL_BUSY;
- ast->state = AST_STATE_BUSY;
- break;
- case 0x5:
-
- fr->frametype = AST_FRAME_CONTROL;
- fr->subclass = AST_CONTROL_RINGING;
- ast->state = AST_STATE_RINGING;
- break;
- case 0x6:
-
- return NULL;
- default:
- ast_log(LOG_WARNING, "Don't know what to do with 'unknown' signal '%d'\n", vh->data[1]);
- fr->frametype = AST_FRAME_NULL;
- fr->subclass = 0;
- }
- return fr;
- break;
- default:
- ast_log(LOG_WARNING, "Don't know what to do with signal '%d'\n", vh->data[0]);
- }
- break;
- case VOFR_TYPE_DTMF:
-
-
- tone = vofr_2digit(vh->data[0]);
- timeout = 50;
- do {
- if ((timeout = ast_waitfor(ast, timeout)) < 1)
- break;
- CHECK_BLOCKING(ast);
- res = read(p->s, ((char *)vh) - FR_API_MESS,
- G723_MAX_BUF - AST_FRIENDLY_OFFSET - sizeof(struct ast_frame) + sizeof(struct vofr_hdr) + FR_API_MESS);
- ast->blocking = 0;
- res -= FR_API_MESS;
- if (res < sizeof(struct vofr_hdr *)) {
- ast_log(LOG_WARNING, "Nonsense frame on %s\n", ast->name);
- return NULL;
- }
- if (vh->dtype == VOFR_TYPE_DTMF) {
-
- timeout = 50;
- if ((tone != vofr_2digit(vh->data[0])) )
-
- break;
- }
-
- } while (timeout);
- fr->frametype = AST_FRAME_DTMF;
- fr->subclass = tone;
- fr->datalen = 0;
- fr->data = NULL;
- fr->offset = 0;
- return fr;
- case VOFR_TYPE_VOICE:
-
- if ((ast->state == AST_STATE_RINGING) || (ast->state == AST_STATE_DIALING)) {
- ast_log(LOG_DEBUG, "Adtran bug! (state = %d)\n", ast->state);
- fr->frametype = AST_FRAME_CONTROL;
- fr->subclass = AST_CONTROL_ANSWER;
- ast->state = AST_STATE_UP;
- return fr;
- } else if (ast->state != AST_STATE_UP) {
- ast_log(LOG_WARNING, "%s: Voice in weird state %d\n", ast->name, ast->state);
- }
- fr->frametype = AST_FRAME_VOICE;
- fr->subclass = AST_FORMAT_G723_1;
- fr->datalen = res - sizeof(struct vofr_hdr);
- fr->data = ((char *)vh) + sizeof(struct vofr_hdr);
- fr->src = type;
-
- swapping = fr->data;
- for (x=0;x<fr->datalen/2;x++)
- swapping[x] = ntohs(swapping[x]);
- fr->offset = AST_FRIENDLY_OFFSET;
-
- fr->timelen = 30;
- return fr;
- default:
- ast_log(LOG_WARNING, "Don't know what to do with data type %d frames\n", vh->dtype);
- }
-
- fr->frametype = AST_FRAME_NULL;
- fr->subclass = 0;
- return fr;
- }
- static int vofr_write(struct ast_channel *ast, struct ast_frame *frame)
- {
- struct vofr_hdr *vh;
- struct vofr_pvt *p = ast->pvt->pvt;
- short *swapping;
- int x;
- char *start;
- int res;
-
- if (frame->frametype != AST_FRAME_VOICE) {
- ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
- return -1;
- }
- if (frame->subclass != AST_FORMAT_G723_1) {
- ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
- return -1;
- }
-
- if (frame->offset < sizeof(struct vofr_hdr) + FR_API_MESS) {
- ast_log(LOG_WARNING, "Frame source '%s' didn't provide a friendly enough offset\n", (frame->src ? frame->src : "**Unknown**"));
- return -1;
- }
-
- swapping = frame->data;
- for (x=0;x<frame->datalen/2;x++)
- swapping[x] = ntohs(swapping[x]);
- vh = (struct vofr_hdr *)(frame->data - sizeof(struct vofr_hdr));
-
- start = ((void *)vh) - FR_API_MESS;
- if (start)
- memset(start, 0, FR_API_MESS);
-
- vh->control = VOFR_CONTROL_VOICE;
- vh->dtype = VOFR_TYPE_VOICE;
- vh->vflags = VOFR_ROUTE_NONE;
- vh->dlcih = p->dlcih;
- vh->dlcil = p->dlcil;
- vh->cid = p->cid;
- vh->remid = VOFR_CARD_TYPE_ASTERISK;
- vh->mod = VOFR_MODULATION_SINGLE;
- res = vofr_xmit(p, start,
- VOFR_HDR_SIZE + frame->datalen + FR_API_MESS);
- res -= FR_API_MESS;
-
- swapping = frame->data;
- for (x=0;x<frame->datalen/2;x++)
- swapping[x] = htons(swapping[x]);
- if (res != VOFR_HDR_SIZE + frame->datalen) {
- ast_log(LOG_WARNING, "Unable to write frame correctly\n");
- return -1;
- }
- return 0;
- }
- static int vofr_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
- {
- struct vofr_pvt *p = newchan->pvt->pvt;
- if (p->owner != oldchan) {
- ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
- return -1;
- }
- p->owner = newchan;
- return 0;
- }
- static struct ast_channel *vofr_new(struct vofr_pvt *i, int state)
- {
- struct ast_channel *tmp;
- tmp = ast_channel_alloc(0);
- if (tmp) {
- #ifdef OLD_SANGOMA_API
- snprintf(tmp->name, sizeof(tmp->name), "AdtranVoFR/%s", i->sa.spkt_device);
- #else
- snprintf(tmp->name, sizeof(tmp->name), "AdtranVoFR/%s", i->sa.sll_device);
- #endif
- tmp->type = type;
- tmp->fds[0] = i->s;
-
- tmp->nativeformats = AST_FORMAT_G723_1;
- tmp->state = state;
- if (state == AST_STATE_RING)
- tmp->rings = 1;
- tmp->writeformat = AST_FORMAT_G723_1;
- tmp->readformat = AST_FORMAT_G723_1;
- tmp->pvt->pvt = i;
- tmp->pvt->send_digit = vofr_digit;
- tmp->pvt->call = vofr_call;
- tmp->pvt->hangup = vofr_hangup;
- tmp->pvt->answer = vofr_answer;
- tmp->pvt->read = vofr_read;
- tmp->pvt->write = vofr_write;
- tmp->pvt->fixup = vofr_fixup;
- if (strlen(i->language))
- strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
- i->owner = tmp;
- ast_mutex_lock(&usecnt_lock);
- usecnt++;
- ast_mutex_unlock(&usecnt_lock);
- ast_update_use_count();
- strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
- if (state != AST_STATE_DOWN) {
- if (ast_pbx_start(tmp)) {
- ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
- ast_hangup(tmp);
- tmp = NULL;
- }
- }
- } else
- ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
- return tmp;
- }
- static int vofr_mini_packet(struct vofr_pvt *i, struct vofr_hdr *pkt, int len)
- {
-
- switch(pkt->dtype) {
- case VOFR_TYPE_SIGNAL:
- switch(pkt->data[0]) {
- case VOFR_SIGNAL_RING:
-
- if (!i->owner) {
- i->ringgothangup = 0;
- vofr_new(i, AST_STATE_RING);
- } else
- ast_log(LOG_WARNING, "Got a ring, but there's an owner?\n");
- break;
- case VOFR_SIGNAL_OFF_HOOK:
-
- #if 0
- ast_log(LOG_DEBUG, "Off hook\n");
- #endif
- vofr_xmit_signal(i, 0x10, 2);
- if (!i->owner)
- vofr_new(i, AST_STATE_UP);
- else
- ast_log(LOG_WARNING, "Got an offhook, but there's an owner?\n");
- break;
- case VOFR_SIGNAL_ON_HOOK:
- break;
- case VOFR_SIGNAL_UNKNOWN:
- switch(pkt->data[1]) {
- case 0x1:
-
- break;
- case 0x6:
-
- if (option_debug)
- ast_log(LOG_DEBUG, "Sending hangup reply\n");
- send_hangup(i);
- break;
- default:
- ast_log(LOG_WARNING, "Unexected 'unknown' signal '%d'\n", pkt->data[1]);
- }
- break;
- default:
- ast_log(LOG_DEBUG, "Unknown signal type '%d'\n", pkt->data[0]);
- }
- break;
- case VOFR_TYPE_VOICE:
- break;
- default:
- ast_log(LOG_DEBUG, "Unknown packet type '%d'\n", pkt->dtype);
- }
- return 0;
- }
- static void *do_monitor(void *data)
- {
- fd_set rfds;
- int n, res;
- struct vofr_pvt *i;
-
-
- #if 0
- if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
- ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
- return NULL;
- }
- #endif
- for(;;) {
-
- if (ast_mutex_lock(&monlock)) {
- ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
- return NULL;
- }
-
- if (ast_mutex_lock(&iflock)) {
- ast_log(LOG_ERROR, "Unable to grab interface lock\n");
- ast_mutex_unlock(&monlock);
- return NULL;
- }
-
- n = -1;
- FD_ZERO(&rfds);
- i = iflist;
- while(i) {
- if (FD_ISSET(i->s, &rfds))
- #ifdef OLD_SANGOMA_API
- ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->s, i->sa.spkt_device);
- #else
- ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->s, i->sa.sll_device);
- #endif
- if (!i->owner) {
-
- FD_SET(i->s, &rfds);
- if (i->s > n)
- n = i->s;
- }
- i = i->next;
- }
-
- ast_mutex_unlock(&iflock);
-
-
- ast_mutex_unlock(&monlock);
- pthread_testcancel();
-
- res = ast_select(n + 1, &rfds, NULL, NULL, NULL);
- pthread_testcancel();
-
- if (res < 0) {
- if ((errno != EAGAIN) && (errno != EINTR))
- ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
- continue;
- }
-
- if (ast_mutex_lock(&iflock)) {
- ast_log(LOG_WARNING, "Unable to lock the interface list\n");
- continue;
- }
- i = iflist;
- while(i) {
- if (FD_ISSET(i->s, &rfds)) {
- if (i->owner) {
- #ifdef OLD_SANGOMA_API
- ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d, %s)...\n", i->s, i->sa.spkt_device);
- #else
- ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d, %s)...\n", i->s, i->sa.sll_device);
- #endif
- continue;
- }
- res = read(i->s, i->buf, sizeof(i->buf));
- res -= FR_API_MESS;
- #ifdef VOFRDUMPER
- vofr_dump_packet(i->hdr, res);
- #endif
- vofr_mini_packet(i, i->hdr, res);
- }
- i=i->next;
- }
- ast_mutex_unlock(&iflock);
- }
-
- return NULL;
-
- }
- static int restart_monitor(void)
- {
-
- if (monitor_thread == AST_PTHREADT_STOP)
- return 0;
- if (ast_mutex_lock(&monlock)) {
- ast_log(LOG_WARNING, "Unable to lock monitor\n");
- return -1;
- }
- if (monitor_thread == pthread_self()) {
- ast_mutex_unlock(&monlock);
- ast_log(LOG_WARNING, "Cannot kill myself\n");
- return -1;
- }
- if (monitor_thread != AST_PTHREADT_NULL) {
-
- pthread_kill(monitor_thread, SIGURG);
- } else {
-
- if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
- ast_mutex_unlock(&monlock);
- ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
- return -1;
- }
- }
- ast_mutex_unlock(&monlock);
- return 0;
- }
- static struct vofr_pvt *mkif(char *type, char *iface)
- {
-
- struct vofr_pvt *tmp;
- int sndbuf = 4096;
- tmp = malloc(sizeof(struct vofr_pvt));
- if (tmp) {
-
- #ifdef OLD_SANGOMA_API
- tmp->s = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL));
- #else
-
- tmp->s = socket(AF_WANPIPE, SOCK_RAW, 0);
- #endif
- if (tmp->s < 0) {
- ast_log(LOG_ERROR, "Unable to create socket: %s\n", strerror(errno));
- free(tmp);
- return NULL;
- }
- #ifdef OLD_SANGOMA_API
-
- memset(&tmp->sa, 0, sizeof(tmp->sa));
- strncpy(tmp->sa.spkt_device, iface, sizeof(tmp->sa.spkt_device)-1);
- tmp->sa.spkt_protocol = htons(0x16);
- tmp->sa.spkt_family = AF_PACKET;
- if (bind(tmp->s, (struct sockaddr *)&tmp->sa, sizeof(struct sockaddr))) {
- #else
-
- memset(&tmp->sa, 0, sizeof(tmp->sa));
- tmp->sa.sll_family = AF_WANPIPE;
- tmp->sa.sll_protocol = htons(ETH_P_IP);
- strncpy(tmp->sa.sll_device, iface, sizeof(tmp->sa.sll_device)-1);
- strncpy(tmp->sa.sll_card, "wanpipe1", sizeof(tmp->sa.sll_card)-1);
- tmp->sa.sll_ifindex = 0;
- if (bind(tmp->s, (struct sockaddr *)&tmp->sa, sizeof(struct wan_sockaddr_ll))) {
- #endif
-
- #ifdef OLD_SANGOMA_API
- ast_log(LOG_ERROR, "Unable to bind to '%s': %s\n", tmp->sa.spkt_device,
- #else
- ast_log(LOG_ERROR, "Unable to bind to '%s': %s\n", tmp->sa.sll_device,
- #endif
- strerror(errno));
- free(tmp);
- return NULL;
- }
-
-
- if (setsockopt(tmp->s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))) {
- ast_log(LOG_ERROR, "Unable to set send buffer size to %d: %s\n", sndbuf, strerror(errno));
- free(tmp);
- return NULL;
- }
- tmp->owner = NULL;
- tmp->hdr = (struct vofr_hdr *)(tmp->buf + FR_API_MESS);
- tmp->ohdr = (struct vofr_hdr *)(tmp->obuf + FR_API_MESS);
- tmp->dlcil = 0;
- tmp->dlcih = 0;
- tmp->cid = 1;
- tmp->ringgothangup = 0;
- strncpy(tmp->language, language, sizeof(tmp->language)-1);
- strncpy(tmp->context, context, sizeof(tmp->context)-1);
-
- if (!strcasecmp(type, "user"))
- tmp->outgoing = 1;
- else
- tmp->outgoing = 0;
- tmp->next = NULL;
-
- send_hangup(tmp);
-
- }
- return tmp;
- }
- static struct ast_channel *vofr_request(char *type, int format, void *data)
- {
- int oldformat;
- struct vofr_pvt *p;
- struct ast_channel *tmp = NULL;
-
- oldformat = format;
- format &= AST_FORMAT_G723_1;
- if (!format) {
- ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
- return NULL;
- }
-
- if (ast_mutex_lock(&iflock)) {
- ast_log(LOG_ERROR, "Unable to lock interface list???\n");
- return NULL;
- }
- p = iflist;
- while(p) {
- if (!p->owner && p->outgoing) {
- tmp = vofr_new(p, AST_STATE_DOWN);
- break;
- }
- p = p->next;
- }
- ast_mutex_unlock(&iflock);
- restart_monitor();
- return tmp;
- }
- static int __unload_module(void)
- {
- struct vofr_pvt *p, *pl;
-
- ast_channel_unregister(type);
- if (!ast_mutex_lock(&iflock)) {
-
- p = iflist;
- while(p) {
- if (p->owner)
- ast_softhangup(p->owner);
- p = p->next;
- }
- iflist = NULL;
- ast_mutex_unlock(&iflock);
- } else {
- ast_log(LOG_WARNING, "Unable to lock the monitor\n");
- return -1;
- }
- if (!ast_mutex_lock(&monlock)) {
- if (monitor_thread) {
- pthread_cancel(monitor_thread);
- pthread_kill(monitor_thread, SIGURG);
- pthread_join(monitor_thread, NULL);
- }
- monitor_thread = AST_PTHREADT_STOP;
- ast_mutex_unlock(&monlock);
- } else {
- ast_log(LOG_WARNING, "Unable to lock the monitor\n");
- return -1;
- }
- if (!ast_mutex_lock(&iflock)) {
-
- p = iflist;
- while(p) {
-
- if (p->s > -1)
- close(p->s);
- pl = p;
- p = p->next;
-
- free(pl);
- }
- iflist = NULL;
- ast_mutex_unlock(&iflock);
- } else {
- ast_log(LOG_WARNING, "Unable to lock the monitor\n");
- return -1;
- }
-
- return 0;
- }
- int unload_module()
- {
- return __unload_module();
- }
- int load_module()
- {
- struct ast_config *cfg;
- struct ast_variable *v;
- struct vofr_pvt *tmp;
- cfg = ast_load(config);
-
- if (!cfg) {
- ast_log(LOG_ERROR, "Unable to load config %s\n", config);
- return -1;
- }
- if (ast_mutex_lock(&iflock)) {
-
- ast_log(LOG_ERROR, "Unable to lock interface list???\n");
- return -1;
- }
- v = ast_variable_browse(cfg, "interfaces");
- while(v) {
-
- if (!strcasecmp(v->name, "user") ||
- !strcasecmp(v->name, "network")) {
- tmp = mkif(v->name, v->value);
- if (tmp) {
- tmp->next = iflist;
- iflist = tmp;
- } else {
- ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
- ast_destroy(cfg);
- ast_mutex_unlock(&iflock);
- __unload_module();
- return -1;
- }
- } else if (!strcasecmp(v->name, "context")) {
- strncpy(context, v->value, sizeof(context)-1);
- } else if (!strcasecmp(v->name, "language")) {
- strncpy(language, v->value, sizeof(language)-1);
- }
- v = v->next;
- }
- ast_mutex_unlock(&iflock);
-
- if (ast_channel_register(type, tdesc, AST_FORMAT_G723_1, vofr_request)) {
- ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
- ast_destroy(cfg);
- __unload_module();
- return -1;
- }
- ast_destroy(cfg);
-
- restart_monitor();
- return 0;
- }
- int usecount()
- {
- int res;
- ast_mutex_lock(&usecnt_lock);
- res = usecnt;
- ast_mutex_unlock(&usecnt_lock);
- return res;
- }
- char *key()
- {
- return ASTERISK_GPL_KEY;
- }
- char *description()
- {
- return desc;
- }
|