Index: contrib/debian/changelog
===================================================================
--- contrib/debian/changelog	(revision 1297)
+++ contrib/debian/changelog	(revision 1299)
@@ -4,4 +4,6 @@
   * New contrib: Gx extension.
   * rt_redirect.fdx and rt_load_balance.fdx improvement: use a hash table. Thanks Thomas.
+  * New hook HOOK_MESSAGE_SENDING to give a last chance to edit messages before they are sent.
+    This will be mostly used for enforcing interoperability constraints on non-compliant peers.
   * Simplified log output. Old output can be recovered with DEBUG_WITH_META build option.
   * Updated the internal counters for finer control on the load.
Index: extensions/dbg_msg_dumps/dbg_msg_dumps.c
===================================================================
--- extensions/dbg_msg_dumps/dbg_msg_dumps.c	(revision 1247)
+++ extensions/dbg_msg_dumps/dbg_msg_dumps.c	(revision 1299)
@@ -119,4 +119,8 @@
 		LOG_SPLIT(FD_LOG_NOTICE, "     ", buf, NULL);
 		break;
+	case HOOK_MESSAGE_SENDING:
+		LOG_N("SNDING to '%s':", peer_name);
+		LOG_SPLIT(FD_LOG_NOTICE, "     ", buf, NULL);
+		break;
 	case HOOK_MESSAGE_SENT:
 		LOG_N("SND to '%s':", peer_name);
@@ -205,4 +209,7 @@
 		LOG_N("RCV from '%s': %s", peer_name, buf);
 		break;
+	case HOOK_MESSAGE_SENDING:
+		LOG_N("SNDING to '%s': %s", peer_name, buf);
+		break;
 	case HOOK_MESSAGE_SENT:
 		LOG_N("SND to '%s': %s", peer_name, buf);
@@ -284,4 +291,7 @@
 		LOG_N("RCV from '%s': %s", peer_name, buf);
 		break;
+	case HOOK_MESSAGE_SENDING:
+		LOG_N("SNDING to '%s': %s", peer_name, buf);
+		break;
 	case HOOK_MESSAGE_SENT:
 		LOG_N("SND to '%s': %s", peer_name, buf);
@@ -342,5 +352,5 @@
 	
 	mask_errors = HOOK_MASK( HOOK_MESSAGE_FAILOVER, HOOK_MESSAGE_PARSING_ERROR, HOOK_MESSAGE_ROUTING_ERROR, HOOK_MESSAGE_DROPPED  );
-	mask_sndrcv = HOOK_MASK( HOOK_MESSAGE_RECEIVED, HOOK_MESSAGE_SENT );
+	mask_sndrcv = HOOK_MASK( HOOK_MESSAGE_RECEIVED, HOOK_MESSAGE_SENT ); /* We don t access SENDING hook here */
 	mask_routing= HOOK_MASK( HOOK_MESSAGE_LOCAL, HOOK_MESSAGE_ROUTING_FORWARD, HOOK_MESSAGE_ROUTING_LOCAL );
 	mask_peers  = HOOK_MASK( HOOK_PEER_CONNECT_FAILED, HOOK_PEER_CONNECT_SUCCESS );
Index: extensions/test_app/ta_cli.c
===================================================================
--- extensions/test_app/ta_cli.c	(revision 1214)
+++ extensions/test_app/ta_cli.c	(revision 1299)
@@ -71,5 +71,4 @@
 		
 		CHECK_FCT_DO( fd_sess_state_retrieve( ta_cli_reg, sess, &mi ), return );
-		TRACE_DEBUG( INFO, "%p %p", mi, data);
 		ASSERT( (void *)mi == data );
 	}
Index: include/freeDiameter/libfdcore.h
===================================================================
--- include/freeDiameter/libfdcore.h	(revision 1220)
+++ include/freeDiameter/libfdcore.h	(revision 1299)
@@ -965,4 +965,14 @@
 		 */
 	
+	HOOK_MESSAGE_SENDING,
+		/* Hook called when a message is about to be sent to a peer. The message pointer cannot be modified here, but the content of the message
+		  could still be changed (for example add or remove some AVP. This is the last chance.
+		 - {msg} points to the message. Some objects may not have been dictionary resolved. If you
+		   try to call fd_msg_parse_dict, it will slow down the operation of the instance.
+		 - {peer} is the one the message is being sent to.
+		 - {other} is NULL.
+		 - {permsgdata} points to existing structure if any, or a new structure otherwise. 
+		 */
+	
 	HOOK_MESSAGE_SENT,
 		/* Hook called when a message has been sent to a peer. The message might be freed as soon as the hook function returns,
Index: libfdcore/hooks.c
===================================================================
--- libfdcore/hooks.c	(revision 1245)
+++ libfdcore/hooks.c	(revision 1299)
@@ -345,4 +345,9 @@
 			}
 			
+			case HOOK_MESSAGE_SENDING: {
+				LOG_A("SENDING message to '%s'", peer ? peer->p_hdr.info.pi_diamid : "<unknown>");
+				break;
+			}
+			
 			case HOOK_MESSAGE_SENT: {
 				CHECK_MALLOC_DO(fd_msg_dump_summary(&hook_default_buf, &hook_default_len, NULL, msg, NULL, 0, 1), break);
Index: libfdcore/p_out.c
===================================================================
--- libfdcore/p_out.c	(revision 1238)
+++ libfdcore/p_out.c	(revision 1299)
@@ -166,4 +166,6 @@
 	TRACE_ENTRY("%p %p %p", msg, cnx, peer);
 	CHECK_PARAMS( msg && *msg && (cnx || (peer && peer->p_cnxctx)));
+
+	fd_hook_call(HOOK_MESSAGE_SENDING, *msg, peer, NULL, fd_msg_pmdl_get(*msg));
 	
 	if (update_reqin_cnt && peer) {
