ensure that the private structure for pseudo channels is created without 'leaking...
[asterisk-bristuff.git] / apps / app_dahdibarge.c
blob853f2853e8c403fdfeb212018cc8acd0d65bd449
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * Special thanks to comphealth.com for sponsoring this
9 * GPL application.
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
22 /*! \file
24 * \brief Zap Barge support
26 * \author Mark Spencer <markster@digium.com>
28 * \note Special thanks to comphealth.com for sponsoring this
29 * GPL application.
31 * \ingroup applications
34 /*** MODULEINFO
35 <depend>dahdi</depend>
36 ***/
38 #include "asterisk.h"
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <errno.h>
47 #include <sys/ioctl.h>
49 #include "asterisk/lock.h"
50 #include "asterisk/file.h"
51 #include "asterisk/logger.h"
52 #include "asterisk/channel.h"
53 #include "asterisk/pbx.h"
54 #include "asterisk/module.h"
55 #include "asterisk/config.h"
56 #include "asterisk/app.h"
57 #include "asterisk/options.h"
58 #include "asterisk/cli.h"
59 #include "asterisk/say.h"
60 #include "asterisk/utils.h"
62 #include "asterisk/dahdi_compat.h"
64 static char *dahdi_app = "DAHDIBarge";
65 static char *zap_app = "ZapBarge";
67 static char *dahdi_synopsis = "Barge in (monitor) DAHDI channel";
68 static char *zap_synopsis = "Barge in (monitor) Zap channel";
70 static char *dahdi_descrip =
71 " DAHDIBarge([channel]): Barges in on a specified DAHDI\n"
72 "channel or prompts if one is not specified. Returns\n"
73 "-1 when caller user hangs up and is independent of the\n"
74 "state of the channel being monitored.";
76 static char *zap_descrip =
77 " ZapBarge([channel]): Barges in on a specified Zaptel\n"
78 "channel or prompts if one is not specified. Returns\n"
79 "-1 when caller user hangs up and is independent of the\n"
80 "state of the channel being monitored.";
82 #define CONF_SIZE 160
84 static int careful_write(int fd, unsigned char *data, int len)
86 int res;
87 while(len) {
88 res = write(fd, data, len);
89 if (res < 1) {
90 if (errno != EAGAIN) {
91 ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
92 return -1;
93 } else
94 return 0;
96 len -= res;
97 data += res;
99 return 0;
102 static int conf_run(struct ast_channel *chan, int confno, int confflags)
104 int fd;
105 struct dahdi_confinfo ztc;
106 struct ast_frame *f;
107 struct ast_channel *c;
108 struct ast_frame fr;
109 int outfd;
110 int ms;
111 int nfds;
112 int res;
113 int flags;
114 int retryzap;
115 int origfd;
116 int ret = -1;
117 struct dahdi_bufferinfo bi;
118 char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
119 char *buf = __buf + AST_FRIENDLY_OFFSET;
121 /* Set it into U-law mode (write) */
122 if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
123 ast_log(LOG_WARNING, "Unable to set '%s' to write ulaw mode\n", chan->name);
124 goto outrun;
127 /* Set it into U-law mode (read) */
128 if (ast_set_read_format(chan, AST_FORMAT_ULAW) < 0) {
129 ast_log(LOG_WARNING, "Unable to set '%s' to read ulaw mode\n", chan->name);
130 goto outrun;
132 ast_indicate(chan, -1);
133 retryzap = strcasecmp(chan->tech->type, dahdi_chan_name);
134 zapretry:
135 origfd = chan->fds[0];
136 if (retryzap) {
137 #ifdef HAVE_ZAPTEL
138 fd = open("/dev/zap/pseudo", O_RDWR);
139 #else
140 fd = open("/dev/dahdi/pseudo", O_RDWR);
141 #endif
142 if (fd < 0) {
143 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
144 goto outrun;
146 /* Make non-blocking */
147 flags = fcntl(fd, F_GETFL);
148 if (flags < 0) {
149 ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
150 close(fd);
151 goto outrun;
153 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
154 ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
155 close(fd);
156 goto outrun;
158 /* Setup buffering information */
159 memset(&bi, 0, sizeof(bi));
160 bi.bufsize = CONF_SIZE;
161 bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
162 bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
163 bi.numbufs = 4;
164 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
165 ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
166 close(fd);
167 goto outrun;
169 nfds = 1;
170 } else {
171 /* XXX Make sure we're not running on a pseudo channel XXX */
172 fd = chan->fds[0];
173 nfds = 0;
175 memset(&ztc, 0, sizeof(ztc));
176 /* Check to see if we're in a conference... */
177 ztc.chan = 0;
178 if (ioctl(fd, DAHDI_GETCONF, &ztc)) {
179 ast_log(LOG_WARNING, "Error getting conference\n");
180 close(fd);
181 goto outrun;
183 if (ztc.confmode) {
184 /* Whoa, already in a conference... Retry... */
185 if (!retryzap) {
186 ast_log(LOG_DEBUG, "Channel is in a conference already, retrying with pseudo\n");
187 retryzap = 1;
188 goto zapretry;
191 memset(&ztc, 0, sizeof(ztc));
192 /* Add us to the conference */
193 ztc.chan = 0;
194 ztc.confno = confno;
195 ztc.confmode = DAHDI_CONF_MONITORBOTH;
197 if (ioctl(fd, DAHDI_SETCONF, &ztc)) {
198 ast_log(LOG_WARNING, "Error setting conference\n");
199 close(fd);
200 goto outrun;
202 ast_log(LOG_DEBUG, "Placed channel %s in channel %d monitor\n", chan->name, confno);
204 for(;;) {
205 outfd = -1;
206 ms = -1;
207 c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
208 if (c) {
209 if (c->fds[0] != origfd) {
210 if (retryzap) {
211 /* Kill old pseudo */
212 close(fd);
214 ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
215 retryzap = 0;
216 goto zapretry;
218 f = ast_read(c);
219 if (!f)
220 break;
221 if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
222 ret = 0;
223 ast_frfree(f);
224 break;
225 } else if (fd != chan->fds[0]) {
226 if (f->frametype == AST_FRAME_VOICE) {
227 if (f->subclass == AST_FORMAT_ULAW) {
228 /* Carefully write */
229 careful_write(fd, f->data, f->datalen);
230 } else
231 ast_log(LOG_WARNING, "Huh? Got a non-ulaw (%d) frame in the conference\n", f->subclass);
234 ast_frfree(f);
235 } else if (outfd > -1) {
236 res = read(outfd, buf, CONF_SIZE);
237 if (res > 0) {
238 memset(&fr, 0, sizeof(fr));
239 fr.frametype = AST_FRAME_VOICE;
240 fr.subclass = AST_FORMAT_ULAW;
241 fr.datalen = res;
242 fr.samples = res;
243 fr.data = buf;
244 fr.offset = AST_FRIENDLY_OFFSET;
245 if (ast_write(chan, &fr) < 0) {
246 ast_log(LOG_WARNING, "Unable to write frame to channel: %s\n", strerror(errno));
247 /* break; */
249 } else
250 ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
253 if (fd != chan->fds[0])
254 close(fd);
255 else {
256 /* Take out of conference */
257 /* Add us to the conference */
258 ztc.chan = 0;
259 ztc.confno = 0;
260 ztc.confmode = 0;
261 if (ioctl(fd, DAHDI_SETCONF, &ztc)) {
262 ast_log(LOG_WARNING, "Error setting conference\n");
266 outrun:
268 return ret;
271 static int exec(struct ast_channel *chan, void *data, int dahdimode)
273 int res=-1;
274 struct ast_module_user *u;
275 int retrycnt = 0;
276 int confflags = 0;
277 int confno = 0;
278 char confstr[80] = "";
280 u = ast_module_user_add(chan);
282 if (!ast_strlen_zero(data)) {
283 if (dahdimode) {
284 if ((sscanf(data, "DAHDI/%d", &confno) != 1) &&
285 (sscanf(data, "%d", &confno) != 1)) {
286 ast_log(LOG_WARNING, "Argument (if specified) must be a channel number, not '%s'\n", (char *) data);
287 ast_module_user_remove(u);
288 return 0;
290 } else {
291 if ((sscanf(data, "Zap/%d", &confno) != 1) &&
292 (sscanf(data, "%d", &confno) != 1)) {
293 ast_log(LOG_WARNING, "Argument (if specified) must be a channel number, not '%s'\n", (char *) data);
294 ast_module_user_remove(u);
295 return 0;
300 if (chan->_state != AST_STATE_UP)
301 ast_answer(chan);
303 while(!confno && (++retrycnt < 4)) {
304 /* Prompt user for conference number */
305 confstr[0] = '\0';
306 res = ast_app_getdata(chan, "conf-getchannel",confstr, sizeof(confstr) - 1, 0);
307 if (res <0) goto out;
308 if (sscanf(confstr, "%d", &confno) != 1)
309 confno = 0;
311 if (confno) {
312 /* XXX Should prompt user for pin if pin is required XXX */
313 /* Run the conference */
314 res = conf_run(chan, confno, confflags);
316 out:
317 /* Do the conference */
318 ast_module_user_remove(u);
319 return res;
322 static int exec_zap(struct ast_channel *chan, void *data)
324 ast_log(LOG_WARNING, "Use of the command %s is deprecated, please use %s instead.\n", zap_app, dahdi_app);
326 return exec(chan, data, 0);
329 static int exec_dahdi(struct ast_channel *chan, void *data)
331 return exec(chan, data, 1);
334 static int unload_module(void)
336 int res = 0;
338 if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) {
339 res |= ast_unregister_application(dahdi_app);
342 res |= ast_unregister_application(zap_app);
344 ast_module_user_hangup_all();
346 return res;
349 static int load_module(void)
351 int res = 0;
353 if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) {
354 res |= ast_register_application(dahdi_app, exec_dahdi, dahdi_synopsis, dahdi_descrip);
357 res |= ast_register_application(zap_app, exec_zap, zap_synopsis, zap_descrip);
359 return res;
362 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Barge in on channel application");