@@ -361,6 +361,7 @@ void Binding::DoReset(State newState)
361361 if (GetFlag (kFlag_KeyReserved ))
362362 {
363363 sm->ReleaseKey (mPeerNodeId , mKeyId );
364+ ClearFlag (kFlag_KeyReserved );
364365 }
365366
366367#if WEAVE_CONFIG_ENABLE_DNS_RESOLVER
@@ -379,16 +380,26 @@ void Binding::DoReset(State newState)
379380 {
380381 mCon ->OnConnectionComplete = NULL ;
381382 mCon ->Release ();
383+ ClearFlag (kFlag_ConnectionReferenced );
382384 }
385+ mCon = NULL ;
383386
384387 // If a session establishment was in progress, cancel it.
385388 if (origState == kState_PreparingSecurity_EstablishSession )
386389 {
387390 sm->CancelSessionEstablishment (this );
388391 }
389392
390- // Reset the configuration state of the binding.
391- ResetConfig ();
393+ // Reset the configuration state of the binding, except when entering the Failed state.
394+ //
395+ // We leave the configuration state of the binding intact in the Failed state so that
396+ // applications can inspected it during failure handling. If the application decides
397+ // to re-prepare the bind, the configuration state will be reset when binding enters
398+ // the Configuring state.
399+ if (newState != kState_Failed )
400+ {
401+ ResetConfig ();
402+ }
392403
393404 // Advance to the new state.
394405 mState = newState;
@@ -1195,8 +1206,15 @@ WEAVE_ERROR Binding::AdjustResponseTimeout(nl::Weave::ExchangeContext *apExchang
11951206/* *
11961207 * Determine if a particular incoming message is from the configured peer and is suitably authenticated.
11971208 *
1198- * This method confirms that the message in question originated from the peer node of the binding and
1199- * that the encryption key and type used to encrypt the message matches those configured in the binding.
1209+ * This method confirms the following details about the given message:
1210+ *
1211+ * - The message originated from the peer node of the binding
1212+ *
1213+ * - The message was received over the same transport type as the binding. If the message was
1214+ * received over a connection, the method also confirms that the message was received over the
1215+ * exact connection associated with the binding.
1216+ *
1217+ * - The encryption key and type used to encrypt the message matches those configured in the binding.
12001218 * For bindings configured without the use of security, the method confirms that the incoming message is
12011219 * NOT encrypted.
12021220 *
@@ -1205,7 +1223,7 @@ WEAVE_ERROR Binding::AdjustResponseTimeout(nl::Weave::ExchangeContext *apExchang
12051223 * the method allows the local node to confirm that the incoming unsolicited message was sent by the
12061224 * associated peer. (Of course, for Bindings configured without the use of message encryption, this
12071225 * assertion provides no value from a security perspective. It merely confirms that the sender node
1208- * id in the received message matches the peer's node id .)
1226+ * id and transport types match .)
12091227 *
12101228 * Note that if the binding is not in the Ready state, this method will always return false.
12111229 *
@@ -1221,6 +1239,17 @@ bool Binding::IsAuthenticMessageFromPeer(const nl::Weave::WeaveMessageHeader *ms
12211239 if (msgInfo->SourceNodeId != mPeerNodeId )
12221240 return false ;
12231241
1242+ if (msgInfo->InCon != NULL )
1243+ {
1244+ if ((mTransportOption != kTransport_TCP && mTransportOption != kTransport_ExistingConnection ) || msgInfo->InCon != mCon )
1245+ return false ;
1246+ }
1247+ else
1248+ {
1249+ if (mTransportOption != kTransport_UDP && mTransportOption != kTransport_UDP_WRM )
1250+ return false ;
1251+ }
1252+
12241253 if (msgInfo->EncryptionType != mEncType )
12251254 return false ;
12261255
@@ -1429,6 +1458,11 @@ Binding::Configuration::Configuration(Binding& aBinding)
14291458{
14301459 if (mBinding .CanBePrepared ())
14311460 {
1461+ if (mBinding .mState != kState_NotConfigured )
1462+ {
1463+ mBinding .ResetConfig ();
1464+ }
1465+
14321466 mBinding .mState = kState_Configuring ;
14331467 mError = WEAVE_NO_ERROR;
14341468
@@ -1652,9 +1686,13 @@ Binding::Configuration& Binding::Configuration::Transport_DefaultWRMPConfig(cons
16521686/* *
16531687 * Use an existing Weave connection to communicate with the peer.
16541688 *
1689+ * NOTE: The reference count on the connection object is incremented when binding
1690+ * preparation succeeds. Thus the application is responsible for ensuring the
1691+ * connection object remain alive until that time.
1692+ *
16551693 * @param[in] con A pointer to the existing Weave connection.
16561694 *
1657- * @return A reference to the binding object.
1695+ * @return A reference to the binding object.
16581696 */
16591697Binding::Configuration& Binding::Configuration::Transport_ExistingConnection (WeaveConnection *con)
16601698{
@@ -1895,51 +1933,51 @@ Binding::Configuration& Binding::Configuration::Security_AuthenticationMode(Weav
18951933/* *
18961934 * Configure the binding to allow communication with the sender of a received message.
18971935 *
1898- * @param[in] apMsgHeader Message information structure associated with the received message.
1899- * @param[in] apConnection The connection over which the message was received; or NULL if the message
1900- * was not received via a connection.
1901- * @param[in] apPktInfo Packet information for the received message.
1936+ * @param[in] aMsgInfo Message information structure associated with the received message.
1937+ * @param[in] aPacketInfo Packet information for the received message.
19021938 *
19031939 */
19041940Binding::Configuration& Binding::Configuration::ConfigureFromMessage (
1905- const nl::Weave::WeaveMessageHeader *apMsgHeader,
1906- const nl::Inet::IPPacketInfo *apPktInfo,
1907- WeaveConnection *apConnection)
1941+ const nl::Weave::WeaveMessageInfo *aMsgInfo,
1942+ const nl::Inet::IPPacketInfo *aPacketInfo)
19081943{
1909- mBinding .mPeerNodeId = apMsgHeader ->SourceNodeId ;
1944+ mBinding .mPeerNodeId = aMsgInfo ->SourceNodeId ;
19101945
1911- // Configure the outgoing interface only if the received message is from a
1912- // link-local address because we need to specify the interface when we are
1913- // sending to a link local address. Otherwise, defer to the routing logic
1914- // to choose the outgoing interface.
1915- TargetAddress_IP (apPktInfo->SrcAddress , apPktInfo->SrcPort ,
1916- apPktInfo->SrcAddress .IsIPv6LinkLocal () ? apPktInfo->Interface : INET_NULL_INTERFACEID);
1917-
1918- if (apConnection != NULL )
1946+ if (aMsgInfo->InCon != NULL )
19191947 {
1920- Transport_ExistingConnection (apConnection );
1948+ Transport_ExistingConnection (aMsgInfo-> InCon );
19211949 }
1922- else if (apMsgHeader-> Flags & kWeaveMessageFlag_PeerRequestedAck )
1950+ else
19231951 {
1952+ if (aMsgInfo->Flags & kWeaveMessageFlag_PeerRequestedAck )
1953+ {
19241954#if WEAVE_CONFIG_ENABLE_RELIABLE_MESSAGING
1925- Transport_UDP_WRM ();
1955+ Transport_UDP_WRM ();
19261956#else
1927- mError = WEAVE_ERROR_NOT_IMPLEMENTED;
1957+ mError = WEAVE_ERROR_NOT_IMPLEMENTED;
19281958#endif // #if WEAVE_CONFIG_ENABLE_RELIABLE_MESSAGING
1929- }
1930- else
1931- {
1932- Transport_UDP ();
1959+ }
1960+ else
1961+ {
1962+ Transport_UDP ();
1963+ }
1964+
1965+ // Configure the outgoing interface only if the received message is from a
1966+ // link-local address because we need to specify the interface when we are
1967+ // sending to a link local address. Otherwise, defer to the routing logic
1968+ // to choose the outgoing interface.
1969+ TargetAddress_IP (aPacketInfo->SrcAddress , aPacketInfo->SrcPort ,
1970+ aPacketInfo->SrcAddress .IsIPv6LinkLocal () ? aPacketInfo->Interface : INET_NULL_INTERFACEID);
19331971 }
19341972
1935- if (apMsgHeader ->KeyId == WeaveKeyId::kNone )
1973+ if (aMsgInfo ->KeyId == WeaveKeyId::kNone )
19361974 {
19371975 Security_None ();
19381976 }
19391977 else
19401978 {
1941- Security_Key (apMsgHeader ->KeyId );
1942- Security_EncryptionType (apMsgHeader ->EncryptionType );
1979+ Security_Key (aMsgInfo ->KeyId );
1980+ Security_EncryptionType (aMsgInfo ->EncryptionType );
19431981 }
19441982
19451983 return *this ;
0 commit comments