diff --git a/src/org/jgroups/blocks/TCPConnectionMap.java b/src/org/jgroups/blocks/TCPConnectionMap.java index b282fc1..fdf5454 100644 --- a/src/org/jgroups/blocks/TCPConnectionMap.java +++ b/src/org/jgroups/blocks/TCPConnectionMap.java @@ -365,14 +365,24 @@ public class TCPConnectionMap { TCPConnection(Address peer_addr) throws Exception { if(peer_addr == null) throw new IllegalArgumentException("Invalid parameter peer_addr="+ peer_addr); - SocketAddress destAddr=new InetSocketAddress(((IpAddress)peer_addr).getIpAddress(),((IpAddress)peer_addr).getPort()); + InetSocketAddress destAddr=new InetSocketAddress(((IpAddress)peer_addr).getIpAddress(),((IpAddress)peer_addr).getPort()); this.sock=socket_factory.createSocket("jgroups.tcp.sock"); try { if (!defer_client_bind_addr) this.sock.bind(new InetSocketAddress(client_bind_addr, client_bind_port)); - if (this.sock.getLocalSocketAddress() != null && this.sock.getLocalSocketAddress().equals(destAddr)) + if (this.sock.getLocalAddress() != null && this.sock.getLocalAddress().equals(destAddr)) throw new IllegalStateException("socket's bind and connect address are the same: " + destAddr); + if (checkInetAddress(destAddr.getAddress())) { + Exception e = new IllegalStateException("socket's bind and connect address are the same, but JGRP-1530 did not catch it: destAddr = " + destAddr); + log.fatal(e.getMessage(), e); + throw e; + } Util.connect(this.sock, destAddr, sock_conn_timeout); + if (checkInetAddress(this.sock.getInetAddress())) { + Exception e = new IllegalStateException("socket's bind and connect address are the same, but JGRP-1530 did not catch it: destAddr = " + destAddr + ", sock.remoteSocketAddress = " + this.sock.getRemoteSocketAddr + log.fatal(e.getMessage(), e); + throw e; + } } catch(Exception t) { socket_factory.close(this.sock); @@ -435,6 +445,32 @@ public class TCPConnectionMap { return sb.toString(); } + protected boolean checkInetAddress(InetAddress remoteAddr) { + if (this.sock != null) { + return false; + } + + InetAddress localAddr = this.sock.getLocalAddress(); + + if ((localAddr == null) || (remoteAddr == null)) { + return false; + } + + String localAddress = localAddr.getHostAddress(); + int localPort = sock.getLocalPort(); + + String remoteAddress = remoteAddr.getHostAddress(); + int remotePort = sock.getPort(); + + if ((localAddress.equals(remoteAddress)) && + (localPort == remotePort)) { + + return true; + } + + return false; + } +