Version 0.3.0 from FTP
[asterisk/asterisk.git] / channels / chan_vofr.c
index aee44ff..f1bb5e5 100755 (executable)
@@ -14,6 +14,7 @@
 #include <stdio.h>
 #include <pthread.h>
 #include <string.h>
+#include <asterisk/lock.h>
 #include <asterisk/channel.h>
 #include <asterisk/channel_pvt.h>
 #include <asterisk/config.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 VOFRDUMPER */
+
 #define G723_MAX_BUF 2048
 
 #define FR_API_MESS 16
@@ -41,19 +50,21 @@ static char *config = "adtranvofr.conf";
 
 static char context[AST_MAX_EXTENSION] = "default";
 
+static char language[MAX_LANGUAGE] = "";
+
 static int usecnt =0;
-static pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
 
 /* Protect the interface list (of vofr_pvt's) */
-static pthread_mutex_t iflock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t iflock = AST_MUTEX_INITIALIZER;
 
 /* Protect the monitoring thread, so only one process can kill or start it, and not
    when it's doing something critical. */
-static pthread_mutex_t monlock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t monlock = AST_MUTEX_INITIALIZER;
 
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use.  */
-static pthread_t monitor_thread = -1;
+static pthread_t monitor_thread = 0;
 
 static int restart_monitor(void);
 
@@ -62,7 +73,11 @@ static int restart_monitor(void);
    
 static struct vofr_pvt {
        int s;                                                  /* Raw socket for this DLCI */
+#ifdef OLD_SANGOMA_API
        struct sockaddr_pkt sa;                 /* Sockaddr needed for sending, also has iface name */
+#else
+       struct wan_sockaddr_ll sa;              /* Wanpipe sockaddr */
+#endif
        struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
        int outgoing;                                   /* Does this channel support outgoing calls? */
        struct vofr_pvt *next;                  /* Next channel in list */
@@ -74,6 +89,8 @@ static struct vofr_pvt {
        char buf[G723_MAX_BUF];                                 /* Static buffer for reading frames */
        char obuf[G723_MAX_BUF];                                /* Output buffer */
        char context[AST_MAX_EXTENSION];
+       char language[MAX_LANGUAGE];
+       int ringgothangup;                              /* Have we received exactly one hangup after a ring */
 } *iflist = NULL;
 
 #ifdef VOFRDUMPER
@@ -237,10 +254,16 @@ static void vofr_dump_packet(struct vofr_hdr *vh, int len)
 
 #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);
        }
