Merge FreeBSD locking fixes (bug #1411)
[asterisk/asterisk.git] / channels / chan_vofr.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Implementation of Voice over Frame Relay, Adtran Style
5  * 
6  * Copyright (C) 1999, Mark Spencer
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
14 #include <stdio.h>
15 #include <pthread.h>
16 #include <string.h>
17 #include <asterisk/lock.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/channel_pvt.h>
20 #include <asterisk/config.h>
21 #include <asterisk/logger.h>
22 #include <asterisk/module.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/options.h>
25 #include <sys/socket.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <fcntl.h>
30 #include <arpa/inet.h>
31 #include <linux/if_packet.h>
32 #include <linux/if_ether.h>
33 #ifndef OLD_SANGOMA_API
34 #include <linux/if_wanpipe.h>
35 #include <linux/wanpipe.h>
36 #endif
37 #include <sys/signal.h>
38 #include "adtranvofr.h"
39
40 /* #define VOFRDUMPER */
41
42 #define G723_MAX_BUF 2048
43
44 #define FR_API_MESS 16
45
46 static char *desc = "Adtran Voice over Frame Relay";
47 static char *type = "AdtranVoFR";
48 static char *tdesc = "Voice over Frame Relay/Adtran style";
49 static char *config = "adtranvofr.conf";
50
51 static char context[AST_MAX_EXTENSION] = "default";
52
53 static char language[MAX_LANGUAGE] = "";
54
55 static int usecnt =0;
56 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
57
58 /* Protect the interface list (of vofr_pvt's) */
59 AST_MUTEX_DEFINE_STATIC(iflock);
60
61 /* Protect the monitoring thread, so only one process can kill or start it, and not
62    when it's doing something critical. */
63 AST_MUTEX_DEFINE_STATIC(monlock);
64
65 /* This is the thread for the monitor which checks for input on the channels
66    which are not currently in use.  */
67 static pthread_t monitor_thread = AST_PTHREADT_NULL;
68
69 static int restart_monitor(void);
70
71 /* The private structures of the Adtran VoFR channels are linked for
72    selecting outgoing channels */
73    
74 static struct vofr_pvt {
75         int s;                                                  /* Raw socket for this DLCI */
76 #ifdef OLD_SANGOMA_API
77         struct sockaddr_pkt sa;                 /* Sockaddr needed for sending, also has iface name */
78 #else
79         struct wan_sockaddr_ll sa;              /* Wanpipe sockaddr */
80 #endif
81         struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
82         int outgoing;                                   /* Does this channel support outgoing calls? */
83         struct vofr_pvt *next;                  /* Next channel in list */
84         struct vofr_hdr *hdr;                           /* VOFR header version of buf */
85         struct vofr_hdr *ohdr;
86         u_int8_t dlcih;                                 /* High two bits of DLCI */
87         u_int8_t dlcil;                                 /* Bottom two bits of DLCI */
88         u_int8_t cid;                                   /* Call ID */
89         char buf[G723_MAX_BUF];                                 /* Static buffer for reading frames */
90         char obuf[G723_MAX_BUF];                                /* Output buffer */
91         char context[AST_MAX_EXTENSION];
92         char language[MAX_LANGUAGE];
93         int ringgothangup;                              /* Have we received exactly one hangup after a ring */
94 } *iflist = NULL;
95
96 #ifdef VOFRDUMPER
97
98 /* Some useful debugging routines */
99
100 static char *set(int val)
101 {
102         return (val ? "Set  " : "Unset");
103 }
104
105 static char *controlstr(int control)
106 {
107         switch(control) {
108         case VOFR_CONTROL_ADTRAN:
109                 return "Adtran Proprietary";
110         case VOFR_CONTROL_VOICE:
111                 return "Voice";
112         case VOFR_CONTROL_RFC1490:
113                 return "RFC 1490";
114         }
115         return "Unknown";
116 }
117
118 static char *dtypestr(int control)
119 {
120         switch(control) {
121         case VOFR_TYPE_SIGNAL:
122                 return "Signal Frame";
123         case VOFR_TYPE_VOICE:
124                 return "Voice Frame";
125         case VOFR_TYPE_ANSWER:
126                 return "Answer Tone";
127         case VOFR_TYPE_FAX:     
128                 return "FAX";
129         case VOFR_TYPE_DTMF:
130                 return "DTMF Digit";
131         }
132         return "Unknown";
133 }
134
135 static char *vflagsstr(int flags)
136 {
137         static char buf[80];
138         buf[0] = '\0';
139         if (!flags)
140                 return "(None)";
141         if (flags & VOFR_ROUTE_LOCAL)
142                 strcat(buf, "Local ");
143         if (flags & VOFR_ROUTE_VOICE)
144                 strcat(buf, "Voice ");
145         if (flags & VOFR_ROUTE_DTE)
146                 strcat(buf, "DTE ");
147         else if (flags & VOFR_ROUTE_DTE1)
148                 strcat(buf, "DTE1 ");
149         else if (flags & VOFR_ROUTE_DTE2)       
150                 strcat(buf, "DTE2 ");
151         return buf;
152 }
153
154 static char *remidstr(int remid)
155 {
156         switch(remid) {
157         case VOFR_CARD_TYPE_UNSPEC:
158                 return "Unspecified";
159         case VOFR_CARD_TYPE_FXS:
160                 return "FXS";
161         case VOFR_CARD_TYPE_FXO:
162                 return "FXO";
163         case VOFR_CARD_TYPE_ENM:        
164                 return "E&M";
165         case VOFR_CARD_TYPE_VCOM:       
166                 return "Atlas/VCOM";
167         }
168         return "Unknown";
169 }
170
171 static char *modulationstr(int modulation)
172 {
173         switch(modulation) {
174         case VOFR_MODULATION_SINGLE:
175                 return "Single Frequency";
176         case VOFR_MODULATION_V21:
177                 return "V.21";
178         case VOFR_MODULATION_V27ter_2:
179                 return "V.27 (2400bps)";
180         case VOFR_MODULATION_V27ter_4:
181                 return "V.27 (4800bps)";
182         case VOFR_MODULATION_V29_7:
183                 return "V.29 (7200bps)";
184         case VOFR_MODULATION_V29_9:
185                 return "V.29 (9600bps)";
186         case VOFR_MODULATION_V33_12:
187                 return "V.33 (12000bps)";
188         case VOFR_MODULATION_V33_14:
189                 return "V.33 (14400BPS)";
190         }
191         return "Unknown";
192 }
193
194 static char *signalstr(int signal)
195 {
196         switch(signal) {
197         case VOFR_SIGNAL_ON_HOOK:
198                 return "On Hook";
199         case VOFR_SIGNAL_OFF_HOOK:
200                 return "Off Hook";
201         case VOFR_SIGNAL_RING:
202                 return "Ring";
203         case VOFR_SIGNAL_SWITCHED_DIAL:
204                 return "Switched Dial";
205         case VOFR_SIGNAL_BUSY:
206                 return "Busy";
207         case VOFR_SIGNAL_TRUNK_BUSY:
208                 return "Trunk Busy";
209         }
210         return "Unknown";
211 }
212
213 static char *vofr_digitstr(int val)
214 {
215         static char num[5];
216         if (val < 10) {
217                 snprintf(num, sizeof(num), "%d", val);
218                 return num;
219         }
220         switch(val) {
221         case 10:
222                 return "*";
223         case 11:
224                 return "#";
225         }
226         return "Unknown";
227 }
228
229
230 static void vofr_dump_packet(struct vofr_hdr *vh, int len)
231 {
232         printf("VoFR Packet Dump\n");
233         printf("================\n");
234         printf("EI: %s ", set(vh->control & VOFR_MASK_EI));
235         printf("LI: %s\n", set(vh->control & VOFR_MASK_LI));
236         printf("Control: %s (0x%02x)\n", 
237                 controlstr(vh->control & VOFR_MASK_CONTROL), vh->control & VOFR_MASK_CONTROL);
238         printf("Data Type: %s (0x%02x)\n", dtypestr(vh->dtype), vh->dtype);
239         if (vh->dtype == VOFR_TYPE_SIGNAL) {
240                 printf(" \\--Signal: %s (0x%02x)\n", signalstr(vh->data[0]), vh->data[0]);
241         }
242         if (vh->dtype == VOFR_TYPE_DTMF) {
243                 printf(" \\--Digit: %s (0x%02x)\n", vofr_digitstr(vh->data[0]), vh->data[0]);
244         }
245         printf("Connect Tag: 0x%02x\n", vh->ctag);
246         printf("Voice Rt Flags: %s\n", vflagsstr(vh->vflags));
247         printf("DLCI X-Ref: %d\n", (vh->dlcih << 8) | (vh->dlcil));
248         printf("Channel ID: %d\n", vh->cid);
249         printf("Remote ID: %s (0x%02x)\n", remidstr(vh->remid), vh->remid);
250         printf("Modulation: %s (0x%02x)\n", modulationstr(vh->mod), vh->mod);
251         printf("\n");
252         fflush(stdout);
253 }
254
255 #endif
256
257 static struct ast_frame  *vofr_read(struct ast_channel *ast);
258
259 static int vofr_xmit(struct vofr_pvt *p, char *data, int len)
260 {
261         int res;
262 #ifdef OLD_SANGOMA_API
263     res=sendto(p->s, data, len, 0, (struct sockaddr *)&p->sa, sizeof(struct sockaddr_pkt));
264 #else
265     res=sendto(p->s, data, len, 0, (struct sockaddr *)&p->sa, sizeof(struct wan_sockaddr_ll));
266 #endif
267         if (res != len) {
268                 ast_log(LOG_WARNING, "vofr_xmit returned %d\n", res);
269         }
270         return res;
271 }
272
273 static int vofr_digit(struct ast_channel *ast, char digit)
274 {
275         /*
276          * T H I S   I S   T O T A L L Y   U N D O C U M E N T E D
277          *     A N D   D O E S   N O T   S O U N D   R I G H T
278          *   XXX Figure out how to really send a decent digit XXX
279          */
280         struct vofr_pvt *p;
281         struct vofr_hdr *vh;
282         p = ast->pvt->pvt;
283         vh = p->ohdr;
284         vh->control = VOFR_CONTROL_VOICE;
285         vh->dtype = VOFR_TYPE_DTMF;
286         vh->vflags = VOFR_ROUTE_NONE;
287         vh->dlcih = p->dlcih;
288         vh->dlcil = p->dlcil;
289         vh->cid = p->cid;
290         vh->remid = VOFR_CARD_TYPE_ASTERISK;
291         vh->mod = VOFR_MODULATION_SINGLE;
292         if ((digit >= '0') && (digit <= '9'))
293                 vh->data[0] = digit - '0';
294         else if (digit == '*')
295                 vh->data[0] = 10;
296         else if (digit == '#')
297                 vh->data[0] = 11;
298         else {
299                 ast_log(LOG_WARNING, "%s: tried to dial a non digit '%c'\n", ast->name, digit);
300                 return -1;
301         }
302         vh->data[1] = 0x14;
303         vh->data[2] = 0x1f;
304         vh->data[3] = 0x70;
305                 /* We sorta start the digit */
306         vofr_xmit(p, p->obuf, VOFR_HDR_SIZE + 4 + FR_API_MESS);
307         usleep(30000);
308                 /* And terminate with an empty voice frame */
309         vh->control = VOFR_CONTROL_VOICE;
310         vh->dtype = VOFR_TYPE_VOICE;
311         vh->vflags = VOFR_ROUTE_NONE;
312         vh->dlcih = p->dlcih;
313         vh->dlcil = p->dlcil;
314         vh->cid = p->cid;
315         vh->remid = VOFR_CARD_TYPE_ASTERISK;
316         vh->mod = VOFR_MODULATION_SINGLE;
317         vofr_xmit(p, p->obuf, VOFR_HDR_SIZE + FR_API_MESS);
318         return 0;
319 }
320
321 static int vofr_xmit_signal(struct vofr_pvt *p, int signal, int pad)
322 {
323         /* Prepare and transmit outgoing buffer with given signal and
324            pad the end with *pad* bytes of data presumed to already
325            be in the buffer (like DTMF tones, etc) */
326         struct vofr_hdr *vh = p->ohdr;
327         int res;
328     vh->control = VOFR_CONTROL_VOICE;
329     vh->dtype = VOFR_TYPE_SIGNAL;
330     vh->vflags = VOFR_ROUTE_NONE;
331     vh->dlcih = p->dlcih;
332     vh->dlcil = p->dlcil;
333     vh->cid = p->cid;
334     vh->remid = VOFR_CARD_TYPE_ASTERISK;
335     vh->mod = VOFR_MODULATION_SINGLE;
336     vh->data[0] = signal;
337         if (FR_API_MESS)
338                 memset(p->obuf, 0, FR_API_MESS);
339         res = vofr_xmit(p, p->obuf,  VOFR_HDR_SIZE + pad + 1 + FR_API_MESS);
340         return res;
341
342 }
343
344 static int vofr_call(struct ast_channel *ast, char *dest, int timeout)
345 {
346         int res;
347         int otimeout;
348         struct ast_frame *f;
349         struct vofr_pvt *p;
350         p = ast->pvt->pvt;
351         if ((ast->state != AST_STATE_DOWN) && (ast->state != AST_STATE_RESERVED)) {
352                 ast_log(LOG_WARNING, "vofr_call called on %s, neither down nor reserved\n", ast->name);
353                 return -1;
354         }
355         /* Take the system off hook */
356         vofr_xmit_signal(p, VOFR_SIGNAL_OFFHOOK, 0);
357         /* Wait for an acknowledgement */
358         otimeout = 1000;
359         while(otimeout) {
360                 otimeout = ast_waitfor(ast, 1000);
361                 if (otimeout < 1) {
362                         ast_log(LOG_WARNING, "Unable to take line '%s' off hook\n", ast->name);
363                         /* Musta gotten hung up, or no ack on off hook */
364                         return -1;      
365                 }
366                 f = vofr_read(ast);
367                 if (!f)
368                         return -1;
369                 if ((f->frametype == AST_FRAME_CONTROL) &&
370                     (f->subclass == AST_CONTROL_OFFHOOK)) 
371                         /* Off hook */
372                                 break;
373         }
374         if (!otimeout) {
375                 ast_log(LOG_WARNING, "Unable to take line off hook\n");
376                 return -1;
377         }
378         /* Send the digits */
379         while(*dest) {
380                 ast->state = AST_STATE_DIALING;
381                 vofr_digit(ast, *dest);
382                 /* Wait .1 seconds before dialing next digit */
383                 usleep(100000);
384                 dest++;
385         }
386         if (timeout) {
387                 /* Wait for the ack that it's ringing */
388                 otimeout = 1000;
389                 while(otimeout) {
390                         otimeout = ast_waitfor(ast, 1000);
391                         if (otimeout < 1) {
392                                 ast_log(LOG_WARNING, "No acknowledgement for ringing\n");
393                                 /* Musta gotten hung up, or no ack on off hook */
394                                 return -1;      
395                         }
396                         f = vofr_read(ast);
397                         if (!f) 
398                                 return -1;
399                         
400                         if (f->frametype == AST_FRAME_CONTROL) {
401                             if (f->subclass == AST_CONTROL_RINGING) {
402                                         ast->state = AST_STATE_RINGING;
403                                         /* We're ringing -- good enough */
404                                         break;
405                                 }
406                                 if (f->subclass == AST_CONTROL_BUSY)
407                                 /* It's busy */
408                                         return -1;
409                         }
410                         ast_frfree(f);
411                 }               
412         }
413         otimeout = timeout;
414         while(timeout) {
415                 /* Wait for an answer, up to timeout... */
416                 res = ast_waitfor(ast, timeout);
417                 if (res < 0)
418                         /* Musta gotten hung up */
419                         return -1;
420                 else
421                         timeout = res;
422                 if (res) {
423                         /* Ooh, read what's there. */
424                         f = vofr_read(ast);
425                         if (!f)
426                                 return -1;
427                         if ((f->frametype == AST_FRAME_CONTROL) && 
428                             (f->subclass == AST_CONTROL_ANSWER)) 
429                                 /* Got an answer -- return the # of ms it took */
430                                 return otimeout - res;
431                                 
432                 }
433         }
434         return 0;
435 }
436
437 static int send_hangup(struct vofr_pvt *p)
438 {
439         /* Just send the hangup sequence */
440         return vofr_xmit_signal(p, 0x80, 0);
441 }
442
443 static int vofr_hangup(struct ast_channel *ast)
444 {
445         int res;
446         if (option_debug)
447                 ast_log(LOG_DEBUG, "vofr_hangup(%s)\n", ast->name);
448         if (!ast->pvt->pvt) {
449                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
450                 return 0;
451         }
452         res = send_hangup(ast->pvt->pvt);
453         if (res < 0) {
454                 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
455                 return -1;
456         }
457         ast->state = AST_STATE_DOWN;
458         ((struct vofr_pvt *)(ast->pvt->pvt))->owner = NULL;
459         ((struct vofr_pvt *)(ast->pvt->pvt))->ringgothangup = 0;
460         ast_mutex_lock(&usecnt_lock);
461         usecnt--;
462         if (usecnt < 0) 
463                 ast_log(LOG_WARNING, "Usecnt < 0???\n");
464         ast_mutex_unlock(&usecnt_lock);
465         ast_update_use_count();
466         if (option_verbose > 2) 
467                 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
468         ast->pvt->pvt = NULL;
469         ast->state = AST_STATE_DOWN;
470         restart_monitor();
471         return 0;
472 }
473
474 static int vofr_answer(struct ast_channel *ast)
475 {
476         int res;
477         int cnt = 1000;
478         char buf[2048];
479         struct vofr_hdr *vh;
480         ast->rings = 0;
481         if (option_debug)
482                 ast_log(LOG_DEBUG, "vofr_answer(%s)\n", ast->name);
483         res = vofr_xmit_signal(ast->pvt->pvt, VOFR_SIGNAL_OFFHOOK, 0);
484         if (res < 0)
485                 ast_log(LOG_WARNING, "Unable to anaswer line %s\n", ast->name);
486         ast->state = AST_STATE_UP;
487         while(cnt > 0) {
488                 cnt = ast_waitfor(ast, cnt);
489                 if (cnt > 0) {
490                         res = read(ast->fds[0], buf, sizeof(buf));
491 #ifdef VOFRDUMPER
492                                 vofr_dump_packet((void *)(buf +FR_API_MESS), res - FR_API_MESS);
493 #endif
494                         res -= FR_API_MESS;
495                         if (res < 0)
496                                 ast_log(LOG_WARNING, "Warning:  read failed (%s) on %s\n", strerror(errno), ast->name);
497                         else {
498                                 /* We're looking for an answer */
499                                 vh = (struct vofr_hdr *)(buf + FR_API_MESS);
500                                 switch(vh->dtype) {
501                                 case VOFR_TYPE_SIGNAL:
502                                         switch(vh->data[0]) {
503                                         case VOFR_SIGNAL_UNKNOWN:
504                                                 switch(vh->data[1]) {
505                                                 case 0x1:
506                                                         if (option_debug) 
507                                                                 ast_log(LOG_DEBUG, "Answered '%s'\n", ast->name);
508                                                         else if (option_verbose > 2) 
509                                                                 ast_verbose( VERBOSE_PREFIX_3 "Answered '%s'\n", ast->name);
510                                                         ast->state = AST_STATE_UP;
511                                                         return 0;
512                                                         break;
513                                                 default:
514                                                         ast_log(LOG_WARNING, "Unexpected 'unknown' frame type %d\n", vh->data[1]);
515                                                 }
516                                                 break;
517                                         case VOFR_SIGNAL_ON_HOOK:
518                                                 /* Ignore onhooks.  */
519                                                 break;
520                                         default:
521                                                 ast_log(LOG_WARNING, "Unexpected signal type %d\n", vh->data[0]);
522                                         }
523                                         break;
524                                 default:
525                                         ast_log(LOG_WARNING, "Unexpected data type %d\n", vh->dtype);
526                                 }
527                         }
528                 }
529         }
530         ast_log(LOG_WARNING, "Did not get acknowledged answer\n");
531         return -1;
532 }
533
534 static char vofr_2digit(char c)
535 {
536         if (c == 11)
537                 return '#';
538         else if (c == 10)
539                 return '*';
540         else if ((c < 10) && (c >= 0))
541                 return '0' + c;
542         else
543                 return '?';
544 }
545
546 static struct ast_frame  *vofr_read(struct ast_channel *ast)
547 {
548         int res;
549         char tone;
550         int timeout,x;
551         struct vofr_pvt *p = ast->pvt->pvt;
552         short *swapping;
553         struct ast_frame *fr = (struct ast_frame *)(p->buf);
554         struct vofr_hdr *vh = (struct vofr_hdr *)(p->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET - sizeof(struct vofr_hdr));
555         /* Read into the right place in the buffer, in case we send this
556            as a voice frame. */
557         CHECK_BLOCKING(ast);
558 retry:
559         res = read(p->s, ((char *)vh)  - FR_API_MESS, 
560                                 G723_MAX_BUF - AST_FRIENDLY_OFFSET - sizeof(struct ast_frame) + sizeof(struct vofr_hdr) + FR_API_MESS);
561         if (res < 0) {
562                 /*  XXX HUGE BUG IN SANGOMA'S STACK: IT IGNORES O_NONBLOCK XXX */
563                 if (errno == EAGAIN) {
564                         fd_set fds;
565                         FD_ZERO(&fds);
566                         FD_SET(p->s, &fds);
567                         ast_select(p->s + 1, &fds, NULL, NULL, NULL);
568                         goto retry;
569                 }
570                 ast->blocking = 0;
571                 ast_log(LOG_WARNING, "Read error on %s: %s (%d)\n", ast->name, strerror(errno));
572                 return NULL;
573         }
574         ast->blocking = 0;
575                 
576 #ifdef VOFRDUMPER
577         vofr_dump_packet((void *)(vh), res);
578 #endif
579         res -= FR_API_MESS;             
580         if (res < sizeof(struct vofr_hdr)) {
581                 ast_log(LOG_WARNING, "Nonsense frame on %s\n", ast->name);
582                 return NULL;
583         }
584         /* Some nice norms */
585         fr->datalen = 0;
586         fr->timelen = 0;
587         fr->data =  NULL;
588         fr->src = type;
589         fr->offset = 0;
590         fr->mallocd=0;
591         
592         /* Now, what we do depends on what we read */
593         switch(vh->dtype) {
594         case VOFR_TYPE_SIGNAL:
595                 switch(vh->data[0]) {
596                 case VOFR_SIGNAL_ON_HOOK:
597                         /* Hang up this line */
598                         if ((ast->state == AST_STATE_UP) || (p->ringgothangup)) {
599                                 return NULL;
600                         } else {
601                                 fr->frametype = AST_FRAME_NULL;
602                                 fr->subclass = 0;
603                                 p->ringgothangup=1;
604                         }
605                         break;
606                 case VOFR_SIGNAL_RING:
607                         ast->rings++;
608                         p->ringgothangup = 0;
609                         break;
610                 case VOFR_SIGNAL_UNKNOWN:
611                         switch(vh->data[1]) {
612                         case 0x1:
613                                 /* This is a little tricky, because it depends
614                                    on the context of what state we're in */
615                                 switch(ast->state) {
616                                 case AST_STATE_RINGING:
617                                         fr->frametype = AST_FRAME_CONTROL;
618                                         fr->subclass = AST_CONTROL_ANSWER;
619                                         ast->state = AST_STATE_UP;
620                                         break;
621                                 case AST_STATE_DOWN:
622                                 case AST_STATE_UP:
623                                         fr->frametype = AST_FRAME_NULL;
624                                         fr->subclass = 0;
625                                         break;
626                                 }
627                                 break;
628                         case 0x2:
629                                 /* Remote acknowledged off hook */
630                                 fr->frametype = AST_FRAME_CONTROL;
631                                 fr->subclass = AST_CONTROL_OFFHOOK;
632                                 ast->state = AST_STATE_OFFHOOK;
633                                 break;
634                         case 0x3:
635                                 /* Busy signal */
636                                 fr->frametype = AST_FRAME_CONTROL;
637                                 fr->subclass = AST_CONTROL_BUSY;
638                                 ast->state = AST_STATE_BUSY;
639                                 break;
640                         case 0x5:
641                                 /* Ringing -- acknowledged */
642                                 fr->frametype = AST_FRAME_CONTROL;
643                                 fr->subclass = AST_CONTROL_RINGING;
644                                 ast->state = AST_STATE_RINGING;
645                                 break;
646                         case 0x6:
647                                 /* Hang up detected.  Return NULL */
648                                 return NULL;
649                         default:
650                                 ast_log(LOG_WARNING, "Don't know what to do with 'unknown' signal '%d'\n", vh->data[1]);
651                                 fr->frametype = AST_FRAME_NULL;
652                                 fr->subclass = 0;
653                         }
654                         return fr;
655                         break;
656                 default:
657                         ast_log(LOG_WARNING, "Don't know what to do with signal '%d'\n", vh->data[0]);
658                 }
659                 break;
660         case VOFR_TYPE_DTMF:
661                 /* If it's a DTMF tone, then we want to wait until we don't get any more dtmf tones or
662                    the DTMF tone changes.  
663                        XXX Note: We will drop at least one frame here though XXX */
664                 
665                 tone = vofr_2digit(vh->data[0]);
666                 timeout = 50;
667                 do {
668                         if ((timeout = ast_waitfor(ast, timeout)) < 1)
669                                 break;
670                         CHECK_BLOCKING(ast);
671                         res = read(p->s, ((char *)vh)  - FR_API_MESS, 
672                                         G723_MAX_BUF - AST_FRIENDLY_OFFSET - sizeof(struct ast_frame) + sizeof(struct vofr_hdr) + FR_API_MESS);
673                         ast->blocking = 0;
674                         res -= FR_API_MESS;             
675                         if (res < sizeof(struct vofr_hdr *)) {
676                                 ast_log(LOG_WARNING, "Nonsense frame on %s\n", ast->name);
677                                 return NULL;
678                         }
679                         if (vh->dtype == VOFR_TYPE_DTMF) {
680                                 /* Reset the timeout */
681                                 timeout = 50;
682                                 if ((tone != vofr_2digit(vh->data[0])) )
683                                         /* Or not...  Something else now.. Just send our first frame */
684                                         break;
685                         }
686                         
687                 } while (timeout);
688                 fr->frametype = AST_FRAME_DTMF;
689                 fr->subclass = tone;
690                 fr->datalen = 0;
691                 fr->data = NULL;
692                 fr->offset = 0;
693                 return fr;
694         case VOFR_TYPE_VOICE:
695                 /* XXX Bug in the Adtran: Sometimes we don't know when calls are picked up, so if we
696                        get voice frames, go ahead and consider it answered even though it probably has
697                            not been answered XXX */
698                 if ((ast->state == AST_STATE_RINGING) || (ast->state == AST_STATE_DIALING))  {
699                         ast_log(LOG_DEBUG, "Adtran bug! (state = %d)\n", ast->state);
700                         fr->frametype = AST_FRAME_CONTROL;
701                         fr->subclass = AST_CONTROL_ANSWER;
702                         ast->state = AST_STATE_UP;
703                         return fr;
704                 } else if (ast->state !=  AST_STATE_UP) {
705                         ast_log(LOG_WARNING, "%s: Voice in weird state %d\n", ast->name, ast->state);
706                 }
707                 fr->frametype = AST_FRAME_VOICE;
708                 fr->subclass = AST_FORMAT_G723_1;
709                 fr->datalen = res - sizeof(struct vofr_hdr);
710                 fr->data = ((char *)vh) + sizeof(struct vofr_hdr);
711                 fr->src = type;
712                 /* XXX Byte swapping is a bug XXX */
713                 swapping = fr->data;
714                 for (x=0;x<fr->datalen/2;x++)
715                         swapping[x] = ntohs(swapping[x]);
716                 fr->offset = AST_FRIENDLY_OFFSET;
717                 /* Thirty ms of sound per frame */
718                 fr->timelen = 30;
719                 return fr;
720         default:
721                 ast_log(LOG_WARNING, "Don't know what to do with data type %d frames\n", vh->dtype);
722         }
723         /* If we don't know what it is, send a NULL frame */
724         fr->frametype = AST_FRAME_NULL;
725         fr->subclass = 0;
726         return fr;
727 }
728
729 static int vofr_write(struct ast_channel *ast, struct ast_frame *frame)
730 {
731         struct vofr_hdr *vh;
732         struct vofr_pvt *p = ast->pvt->pvt;
733         short *swapping;
734     int x;
735         char *start;
736         int res;
737         /* Write a frame of (presumably voice) data */
738         if (frame->frametype != AST_FRAME_VOICE) {
739                 ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
740                 return -1;
741         }
742         if (frame->subclass != AST_FORMAT_G723_1) {
743                 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
744                 return -1;
745         }
746         /* If we get here, we have a voice frame of G.723.1 data.  First check to be
747            sure we have enough headroom for the vofr header.  If there isn't enough
748            headroom, we're lazy and just error out rather than copying it into the
749            output buffer, because applications should always leave AST_FRIENDLY_OFFSET
750            bytes just for this reason. */
751         if (frame->offset < sizeof(struct vofr_hdr) + FR_API_MESS) {
752                 ast_log(LOG_WARNING, "Frame source '%s' didn't provide a friendly enough offset\n", (frame->src ? frame->src : "**Unknown**"));
753                 return -1;
754         }
755         /* XXX Byte swapping is a bug XXX */
756         swapping = frame->data;
757         for (x=0;x<frame->datalen/2;x++)
758                 swapping[x] = ntohs(swapping[x]);
759         vh = (struct vofr_hdr *)(frame->data - sizeof(struct vofr_hdr));
760         /* Some versions of the API have some header mess that needs to be
761            zero'd out and acounted for..  */
762         start = ((void *)vh) - FR_API_MESS;
763         if (start)
764                 memset(start, 0, FR_API_MESS);
765         /* Now we fill in the vofr header */
766         vh->control = VOFR_CONTROL_VOICE;
767         vh->dtype = VOFR_TYPE_VOICE;
768         vh->vflags = VOFR_ROUTE_NONE;
769         vh->dlcih = p->dlcih;
770         vh->dlcil = p->dlcil;
771         vh->cid = p->cid;
772         vh->remid = VOFR_CARD_TYPE_ASTERISK;
773         vh->mod = VOFR_MODULATION_SINGLE;
774         res = vofr_xmit(p, start, 
775                                 VOFR_HDR_SIZE + frame->datalen + FR_API_MESS);
776         res -= FR_API_MESS;
777         /* XXX Byte swapping is a bug, but get it back to the right format XXX */
778         swapping = frame->data;
779         for (x=0;x<frame->datalen/2;x++)
780                 swapping[x] = htons(swapping[x]);
781         if (res != VOFR_HDR_SIZE + frame->datalen) {
782                 ast_log(LOG_WARNING, "Unable to write frame correctly\n");
783                 return -1;
784         }
785         return 0;
786 }
787
788 static int vofr_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
789 {
790         struct vofr_pvt *p = newchan->pvt->pvt;
791         if (p->owner != oldchan) {
792                 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
793                 return -1;
794         }
795         p->owner = newchan;
796         return 0;
797 }
798
799 static struct ast_channel *vofr_new(struct vofr_pvt *i, int state)
800 {
801         struct ast_channel *tmp;
802         tmp = ast_channel_alloc(0);
803         if (tmp) {
804 #ifdef OLD_SANGOMA_API
805                 snprintf(tmp->name, sizeof(tmp->name), "AdtranVoFR/%s", i->sa.spkt_device);
806 #else
807                 snprintf(tmp->name, sizeof(tmp->name), "AdtranVoFR/%s", i->sa.sll_device);
808 #endif
809                 tmp->type = type;
810                 tmp->fds[0] = i->s;
811                 /* Adtran VoFR supports only G723.1 format data.  G711 (ulaw) would be nice too */
812                 tmp->nativeformats = AST_FORMAT_G723_1;
813                 tmp->state = state;
814                 if (state == AST_STATE_RING)
815                         tmp->rings = 1;
816                 tmp->writeformat = AST_FORMAT_G723_1;
817                 tmp->readformat = AST_FORMAT_G723_1;
818                 tmp->pvt->pvt = i;
819                 tmp->pvt->send_digit = vofr_digit;
820                 tmp->pvt->call = vofr_call;
821                 tmp->pvt->hangup = vofr_hangup;
822                 tmp->pvt->answer = vofr_answer;
823                 tmp->pvt->read = vofr_read;
824                 tmp->pvt->write = vofr_write;
825                 tmp->pvt->fixup = vofr_fixup;
826                 if (strlen(i->language))
827                         strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
828                 i->owner = tmp;
829                 ast_mutex_lock(&usecnt_lock);
830                 usecnt++;
831                 ast_mutex_unlock(&usecnt_lock);
832                 ast_update_use_count();
833                 strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
834                 if (state != AST_STATE_DOWN) {
835                         if (ast_pbx_start(tmp)) {
836                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
837                                 ast_hangup(tmp);
838                                 tmp = NULL;
839                         }
840                 }
841         } else
842                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
843         return tmp;
844 }
845
846 static int vofr_mini_packet(struct vofr_pvt *i, struct vofr_hdr *pkt, int len)
847 {
848         /* Here, we're looking for rings or off hooks -- signals that
849            something is about to happen and we need to start the 
850            PBX thread */
851         switch(pkt->dtype) {
852         case VOFR_TYPE_SIGNAL:
853                 switch(pkt->data[0]) {
854                 case VOFR_SIGNAL_RING:
855                         /* If we get a RING, we definitely want to start a new thread */
856                         if (!i->owner) {
857                                 i->ringgothangup = 0;
858                                 vofr_new(i, AST_STATE_RING);
859                         } else
860                                 ast_log(LOG_WARNING, "Got a ring, but there's an owner?\n");
861                         break;
862                 case VOFR_SIGNAL_OFF_HOOK:
863                         /* Network termination, go off hook */
864 #if 0
865                         ast_log(LOG_DEBUG, "Off hook\n");
866 #endif
867                         vofr_xmit_signal(i, 0x10, 2);
868                         if (!i->owner)
869                                 vofr_new(i, AST_STATE_UP);
870                         else
871                                 ast_log(LOG_WARNING, "Got an offhook, but there's an owner?\n");
872                         break;
873                 case VOFR_SIGNAL_ON_HOOK:
874                         break;
875                 case VOFR_SIGNAL_UNKNOWN:
876                         switch(pkt->data[1]) {
877                         case 0x1:
878                                 /* ignore */
879                                 break;
880                         case 0x6:
881                                 /* A remote hangup request */
882                                 if (option_debug)
883                                         ast_log(LOG_DEBUG, "Sending hangup reply\n");
884                                 send_hangup(i);
885                                 break;
886                         default:
887                                 ast_log(LOG_WARNING, "Unexected 'unknown' signal '%d'\n", pkt->data[1]);
888                         }
889                         break;
890                 default:
891                         ast_log(LOG_DEBUG, "Unknown signal type '%d'\n", pkt->data[0]);
892                 }                       
893                 break;
894         case VOFR_TYPE_VOICE:
895                 break;
896         default:
897                 ast_log(LOG_DEBUG, "Unknown packet type '%d'\n", pkt->dtype);
898         }
899         return 0;
900 }
901
902 static void *do_monitor(void *data)
903 {
904         fd_set rfds;
905         int n, res;
906         struct vofr_pvt *i;
907         /* This thread monitors all the frame relay interfaces which are not yet in use
908            (and thus do not have a separate thread) indefinitely */
909         /* From here on out, we die whenever asked */
910 #if 0
911         if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
912                 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
913                 return NULL;
914         }
915 #endif
916         for(;;) {
917                 /* Don't let anybody kill us right away.  Nobody should lock the interface list
918                    and wait for the monitor list, but the other way around is okay. */
919                 if (ast_mutex_lock(&monlock)) {
920                         ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
921                         return NULL;
922                 }
923                 /* Lock the interface list */
924                 if (ast_mutex_lock(&iflock)) {
925                         ast_log(LOG_ERROR, "Unable to grab interface lock\n");
926                         ast_mutex_unlock(&monlock);
927                         return NULL;
928                 }
929                 /* Build the stuff we're going to select on, that is the socket of every
930                    vofr_pvt that does not have an associated owner channel */
931                 n = -1;
932                 FD_ZERO(&rfds);
933                 i = iflist;
934                 while(i) {
935                         if (FD_ISSET(i->s, &rfds)) 
936 #ifdef OLD_SANGOMA_API
937                                 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->s, i->sa.spkt_device);
938 #else
939                                 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->s, i->sa.sll_device);
940 #endif
941                         if (!i->owner) {
942                                 /* This needs to be watched, as it lacks an owner */
943                                 FD_SET(i->s, &rfds);
944                                 if (i->s > n)
945                                         n = i->s;
946                         }
947                         i = i->next;
948                 }
949                 /* Okay, now that we know what to do, release the interface lock */
950                 ast_mutex_unlock(&iflock);
951                 
952                 /* And from now on, we're okay to be killed, so release the monitor lock as well */
953                 ast_mutex_unlock(&monlock);
954                 pthread_testcancel();
955                 /* Wait indefinitely for something to happen */
956                 res = ast_select(n + 1, &rfds, NULL, NULL, NULL);
957                 pthread_testcancel();
958                 /* Okay, select has finished.  Let's see what happened.  */
959                 if (res < 0) {
960                         if ((errno != EAGAIN) && (errno != EINTR))
961                                 ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
962                         continue;
963                 }
964                 /* Alright, lock the interface list again, and let's look and see what has
965                    happened */
966                 if (ast_mutex_lock(&iflock)) {
967                         ast_log(LOG_WARNING, "Unable to lock the interface list\n");
968                         continue;
969                 }
970                 i = iflist;
971                 while(i) {
972                         if (FD_ISSET(i->s, &rfds)) {
973                                 if (i->owner) {
974 #ifdef OLD_SANGOMA_API
975                                         ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->s, i->sa.spkt_device);
976 #else
977                                         ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->s, i->sa.sll_device);
978 #endif
979                                         continue;
980                                 }
981                                 res = read(i->s, i->buf, sizeof(i->buf));
982                                 res -= FR_API_MESS;
983 #ifdef VOFRDUMPER
984                                 vofr_dump_packet(i->hdr, res);
985 #endif
986                                 vofr_mini_packet(i, i->hdr, res);
987                         }
988                         i=i->next;
989                 }
990                 ast_mutex_unlock(&iflock);
991         }
992         /* Never reached */
993         return NULL;
994         
995 }
996
997 static int restart_monitor(void)
998 {
999         /* If we're supposed to be stopped -- stay stopped */
1000         if (monitor_thread == AST_PTHREADT_STOP)
1001                 return 0;
1002         if (ast_mutex_lock(&monlock)) {
1003                 ast_log(LOG_WARNING, "Unable to lock monitor\n");
1004                 return -1;
1005         }
1006         if (monitor_thread == pthread_self()) {
1007                 ast_mutex_unlock(&monlock);
1008                 ast_log(LOG_WARNING, "Cannot kill myself\n");
1009                 return -1;
1010         }
1011         if (monitor_thread != AST_PTHREADT_NULL) {
1012                 /* Wake up the thread */
1013                 pthread_kill(monitor_thread, SIGURG);
1014         } else {
1015                 /* Start a new monitor */
1016                 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
1017                         ast_mutex_unlock(&monlock);
1018                         ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1019                         return -1;
1020                 }
1021         }
1022         ast_mutex_unlock(&monlock);
1023         return 0;
1024 }
1025
1026 static struct vofr_pvt *mkif(char *type, char *iface)
1027 {
1028         /* Make a vofr_pvt structure for this interface */
1029         struct vofr_pvt *tmp;
1030         int sndbuf = 4096;
1031
1032         tmp = malloc(sizeof(struct vofr_pvt));
1033         if (tmp) {
1034
1035                 /* Allocate a packet socket */
1036 #ifdef OLD_SANGOMA_API
1037                 tmp->s = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL));
1038 #else
1039                 /* Why the HELL does Sangoma change their API every damn time
1040                    they make a new driver release?!?!?!  Leave it the hell
1041                    alone this time.  */
1042                 tmp->s = socket(AF_WANPIPE, SOCK_RAW, 0);
1043 #endif          
1044
1045                 if (tmp->s < 0) {
1046                         ast_log(LOG_ERROR, "Unable to create socket: %s\n", strerror(errno));
1047                         free(tmp);
1048                         return NULL;
1049                 }
1050
1051 #ifdef OLD_SANGOMA_API
1052                 /* Prepare sockaddr for binding */
1053                 memset(&tmp->sa, 0, sizeof(tmp->sa));
1054                 strncpy(tmp->sa.spkt_device, iface, sizeof(tmp->sa.spkt_device)-1);
1055                 tmp->sa.spkt_protocol = htons(0x16);
1056                 tmp->sa.spkt_family = AF_PACKET;
1057                 if (bind(tmp->s, (struct sockaddr *)&tmp->sa, sizeof(struct sockaddr))) {
1058 #else
1059                 /* Prepare sockaddr for binding */
1060                 memset(&tmp->sa, 0, sizeof(tmp->sa));
1061                 tmp->sa.sll_family = AF_WANPIPE;
1062                 tmp->sa.sll_protocol = htons(ETH_P_IP);
1063                 strncpy(tmp->sa.sll_device, iface, sizeof(tmp->sa.sll_device)-1);
1064                 strncpy(tmp->sa.sll_card, "wanpipe1", sizeof(tmp->sa.sll_card)-1);
1065                 tmp->sa.sll_ifindex = 0;
1066                 if (bind(tmp->s, (struct sockaddr *)&tmp->sa, sizeof(struct wan_sockaddr_ll))) {
1067 #endif          
1068                 /* Bind socket to specific interface */
1069 #ifdef OLD_SANGOMA_API
1070                         ast_log(LOG_ERROR, "Unable to bind to '%s': %s\n", tmp->sa.spkt_device, 
1071 #else
1072                         ast_log(LOG_ERROR, "Unable to bind to '%s': %s\n", tmp->sa.sll_device, 
1073 #endif
1074                                                                                 strerror(errno));
1075                         free(tmp);
1076                         return NULL;
1077                 }
1078                 
1079                 /* Set magic send buffer size */
1080                 if (setsockopt(tmp->s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))) {
1081                         ast_log(LOG_ERROR, "Unable to set send buffer size to %d: %s\n", sndbuf, strerror(errno));
1082                         free(tmp);
1083                         return NULL;
1084                 }
1085                 tmp->owner =  NULL;
1086                 tmp->hdr = (struct vofr_hdr *)(tmp->buf + FR_API_MESS);
1087                 tmp->ohdr = (struct vofr_hdr *)(tmp->obuf + FR_API_MESS);
1088                 tmp->dlcil = 0;
1089                 tmp->dlcih = 0;
1090                 tmp->cid = 1;
1091                 tmp->ringgothangup = 0;
1092                 strncpy(tmp->language, language, sizeof(tmp->language)-1);
1093                 strncpy(tmp->context, context, sizeof(tmp->context)-1);
1094                 /* User terminations are game for outgoing connections */
1095                 if (!strcasecmp(type, "user")) 
1096                         tmp->outgoing = 1;
1097                 else
1098                         tmp->outgoing = 0;
1099                 tmp->next = NULL;
1100                 /* Hang it up to be sure it's good */
1101                 send_hangup(tmp);
1102                 
1103         }
1104         return tmp;
1105 }
1106
1107 static struct ast_channel *vofr_request(char *type, int format, void *data)
1108 {
1109         int oldformat;
1110         struct vofr_pvt *p;
1111         struct ast_channel *tmp = NULL;
1112         /* We can only support G.723.1 formatted frames, but we should never
1113            be asked to support anything else anyway, since we've published
1114            our capabilities when we registered. */
1115         oldformat = format;
1116         format &= AST_FORMAT_G723_1;
1117         if (!format) {
1118                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
1119                 return NULL;
1120         }
1121         /* Search for an unowned channel */
1122         if (ast_mutex_lock(&iflock)) {
1123                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1124                 return NULL;
1125         }
1126         p = iflist;
1127         while(p) {
1128                 if (!p->owner && p->outgoing) {
1129                         tmp = vofr_new(p, AST_STATE_DOWN);
1130                         break;
1131                 }
1132                 p = p->next;
1133         }
1134         ast_mutex_unlock(&iflock);
1135         restart_monitor();
1136         return tmp;
1137 }
1138
1139 static int __unload_module(void)
1140 {
1141         struct vofr_pvt *p, *pl;
1142         /* First, take us out of the channel loop */
1143         ast_channel_unregister(type);
1144         if (!ast_mutex_lock(&iflock)) {
1145                 /* Hangup all interfaces if they have an owner */
1146                 p = iflist;
1147                 while(p) {
1148                         if (p->owner)
1149                                 ast_softhangup(p->owner);
1150                         p = p->next;
1151                 }
1152                 iflist = NULL;
1153                 ast_mutex_unlock(&iflock);
1154         } else {
1155                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1156                 return -1;
1157         }
1158         if (!ast_mutex_lock(&monlock)) {
1159                 if (monitor_thread) {
1160                         pthread_cancel(monitor_thread);
1161                         pthread_kill(monitor_thread, SIGURG);
1162                         pthread_join(monitor_thread, NULL);
1163                 }
1164                 monitor_thread = AST_PTHREADT_STOP;
1165                 ast_mutex_unlock(&monlock);
1166         } else {
1167                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1168                 return -1;
1169         }
1170
1171         if (!ast_mutex_lock(&iflock)) {
1172                 /* Destroy all the interfaces and free their memory */
1173                 p = iflist;
1174                 while(p) {
1175                         /* Close the socket, assuming it's real */
1176                         if (p->s > -1)
1177                                 close(p->s);
1178                         pl = p;
1179                         p = p->next;
1180                         /* Free associated memory */
1181                         free(pl);
1182                 }
1183                 iflist = NULL;
1184                 ast_mutex_unlock(&iflock);
1185         } else {
1186                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1187                 return -1;
1188         }
1189                 
1190         return 0;
1191 }
1192
1193 int unload_module()
1194 {
1195         return __unload_module();
1196 }
1197
1198 int load_module()
1199 {
1200         struct ast_config *cfg;
1201         struct ast_variable *v;
1202         struct vofr_pvt *tmp;
1203         cfg = ast_load(config);
1204
1205         /* We *must* have a config file otherwise stop immediately */
1206         if (!cfg) {
1207                 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1208                 return -1;
1209         }
1210         if (ast_mutex_lock(&iflock)) {
1211                 /* It's a little silly to lock it, but we mind as well just to be sure */
1212                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1213                 return -1;
1214         }
1215         v = ast_variable_browse(cfg, "interfaces");
1216         while(v) {
1217                 /* Create the interface list */
1218                 if (!strcasecmp(v->name, "user") ||
1219                         !strcasecmp(v->name, "network")) {
1220                                 tmp = mkif(v->name, v->value);
1221                                 if (tmp) {
1222                                         tmp->next = iflist;
1223                                         iflist = tmp;
1224                                 } else {
1225                                         ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
1226                                         ast_destroy(cfg);
1227                                         ast_mutex_unlock(&iflock);
1228                                         __unload_module();
1229                                         return -1;
1230                                 }
1231                 } else if (!strcasecmp(v->name, "context")) {
1232                         strncpy(context, v->value, sizeof(context)-1);
1233                 } else if (!strcasecmp(v->name, "language")) {
1234                         strncpy(language, v->value, sizeof(language)-1);
1235                 }
1236                 v = v->next;
1237         }
1238         ast_mutex_unlock(&iflock);
1239         /* Make sure we can register our AdtranVoFR channel type */
1240         if (ast_channel_register(type, tdesc, AST_FORMAT_G723_1, vofr_request)) {
1241                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1242                 ast_destroy(cfg);
1243                 __unload_module();
1244                 return -1;
1245         }
1246         ast_destroy(cfg);
1247         /* And start the monitor for the first time */
1248         restart_monitor();
1249         return 0;
1250 }
1251
1252 int usecount()
1253 {
1254         int res;
1255         ast_mutex_lock(&usecnt_lock);
1256         res = usecnt;
1257         ast_mutex_unlock(&usecnt_lock);
1258         return res;
1259 }
1260
1261 char *key()
1262 {
1263         return ASTERISK_GPL_KEY;
1264 }
1265
1266 char *description()
1267 {
1268         return desc;
1269 }
1270