diff --git a/app.c b/app.c
index 19a821ae717cc8716a62bbade2f4620adea7f727..2b31daf16d88f077e83a21348a6fdff2171ce2ba 100755
--- a/app.c
+++ b/app.c
@@ -390,7 +390,7 @@ int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, in
 	return res;
 }
 
-int ast_control_streamfile(struct ast_channel *chan, char *file, char *fwd, char *rev, char *stop, char *pause, int skipms) 
+int ast_control_streamfile(struct ast_channel *chan, const char *file, const char *fwd, const char *rev, const char *stop, const char *pause, int skipms) 
 {
 	struct timeval started, ended;
 	long elapsed = 0,last_elapsed =0;
@@ -483,7 +483,7 @@ int ast_control_streamfile(struct ast_channel *chan, char *file, char *fwd, char
 	return res;
 }
 
-int ast_play_and_wait(struct ast_channel *chan, char *fn)
+int ast_play_and_wait(struct ast_channel *chan, const char *fn)
 {
 	int d;
 	d = ast_streamfile(chan, fn, chan->language);
diff --git a/apps/app_dial.c b/apps/app_dial.c
index 6604ca18aab7efdcc07924ec8e89909545666105..20ca543449bcf2310a2a13243de760abc69a4115 100755
--- a/apps/app_dial.c
+++ b/apps/app_dial.c
@@ -1085,21 +1085,21 @@ static int dial_exec(struct ast_channel *chan, void *data)
 		if (!res) {
 			memset(&config,0,sizeof(struct ast_bridge_config));
 			if (play_to_caller)
-				config.features_caller |= AST_FEATURE_PLAY_WARNING;
+				ast_set_flag(&(config.features_caller), AST_FEATURE_PLAY_WARNING);
 			if (play_to_callee)
-				config.features_callee |= AST_FEATURE_PLAY_WARNING;
+				ast_set_flag(&(config.features_callee), AST_FEATURE_PLAY_WARNING);
 			if (ast_test_flag(&peerflags, DIAL_ALLOWREDIRECT_IN))
-				config.features_callee |= AST_FEATURE_REDIRECT;
+				ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
 			if (ast_test_flag(&peerflags, DIAL_ALLOWREDIRECT_OUT))
-				config.features_caller |= AST_FEATURE_REDIRECT;
+				ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
 			if (ast_test_flag(&peerflags, DIAL_ALLOWDISCONNECT_IN))
-				config.features_callee |= AST_FEATURE_DISCONNECT;
+				ast_set_flag(&(config.features_callee), AST_FEATURE_DISCONNECT);
 			if (ast_test_flag(&peerflags, DIAL_ALLOWDISCONNECT_OUT))
-				config.features_caller |= AST_FEATURE_DISCONNECT;
+				ast_set_flag(&(config.features_caller), AST_FEATURE_DISCONNECT);
 			if (ast_test_flag(&peerflags, DIAL_MONITOR_IN))
-				config.features_callee |= AST_FEATURE_AUTOMON;
+				ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
 			if (ast_test_flag(&peerflags, DIAL_MONITOR_OUT))
-				config.features_caller |= AST_FEATURE_AUTOMON;
+				ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
 			config.timelimit = timelimit;
 			config.play_warning = play_warning;
 			config.warning_freq = warning_freq;
diff --git a/apps/app_forkcdr.c b/apps/app_forkcdr.c
index 4fd0953c77730f3f684aacfdbb6cd3428ac108b7..227fb8737b07a729ac3cf8e58f396fcaa21fd7d6 100755
--- a/apps/app_forkcdr.c
+++ b/apps/app_forkcdr.c
@@ -41,7 +41,7 @@ static void ast_cdr_clone(struct ast_cdr *cdr) {
 	gettimeofday(&newcdr->start, NULL);
 	memset(&newcdr->answer, 0, sizeof(newcdr->answer));
 	newcdr->disposition = AST_CDR_NOANSWER;
-	ast_cdr_add_flag(cdr,AST_CDR_FLAG_CHILD|AST_CDR_FLAG_LOCKED);
+	ast_set_flag(cdr, AST_CDR_FLAG_CHILD|AST_CDR_FLAG_LOCKED);
 }
 
 static void ast_cdr_fork(struct ast_channel *chan) {
diff --git a/apps/app_queue.c b/apps/app_queue.c
index ef7115db4acb437ee0d8011dd1323a7d35db13ee..4d2cd9813610661e73aaf5ee25566888a2acea92 100755
--- a/apps/app_queue.c
+++ b/apps/app_queue.c
@@ -1543,13 +1543,13 @@ static int try_calling(struct queue_ent *qe, char *options, char *announceoverri
 
 		memset(&config,0,sizeof(struct ast_bridge_config));
 		if (ast_test_flag(&flags, QUEUE_FLAG_REDIR_IN))
-			config.features_callee |= AST_FEATURE_REDIRECT;
+			ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
 		if (ast_test_flag(&flags, QUEUE_FLAG_REDIR_OUT))
-			config.features_caller |= AST_FEATURE_REDIRECT;
+			ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
 		if (ast_test_flag(&flags, QUEUE_FLAG_DISCON_IN))
-			config.features_callee |= AST_FEATURE_DISCONNECT;
+			ast_set_flag(&(config.features_callee), AST_FEATURE_DISCONNECT);
 		if (ast_test_flag(&flags, QUEUE_FLAG_DISCON_OUT))
-			config.features_caller |= AST_FEATURE_DISCONNECT;
+			ast_set_flag(&(config.features_caller), AST_FEATURE_DISCONNECT);
 		bridge = ast_bridge_call(qe->chan,peer,&config);
 
 		if (strcasecmp(oldcontext, qe->chan->context) || strcasecmp(oldexten, qe->chan->exten)) {
diff --git a/cdr.c b/cdr.c
index 328a20b5144b05de7084bb65500487223843a848..d6c869f2ab963286c0dc7d9ce1be5e18b91e03fc 100755
--- a/cdr.c
+++ b/cdr.c
@@ -110,7 +110,7 @@ void ast_cdr_free(struct ast_cdr *cdr)
 	while (cdr) {
 		next = cdr->next;
 		chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
-		if (!ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
+		if (!ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
 			ast_log(LOG_WARNING, "CDR on channel '%s' not posted\n", chan);
 		if (!cdr->end.tv_sec && !cdr->end.tv_usec)
 			ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
@@ -135,9 +135,9 @@ void ast_cdr_start(struct ast_cdr *cdr)
 {
 	char *chan; 
 	while (cdr) {
-		if (!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
+		if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
 			chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
-			if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
+			if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
 				ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
 			if (cdr->start.tv_sec || cdr->start.tv_usec)
 				ast_log(LOG_WARNING, "CDR on channel '%s' already started\n", chan);
@@ -152,7 +152,7 @@ void ast_cdr_answer(struct ast_cdr *cdr)
 	char *chan; 
 	while (cdr) {
 		chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
-		if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
+		if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
 			ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
 		if (cdr->disposition < AST_CDR_ANSWERED)
 			cdr->disposition = AST_CDR_ANSWERED;
@@ -167,9 +167,9 @@ void ast_cdr_busy(struct ast_cdr *cdr)
 {
 	char *chan; 
 	while (cdr) {
-		if (!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
+		if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
 			chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
-			if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
+			if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
 				ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
 			if (cdr->disposition < AST_CDR_BUSY)
 				cdr->disposition = AST_CDR_BUSY;
@@ -183,9 +183,9 @@ void ast_cdr_failed(struct ast_cdr *cdr)
 	char *chan; 
 	while (cdr) {
 		chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
-		if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
+		if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
 			ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
-		if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED))
+		if(!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
 			cdr->disposition = AST_CDR_FAILED;
 		cdr = cdr->next;
 	}
@@ -221,9 +221,9 @@ void ast_cdr_setdestchan(struct ast_cdr *cdr, char *chann)
 	char *chan; 
 	while (cdr) {
 		chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
-		if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
+		if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
 			ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
-		if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED))
+		if(!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
 			strncpy(cdr->dstchannel, chann, sizeof(cdr->dstchannel) - 1);
 		cdr = cdr->next;
 	}
@@ -233,9 +233,9 @@ void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data)
 {
 	char *chan; 
 	while (cdr) {
-		if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
+		if(!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
 			chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
-			if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
+			if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
 				ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
 			if (!app)
 				app = "";
@@ -253,7 +253,7 @@ int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
 	char tmp[AST_MAX_EXTENSION] = "";
 	char *num;
 	while (cdr) {
-		if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
+		if(!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
 			/* Grab source from ANI or normal Caller*ID */
 			if (c->cid.cid_ani)
 				num = c->cid.cid_ani;
@@ -285,7 +285,7 @@ int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
 	char *num;
 	char tmp[AST_MAX_EXTENSION] = "";
 	while (cdr) {
-		if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
+		if(!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
 			chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
 			if (!ast_strlen_zero(cdr->channel)) 
 				ast_log(LOG_WARNING, "CDR already initialized on '%s'\n", chan); 
@@ -335,7 +335,7 @@ void ast_cdr_end(struct ast_cdr *cdr)
 	char *chan;
 	while (cdr) {
 		chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
-		if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
+		if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
 			ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
 		if (!cdr->start.tv_sec && !cdr->start.tv_usec)
 			ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", chan);
@@ -380,7 +380,7 @@ int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
 
 	strncpy(chan->accountcode, account, sizeof(chan->accountcode) - 1);
 	while (cdr) {
-		if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED))
+		if(!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
 			strncpy(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode) - 1);
 		cdr = cdr->next;
 	}
@@ -404,7 +404,7 @@ int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
 	struct ast_cdr *cdr = chan->cdr;
 
 	while (cdr) {
-		if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) 
+		if(!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) 
 			strncpy(cdr->userfield, userfield, sizeof(cdr->userfield) - 1);
 		cdr = cdr->next;
 	}
@@ -419,7 +419,7 @@ int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
 	{
 
 		int len = strlen(cdr->userfield);
-		if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED))
+		if(!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
 			strncpy(cdr->userfield+len, userfield, sizeof(cdr->userfield) - len - 1);
 		cdr = cdr->next;
 	}
@@ -433,7 +433,7 @@ int ast_cdr_update(struct ast_channel *c)
 	char tmp[AST_MAX_EXTENSION] = "";
 	/* Grab source from ANI or normal Caller*ID */
 	while (cdr) {
-		if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
+		if(!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
 			/* Grab source from ANI or normal Caller*ID */
 			if (c->cid.cid_ani)
 				num = c->cid.cid_ani;
@@ -491,7 +491,7 @@ void ast_cdr_post(struct ast_cdr *cdr)
 	struct ast_cdr_beitem *i;
 	while (cdr) {
 		chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
-		if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
+		if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
 			ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
 		if (!cdr->end.tv_sec && !cdr->end.tv_usec)
 			ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
@@ -502,7 +502,7 @@ void ast_cdr_post(struct ast_cdr *cdr)
 			cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec + (cdr->end.tv_usec - cdr->answer.tv_usec) / 1000000;
 		} else
 			cdr->billsec = 0;
-		ast_cdr_add_flag(cdr,AST_CDR_FLAG_POSTED);
+		ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
 		ast_mutex_lock(&cdrlock);
 		i = bes;
 		while(i) {
@@ -516,15 +516,16 @@ void ast_cdr_post(struct ast_cdr *cdr)
 
 void ast_cdr_reset(struct ast_cdr *cdr, int flags)
 {
+	struct ast_flags tmp = {flags};
 	while (cdr) {
 		/* Post if requested */
-		if (ast_cdr_compare_flag(flags,AST_CDR_FLAG_LOCKED) || !ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
-			if (ast_cdr_compare_flag(flags,AST_CDR_FLAG_POSTED)) {
+		if (ast_test_flag(&tmp, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
+			if (ast_test_flag(&tmp, AST_CDR_FLAG_POSTED)) {
 				ast_cdr_end(cdr);
 				ast_cdr_post(cdr);
 			}
 			/* Reset to initial state */
-			cdr->flags=0;
+			ast_clear_flag(cdr, AST_FLAGS_ALL);	
 			memset(&cdr->start, 0, sizeof(cdr->start));
 			memset(&cdr->end, 0, sizeof(cdr->end));
 			memset(&cdr->answer, 0, sizeof(cdr->answer));
diff --git a/channel.c b/channel.c
index 97632ef1fa64649138c7d635b8db6cffb6bc7b65..36bef6c0f908fd6e88cfd5ad5ed34822ebd2c47d 100755
--- a/channel.c
+++ b/channel.c
@@ -2675,9 +2675,9 @@ int ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1, struct as
 	gettimeofday(&start_time,NULL);
 	time_left_ms = config->timelimit;
 
-	if ((config->features_caller & AST_FEATURE_PLAY_WARNING) && config->start_sound && firstpass)
+	if ((ast_test_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING)) && config->start_sound && firstpass)
 		bridge_playfile(c0,c1,config->start_sound,time_left_ms / 1000);
-	if ((config->features_callee & AST_FEATURE_PLAY_WARNING) && config->start_sound && firstpass)
+	if ((ast_test_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING)) && config->start_sound && firstpass)
 		bridge_playfile(c1,c0,config->start_sound,time_left_ms / 1000);
 
 	/* Stop if we're a zombie or need a soft hangup */
@@ -2715,7 +2715,7 @@ int ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1, struct as
 			elapsed_ms = tvdiff(&precise_now,&start_time);
 			time_left_ms = config->timelimit - elapsed_ms;
 
-			if (playitagain && ((config->features_caller & AST_FEATURE_PLAY_WARNING) || (config->features_callee & AST_FEATURE_PLAY_WARNING)) && (config->play_warning && time_left_ms <= config->play_warning)) { 
+			if (playitagain && ((ast_test_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING)) || (ast_test_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING))) && (config->play_warning && time_left_ms <= config->play_warning)) { 
 				/* narrowing down to the end */
 				if (config->warning_freq == 0) {
 					playit = 1;
@@ -2731,9 +2731,9 @@ int ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1, struct as
 				}
 			}
 			if (time_left_ms <= 0) {
-				if ((config->features_caller & AST_FEATURE_PLAY_WARNING) && config->end_sound)
+				if ((ast_test_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING)) && config->end_sound)
 					bridge_playfile(c0,c1,config->end_sound,0);
-				if ((config->features_callee & AST_FEATURE_PLAY_WARNING) && config->end_sound)
+				if ((ast_test_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING)) && config->end_sound)
 					bridge_playfile(c1,c0,config->end_sound,0);
 				*fo = NULL;
 				if (who) *rc = who;
@@ -2741,9 +2741,9 @@ int ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1, struct as
 				break;
 			}
 			if (time_left_ms >= 5000 && playit) {
-				if ((config->features_caller & AST_FEATURE_PLAY_WARNING) && config->warning_sound && config->play_warning)
+				if ((ast_test_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING)) && config->warning_sound && config->play_warning)
 					bridge_playfile(c0,c1,config->warning_sound,time_left_ms / 1000);
-				if ((config->features_callee & AST_FEATURE_PLAY_WARNING) && config->warning_sound && config->play_warning)
+				if ((ast_test_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING)) && config->warning_sound && config->play_warning)
 					bridge_playfile(c1,c0,config->warning_sound,time_left_ms / 1000);
 				playit = 0;
 			}
diff --git a/include/asterisk/app.h b/include/asterisk/app.h
index 1af21f3e875b10fa97367bd88ece1b48a9f0b066..57925174b90907699d131febe02e4f32d8f911c8 100755
--- a/include/asterisk/app.h
+++ b/include/asterisk/app.h
@@ -60,10 +60,10 @@ int ast_dtmf_stream(struct ast_channel *chan, struct ast_channel *peer, char *di
 int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, int allowoverride);
 
 //! Stream a file with fast forward, pause, reverse.
-int ast_control_streamfile(struct ast_channel *chan, char *file, char *fwd, char *rev, char *stop, char *pause, int skipms);
+int ast_control_streamfile(struct ast_channel *chan, const char *file, const char *fwd, const char *rev, const char *stop, const char *pause, int skipms);
 
 //! Play a stream and wait for a digit, returning the digit that was pressed
-int ast_play_and_wait(struct ast_channel *chan, char *fn);
+int ast_play_and_wait(struct ast_channel *chan, const char *fn);
 
 //! Record a file for a max amount of time (in seconds), in a given list of formats separated by '|', outputting the duration of the recording, and with a maximum
 //  permitted silence time in milliseconds of 'maxsilence' under 'silencethreshold' or use '-1' for either or both parameters for defaults.
diff --git a/include/asterisk/cdr.h b/include/asterisk/cdr.h
index 807ad6419891f10470678b14ece46a02207bbe9a..88f210e698666ab9eac5685277c54ab5983575ad 100755
--- a/include/asterisk/cdr.h
+++ b/include/asterisk/cdr.h
@@ -73,7 +73,7 @@ struct ast_cdr {
 	/*! What account number to use */
 	char accountcode[20];			
 	/*! flags */
-	int flags;				
+	unsigned int flags;				
 	/* Unique Channel Identifier */
 	char uniqueid[32];
 	/* User field */
@@ -249,11 +249,6 @@ extern int ast_default_amaflags;
 
 extern char ast_default_accountcode[20];
 
-#define ast_cdr_compare_flag(flags, flag) (flags & (flag))
-#define ast_cdr_has_flag(cdr, flag) ((cdr)->flags & (flag))
-#define ast_cdr_add_flag(cdr, flag) ((cdr)->flags |= (flag))
-#define ast_cdr_del_flag(cdr, flag) ((cdr)->flags &= ~(flag))
-
 extern struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr);
 
 #endif /* _CDR_H */
diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h
index 294d15557f8df76084c13b47c4e00acf4e65940a..4d24150db535a5713a2af6c635044cb8214504cc 100755
--- a/include/asterisk/channel.h
+++ b/include/asterisk/channel.h
@@ -36,6 +36,7 @@ extern "C" {
 
 #include <asterisk/cdr.h>
 #include <asterisk/monitor.h>
+#include <asterisk/utils.h>
 
 
 #define AST_CHANNEL_NAME 80
@@ -238,8 +239,8 @@ struct ast_channel {
 #define AST_FEATURE_FLAG_NEEDSDTMF		(1 << 0)
 
 struct ast_bridge_config {
-	unsigned int features_caller;
-	unsigned int features_callee;
+	struct ast_flags features_caller;
+	struct ast_flags features_callee;
 	long timelimit;
 	long play_warning;
 	long warning_freq;
diff --git a/include/asterisk/dundi.h b/include/asterisk/dundi.h
index 5a6920b30d44eb5abfcb4929f6e5102d7aa335de..97d67e8d16cee1cf5fa3fdf40ae1246b9128e03b 100755
--- a/include/asterisk/dundi.h
+++ b/include/asterisk/dundi.h
@@ -179,7 +179,7 @@ struct dundi_peer_status {
 #define DEFAULT_MAXMS				2000
 
 struct dundi_result {
-	int flags;
+	unsigned int flags;
 	int weight;
 	int expiration;
 	int techint;
diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h
index ff42c13128975279d13ca8f331cb43fc7b5396b0..ff0c2cb7e0c3547ccb2d59b6dffa762778587b18 100755
--- a/include/asterisk/utils.h
+++ b/include/asterisk/utils.h
@@ -82,6 +82,33 @@ extern unsigned int __unsigned_int_flags_dummy;
 						(p)->flags &= ~(flag); \
 					} while (0)
 
+/* Non-type checking variations for non-unsigned int flags.  You
+   should only use non-unsigned int flags where required by 
+   protocol etc and if you know what you're doing :)  */
+#define ast_test_flag_nonstd(p,flag) 		({ \
+					((p)->flags & (flag)); \
+					})
+
+#define ast_set_flag_nonstd(p,flag) 		do { \
+					((p)->flags |= (flag)); \
+					} while(0)
+
+#define ast_clear_flag_nonstd(p,flag) 		do { \
+					((p)->flags &= ~(flag)); \
+					} while(0)
+
+#define ast_copy_flags_nonstd(dest,src,flagz)	do { \
+					(dest)->flags &= ~(flagz); \
+					(dest)->flags |= ((src)->flags & (flagz)); \
+					} while (0)
+
+#define ast_set2_flag_nonstd(p,value,flag)	do { \
+					if (value) \
+						(p)->flags |= (flag); \
+					else \
+						(p)->flags &= ~(flag); \
+					} while (0)
+
 #define AST_FLAGS_ALL UINT_MAX
 
 struct ast_flags {
diff --git a/pbx/pbx_dundi.c b/pbx/pbx_dundi.c
index f581af694337a623c417a8a45f954b713f857f79..dae634862c7f891a1d7771c923b017b544ae85a0 100755
--- a/pbx/pbx_dundi.c
+++ b/pbx/pbx_dundi.c
@@ -164,7 +164,7 @@ struct dundi_transaction {
 	dundi_eid them_eid;			/* Their EID, to us */
 	aes_encrypt_ctx	ecx;		/* AES 128 Encryption context */
 	aes_decrypt_ctx	dcx;		/* AES 128 Decryption context */
-	int flags;					/* Has final packet been sent */
+	unsigned int flags;				/* Has final packet been sent */
 	int ttl;					/* Remaining TTL for queries on this one */
 	int thread;					/* We have a calling thread */
 	int retranstimer;			/* How long to wait before retransmissions */
@@ -461,7 +461,7 @@ static int reset_transaction(struct dundi_transaction *trans)
 	trans->oiseqno = 0;
 	trans->oseqno = 0;
 	trans->aseqno = 0;
-	trans->flags &= ~FLAG_FINAL;
+	ast_clear_flag(trans, FLAG_FINAL);	
 	return 0;
 }
 
@@ -507,39 +507,38 @@ struct dundi_query_state {
 
 static int dundi_lookup_local(struct dundi_result *dr, struct dundi_mapping *map, char *called_number, dundi_eid *us_eid, int anscnt, struct dundi_hint_metadata *hmd)
 {
-	int flags;
+	struct ast_flags flags = {0};
 	int x;
 	if (!ast_strlen_zero(map->lcontext)) {
-		flags = 0;
 		if (ast_exists_extension(NULL, map->lcontext, called_number, 1, NULL))
-			flags |= DUNDI_FLAG_EXISTS;
+			ast_set_flag(&flags, DUNDI_FLAG_EXISTS);
 		if (ast_canmatch_extension(NULL, map->lcontext, called_number, 1, NULL))
-			flags |= DUNDI_FLAG_CANMATCH;
+			ast_set_flag(&flags, DUNDI_FLAG_CANMATCH);
 		if (ast_matchmore_extension(NULL, map->lcontext, called_number, 1, NULL))
-			flags |= DUNDI_FLAG_MATCHMORE;
+			ast_set_flag(&flags, DUNDI_FLAG_MATCHMORE);
 		if (ast_ignore_pattern(map->lcontext, called_number))
-			flags |= DUNDI_FLAG_IGNOREPAT;
+			ast_set_flag(&flags, DUNDI_FLAG_IGNOREPAT);
 
 		/* Clearly we can't say 'don't ask' anymore if we found anything... */
-		if (flags) 
-			hmd->flags &= ~DUNDI_HINT_DONT_ASK;
+		if (ast_test_flag(&flags, AST_FLAGS_ALL)) 
+			ast_clear_flag_nonstd(hmd, DUNDI_HINT_DONT_ASK);
 
 		if (map->options & DUNDI_FLAG_INTERNAL_NOPARTIAL) {
 			/* Skip partial answers */
-			flags &= ~(DUNDI_FLAG_MATCHMORE|DUNDI_FLAG_CANMATCH);
+			ast_clear_flag(&flags, DUNDI_FLAG_MATCHMORE|DUNDI_FLAG_CANMATCH);
 		}
-		if (flags) {
+		if (ast_test_flag(&flags, AST_FLAGS_ALL)) {
 			struct varshead headp;
 			struct ast_var_t *newvariable;
-			flags |= map->options & 0xffff;
-			dr[anscnt].flags = flags;
+			ast_set_flag(&flags, map->options & 0xffff);
+			ast_copy_flags(dr + anscnt, &flags, AST_FLAGS_ALL);
 			dr[anscnt].techint = map->tech;
 			dr[anscnt].weight = map->weight;
 			dr[anscnt].expiration = DUNDI_DEFAULT_CACHE_TIME;
 			strncpy(dr[anscnt].tech, tech2str(map->tech), sizeof(dr[anscnt].tech));
 			dr[anscnt].eid = *us_eid;
 			dundi_eid_to_str(dr[anscnt].eid_str, sizeof(dr[anscnt].eid_str), &dr[anscnt].eid);
-			if (flags & DUNDI_FLAG_EXISTS) {
+			if (ast_test_flag(&flags, DUNDI_FLAG_EXISTS)) {
 				AST_LIST_HEAD_INIT(&headp);
 				newvariable = ast_var_assign("NUMBER", called_number);
 				AST_LIST_INSERT_HEAD(&headp, newvariable, entries);
@@ -622,9 +621,9 @@ static void *dundi_lookup_thread(void *data)
 	}
 	ast_mutex_lock(&peerlock);
 	/* Truncate if "don't ask" isn't present */
-	if (!(hmd.flags & DUNDI_HINT_DONT_ASK))
+	if (!ast_test_flag_nonstd(&hmd, DUNDI_HINT_DONT_ASK))
 		hmd.exten[0] = '\0';
-	if (st->trans->flags & FLAG_DEAD) {
+	if (ast_test_flag(st->trans, FLAG_DEAD)) {
 		ast_log(LOG_DEBUG, "Our transaction went away!\n");
 		st->trans->thread = 0;
 		destroy_trans(st->trans, 0);
@@ -661,9 +660,9 @@ static void *dundi_precache_thread(void *data)
 
 	ast_mutex_lock(&peerlock);
 	/* Truncate if "don't ask" isn't present */
-	if (!(hmd.flags & DUNDI_HINT_DONT_ASK))
+	if (!ast_test_flag_nonstd(&hmd, DUNDI_HINT_DONT_ASK))
 		hmd.exten[0] = '\0';
-	if (st->trans->flags & FLAG_DEAD) {
+	if (ast_test_flag(st->trans, FLAG_DEAD)) {
 		ast_log(LOG_DEBUG, "Our transaction went away!\n");
 		st->trans->thread = 0;
 		destroy_trans(st->trans, 0);
@@ -714,7 +713,7 @@ static void *dundi_query_thread(void *data)
 		res = dundi_query_eid_internal(&dei, st->called_context, &st->reqeid, &hmd, st->ttl, 1, st->eids);
 	}
 	ast_mutex_lock(&peerlock);
-	if (st->trans->flags & FLAG_DEAD) {
+	if (ast_test_flag(st->trans, FLAG_DEAD)) {
 		ast_log(LOG_DEBUG, "Our transaction went away!\n");
 		st->trans->thread = 0;
 		destroy_trans(st->trans, 0);
@@ -812,10 +811,10 @@ static int cache_save_hint(dundi_eid *eidpeer, struct dundi_request *req, struct
 		expiration = DUNDI_DEFAULT_CACHE_TIME;
 
 	/* Only cache hint if "don't ask" is there... */
-	if (!(ntohs(hint->flags)& DUNDI_HINT_DONT_ASK))
+	if (!ast_test_flag_nonstd(hint, htons(DUNDI_HINT_DONT_ASK)))	
 		return 0;
 
-	unaffected = ntohs(hint->flags) & DUNDI_HINT_UNAFFECTED;
+	unaffected = ast_test_flag_nonstd(hint, htons(DUNDI_HINT_UNAFFECTED));
 
 	dundi_eid_to_str_short(eidpeer_str, sizeof(eidpeer_str), eidpeer);
 	dundi_eid_to_str_short(eidroot_str, sizeof(eidroot_str), &req->root_eid);
@@ -933,7 +932,7 @@ static int dundi_prop_precache(struct dundi_transaction *trans, struct dundi_ies
 					strncpy(trans->parent->dr[trans->parent->respcount].tech, tech2str(ies->answers[x]->protocol),
 					sizeof(trans->parent->dr[trans->parent->respcount].tech));
 				trans->parent->respcount++;
-				trans->parent->hmd->flags &= ~DUNDI_HINT_DONT_ASK;
+				ast_clear_flag_nonstd(trans->parent->hmd, DUNDI_HINT_DONT_ASK);	
 			} else if (trans->parent->dr[z].weight > ies->answers[x]->weight) {
 				/* Update weight if appropriate */
 				trans->parent->dr[z].weight = ies->answers[x]->weight;
@@ -1126,7 +1125,7 @@ static int cache_lookup_internal(time_t now, struct dundi_request *req, char *ke
 	char data[1024]="";
 	char *ptr, *term, *src;
 	int tech;
-	int flags;
+	struct ast_flags flags;
 	int weight;
 	int length;
 	int z;
@@ -1141,7 +1140,7 @@ static int cache_lookup_internal(time_t now, struct dundi_request *req, char *ke
 			if (expiration > 0) {
 				ast_log(LOG_DEBUG, "Found cache expiring in %d seconds!\n", (int)(timeout - now));
 				ptr += length;
-				while((sscanf(ptr, "%d/%d/%d/%n", &flags, &weight, &tech, &length) == 3)) {
+				while((sscanf(ptr, "%d/%d/%d/%n", &(flags.flags), &weight, &tech, &length) == 3)) {
 					ptr += length;
 					term = strchr(ptr, '|');
 					if (term) {
@@ -1153,7 +1152,7 @@ static int cache_lookup_internal(time_t now, struct dundi_request *req, char *ke
 						} else
 							src = "";
 						ast_log(LOG_DEBUG, "Found cached answer '%s/%s' originally from '%s' with flags '%s' on behalf of '%s'\n", 
-							tech2str(tech), ptr, src, dundi_flags2str(fs, sizeof(fs), flags), eid_str_full);
+							tech2str(tech), ptr, src, dundi_flags2str(fs, sizeof(fs), flags.flags), eid_str_full);
 						/* Make sure it's not already there */
 						for (z=0;z<req->respcount;z++) {
 							if ((req->dr[z].techint == tech) &&
@@ -1162,7 +1161,7 @@ static int cache_lookup_internal(time_t now, struct dundi_request *req, char *ke
 						}
 						if (z == req->respcount) {
 							/* Copy into parent responses */
-							req->dr[req->respcount].flags = flags;
+							ast_copy_flags(&(req->dr[req->respcount]), &flags, AST_FLAGS_ALL);	
 							req->dr[req->respcount].weight = weight;
 							req->dr[req->respcount].techint = tech;
 							req->dr[req->respcount].expiration = expiration;
@@ -1174,7 +1173,7 @@ static int cache_lookup_internal(time_t now, struct dundi_request *req, char *ke
 							strncpy(req->dr[req->respcount].tech, tech2str(tech),
 								sizeof(req->dr[req->respcount].tech));
 							req->respcount++;
-							req->hmd->flags &= ~DUNDI_HINT_DONT_ASK;
+							ast_clear_flag_nonstd(req->hmd, DUNDI_HINT_DONT_ASK);	
 						} else if (req->dr[z].weight > weight)
 							req->dr[z].weight = weight;
 						ptr = term + 1;
@@ -1253,7 +1252,7 @@ static void apply_peer(struct dundi_transaction *trans, struct dundi_peer *p)
 	trans->them_eid = p->eid;
 	/* Enable encryption if appropriate */
 	if (!ast_strlen_zero(p->inkey))
-		trans->flags |= FLAG_ENCRYPT;
+		ast_set_flag(trans, FLAG_ENCRYPT);	
 	if (p->maxms) {
 		trans->autokilltimeout = p->maxms;
 		trans->retranstimer = DUNDI_DEFAULT_RETRANS_TIMER;
@@ -1406,10 +1405,10 @@ static int dundi_encrypt(struct dundi_transaction *trans, struct dundi_packet *p
 			if (update_key(peer))
 				return -1;
 			if (!peer->sentfullkey)
-				trans->flags |= FLAG_SENDFULLKEY;
+				ast_set_flag(trans, FLAG_SENDFULLKEY);	
 			/* Append key data */
 			dundi_ie_append_eid(&ied, DUNDI_IE_EID, &trans->us_eid);
-			if (trans->flags & FLAG_SENDFULLKEY) {
+			if (ast_test_flag(trans, FLAG_SENDFULLKEY)) {
 				dundi_ie_append_raw(&ied, DUNDI_IE_SHAREDKEY, peer->txenckey, 128);
 				dundi_ie_append_raw(&ied, DUNDI_IE_SIGNATURE, peer->txenckey + 128, 128);
 			} else {
@@ -1634,7 +1633,7 @@ static int handle_command_response(struct dundi_transaction *trans, struct dundi
 		if (ies.cause < 1) {
 			/* Success of some sort */
 			ast_log(LOG_DEBUG, "Looks like success of some sort (%d), %d answers\n", ies.cause, ies.anscount);
-			if (trans->flags & FLAG_ENCRYPT) {
+			if (ast_test_flag(trans, FLAG_ENCRYPT)) {
 				authpass = encrypted;
 			} else 
 				authpass = 1;
@@ -1668,7 +1667,7 @@ static int handle_command_response(struct dundi_transaction *trans, struct dundi
 								strncpy(trans->parent->dr[trans->parent->respcount].tech, tech2str(ies.answers[x]->protocol),
 									sizeof(trans->parent->dr[trans->parent->respcount].tech));
 								trans->parent->respcount++;
-								trans->parent->hmd->flags &= ~DUNDI_HINT_DONT_ASK;
+								ast_clear_flag_nonstd(trans->parent->hmd, DUNDI_HINT_DONT_ASK);
 							} else if (trans->parent->dr[z].weight > ies.answers[x]->weight) {
 								/* Update weight if appropriate */
 								trans->parent->dr[z].weight = ies.answers[x]->weight;
@@ -1680,18 +1679,18 @@ static int handle_command_response(struct dundi_transaction *trans, struct dundi
 					/* Save all the results (if any) we had.  Even if no results, still cache lookup.  Let
 					   the cache know if this request was unaffected by our entity list. */
 					cache_save(&trans->them_eid, trans->parent, y, 
-							ies.hint ? ntohs(ies.hint->flags) & DUNDI_HINT_UNAFFECTED : 0, ies.expiration, 0);
+							ies.hint ? ast_test_flag_nonstd(ies.hint, htons(DUNDI_HINT_UNAFFECTED)) : 0, ies.expiration, 0);
 					if (ies.hint) {
 						cache_save_hint(&trans->them_eid, trans->parent, ies.hint, ies.expiration);
-						if (ntohs(ies.hint->flags) & DUNDI_HINT_TTL_EXPIRED)
-							trans->parent->hmd->flags |= DUNDI_HINT_TTL_EXPIRED;
-						if (ntohs(ies.hint->flags) & DUNDI_HINT_DONT_ASK) { 
+						if (ast_test_flag_nonstd(ies.hint, htons(DUNDI_HINT_TTL_EXPIRED)))
+							ast_set_flag_nonstd(trans->parent->hmd, DUNDI_HINT_TTL_EXPIRED);
+						if (ast_test_flag_nonstd(ies.hint, htons(DUNDI_HINT_DONT_ASK))) { 
 							if (strlen(ies.hint->data) > strlen(trans->parent->hmd->exten)) {
 								strncpy(trans->parent->hmd->exten, ies.hint->data, 
 									sizeof(trans->parent->hmd->exten) - 1);
 							}
 						} else {
-							trans->parent->hmd->flags &= ~DUNDI_HINT_DONT_ASK;
+							ast_clear_flag_nonstd(trans->parent->hmd, DUNDI_HINT_DONT_ASK);
 						}
 					}
 					if (ies.expiration > 0) {
@@ -1718,7 +1717,7 @@ static int handle_command_response(struct dundi_transaction *trans, struct dundi
 		if (ies.cause < 1) {
 			/* Success of some sort */
 			ast_log(LOG_DEBUG, "Looks like success of some sort (%d)\n", ies.cause);
-			if (trans->flags & FLAG_ENCRYPT) {
+			if (ast_test_flag(trans, FLAG_ENCRYPT)) {
 				authpass = encrypted;
 			} else 
 				authpass = 1;
@@ -1750,8 +1749,8 @@ static int handle_command_response(struct dundi_transaction *trans, struct dundi
 						}
 					}
 					if (ies.hint) {
-						if (ntohs(ies.hint->flags) & DUNDI_HINT_TTL_EXPIRED)
-							trans->parent->hmd->flags |= DUNDI_HINT_TTL_EXPIRED;
+						if (ast_test_flag_nonstd(ies.hint, htons(DUNDI_HINT_TTL_EXPIRED)))
+							ast_set_flag_nonstd(trans->parent->hmd, DUNDI_HINT_TTL_EXPIRED);
 					}
 				}
 				/* Close connection if not final */
@@ -1772,7 +1771,7 @@ static int handle_command_response(struct dundi_transaction *trans, struct dundi
 		if (ies.cause < 1) {
 			int hasauth;
 			/* Success of some sort */
-			if (trans->flags & FLAG_ENCRYPT) {
+			if (ast_test_flag(trans, FLAG_ENCRYPT)) {
 				hasauth = encrypted;
 			} else 
 				hasauth = 1;
@@ -1805,13 +1804,13 @@ static int handle_command_response(struct dundi_transaction *trans, struct dundi
 			dundi_send(trans, DUNDI_COMMAND_CANCEL, 0, 1, NULL);
 		break;
 	case DUNDI_COMMAND_ENCREJ:
-		if ((trans->flags & FLAG_SENDFULLKEY) || !trans->lasttrans || !(peer = find_peer(&trans->them_eid))) {
+		if ((ast_test_flag(trans, FLAG_SENDFULLKEY)) || !trans->lasttrans || !(peer = find_peer(&trans->them_eid))) {
 			/* No really, it's over at this point */
 			if (!final) 
 				dundi_send(trans, DUNDI_COMMAND_CANCEL, 0, 1, NULL);
 		} else {
 			/* Send with full key */
-			trans->flags |= FLAG_SENDFULLKEY;
+			ast_set_flag(trans, FLAG_SENDFULLKEY);
 			if (final) {
 				/* Ooops, we got a final message, start by sending ACK... */
 				dundi_ack(trans, hdr->cmdresp & 0x80);
@@ -1853,7 +1852,7 @@ static int handle_command_response(struct dundi_transaction *trans, struct dundi
 				trans->ecx = peer->them_ecx;
 				trans->dcx = peer->them_dcx;
 			}
-			if ((trans->flags & FLAG_ENCRYPT) && ies.encblock && ies.enclen) {
+			if (ast_test_flag(trans, FLAG_ENCRYPT) && ies.encblock && ies.enclen) {
 				struct dundi_hdr *dhdr;
 				unsigned char decoded[MAX_PACKET_SIZE];
 				int ddatalen;
@@ -1873,7 +1872,7 @@ static int handle_command_response(struct dundi_transaction *trans, struct dundi
 		}
 		if (!final) {
 			/* Turn off encryption */
-			trans->flags &= ~FLAG_ENCRYPT;
+			ast_clear_flag(trans, FLAG_ENCRYPT);
 			dundi_send(trans, DUNDI_COMMAND_ENCREJ, 0, 1, NULL);
 		}
 		break;
@@ -1936,7 +1935,7 @@ static int handle_frame(struct dundi_hdr *h, struct sockaddr_in *sin, int datale
 	/* Got a transaction, see where this header fits in */
 	if (h->oseqno == trans->iseqno) {
 		/* Just what we were looking for...  Anything but ack increments iseqno */
-		if (ack_trans(trans, h->iseqno) && (trans->flags & FLAG_FINAL)) {
+		if (ack_trans(trans, h->iseqno) && ast_test_flag(trans, FLAG_FINAL)) {
 			/* If final, we're done */
 			destroy_trans(trans, 0);
 			return 0;
@@ -2759,14 +2758,14 @@ static struct dundi_transaction *create_transaction(struct dundi_peer *p)
 		memset(trans, 0, sizeof(struct dundi_transaction));
 		if (global_storehistory) {
 			gettimeofday(&trans->start, NULL);
-			trans->flags |= FLAG_STOREHIST;
+			ast_set_flag(trans, FLAG_STOREHIST);
 		}
 		trans->retranstimer = DUNDI_DEFAULT_RETRANS_TIMER;
 		trans->autokillid = -1;
 		if (p) {
 			apply_peer(trans, p);
 			if (!p->sentfullkey)
-				trans->flags |= FLAG_SENDFULLKEY;
+				ast_set_flag(trans, FLAG_SENDFULLKEY);
 		}
 		trans->strans = tid;
 		trans->allnext = alltrans;
@@ -2829,7 +2828,7 @@ static void destroy_trans(struct dundi_transaction *trans, int fromtimeout)
 	int x;
 	int cnt;
 	char eid_str[20];
-	if (trans->flags & (FLAG_ISREG | FLAG_ISQUAL | FLAG_STOREHIST)) {
+	if (ast_test_flag(trans, FLAG_ISREG | FLAG_ISQUAL | FLAG_STOREHIST)) {
 		peer = peers;
 		while (peer) {
 			if (peer->regtrans == trans)
@@ -2857,7 +2856,7 @@ static void destroy_trans(struct dundi_transaction *trans, int fromtimeout)
 				}
 				peer->qualtrans = NULL;
 			}
-			if (trans->flags & FLAG_STOREHIST) {
+			if (ast_test_flag(trans, FLAG_STOREHIST)) {
 				if (trans->parent && !ast_strlen_zero(trans->parent->number)) {
 					if (!dundi_eid_cmp(&trans->them_eid, &peer->eid)) {
 						peer->avgms = 0;
@@ -2932,7 +2931,7 @@ static void destroy_trans(struct dundi_transaction *trans, int fromtimeout)
 	trans->autokillid = -1;
 	if (trans->thread) {
 		/* If used by a thread, mark as dead and be done */
-		trans->flags |= FLAG_DEAD;
+		ast_set_flag(trans, FLAG_DEAD);
 	} else
 		free(trans);
 }
@@ -2946,7 +2945,7 @@ static int dundi_rexmit(void *data)
 	pack = data;
 	if (pack->retrans < 1) {
 		pack->retransid = -1;
-		if (!(pack->parent->flags & FLAG_ISQUAL))
+		if (!ast_test_flag(pack->parent, FLAG_ISQUAL))
 			ast_log(LOG_NOTICE, "Max retries exceeded to host '%s:%d' msg %d on call %d\n", 
 				ast_inet_ntoa(iabuf, sizeof(iabuf), pack->parent->addr.sin_addr), 
 				ntohs(pack->parent->addr.sin_port), pack->h->oseqno, ntohs(pack->h->strans));
@@ -2970,7 +2969,7 @@ static int dundi_send(struct dundi_transaction *trans, int cmdresp, int flags, i
 	char eid_str[20];
 	len = sizeof(struct dundi_packet) + sizeof(struct dundi_hdr) + (ied ? ied->pos : 0);
 	/* Reserve enough space for encryption */
-	if (trans->flags & FLAG_ENCRYPT)
+	if (ast_test_flag(trans, FLAG_ENCRYPT))
 		len += 384;
 	pack = malloc(len);
 	if (pack) {
@@ -2995,7 +2994,7 @@ static int dundi_send(struct dundi_transaction *trans, int cmdresp, int flags, i
 		} 
 		if (final) {
 			pack->h->cmdresp |= DUNDI_COMMAND_FINAL;
-			trans->flags |= FLAG_FINAL;
+			ast_set_flag(trans, FLAG_FINAL);
 		}
 		pack->h->cmdflags = flags;
 		if (cmdresp != DUNDI_COMMAND_ACK) {
@@ -3004,7 +3003,7 @@ static int dundi_send(struct dundi_transaction *trans, int cmdresp, int flags, i
 		}
 		trans->aseqno = trans->iseqno;
 		/* If we have their public key, encrypt */
-		if (trans->flags & FLAG_ENCRYPT) {
+		if (ast_test_flag(trans, FLAG_ENCRYPT)) {
 			switch(cmdresp) {
 			case DUNDI_COMMAND_REGREQ:
 			case DUNDI_COMMAND_REGRESPONSE:
@@ -3211,7 +3210,7 @@ static int precache_transactions(struct dundi_request *dr, struct dundi_mapping
 
 	trans = dr->trans;
 	while(trans) {
-		if (!(trans->flags & FLAG_DEAD))
+		if (!ast_test_flag(trans, FLAG_DEAD))
 			precache_trans(trans, maps, mapcount, expiration, foundanswers);
 		trans = trans->next;
 	}
@@ -3222,7 +3221,7 @@ static int precache_transactions(struct dundi_request *dr, struct dundi_mapping
 	while(trans) {
 		transn = trans->next;
 		trans->thread = 0;
-		if (trans->flags & FLAG_DEAD) {
+		if (ast_test_flag(trans, FLAG_DEAD)) {
 			ast_log(LOG_DEBUG, "Our transaction went away!\n");
 			destroy_trans(trans, 0);
 		}
@@ -3395,7 +3394,7 @@ static void build_transactions(struct dundi_request *dr, int ttl, int order, int
 						if (!dundi_eid_cmp(avoid[x], &p->eid) || !dundi_eid_cmp(avoid[x], &p->us_eid)) {
 							/* If not a direct connection, it affects our answer */
 							if (directs && !directs[x]) 
-								dr->hmd->flags &= ~DUNDI_HINT_UNAFFECTED;
+								ast_clear_flag_nonstd(dr->hmd, DUNDI_HINT_UNAFFECTED);
 							break;
 						}
 					}
@@ -3575,7 +3574,7 @@ static int dundi_lookup_internal(struct dundi_result *result, int maxret, struct
 	   do this earlier because we didn't know if we were going to have transactions
 	   or not. */
 	if (!ttl) {
-		hmd->flags |= DUNDI_HINT_TTL_EXPIRED;
+		ast_set_flag_nonstd(hmd, DUNDI_HINT_TTL_EXPIRED);
 		abort_request(&dr);
 		unregister_request(&dr);
 		close(dr.pfds[0]);
@@ -3799,7 +3798,7 @@ static int dundi_query_eid_internal(struct dundi_entity_info *dei, const char *d
 	   do this earlier because we didn't know if we were going to have transactions
 	   or not. */
 	if (!ttl) {
-		hmd->flags |= DUNDI_HINT_TTL_EXPIRED;
+		ast_set_flag_nonstd(hmd, DUNDI_HINT_TTL_EXPIRED);
 		return 0;
 	}
 		
@@ -3863,7 +3862,7 @@ static int dundi_lookup_exec(struct ast_channel *chan, void *data)
 	if (results > 0) {
         sort_results(dr, results);
         for (x=0;x<results;x++) {
-			if (dr[x].flags & DUNDI_FLAG_EXISTS) {
+			if (ast_test_flag(dr + x, DUNDI_FLAG_EXISTS)) {
 				pbx_builtin_setvar_helper(chan, "DUNDTECH", dr[x].tech);
 				pbx_builtin_setvar_helper(chan, "DUNDDEST", dr[x].dest);
 				break;
@@ -4088,7 +4087,7 @@ static int do_register(void *data)
 		destroy_trans(peer->regtrans, 0);
 	peer->regtrans = create_transaction(peer);
 	if (peer->regtrans) {
-		peer->regtrans->flags |= FLAG_ISREG;
+		ast_set_flag(peer->regtrans, FLAG_ISREG);
 		memset(&ied, 0, sizeof(ied));
 		dundi_ie_append_short(&ied, DUNDI_IE_VERSION, DUNDI_DEFAULT_VERSION);
 		dundi_ie_append_eid(&ied, DUNDI_IE_EID, &peer->regtrans->us_eid);
@@ -4130,7 +4129,7 @@ static void qualify_peer(struct dundi_peer *peer, int schedonly)
 			peer->qualtrans = create_transaction(peer);
 		if (peer->qualtrans) {
 			gettimeofday(&peer->qualtx, NULL);
-			peer->qualtrans->flags |= FLAG_ISQUAL;
+			ast_set_flag(peer->qualtrans, FLAG_ISQUAL);
 			dundi_send(peer->qualtrans, DUNDI_COMMAND_NULL, 0, 1, NULL);
 		}
 	}
@@ -4346,7 +4345,7 @@ static int dundi_helper(struct ast_channel *chan, const char *context, const cha
 	}
 	res = dundi_lookup(results, MAX_RESULTS, chan, data, exten, 0);
 	for (x=0;x<res;x++) {
-		if (results[x].flags & flag)
+		if (ast_test_flag(results + x, flag))
 			found++;
 	}
 	if (found >= priority)
@@ -4399,7 +4398,7 @@ static int dundi_exec(struct ast_channel *chan, const char *context, const char
 	if (res > 0) {
 		sort_results(results, res);
 		for (x=0;x<res;x++) {
-			if (results[x].flags & DUNDI_FLAG_EXISTS) {
+			if (ast_test_flag(results + x, DUNDI_FLAG_EXISTS)) {
 				if (!--priority)
 					break;
 			}
diff --git a/res/res_features.c b/res/res_features.c
index 374199799b35f30a6aca49fa8dd1a5f22bc3c49b..431d786cc24ef7e4f6a8438d548c67099a63be6c 100755
--- a/res/res_features.c
+++ b/res/res_features.c
@@ -623,8 +623,8 @@ static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, st
 					return -1;
 				}
 				memset(&bconfig,0,sizeof(struct ast_bridge_config));
-				bconfig.features_caller |= AST_FEATURE_DISCONNECT;
-				bconfig.features_callee |= AST_FEATURE_DISCONNECT;
+				ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
+				ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
 				res = ast_bridge_call(transferer,newchan,&bconfig);
 				if (newchan->_softhangup || newchan->_state != AST_STATE_UP) {
 					ast_hangup(newchan);
@@ -671,7 +671,7 @@ static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, st
 					ast_channel_masquerade(xferchan, transferee);
 					ast_explicit_goto(xferchan, transferee->context, transferee->exten, transferee->priority);
 					xferchan->_state = AST_STATE_UP;
-					xferchan->flags = 0;
+					ast_clear_flag(xferchan, AST_FLAGS_ALL);	
 					xferchan->_softhangup = 0;
 
 					if ((f = ast_read(xferchan))) {
@@ -685,7 +685,7 @@ static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, st
 				}
 
 				newchan->_state = AST_STATE_UP;
-				newchan->flags = 0;
+				ast_clear_flag(newchan, AST_FLAGS_ALL);	
 				newchan->_softhangup = 0;
 
 				tobj = malloc(sizeof(struct ast_bridge_thread_obj));
@@ -789,16 +789,16 @@ static int remap_feature(const char *name, const char *value)
 static int ast_feature_interpret(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense)
 {
 	int x;
-	unsigned int features;
+	struct ast_flags features;
 	int res = FEATURE_RETURN_PASSDIGITS;
 
 	if (sense == FEATURE_SENSE_CHAN)
-		features = config->features_caller;
+		ast_copy_flags(&features, &(config->features_caller), AST_FLAGS_ALL);	
 	else
-		features = config->features_callee;
-	ast_log(LOG_DEBUG, "Feature interpret: chan=%s, peer=%s, sense=%d, features=%d\n", chan->name, peer->name, sense, features);
+		ast_copy_flags(&features, &(config->features_callee), AST_FLAGS_ALL);	
+	ast_log(LOG_DEBUG, "Feature interpret: chan=%s, peer=%s, sense=%d, features=%d\n", chan->name, peer->name, sense, features.flags);
 	for (x=0;x<FEATURES_COUNT;x++) {
-		if ((features & builtin_features[x].feature_mask) &&
+		if ((ast_test_flag(&features, builtin_features[x].feature_mask)) &&
 		    !ast_strlen_zero(builtin_features[x].exten)) {
 			/* Feature is up for consideration */
 			if (!strcmp(builtin_features[x].exten, code)) {
@@ -816,14 +816,14 @@ static int ast_feature_interpret(struct ast_channel *chan, struct ast_channel *p
 static void set_config_flags(struct ast_bridge_config *config)
 {
 	int x;
-	config->flags = 0;
+	ast_clear_flag(config, AST_FLAGS_ALL);	
 	for (x=0;x<FEATURES_COUNT;x++) {
-		if (config->features_caller & builtin_features[x].feature_mask) {
-			if (builtin_features[x].flags & AST_FEATURE_FLAG_NEEDSDTMF)
+		if (ast_test_flag(&(config->features_caller), builtin_features[x].feature_mask)) {
+			if (ast_test_flag(builtin_features + x, AST_FEATURE_FLAG_NEEDSDTMF))
 				ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
 		}
-		if (config->features_callee & builtin_features[x].feature_mask) {
-			if (builtin_features[x].flags & AST_FEATURE_FLAG_NEEDSDTMF)
+		if (ast_test_flag(&(config->features_callee), builtin_features[x].feature_mask)) {
+			if (ast_test_flag(builtin_features + x, AST_FEATURE_FLAG_NEEDSDTMF))
 				ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
 		}
 	}
@@ -866,10 +866,10 @@ int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast
 			pbx_exec(peer, monitor_app, monitor_exec, 1);
 	}
 	
-	allowdisconnect_in = (config->features_callee & AST_FEATURE_DISCONNECT);
-	allowdisconnect_out = (config->features_caller & AST_FEATURE_DISCONNECT);
-	allowredirect_in = (config->features_callee & AST_FEATURE_REDIRECT);
-	allowredirect_out = (config->features_caller & AST_FEATURE_REDIRECT);
+	allowdisconnect_in = ast_test_flag(&(config->features_callee), AST_FEATURE_DISCONNECT);
+	allowdisconnect_out = ast_test_flag(&(config->features_caller), AST_FEATURE_DISCONNECT);
+	allowredirect_in = ast_test_flag(&(config->features_callee), AST_FEATURE_REDIRECT);
+	allowredirect_out = ast_test_flag(&(config->features_caller), AST_FEATURE_REDIRECT);
 	set_config_flags(config);
 	config->firstpass = 1;
 
@@ -1030,8 +1030,8 @@ int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast
 					memcpy(&backup_config, config, sizeof(struct ast_bridge_config));
 					/* Setup temporary config options */
 					config->play_warning = 0;
-					config->features_caller &= ~(AST_FEATURE_PLAY_WARNING);
-					config->features_callee &= ~(AST_FEATURE_PLAY_WARNING);
+					ast_clear_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
+					ast_clear_flag(&(config->features_callee),AST_FEATURE_PLAY_WARNING);
 					config->warning_freq = 0;
 					config->warning_sound = NULL;
 					config->end_sound = NULL;
@@ -1301,8 +1301,8 @@ static int park_exec(struct ast_channel *chan, void *data)
 			ast_verbose(VERBOSE_PREFIX_3 "Channel %s connected to parked call %d\n", chan->name, park);
 
 		memset(&config,0,sizeof(struct ast_bridge_config));
-		config.features_callee |= AST_FEATURE_REDIRECT;
-		config.features_caller |= AST_FEATURE_REDIRECT;
+		ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
+		ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
 		config.timelimit = 0;
 		config.play_warning = 0;
 		config.warning_freq = 0;