allow users of RTP to know when the peer endpoint is (apparently) behind a NAT
[asterisk/asterisk.git] / autoservice.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Automatic channel service routines
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 <stdlib.h>
16 #include <string.h>
17 #include <sys/time.h>
18 #include <signal.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <math.h>                       /* For PI */
22
23 #include "asterisk.h"
24
25 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
26
27 #include "asterisk/pbx.h"
28 #include "asterisk/frame.h"
29 #include "asterisk/sched.h"
30 #include "asterisk/options.h"
31 #include "asterisk/channel.h"
32 #include "asterisk/logger.h"
33 #include "asterisk/file.h"
34 #include "asterisk/translate.h"
35 #include "asterisk/manager.h"
36 #include "asterisk/chanvars.h"
37 #include "asterisk/linkedlists.h"
38 #include "asterisk/indications.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/utils.h"
41
42 #define MAX_AUTOMONS 256
43
44 AST_MUTEX_DEFINE_STATIC(autolock);
45
46 struct asent {
47         struct ast_channel *chan;
48         struct asent *next;
49 };
50
51 static struct asent *aslist = NULL;
52 static pthread_t asthread = AST_PTHREADT_NULL;
53
54 static void *autoservice_run(void *ign)
55 {
56         struct ast_channel *mons[MAX_AUTOMONS];
57         int x;
58         int ms;
59         struct ast_channel *chan;
60         struct asent *as;
61         struct ast_frame *f;
62         for(;;) {
63                 x = 0;
64                 ast_mutex_lock(&autolock);
65                 as = aslist;
66                 while(as) {
67                         if (!as->chan->_softhangup) {
68                                 if (x < MAX_AUTOMONS)
69                                         mons[x++] = as->chan;
70                                 else
71                                         ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
72                         }
73                         as = as->next;
74                 }
75                 ast_mutex_unlock(&autolock);
76
77 /*              if (!aslist)
78                         break; */
79                 ms = 500;
80                 chan = ast_waitfor_n(mons, x, &ms);
81                 if (chan) {
82                         /* Read and ignore anything that occurs */
83                         f = ast_read(chan);
84                         if (f)
85                                 ast_frfree(f);
86                 }
87         }
88         asthread = AST_PTHREADT_NULL;
89         return NULL;
90 }
91
92 int ast_autoservice_start(struct ast_channel *chan)
93 {
94         int res = -1;
95         struct asent *as;
96         int needstart;
97         ast_mutex_lock(&autolock);
98         needstart = (asthread == AST_PTHREADT_NULL) ? 1 : 0 /* aslist ? 0 : 1 */;
99         as = aslist;
100         while(as) {
101                 if (as->chan == chan)
102                         break;
103                 as = as->next;
104         }
105         if (!as) {
106                 as = malloc(sizeof(struct asent));
107                 if (as) {
108                         memset(as, 0, sizeof(struct asent));
109                         as->chan = chan;
110                         as->next = aslist;
111                         aslist = as;
112                         res = 0;
113                         if (needstart) {
114                                 if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
115                                         ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
116                                         free(aslist);
117                                         aslist = NULL;
118                                         res = -1;
119                                 } else
120                                         pthread_kill(asthread, SIGURG);
121                         }
122                 }
123         }
124         ast_mutex_unlock(&autolock);
125         return res;
126 }
127
128 int ast_autoservice_stop(struct ast_channel *chan)
129 {
130         int res = -1;
131         struct asent *as, *prev;
132         ast_mutex_lock(&autolock);
133         as = aslist;
134         prev = NULL;
135         while(as) {
136                 if (as->chan == chan)
137                         break;
138                 prev = as;
139                 as = as->next;
140         }
141         if (as) {
142                 if (prev)
143                         prev->next = as->next;
144                 else
145                         aslist = as->next;
146                 free(as);
147                 if (!chan->_softhangup)
148                         res = 0;
149         }
150         if (asthread != AST_PTHREADT_NULL) 
151                 pthread_kill(asthread, SIGURG);
152         ast_mutex_unlock(&autolock);
153         /* Wait for it to un-block */
154         while(ast_test_flag(chan, AST_FLAG_BLOCKING))
155                 usleep(1000);
156         return res;
157 }