[asterisk-commits] russell: branch russell/events r103437 - /team/russell/events/pbx/pbx_dundi.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Feb 12 10:36:50 CST 2008


Author: russell
Date: Tue Feb 12 10:36:49 2008
New Revision: 103437

URL: http://svn.digium.com/view/asterisk?view=rev&rev=103437
Log:
Check in some random code cleanup i had in my working copy

Modified:
    team/russell/events/pbx/pbx_dundi.c

Modified: team/russell/events/pbx/pbx_dundi.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/pbx/pbx_dundi.c?view=diff&rev=103437&r1=103436&r2=103437
==============================================================================
--- team/russell/events/pbx/pbx_dundi.c (original)
+++ team/russell/events/pbx/pbx_dundi.c Tue Feb 12 10:36:49 2008
@@ -79,16 +79,6 @@
 
 /*! Keep times of last 10 lookups */
 #define DUNDI_TIMING_HISTORY	10
-
-enum {
-	FLAG_ISREG =       (1 << 0),  /*!< Transaction is register request */
-	FLAG_DEAD =        (1 << 1),  /*!< Transaction is dead */
-	FLAG_FINAL =       (1 << 2),  /*!< Transaction has final message sent */
-	FLAG_ISQUAL =      (1 << 3),  /*!< Transaction is a qualification */
-	FLAG_ENCRYPT =     (1 << 4),  /*!< Transaction is encrypted wiht ECX/DCX */
-	FLAG_SENDFULLKEY = (1 << 5),  /*!< Send full key on transaction */
-	FLAG_STOREHIST =   (1 << 6),  /*!< Record historic performance */
-};
 
 #define DUNDI_FLAG_INTERNAL_NOPARTIAL (1 << 17)
 
@@ -182,7 +172,16 @@
 	dundi_eid them_eid;                            /*!< Their EID, to us */
 	ast_aes_encrypt_key ecx;                       /*!< AES 128 Encryption context */
 	ast_aes_decrypt_key dcx;                       /*!< AES 128 Decryption context */
-	unsigned int flags;                            /*!< Has final packet been sent */
+
+	/* flags */
+	unsigned int final:1;                          /*!< Has final packet been sent */
+	unsigned int isreg:1;                          /*!< Transaction is a register request */
+	unsigned int isqual:1;                         /*!< Is a qualification requestion */
+	unsigned int storehist:1;                      /*!< Store transaction history */
+	unsigned int sendfullkey:1;                    /*!< Send full key on transaction */
+	unsigned int encrypt:1;                        /*!< Transaction is encrypted wiht ECX/DCX */
+	unsigned int dead:1;                           /*!< Transaction is dead */
+
 	int ttl;                                       /*!< Remaining TTL for queries on this one */
 	int thread;                                    /*!< We have a calling thread */
 	int retranstimer;                              /*!< How long to wait before retransmissions */
@@ -197,6 +196,7 @@
 	AST_LIST_HEAD_NOLOCK(packetlist, dundi_packet) packets;  /*!< Packets to be retransmitted */
 	struct packetlist lasttrans;                   /*!< Last transmitted / ACK'd packet */
 	struct dundi_request *parent;                  /*!< Parent request (if there is one) */
+	struct pub_event_map *parent_pem;          /*!< Parent event mapping, if one exists */
 	AST_LIST_ENTRY(dundi_transaction) parentlist;  /*!< Next with respect to the parent */
 	AST_LIST_ENTRY(dundi_transaction) all;         /*!< Next with respect to all DUNDi transactions */
 };
@@ -324,6 +324,7 @@
 
 #define dundi_eid_to_str(a,b,c) ast_eid_to_str(a,b,c)
 #define dundi_str_to_eid(a,b) ast_str_to_eid(a,b)
+#define dundi_eid_cmp(a,b) ast_eid_cmp(a,b)
 
 static void dundi_debug_output(const char *data)
 {
@@ -495,7 +496,7 @@
 	trans->oiseqno = 0;
 	trans->oseqno = 0;
 	trans->aseqno = 0;
-	ast_clear_flag(trans, FLAG_FINAL);	
+	trans->final = 0;
 	return 0;
 }
 