@@ -336,11 +359,11 @@ static int vofr_call(struct ast_channel *ast, char *dest, int timeout)
        while(otimeout) {
                otimeout = ast_waitfor(ast, 1000);
                if (otimeout < 1) {
-                       ast_log(LOG_WARNING, "Unable to take line off hook\n");
+                       ast_log(LOG_WARNING, "Unable to take line '%s' off hook\n", ast->name);
                        /* Musta gotten hung up, or no ack on off hook */
                        return -1;      
                }
-               f = ast_read(ast);
+               f = vofr_read(ast);
                if (!f)
                        return -1;
                if ((f->frametype == AST_FRAME_CONTROL) &&
@@ -370,7 +393,7 @@ static int vofr_call(struct ast_channel *ast, char *dest, int timeout)
                                /* Musta gotten hung up, or no ack on off hook */
                                return -1;      
                        }
-                       f = ast_read(ast);
+                       f = vofr_read(ast);
                        if (!f) 
                                return -1;
                        
@@ -398,7 +421,7 @@ static int vofr_call(struct ast_channel *ast, char *dest, int timeout)
                        timeout = res;
                if (res) {
                        /* Ooh, read what's there. */
-                       f = ast_read(ast);
+                       f = vofr_read(ast);
                        if (!f)
                                return -1;
                        if ((f->frametype == AST_FRAME_CONTROL) && 
@@ -433,11 +456,12 @@ static int vofr_hangup(struct ast_channel *ast)
        }
        ast->state = AST_STATE_DOWN;
        ((struct vofr_pvt *)(ast->pvt->pvt))->owner = NULL;
-       pthread_mutex_lock(&usecnt_lock);
+       ((struct vofr_pvt *)(ast->pvt->pvt))->ringgothangup = 0;
+       ast_pthread_mutex_lock(&usecnt_lock);
        usecnt--;
        if (usecnt < 0) 
                ast_log(LOG_WARNING, "Usecnt < 0???\n");
-       pthread_mutex_unlock(&usecnt_lock);
+       ast_pthread_mutex_unlock(&usecnt_lock);
        ast_update_use_count();
        if (option_verbose > 2) 
                ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
@@ -463,7 +487,10 @@ static int vofr_answer(struct ast_channel *ast)
        while(cnt > 0) {
                cnt = ast_waitfor(ast, cnt);
                if (cnt > 0) {
-                       res = read(ast->fd, buf, sizeof(buf));
+                       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);
@@ -528,15 +555,32 @@ static struct ast_frame  *vofr_read(struct ast_channel *ast)
        /* Read into the right place in the buffer, in case we send this
           as a voice frame. */
        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) {
+               /*  XXX HUGE BUG IN SANGOMA'S STACK: IT IGNORES O_NONBLOCK XXX */
+               if (errno == EAGAIN) {
+                       fd_set fds;
+                       FD_ZERO(&fds);
+                       FD_SET(p->s, &fds);
+                       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 *)) {
+       if (res < sizeof(struct vofr_hdr)) {
                ast_log(LOG_WARNING, "Nonsense frame on %s\n", ast->name);
                return NULL;
        }
-
        /* Some nice norms */
        fr->datalen = 0;
        fr->timelen = 0;
@@ -551,15 +595,17 @@ static struct ast_frame  *vofr_read(struct ast_channel *ast)
                switch(vh->data[0]) {
                case VOFR_SIGNAL_ON_HOOK:
                        /* Hang up this line */
-                       if (ast->state == AST_STATE_UP)
+                       if ((ast->state == AST_STATE_UP) || (p->ringgothangup)) {
                                return NULL;
-                       else {
+                       } else {
                                fr->frametype = AST_FRAME_NULL;
                                fr->subclass = 0;
-                               break;
+                               p->ringgothangup=1;
                        }
+                       break;
                case VOFR_SIGNAL_RING:
                        ast->rings++;
+                       p->ringgothangup = 0;
                        break;
                case VOFR_SIGNAL_UNKNOWN:
                        switch(vh->data[1]) {
@@ -656,7 +702,7 @@ static struct ast_frame  *vofr_read(struct ast_channel *ast)
                        ast->state = AST_STATE_UP;
                        return fr;
                } else if (ast->state !=  AST_STATE_UP) {
-                       ast_log(LOG_WARNING, "Voice in weird state %d\n", ast->state);
+                       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;
@@ -739,19 +785,36 @@ static int vofr_write(struct ast_channel *ast, struct ast_frame *frame)
        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();
+       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->fd = i->s;
+               tmp->fds[0] = i->s;
                /* Adtran VoFR supports only G723.1 format data.  G711 (ulaw) would be nice too */
-               tmp->format = AST_FORMAT_G723_1;
+               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;
@@ -759,16 +822,20 @@ static struct ast_channel *vofr_new(struct vofr_pvt *i, int state)
                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;
-               pthread_mutex_lock(&usecnt_lock);
+               ast_pthread_mutex_lock(&usecnt_lock);
                usecnt++;
-               pthread_mutex_unlock(&usecnt_lock);
+               ast_pthread_mutex_unlock(&usecnt_lock);
                ast_update_use_count();
-               strncpy(tmp->context, i->context, sizeof(tmp->context));
+               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
@@ -786,9 +853,10 @@ static int vofr_mini_packet(struct vofr_pvt *i, struct vofr_hdr *pkt, int len)
                switch(pkt->data[0]) {
                case VOFR_SIGNAL_RING:
                        /* If we get a RING, we definitely want to start a new thread */
-                       if (!i->owner)
+                       if (!i->owner) {
+                               i->ringgothangup = 0;
                                vofr_new(i, AST_STATE_RING);
-                       else
+                       } else
                                ast_log(LOG_WARNING, "Got a ring, but there's an owner?\n");
                        break;
                case VOFR_SIGNAL_OFF_HOOK:
@@ -839,21 +907,23 @@ static void *do_monitor(void *data)
        /* This thread monitors all the frame relay interfaces which are not yet in use
           (and thus do not have a separate thread) indefinitely */
        /* From here on out, we die whenever asked */
+#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(;;) {
                /* Don't let anybody kill us right away.  Nobody should lock the interface list
                   and wait for the monitor list, but the other way around is okay. */
-               if (pthread_mutex_lock(&monlock)) {
+               if (ast_pthread_mutex_lock(&monlock)) {
                        ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
                        return NULL;
                }
                /* Lock the interface list */
-               if (pthread_mutex_lock(&iflock)) {
+               if (ast_pthread_mutex_lock(&iflock)) {
                        ast_log(LOG_ERROR, "Unable to grab interface lock\n");
-                       pthread_mutex_unlock(&monlock);
+                       ast_pthread_mutex_unlock(&monlock);
                        return NULL;
                }
                /* Build the stuff we're going to select on, that is the socket of every
@@ -863,7 +933,11 @@ static void *do_monitor(void *data)
                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) {
                                /* This needs to be watched, as it lacks an owner */
                                FD_SET(i->s, &rfds);
@@ -873,20 +947,23 @@ static void *do_monitor(void *data)
                        i = i->next;
                }
                /* Okay, now that we know what to do, release the interface lock */
-               pthread_mutex_unlock(&iflock);
+               ast_pthread_mutex_unlock(&iflock);
                
                /* And from now on, we're okay to be killed, so release the monitor lock as well */
-               pthread_mutex_unlock(&monlock);
+               ast_pthread_mutex_unlock(&monlock);
+               pthread_testcancel();
                /* Wait indefinitely for something to happen */
                res = select(n + 1, &rfds, NULL, NULL, NULL);
+               pthread_testcancel();
                /* Okay, select has finished.  Let's see what happened.  */
                if (res < 0) {
-                       ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
+                       if ((errno != EAGAIN) && (errno != EINTR))
+                               ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
                        continue;
                }
                /* Alright, lock the interface list again, and let's look and see what has
                   happened */
-               if (pthread_mutex_lock(&iflock)) {
+               if (ast_pthread_mutex_lock(&iflock)) {
                        ast_log(LOG_WARNING, "Unable to lock the interface list\n");
                        continue;
                }
@@ -894,7 +971,11 @@ static void *do_monitor(void *data)
                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));
@@ -906,7 +987,7 @@ static void *do_monitor(void *data)
                        }
                        i=i->next;
                }
-               pthread_mutex_unlock(&iflock);
+               ast_pthread_mutex_unlock(&iflock);
        }
        /* Never reached */
        return NULL;
@@ -918,28 +999,27 @@ static int restart_monitor(void)
        /* If we're supposed to be stopped -- stay stopped */
        if (monitor_thread == -2)
                return 0;
-       if (pthread_mutex_lock(&monlock)) {
+       if (ast_pthread_mutex_lock(&monlock)) {
                ast_log(LOG_WARNING, "Unable to lock monitor\n");
                return -1;
        }
        if (monitor_thread == pthread_self()) {
-               pthread_mutex_unlock(&monlock);
+               ast_pthread_mutex_unlock(&monlock);
                ast_log(LOG_WARNING, "Cannot kill myself\n");
                return -1;
        }
-       if (monitor_thread != -1) {
-               pthread_cancel(monitor_thread);
-#if 0
-               pthread_join(monitor_thread, NULL);
-#endif
-       }
-       /* Start a new monitor */
-       if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
-               pthread_mutex_unlock(&monlock);
-               ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
-               return -1;
+       if (monitor_thread) {
+               /* Wake up the thread */
+               pthread_kill(monitor_thread, SIGURG);
+       } else {
+               /* Start a new monitor */
+               if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
+                       ast_pthread_mutex_unlock(&monlock);
+                       ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
+                       return -1;
+               }
        }
-       pthread_mutex_unlock(&monlock);
+       ast_pthread_mutex_unlock(&monlock);
        return 0;
 }
 
@@ -948,27 +1028,49 @@ static struct vofr_pvt *mkif(char *type, char *iface)
        /* Make a vofr_pvt structure for this interface */
        struct vofr_pvt *tmp;
        int sndbuf = 4096;
-       
+
        tmp = malloc(sizeof(struct vofr_pvt));
        if (tmp) {
 
                /* Allocate a packet socket */
+#ifdef OLD_SANGOMA_API
                tmp->s = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL));
+#else
+               /* Why the HELL does Sangoma change their API every damn time
+                  they make a new driver release?!?!?!  Leave it the hell
+                  alone this time.  */
+               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
                /* Prepare sockaddr for binding */
                memset(&tmp->sa, 0, sizeof(tmp->sa));
-               strncpy(tmp->sa.spkt_device, iface, sizeof(tmp->sa.spkt_device));
+               strncpy(tmp->sa.spkt_device, iface, sizeof(tmp->sa.spkt_device)-1);
                tmp->sa.spkt_protocol = htons(0x16);
                tmp->sa.spkt_family = AF_PACKET;
-               
-               /* Bind socket to specific interface */
                if (bind(tmp->s, (struct sockaddr *)&tmp->sa, sizeof(struct sockaddr))) {
+#else
+               /* Prepare sockaddr for binding */
+               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         
+               /* Bind socket to specific interface */
+#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;
@@ -986,7 +1088,9 @@ static struct vofr_pvt *mkif(char *type, char *iface)
                tmp->dlcil = 0;
                tmp->dlcih = 0;
                tmp->cid = 1;
-               strncpy(tmp->context, context, sizeof(tmp->context));
+               tmp->ringgothangup = 0;
+               strncpy(tmp->language, language, sizeof(tmp->language)-1);
+               strncpy(tmp->context, context, sizeof(tmp->context)-1);
                /* User terminations are game for outgoing connections */
                if (!strcasecmp(type, "user")) 
                        tmp->outgoing = 1;
@@ -1015,19 +1119,19 @@ static struct ast_channel *vofr_request(char *type, int format, void *data)
                return NULL;
        }
        /* Search for an unowned channel */
-       if (pthread_mutex_lock(&iflock)) {
+       if (ast_pthread_mutex_lock(&iflock)) {
                ast_log(LOG_ERROR, "Unable to lock interface list???\n");
                return NULL;
        }
        p = iflist;
        while(p) {
-               if (!p->owner) {
+               if (!p->owner && p->outgoing) {
                        tmp = vofr_new(p, AST_STATE_DOWN);
                        break;
                }
                p = p->next;
        }
-       pthread_mutex_unlock(&iflock);
+       ast_pthread_mutex_unlock(&iflock);
        restart_monitor();
        return tmp;
 }
@@ -1044,7 +1148,7 @@ int load_module()
                ast_log(LOG_ERROR, "Unable to load config %s\n", config);
                return -1;
        }
-       if (pthread_mutex_lock(&iflock)) {
+       if (ast_pthread_mutex_lock(&iflock)) {
                /* It's a little silly to lock it, but we mind as well just to be sure */
                ast_log(LOG_ERROR, "Unable to lock interface list???\n");
                return -1;
@@ -1061,16 +1165,18 @@ int load_module()
                                } else {
                                        ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
                                        ast_destroy(cfg);
-                                       pthread_mutex_unlock(&iflock);
+                                       ast_pthread_mutex_unlock(&iflock);
                                        unload_module();
                                        return -1;
                                }
                } else if (!strcasecmp(v->name, "context")) {
-                       strncpy(context, v->value, sizeof(context));
+                       strncpy(context, v->value, sizeof(context)-1);
+               } else if (!strcasecmp(v->name, "language")) {
+                       strncpy(language, v->value, sizeof(language)-1);
                }
                v = v->next;
        }
-       pthread_mutex_unlock(&iflock);
+       ast_pthread_mutex_unlock(&iflock);
        /* Make sure we can register our AdtranVoFR channel type */
        if (ast_channel_register(type, tdesc, AST_FORMAT_G723_1, vofr_request)) {
                ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
@@ -1084,14 +1190,12 @@ int load_module()
        return 0;
 }
 
-
-
 int unload_module()
 {
        struct vofr_pvt *p, *pl;
        /* First, take us out of the channel loop */
        ast_channel_unregister(type);
-       if (!pthread_mutex_lock(&iflock)) {
+       if (!ast_pthread_mutex_lock(&iflock)) {
                /* Hangup all interfaces if they have an owner */
                p = iflist;
                while(p) {
@@ -1100,24 +1204,25 @@ int unload_module()
                        p = p->next;
                }
                iflist = NULL;
-               pthread_mutex_unlock(&iflock);
+               ast_pthread_mutex_unlock(&iflock);
        } else {
                ast_log(LOG_WARNING, "Unable to lock the monitor\n");
                return -1;
        }
-       if (!pthread_mutex_lock(&monlock)) {
-               if (monitor_thread > -1) {
+       if (!ast_pthread_mutex_lock(&monlock)) {
+               if (monitor_thread) {
                        pthread_cancel(monitor_thread);
+                       pthread_kill(monitor_thread, SIGURG);
                        pthread_join(monitor_thread, NULL);
                }
                monitor_thread = -2;
-               pthread_mutex_unlock(&monlock);
+               ast_pthread_mutex_unlock(&monlock);
        } else {
                ast_log(LOG_WARNING, "Unable to lock the monitor\n");
                return -1;
        }
 
-       if (!pthread_mutex_lock(&iflock)) {
+       if (!ast_pthread_mutex_lock(&iflock)) {
                /* Destroy all the interfaces and free their memory */
                p = iflist;
                while(p) {
@@ -1130,7 +1235,7 @@ int unload_module()
                        free(pl);
                }
                iflist = NULL;
-               pthread_mutex_unlock(&iflock);
+               ast_pthread_mutex_unlock(&iflock);
        } else {
                ast_log(LOG_WARNING, "Unable to lock the monitor\n");
                return -1;
@@ -1142,12 +1247,17 @@ int unload_module()
 int usecount()
 {
        int res;
-       pthread_mutex_lock(&usecnt_lock);
+       ast_pthread_mutex_lock(&usecnt_lock);
        res = usecnt;
-       pthread_mutex_unlock(&usecnt_lock);
+       ast_pthread_mutex_unlock(&usecnt_lock);
        return res;
 }
 
+char *key()
+{
+       return ASTERISK_GPL_KEY;
+}
+
 char *description()
 {
        return desc;