@@ -506,6 +506,7 @@ impl IoHandler<Message> for Handler {
506506 is_inbound : true ,
507507 } => {
508508 let mut inbound_connections = self . inbound_connections . write ( ) ;
509+ let outbound_connections = self . outbound_connections . write ( ) ;
509510 let target = connection. peer_addr ( ) ;
510511 self . peer_db . insert ( * target) ;
511512 if let Some ( token) = self . inbound_tokens . lock ( ) . gen ( ) {
@@ -526,10 +527,23 @@ impl IoHandler<Message> for Handler {
526527 token
527528 ) ;
528529 }
529-
530- let t = inbound_connections. insert ( token, connection) ;
531- assert ! ( t. is_none( ) ) ;
532- io. register_stream ( token) ;
530+ let mut can_insert: bool = true ;
531+ let mut is_not_out: bool = true ;
532+ for ( _, in_connection) in inbound_connections. iter ( ) {
533+ if in_connection. peer_addr ( ) == connection. peer_addr ( ) {
534+ can_insert = false ;
535+ }
536+ }
537+ for ( _, out_connection) in outbound_connections. iter ( ) {
538+ if out_connection. peer_addr ( ) == connection. peer_addr ( ) {
539+ is_not_out = false ;
540+ }
541+ }
542+ if can_insert && is_not_out {
543+ let t = inbound_connections. insert ( token, connection) ;
544+ assert ! ( t. is_none( ) ) ;
545+ io. register_stream ( token) ;
546+ }
533547 } else {
534548 cwarn ! ( NETWORK , "Cannot establish an inbound connection" ) ;
535549 }
@@ -539,6 +553,7 @@ impl IoHandler<Message> for Handler {
539553 is_inbound : false ,
540554 } => {
541555 let mut outbound_connections = self . outbound_connections . write ( ) ;
556+ let inbound_connections = self . inbound_connections . write ( ) ;
542557 if let Some ( token) = self . outbound_tokens . lock ( ) . gen ( ) {
543558 let peer_addr = * connection. peer_addr ( ) ;
544559 let remote_node_id = peer_addr. into ( ) ;
@@ -570,9 +585,24 @@ impl IoHandler<Message> for Handler {
570585 network_message_size,
571586 ) ;
572587 }
573- let t = outbound_connections. insert ( token, connection) ;
574- assert ! ( t. is_none( ) ) ;
575- io. register_stream ( token) ;
588+ let mut can_insert: bool = true ;
589+ let mut is_not_in: bool = true ;
590+ for ( _, out_connection) in outbound_connections. iter ( ) {
591+ if out_connection. peer_addr ( ) == connection. peer_addr ( ) {
592+ can_insert = false ;
593+ }
594+ }
595+ for ( _, in_connection) in inbound_connections. iter ( ) {
596+ if in_connection. peer_addr ( ) == connection. peer_addr ( ) {
597+ is_not_in = false ;
598+ }
599+ }
600+
601+ if can_insert && is_not_in {
602+ let t = outbound_connections. insert ( token, connection) ;
603+ assert ! ( t. is_none( ) ) ;
604+ io. register_stream ( token) ;
605+ }
576606 } else {
577607 cwarn ! ( NETWORK , "Cannot establish an outbound connection" ) ;
578608 }
0 commit comments