@@ -674,7 +675,7 @@
 	/* Truncate if "don't ask" isn't present */
 	if (!ast_test_flag_nonstd(&hmd, DUNDI_HINT_DONT_ASK))
 		hmd.exten[0] = '\0';
-	if (ast_test_flag(st->trans, FLAG_DEAD)) {
+	if (st->trans->dead) {
 		ast_debug(1, "Our transaction went away!\n");
 		st->trans->thread = 0;
 		destroy_trans(st->trans, 0);
@@ -713,7 +714,7 @@
 	/* Truncate if "don't ask" isn't present */
 	if (!ast_test_flag_nonstd(&hmd, DUNDI_HINT_DONT_ASK))
 		hmd.exten[0] = '\0';
-	if (ast_test_flag(st->trans, FLAG_DEAD)) {
+	if (st->trans->dead) {
 		ast_debug(1, "Our transaction went away!\n");
 		st->trans->thread = 0;
 		destroy_trans(st->trans, 0);
@@ -758,7 +759,7 @@
 		res = dundi_query_eid_internal(&dei, st->called_context, &st->reqeid, &hmd, st->ttl, 1, st->eids);
 	}
 	AST_LIST_LOCK(&peers);
-	if (ast_test_flag(st->trans, FLAG_DEAD)) {
+	if (st->trans->dead) {
 		ast_debug(1, "Our transaction went away!\n");
 		st->trans->thread = 0;
 		destroy_trans(st->trans, 0);
@@ -1276,7 +1277,7 @@
 	trans->them_eid = p->eid;
 	/* Enable encryption if appropriate */
 	if (!ast_strlen_zero(p->inkey))
-		ast_set_flag(trans, FLAG_ENCRYPT);	
+		trans->encrypt = 1;
 	if (p->maxms) {
 		trans->autokilltimeout = p->maxms;
 		trans->retranstimer = DUNDI_DEFAULT_RETRANS_TIMER;
@@ -1429,10 +1430,10 @@
 			if (update_key(peer))
 				return -1;
 			if (!peer->sentfullkey)
-				ast_set_flag(trans, FLAG_SENDFULLKEY);	
+				trans->sendfullkey = 1;
 			/* Append key data */
 			dundi_ie_append_eid(&ied, DUNDI_IE_EID, &trans->us_eid);
-			if (ast_test_flag(trans, FLAG_SENDFULLKEY)) {
+			if (trans->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 {
@@ -1702,9 +1703,9 @@
 		if (ies.cause < 1) {
 			/* Success of some sort */
 			ast_debug(1, "Looks like success of some sort (%d), %d answers\n", ies.cause, ies.anscount);
-			if (ast_test_flag(trans, FLAG_ENCRYPT)) {
+			if (trans->encrypt)
 				authpass = encrypted;
-			} else 
+			else 
 				authpass = 1;
 			if (authpass) {
 				/* Pass back up answers */
@@ -1786,9 +1787,9 @@
 		if (ies.cause < 1) {
 			/* Success of some sort */
 			ast_debug(1, "Looks like success of some sort (%d)\n", ies.cause);
-			if (ast_test_flag(trans, FLAG_ENCRYPT)) {
+			if (trans->encrypt)
 				authpass = encrypted;
-			} else 
+			else 
 				authpass = 1;
 			if (authpass) {
 				/* Pass back up answers */
@@ -1839,9 +1840,9 @@
 		if (ies.cause < 1) {
 			int hasauth;
 			/* Success of some sort */
-			if (ast_test_flag(trans, FLAG_ENCRYPT)) {
+			if (trans->encrypt)
 				hasauth = encrypted;
-			} else 
+			else 
 				hasauth = 1;
 			
 			if (!hasauth) {
@@ -1872,13 +1873,13 @@
 			dundi_send(trans, DUNDI_COMMAND_CANCEL, 0, 1, NULL);
 		break;
 	case DUNDI_COMMAND_ENCREJ:
-		if ((ast_test_flag(trans, FLAG_SENDFULLKEY)) || AST_LIST_EMPTY(&trans->lasttrans) || !(peer = find_peer(&trans->them_eid))) {
+		if (trans->sendfullkey || AST_LIST_EMPTY(&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 */
-			ast_set_flag(trans, FLAG_SENDFULLKEY);
+			trans->sendfullkey = 1;
 			if (final) {
 				/* Ooops, we got a final message, start by sending ACK... */
 				dundi_ack(trans, hdr->cmdresp & 0x80);
@@ -1920,7 +1921,7 @@
 				trans->ecx = peer->them_ecx;
 				trans->dcx = peer->them_dcx;
 			}
-			if (ast_test_flag(trans, FLAG_ENCRYPT) && ies.encblock && ies.enclen) {
+			if (trans->encrypt && ies.encblock && ies.enclen) {
 				struct dundi_hdr *dhdr;
 				unsigned char decoded[MAX_PACKET_SIZE];
 				int ddatalen;
@@ -1941,9 +1942,12 @@
 		}
 		if (!final) {
 			/* Turn off encryption */
-			ast_clear_flag(trans, FLAG_ENCRYPT);
+			trans->encrypt = 0;
 			dundi_send(trans, DUNDI_COMMAND_ENCREJ, 0, 1, NULL);
 		}
+		break;
+	case DUNDI_COMMAND_EVENT:
+		/*! \todo XXX ... */
 		break;
 	default:
 		/* Send unknown command if we don't know it, with final flag IFF it's the
@@ -2000,7 +2004,7 @@
 	/* 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) && ast_test_flag(trans, FLAG_FINAL)) {
+		if (ack_trans(trans, h->iseqno) && trans->final) {
 			/* If final, we're done */
 			destroy_trans(trans, 0);
 			return 0;
@@ -2215,8 +2219,12 @@
 		return -1;
 
 	/* Make sure this peer is included in the specified DUNDi context */
-	if (!has_permission(&peer->include, pub_event_map->context))
+	ao2_lock(pub_event_map);
+	if (!has_permission(&peer->include, pub_event_map->context)) {
+		ao2_unlock(pub_event_map);
 		return -1;
+	}
+	ao2_unlock(pub_event_map);
 
 	/* Not registered ... */
 	if (!peer->addr.sin_addr.s_addr)
@@ -2239,14 +2247,15 @@
 	struct dundi_peer *peer;
 	dundi_eid avoid_eids[DUNDI_MAX_STACK];
 	int cur_avoid_eid = 0;
+	AST_LIST_HEAD_NOLOCK(, dundi_transaction) transactions;
+	struct dundi_transaction *trans;
+
+	AST_LIST_HEAD_INIT_NOLOCK(&transactions);
 
 	memset(avoid_eids, 0, sizeof(avoid_eids));
 
 	AST_LIST_LOCK(&peers);
 	AST_LIST_TRAVERSE(&peers, peer, list) {
-		struct dundi_transaction *trans;
-		int i;
-
 		/* Make sure we can send an event to this peer for this event mapping */
 		if (check_send_event(peer, pub_event_map, 
 			avoid_eids, ARRAY_LEN(avoid_eids), cur_avoid_eid))
@@ -2266,20 +2275,63 @@
 		if (!trans)
 			continue;
 
+		/* Keep the transactions in a temporary list, as we have some more
+		 * processing to do on them after this loop */
+		AST_LIST_INSERT_TAIL(&transactions, trans, parentlist);
+	}
+	AST_LIST_UNLOCK(&peers);
+
+	AST_LIST_TRAVERSE(&transactions, trans, parentlist) {
+		int i;
+
+		/* Record the list of peer EIDs that we are sending the event to so that
+		 * they will be avoided as the event gets forwarded on */
 		for (i = 0; i < cur_avoid_eid; i++)
 			trans->eids[i] = avoid_eids[i];
 		trans->eidcount = cur_avoid_eid;
 
-		AST_LIST_INSERT_TAIL(&pub_event_map->transactions, trans, parentlist);
-	}
-	AST_LIST_UNLOCK(&peers);
+		trans->parent_pem = pub_event_map;
+	}
+
+	ao2_lock(pub_event_map);
+	AST_LIST_APPEND_LIST(&pub_event_map->transactions, &transactions, parentlist);
+	ao2_unlock(pub_event_map);
+}
+
+static void start_event_transactions(struct pending_event *pending_event)
+{
+	struct dundi_ie_data ied = {
+		.pos = 0,
+	};
+	struct dundi_transaction *trans;
+	struct pub_event_map *pub_event_map = pending_event->pub_event_map;
+	size_t event_len;
+
+	dundi_ie_append(&ied, DUNDI_IE_EVENTMARKER);
+
+	event_len = ast_event_get_size(pending_event->event);
+	if (event_len > ((int) sizeof(ied.buf) - ied.pos)) {
+		ast_log(LOG_ERROR, "Not enough space for event to fit in IE buffer, need %d\n", 
+			(int) event_len);
+		return;
+	}
+	memcpy(ied.buf + ied.pos, pending_event->event, event_len);
+	ied.pos += event_len;
+
+	ao2_lock(pub_event_map);
+	AST_LIST_TRAVERSE(&pub_event_map->transactions, trans, parentlist) {
+		/* Don't send the frame if we already did it ... */
+		if (!trans->final)
+			dundi_send(trans, DUNDI_COMMAND_EVENT, 0, 1, &ied);
+	}
+	ao2_unlock(pub_event_map);
 }
 
 static void handle_pending_event(struct pending_event *pending_event)
 {
 	build_event_transactions(pending_event->pub_event_map);
 
-	/* XXX \todo  ... */
+	start_event_transactions(pending_event);
 }
 
 static struct pending_event *destroy_pending_event(struct pending_event *pending_event)
@@ -3105,7 +3157,7 @@
 
 	if (global_storehistory) {
 		trans->start = ast_tvnow();
-		ast_set_flag(trans, FLAG_STOREHIST);
+		trans->storehist = 1;
 	}
 
 	trans->retranstimer = DUNDI_DEFAULT_RETRANS_TIMER;
@@ -3115,7 +3167,7 @@
 	if (p) {
 		apply_peer(trans, p);
 		if (!p->sentfullkey)
-			ast_set_flag(trans, FLAG_SENDFULLKEY);
+			trans->sendfullkey = 1;
 	}
 
 	trans->strans = tid;
@@ -3159,7 +3211,7 @@
 	int x;
 	int cnt;
 	char eid_str[20];
-	if (ast_test_flag(trans, FLAG_ISREG | FLAG_ISQUAL | FLAG_STOREHIST)) {
+	if (trans->storehist || trans->isqual || trans->isreg) {
 		AST_LIST_TRAVERSE(&peers, peer, list) {
 			if (peer->regtrans == trans)
 				peer->regtrans = NULL;
@@ -3182,7 +3234,7 @@
 				}
 				peer->qualtrans = NULL;
 			}
-			if (ast_test_flag(trans, FLAG_STOREHIST)) {
+			if (trans->storehist) {
 				if (trans->parent && !ast_strlen_zero(trans->parent->number)) {
 					if (!dundi_eid_cmp(&trans->them_eid, &peer->eid)) {
 						peer->avgms = 0;
@@ -3211,6 +3263,7 @@
 			}
 		}
 	}
+
 	if (trans->parent) {
 		/* Unlink from parent if appropriate */
 		AST_LIST_REMOVE(&trans->parent->trans, trans, parentlist);
@@ -3221,6 +3274,13 @@
 			}
 		}
 	}
+
+	if (trans->parent_pem) {
+		ao2_lock(trans->parent_pem);
+		AST_LIST_REMOVE(&trans->parent_pem->transactions, trans, parentlist);
+		ao2_unlock(trans->parent_pem);
+	}
+
 	/* Unlink from all trans */
 	AST_LIST_REMOVE(&alltrans, trans, all);
 	destroy_packets(&trans->packets);
@@ -3228,7 +3288,7 @@
 	AST_SCHED_DEL(sched, trans->autokillid);
 	if (trans->thread) {
 		/* If used by a thread, mark as dead and be done */
-		ast_set_flag(trans, FLAG_DEAD);
+		trans->dead = 1;
 	} else
 		ast_free(trans);
 }
@@ -3240,10 +3300,11 @@
 	AST_LIST_LOCK(&peers);
 	if (pack->retrans < 1) {
 		pack->retransid = -1;
-		if (!ast_test_flag(pack->parent, FLAG_ISQUAL))
+		if (!pack->parent->isqual) {
 			ast_log(LOG_NOTICE, "Max retries exceeded to host '%s:%d' msg %d on call %d\n", 
 				ast_inet_ntoa(pack->parent->addr.sin_addr), 
 				ntohs(pack->parent->addr.sin_port), pack->h->oseqno, ntohs(pack->h->strans));
+		}
 		destroy_trans(pack->parent, 1);
 		res = 0;
 	} else {
@@ -3264,7 +3325,7 @@
 	char eid_str[20];
 	len = sizeof(struct dundi_packet) + sizeof(struct dundi_hdr) + (ied ? ied->pos : 0);
 	/* Reserve enough space for encryption */
-	if (ast_test_flag(trans, FLAG_ENCRYPT))
+	if (trans->encrypt)
 		len += 384;
 	pack = ast_calloc(1, len);
 	if (pack) {
@@ -3287,7 +3348,7 @@
 		} 
 		if (final) {
 			pack->h->cmdresp |= DUNDI_COMMAND_FINAL;
-			ast_set_flag(trans, FLAG_FINAL);
+			trans->final = 1;
 		}
 		pack->h->cmdflags = flags;
 		if (cmdresp != DUNDI_COMMAND_ACK) {
@@ -3296,7 +3357,7 @@
 		}
 		trans->aseqno = trans->iseqno;
 		/* If we have their public key, encrypt */
-		if (ast_test_flag(trans, FLAG_ENCRYPT)) {
+		if (trans->encrypt) {
 			switch(cmdresp) {
 			case DUNDI_COMMAND_REGREQ:
 			case DUNDI_COMMAND_REGRESPONSE:
@@ -3497,7 +3558,7 @@
 	AST_LIST_UNLOCK(&peers);
 
 	AST_LIST_TRAVERSE(&dr->trans, trans, parentlist) {
-		if (!ast_test_flag(trans, FLAG_DEAD))
+		if (!trans->dead)
 			precache_trans(trans, maps, mapcount, expiration, foundanswers);
 	}
 
@@ -3505,7 +3566,7 @@
 	AST_LIST_LOCK(&peers);
 	AST_LIST_TRAVERSE_SAFE_BEGIN(&dr->trans, trans, parentlist) {
 		trans->thread = 0;
-		if (ast_test_flag(trans, FLAG_DEAD)) {
+		if (trans->dead) {
 			ast_debug(1, "Our transaction went away!\n");
 			/* This is going to remove the transaction from the dundi_request's list, as well
 			 * as the global transactions list */
@@ -4516,7 +4577,7 @@
 		destroy_trans(peer->regtrans, 0);
 	peer->regtrans = create_transaction(peer);
 	if (peer->regtrans) {
-		ast_set_flag(peer->regtrans, FLAG_ISREG);
+		peer->regtrans->isreg = 1;
 		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);
@@ -4555,7 +4616,7 @@
 			peer->qualtrans = create_transaction(peer);
 		if (peer->qualtrans) {
 			peer->qualtx = ast_tvnow();
-			ast_set_flag(peer->qualtrans, FLAG_ISQUAL);
+			peer->qualtrans->isqual = 1;
 			dundi_send(peer->qualtrans, DUNDI_COMMAND_NULL, 0, 1, NULL);
 		}
 	}




More information about the asterisk-commits mailing list