diff -upr --exclude CVS --exclude say.c --exclude say --exclude config.guess --exclude config.sub /usr/ports/distfiles/ast-curr/asterisk/channel.c work/asterisk-current/channel.c
--- /usr/ports/distfiles/ast-curr/asterisk/channel.c	Mon Nov 21 17:43:46 2005
+++ work/asterisk-current/channel.c	Tue Nov 22 12:51:12 2005
@@ -540,7 +540,7 @@ struct ast_channel *ast_channel_alloc(in
 		return NULL;
 	}
 	
-	for (x=0; x<AST_MAX_FDS - 1; x++)
+	for (x=0; x<AST_MAX_FDS; x++)
 		tmp->fds[x] = -1;
 
 #ifdef ZAPTEL_OPTIMIZATIONS
@@ -572,9 +572,9 @@ struct ast_channel *ast_channel_alloc(in
 		tmp->alertpipe[0] = tmp->alertpipe[1] = -1;
 
 	/* Always watch the alertpipe */
-	tmp->fds[AST_MAX_FDS-1] = tmp->alertpipe[0];
+	tmp->fds[AST_ALERT_FD] = tmp->alertpipe[0];
 	/* And timing pipe */
-	tmp->fds[AST_MAX_FDS-2] = tmp->timingfd;
+	tmp->fds[AST_TIMING_FD] = tmp->timingfd;
 	strcpy(tmp->name, "**Unkown**");
 	/* Initial state */
 	tmp->_state = AST_STATE_DOWN;
@@ -1371,6 +1371,7 @@ void ast_deactivate_generator(struct ast
 			chan->generator->release(chan, chan->generatordata);
 		chan->generatordata = NULL;
 		chan->generator = NULL;
+		chan->fds[AST_GENERATOR_FD] = -1;
 		ast_clear_flag(chan, AST_FLAG_WRITE_INT);
 		ast_settimeout(chan, 0, NULL, NULL);
 	}
@@ -1417,6 +1418,13 @@ int ast_activate_generator(struct ast_ch
 	if (!res) {
 		ast_settimeout(chan, 160, generator_force, chan);
 		chan->generator = gen;
+		/* if the generator supplies an fd, register it */
+		if (gen->magic == AST_GEN_MAGIC) {
+			int gfd = gen->get_fd(chan, chan->generatordata);
+
+			if (gfd > -1)
+				chan->fds[AST_GENERATOR_FD] = gfd;
+		}
 	}
 
 	ast_mutex_unlock(&chan->lock);
@@ -1424,100 +1432,68 @@ int ast_activate_generator(struct ast_ch
 	return res;
 }
 
+/* if fd is a valid descriptor, set *pfd with the descriptor
+ * Return 1 (not -1!) if added, 0 otherwise (so we can add the
+ * return value to the index into the array)
+ */
+static int add_fd(struct pollfd *pfd, int fd)
+{
+	if (fd < 0)
+		return 0;
+	pfd->fd = fd;
+	pfd->events = POLLIN | POLLPRI;
+	return 1;
+}
+
 /*--- ast_waitfor_n_fd: Wait for x amount of time on a file descriptor to have input.  */
 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
 {
-	struct timeval start = { 0 , 0 };
-	int res;
-	int x, y;
 	int winner = -1;
-	int spoint;
-	struct pollfd *pfds;
-	
-	pfds = alloca(sizeof(struct pollfd) * n);
-	if (!pfds) {
-		ast_log(LOG_ERROR, "Out of memory\n");
-		return -1;
-	}
-	if (*ms > 0)
-		start = ast_tvnow();
-	y = 0;
-	for (x=0; x < n; x++) {
-		if (fds[x] > -1) {
-			pfds[y].fd = fds[x];
-			pfds[y].events = POLLIN | POLLPRI;
-			y++;
-		}
-	}
-	res = poll(pfds, y, *ms);
-	if (res < 0) {
-		/* Simulate a timeout if we were interrupted */
-		if (errno != EINTR)
-			*ms = -1;
-		else
-			*ms = 0;
-		return -1;
-	}
-	spoint = 0;
-	for (x=0; x < n; x++) {
-		if (fds[x] > -1) {
-			if ((res = ast_fdisset(pfds, fds[x], y, &spoint))) {
-				winner = fds[x];
-				if (exception) {
-					if (res & POLLPRI)
-						*exception = -1;
-					else
-						*exception = 0;
-				}
-			}
-		}
-	}
-	if (*ms > 0) {
-		*ms -= ast_tvdiff_ms(ast_tvnow(), start);
-		if (*ms < 0)
-			*ms = 0;
-	}
+
+	ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
 	return winner;
 }
 
-/*--- ast_waitfor_nanfds: Wait for x amount of time on a file descriptor to have input.  */
-struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds, 
-	int *exception, int *outfd, int *ms)
+/*--- ast_waitfor_nanfds: Wait for x amount of time on a file descriptor to have input. */
+struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n,
+	int *fds, int nfds, int *exception, int *outfd, int *ms)
 {
 	struct timeval start = { 0 , 0 };
 	struct pollfd *pfds;
-	int res;
-	long rms;
-	int x, y, max;
-	int spoint;
-	time_t now = 0;
-	long whentohangup = 0, havewhen = 0, diff;
 	struct ast_channel *winner = NULL;
+	int x, max, res, rms, sz;
+	time_t t0 = 0; /* timeout, 0 means unset */
+	int *ci, *fdno; /* map pfds entries to channel index and fd index */
 
-	pfds = alloca(sizeof(struct pollfd) * (n * AST_MAX_FDS + nfds));
-	if (!pfds) {
-		ast_log(LOG_ERROR, "Out of memory\n");
+	if (outfd)
 		*outfd = -1;
+	if (exception)
+		*exception = 0;
+
+	/*
+     * allocate array of poll file descriptors, and auxiliary arrays to
+     * map the poll descriptors to channels and file descriptors, to speed up
+     * the decoding of the poll() results.
+	 */
+	sz = (n * AST_MAX_FDS + nfds);
+	pfds = alloca(sizeof(struct pollfd) * sz);
+	ci = alloca(sizeof(int *) * sz);	/* pfds to channel map */
+	fdno = alloca(sizeof(int *) * sz);	/* pfds to fd index */
+
+	if (!pfds || !ci || !fdno) {
+		ast_log(LOG_ERROR, "Out of memory\n");
 		return NULL;
 	}
+	for (x=0; x < sz; x++)
+		ci[x] = -1;		/* no channel linked to this pfd */
+
+	/*
+	 * Perform any pending masquerades, and take the min timeout in t0
+	 */
+	for (x=0; x<n; x++) {
+		time_t t;
 
-	if (outfd)
-		*outfd = -99999;
-	if (exception)
-		*exception = 0;
-	
-	/* Perform any pending masquerades */
-	for (x=0; x < n; x++) {
 		ast_mutex_lock(&c[x]->lock);
-		if (c[x]->whentohangup) {
-			if (!havewhen)
-				time(&now);
-			diff = c[x]->whentohangup - now;
-			if (!havewhen || (diff < whentohangup)) {
-				havewhen++;
-				whentohangup = diff;
-			}
-		}
 		if (c[x]->masq) {
 			if (ast_do_masquerade(c[x])) {
 				ast_log(LOG_WARNING, "Masquerade failed\n");
@@ -1526,40 +1502,42 @@ struct ast_channel *ast_waitfor_nandfds(
 				return NULL;
 			}
 		}
+		t = c[x]->whentohangup;
+		if (t > 0 && (t0 == 0 || t < t0))
+			t0 = t;
 		ast_mutex_unlock(&c[x]->lock);
 	}
-
-	rms = *ms;
-	
-	if (havewhen) {
-		if ((*ms < 0) || (whentohangup * 1000 < *ms)) {
-			rms =  whentohangup * 1000;
-		}
+	if (t0 == 0)	/* no timeout, use *ms */
+		rms = *ms;
+	else {
+		rms = (t0 - time(NULL)) * 1000;	/* timeout in milliseconds */
+		if (*ms >= 0 && *ms < rms)		/* original *ms still smaller */
+			rms =  *ms;
 	}
+	/*
+	 * Build the pollfd array, putting the channels' fds first,
+	 * followed by individual fds. Order is important because
+	 * individual fd's must have priority over channel fds.
+	 */
 	max = 0;
-	for (x=0; x < n; x++) {
-		for (y=0; y< AST_MAX_FDS; y++) {
-			if (c[x]->fds[y] > -1) {
-				pfds[max].fd = c[x]->fds[y];
-				pfds[max].events = POLLIN | POLLPRI;
-				pfds[max].revents = 0;
-				max++;
+	for (x=0; x<n; x++) {
+		int y;
+
+		for (y=0; y<AST_MAX_FDS; y++)
+			if (add_fd(&pfds[max], c[x]->fds[y])) {
+				fdno[max] = y;  /* fd y is linked to this pfds */
+				ci[max++] = x;  /* channel x is linked to this pfds */
 			}
-		}
 		CHECK_BLOCKING(c[x]);
 	}
-	for (x=0; x < nfds; x++) {
-		if (fds[x] > -1) {
-			pfds[max].fd = fds[x];
-			pfds[max].events = POLLIN | POLLPRI;
-			pfds[max].revents = 0;
-			max++;
-		}
-	}
+	/* add the individual fds */
+	for (x=0; x<nfds; x++)
+		max += add_fd(&pfds[max], fds[x]);
+
 	if (*ms > 0) 
 		start = ast_tvnow();
 	
-	if (sizeof(int) == 4) {
+	if (sizeof(int) == 4) {	/* XXX fix timeout > 600000 on linux x86-32 */
 		do {
 			int kbrms = rms;
 			if (kbrms > 600000)
@@ -1571,65 +1549,49 @@ struct ast_channel *ast_waitfor_nandfds(
 	} else {
 		res = poll(pfds, max, rms);
 	}
-	
-	if (res < 0) {
-		for (x=0; x < n; x++) 
-			ast_clear_flag(c[x], AST_FLAG_BLOCKING);
-		/* Simulate a timeout if we were interrupted */
-		if (errno != EINTR)
-			*ms = -1;
-		else {
-			/* Just an interrupt */
-#if 0
-			*ms = 0;
-#endif			
-		}
+	for (x=0; x<n; x++)
+		ast_clear_flag(c[x], AST_FLAG_BLOCKING);
+	if (res < 0) { /* Simulate a timeout if we were interrupted */
+		*ms = (errno != EINTR) ? -1 : 0;
 		return NULL;
-        } else {
-        	/* If no fds signalled, then timeout. So set ms = 0
-		   since we may not have an exact timeout.
-		*/
-		if (res == 0)
-			*ms = 0;
 	}
-
-	if (havewhen)
-		time(&now);
-	spoint = 0;
-	for (x=0; x < n; x++) {
-		ast_clear_flag(c[x], AST_FLAG_BLOCKING);
-		if (havewhen && c[x]->whentohangup && (now > c[x]->whentohangup)) {
-			c[x]->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
-			if (!winner)
-				winner = c[x];
-		}
-		for (y=0; y < AST_MAX_FDS; y++) {
-			if (c[x]->fds[y] > -1) {
-				if ((res = ast_fdisset(pfds, c[x]->fds[y], max, &spoint))) {
-					if (res & POLLPRI)
-						ast_set_flag(c[x], AST_FLAG_EXCEPTION);
-					else
-						ast_clear_flag(c[x], AST_FLAG_EXCEPTION);
-					c[x]->fdno = y;
+	if (t0 > 0) {   /* if we have a timeout, check who expired */
+		time(&t0);
+		for (x=0; x<n; x++) {
+			if (c[x]->whentohangup && t0 > c[x]->whentohangup) {
+				c[x]->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
+				if (winner == NULL)
 					winner = c[x];
-				}
 			}
 		}
 	}
-	for (x=0; x < nfds; x++) {
-		if (fds[x] > -1) {
-			if ((res = ast_fdisset(pfds, fds[x], max, &spoint))) {
-				if (outfd)
-					*outfd = fds[x];
-				if (exception) {	
-					if (res & POLLPRI) 
-						*exception = -1;
-					else
-						*exception = 0;
-				}
-				winner = NULL;
-			}
-		}	
+	if (res == 0) { /* no fd ready, reset timeout and done */
+		*ms = 0;	/* XXX use 0 since we may not have an exact timeout. */
+		return winner;
+	}
+	/*
+	 * Then check if any channel or fd has a pending event.
+	 * Remember to check channels first and fds last, as they
+	 * must have priority on setting 'winner'
+	 */
+	for (x = 0; x < max; x++) {
+		res = pfds[x].revents;
+		if (res == 0)
+			continue;
+		if (ci[x] >= 0) {	/* this is a channel */
+			winner = c[ci[x]];	/* override previous winners */
+			if (res & POLLPRI)
+				ast_set_flag(winner, AST_FLAG_EXCEPTION);
+			else
+				ast_clear_flag(winner, AST_FLAG_EXCEPTION);
+			winner->fdno = fdno[x];
+		} else {			/* this is an fd */
+			if (outfd)
+				*outfd = pfds[x].fd;
+			if (exception)
+				*exception = (res & POLLPRI) ? -1 : 0;
+			winner = NULL;
+		}
 	}
 	if (*ms > 0) {
 		*ms -= ast_tvdiff_ms(ast_tvnow(), start);
@@ -1646,16 +1608,11 @@ struct ast_channel *ast_waitfor_n(struct
 
 int ast_waitfor(struct ast_channel *c, int ms)
 {
-	struct ast_channel *chan;
 	int oldms = ms;
 
-	chan = ast_waitfor_n(&c, 1, &ms);
-	if (ms < 0) {
-		if (oldms < 0)
-			return 0;
-		else
-			return -1;
-	}
+	ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
+	if (ms < 0)			/* !EINTR */
+		return (oldms < 0) ? 0 : -1;
 	return ms;
 }
 
@@ -1813,7 +1770,7 @@ struct ast_frame *ast_read(struct ast_ch
 		read(chan->alertpipe[0], &blah, sizeof(blah));
 	}
 #ifdef ZAPTEL_OPTIMIZATIONS
-	if ((chan->timingfd > -1) && (chan->fdno == AST_MAX_FDS - 2) && ast_test_flag(chan, AST_FLAG_EXCEPTION)) {
+	if (chan->timingfd > -1 && chan->fdno == AST_TIMING_FD && ast_test_flag(chan, AST_FLAG_EXCEPTION)) {
 		ast_clear_flag(chan, AST_FLAG_EXCEPTION);
 		blah = -1;
 		/* IF we can't get event, assume it's an expired as-per the old interface */
@@ -1857,6 +1814,15 @@ struct ast_frame *ast_read(struct ast_ch
 			ast_log(LOG_NOTICE, "No/unknown event '%d' on timer for '%s'?\n", blah, chan->name);
 	}
 #endif
+	/* check for pending generator call */
+	if (chan->fds[AST_GENERATOR_FD] > -1 && chan->fdno == AST_GENERATOR_FD) {
+		void *tmp = chan->generatordata;
+
+		chan->generatordata = NULL;     /* reset to let ast_write get through */
+		chan->generator->generate(chan, tmp, 0, 160);
+		chan->generatordata = tmp;
+	}
+
 	/* Check for pending read queue */
 	if (chan->readq) {
 		f = chan->readq;
@@ -1963,7 +1929,7 @@ struct ast_frame *ast_read(struct ast_ch
 			ast_settimeout(chan, 0, NULL, NULL);
 		}
 		tmp = chan->generatordata;
-		chan->generatordata = NULL;
+		chan->generatordata = NULL;		/* reset to let ast_write pass through */
 		generate = chan->generator->generate;
 		res = generate(chan, tmp, f->datalen, f->samples);
 		chan->generatordata = tmp;
@@ -3006,7 +2972,7 @@ int ast_do_masquerade(struct ast_channel
 	clone->cid = tmpcid;
 	
 	/* Restore original timing file descriptor */
-	original->fds[AST_MAX_FDS - 2] = original->timingfd;
+	original->fds[AST_TIMING_FD] = original->timingfd;
 	
 	/* Our native formats are different now */
 	original->nativeformats = clone->nativeformats;
diff -upr --exclude CVS --exclude say.c --exclude say --exclude config.guess --exclude config.sub /usr/ports/distfiles/ast-curr/asterisk/channels/chan_agent.c work/asterisk-current/channels/chan_agent.c
--- /usr/ports/distfiles/ast-curr/asterisk/channels/chan_agent.c	Tue Nov  8 04:02:35 2005
+++ work/asterisk-current/channels/chan_agent.c	Mon Nov 21 19:46:30 2005
@@ -232,10 +232,10 @@ static struct agent_pvt *agents = NULL; 
 	int x; \
 	if (p->chan) { \
 		for (x=0;x<AST_MAX_FDS;x++) {\
-			if (x != AST_MAX_FDS - 2) \
+			if (x != AST_TIMING_FD) \
 				ast->fds[x] = p->chan->fds[x]; \
 		} \
-		ast->fds[AST_MAX_FDS - 3] = p->chan->fds[AST_MAX_FDS - 2]; \
+		ast->fds[AST_AGENT_FD] = p->chan->fds[AST_TIMING_FD]; \
 	} \
 } while(0)
 
@@ -470,10 +470,7 @@ static struct ast_frame *agent_read(stru
 	CHECK_FORMATS(ast, p);
 	if (p->chan) {
 		ast_copy_flags(p->chan, ast, AST_FLAG_EXCEPTION);
-		if (ast->fdno == AST_MAX_FDS - 3)
-			p->chan->fdno = AST_MAX_FDS - 2;
-		else
-			p->chan->fdno = ast->fdno;
+		p->chan->fdno = (ast->fdno == AST_AGENT_FD) ? AST_TIMING_FD : ast->fdno;
 		f = ast_read(p->chan);
 	} else
 		f = &null_frame;
diff -upr --exclude CVS --exclude say.c --exclude say --exclude config.guess --exclude config.sub /usr/ports/distfiles/ast-curr/asterisk/channels/chan_features.c work/asterisk-current/channels/chan_features.c
--- /usr/ports/distfiles/ast-curr/asterisk/channels/chan_features.c	Sun Nov  6 15:09:46 2005
+++ work/asterisk-current/channels/chan_features.c	Mon Nov 21 19:46:30 2005
@@ -169,8 +169,8 @@ static void restore_channel(struct featu
 	p->subs[index].owner->timingfd = p->subs[index].timingfdbackup;
 	p->subs[index].owner->alertpipe[0] = p->subs[index].alertpipebackup[0];
 	p->subs[index].owner->alertpipe[1] = p->subs[index].alertpipebackup[1];
-	p->subs[index].owner->fds[AST_MAX_FDS-1] = p->subs[index].alertpipebackup[0];
-	p->subs[index].owner->fds[AST_MAX_FDS-2] = p->subs[index].timingfdbackup;
+	p->subs[index].owner->fds[AST_ALERT_FD] = p->subs[index].alertpipebackup[0];
+	p->subs[index].owner->fds[AST_TIMING_FD] = p->subs[index].timingfdbackup;
 }
 
 static void update_features(struct feature_pvt *p, int index)
diff -upr --exclude CVS --exclude say.c --exclude say --exclude config.guess --exclude config.sub /usr/ports/distfiles/ast-curr/asterisk/include/asterisk/channel.h work/asterisk-current/include/asterisk/channel.h
--- /usr/ports/distfiles/ast-curr/asterisk/include/asterisk/channel.h	Fri Nov 11 03:48:28 2005
+++ work/asterisk-current/include/asterisk/channel.h	Mon Nov 21 19:46:30 2005
@@ -120,6 +120,24 @@ extern "C" {
 #define MAX_MUSICCLASS		20
 
 #define AST_MAX_FDS		8
+/*
+ * We have AST_MAX_FDS file descriptors in a channel.
+ * Some of them have a fixed use:
+ */
+#define AST_ALERT_FD	(AST_MAX_FDS-1)		/* used for alertpipe */
+#define AST_TIMING_FD	(AST_MAX_FDS-2)		/* used for timingfd */
+#define AST_AGENT_FD	(AST_MAX_FDS-3)		/* XXX used by agent ? */
+
+/*
+ * 'generators' that provide a timed output can use their own file
+ * descriptors instead of relying on AST_TIMING_FD.
+ * In order to be backward compatible, generators of this kind should
+ * set the 'magic' field to AST_GEN_MAGIC, and get_fd() (to be run
+ * after the alloc() call) should return the file descriptor to set
+ * in the AST_GENERATOR_FD entry of the channel.
+ */
+#define AST_GENERATOR_FD	(AST_MAX_FDS-4) /* used by generator */
+#define AST_GEN_MAGIC	0xa9ea61c
 
 enum ast_bridge_result {
 	AST_BRIDGE_COMPLETE = 0,
@@ -134,6 +152,9 @@ struct ast_generator {
 	void *(*alloc)(struct ast_channel *chan, void *params);
 	void (*release)(struct ast_channel *chan, void *data);
 	int (*generate)(struct ast_channel *chan, void *data, int len, int samples);
+
+	int magic;	/* set AST_GEN_MAGIC to indicate get_fd is valid */
+	int (*get_fd)(struct ast_channel *chan, void *data);
 };
 
 /*! Structure for all kinds of caller ID identifications */
@@ -1106,12 +1127,16 @@ void ast_channel_stop_silence_generator(
 static inline int ast_fdisset(struct pollfd *pfds, int fd, int max, int *start)
 {
 	int x;
-	for (x=start ? *start : 0;x<max;x++)
+	int dummy=0;
+
+	if (fd < 0)
+		return 0;
+	if (start == NULL)
+		start = &dummy;
+	for (x = *start; x<max; x++)
 		if (pfds[x].fd == fd) {
-			if (start) {
-				if (x==*start)
-					(*start)++;
-			}
+			if (x == *start)
+				(*start)++;
 			return pfds[x].revents;
 		}
 	return 0;
