mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 07:09:56 +01:00
Try to close sockets when an exception is caught.
This commit is contained in:
@@ -42,13 +42,22 @@ public class LanSocketPlugin extends SimpleSocketPlugin {
|
|||||||
// Calculate the group address and port from the invitation code
|
// Calculate the group address and port from the invitation code
|
||||||
InetSocketAddress mcast = convertInvitationCodeToMulticastGroup(code);
|
InetSocketAddress mcast = convertInvitationCodeToMulticastGroup(code);
|
||||||
// Bind a multicast socket for receiving packets
|
// Bind a multicast socket for receiving packets
|
||||||
MulticastSocket ms;
|
MulticastSocket ms = null;
|
||||||
try {
|
try {
|
||||||
ms = new MulticastSocket(mcast.getPort());
|
ms = new MulticastSocket(mcast.getPort());
|
||||||
ms.setInterface(chooseInterface());
|
ms.setInterface(chooseInterface());
|
||||||
ms.joinGroup(mcast.getAddress());
|
ms.joinGroup(mcast.getAddress());
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||||
|
if(ms != null) {
|
||||||
|
try {
|
||||||
|
ms.leaveGroup(mcast.getAddress());
|
||||||
|
} catch(IOException e1) {
|
||||||
|
if(LOG.isLoggable(Level.WARNING))
|
||||||
|
LOG.warning(e1.getMessage());
|
||||||
|
}
|
||||||
|
ms.close();
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// Listen until a valid packet is received or the timeout occurs
|
// Listen until a valid packet is received or the timeout occurs
|
||||||
@@ -69,7 +78,6 @@ public class LanSocketPlugin extends SimpleSocketPlugin {
|
|||||||
try {
|
try {
|
||||||
// Connect back on the advertised TCP port
|
// Connect back on the advertised TCP port
|
||||||
Socket s = new Socket(packet.getAddress(), port);
|
Socket s = new Socket(packet.getAddress(), port);
|
||||||
// Close the multicast socket
|
|
||||||
ms.leaveGroup(mcast.getAddress());
|
ms.leaveGroup(mcast.getAddress());
|
||||||
ms.close();
|
ms.close();
|
||||||
return new SocketTransportConnection(s);
|
return new SocketTransportConnection(s);
|
||||||
@@ -83,8 +91,16 @@ public class LanSocketPlugin extends SimpleSocketPlugin {
|
|||||||
}
|
}
|
||||||
now = System.currentTimeMillis();
|
now = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
if(LOG.isLoggable(Level.INFO))
|
||||||
|
LOG.info("Timeout while sending invitation");
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||||
|
try {
|
||||||
|
ms.leaveGroup(mcast.getAddress());
|
||||||
|
} catch(IOException e1) {
|
||||||
|
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e1.getMessage());
|
||||||
|
}
|
||||||
|
ms.close();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -125,8 +141,11 @@ public class LanSocketPlugin extends SimpleSocketPlugin {
|
|||||||
if(iface.supportsMulticast()) {
|
if(iface.supportsMulticast()) {
|
||||||
Enumeration<InetAddress> addrs = iface.getInetAddresses();
|
Enumeration<InetAddress> addrs = iface.getInetAddresses();
|
||||||
for(InetAddress addr : Collections.list(addrs)) {
|
for(InetAddress addr : Collections.list(addrs)) {
|
||||||
if(addr.isLinkLocalAddress() || addr.isSiteLocalAddress())
|
if(addr.isLinkLocalAddress() || addr.isSiteLocalAddress()) {
|
||||||
|
if(LOG.isLoggable(Level.INFO))
|
||||||
|
LOG.info("Binding to " + addr.getHostAddress());
|
||||||
return addr;
|
return addr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -144,21 +163,36 @@ public class LanSocketPlugin extends SimpleSocketPlugin {
|
|||||||
// Calculate the group address and port from the invitation code
|
// Calculate the group address and port from the invitation code
|
||||||
InetSocketAddress mcast = convertInvitationCodeToMulticastGroup(code);
|
InetSocketAddress mcast = convertInvitationCodeToMulticastGroup(code);
|
||||||
// Bind a TCP socket for receiving connections
|
// Bind a TCP socket for receiving connections
|
||||||
ServerSocket ss;
|
ServerSocket ss = null;
|
||||||
try {
|
try {
|
||||||
ss = new ServerSocket();
|
ss = new ServerSocket();
|
||||||
ss.bind(new InetSocketAddress(chooseInterface(), 0));
|
ss.bind(new InetSocketAddress(chooseInterface(), 0));
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||||
|
if(ss != null) {
|
||||||
|
try {
|
||||||
|
ss.close();
|
||||||
|
} catch(IOException e1) {
|
||||||
|
if(LOG.isLoggable(Level.WARNING))
|
||||||
|
LOG.warning(e1.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// Bind a multicast socket for sending packets
|
// Bind a multicast socket for sending packets
|
||||||
MulticastSocket ms;
|
MulticastSocket ms = null;
|
||||||
try {
|
try {
|
||||||
ms = new MulticastSocket();
|
ms = new MulticastSocket();
|
||||||
ms.setInterface(chooseInterface());
|
ms.setInterface(chooseInterface());
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||||
|
if(ms != null) ms.close();
|
||||||
|
try {
|
||||||
|
ss.close();
|
||||||
|
} catch(IOException e1) {
|
||||||
|
if(LOG.isLoggable(Level.WARNING))
|
||||||
|
LOG.warning(e1.getMessage());
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// Send packets until a connection is received or the timeout expires
|
// Send packets until a connection is received or the timeout expires
|
||||||
@@ -178,7 +212,8 @@ public class LanSocketPlugin extends SimpleSocketPlugin {
|
|||||||
ss.setSoTimeout(wait < 1 ? 1 : wait);
|
ss.setSoTimeout(wait < 1 ? 1 : wait);
|
||||||
return new SocketTransportConnection(ss.accept());
|
return new SocketTransportConnection(ss.accept());
|
||||||
} catch(SocketTimeoutException e) {
|
} catch(SocketTimeoutException e) {
|
||||||
if(System.currentTimeMillis() < end) {
|
now = System.currentTimeMillis();
|
||||||
|
if(now < end) {
|
||||||
ms.send(packet);
|
ms.send(packet);
|
||||||
now = System.currentTimeMillis();
|
now = System.currentTimeMillis();
|
||||||
nextPacket = now + interval;
|
nextPacket = now + interval;
|
||||||
@@ -186,8 +221,18 @@ public class LanSocketPlugin extends SimpleSocketPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(LOG.isLoggable(Level.INFO))
|
||||||
|
LOG.info("Timeout while accepting invitation");
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||||
|
} finally {
|
||||||
|
ms.close();
|
||||||
|
try {
|
||||||
|
ss.close();
|
||||||
|
} catch(IOException e1) {
|
||||||
|
if(LOG.isLoggable(Level.WARNING))
|
||||||
|
LOG.warning(e1.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,14 +54,14 @@ abstract class SocketPlugin extends AbstractPlugin implements StreamPlugin {
|
|||||||
|
|
||||||
private void bind() {
|
private void bind() {
|
||||||
SocketAddress addr;
|
SocketAddress addr;
|
||||||
ServerSocket ss;
|
ServerSocket ss = null;
|
||||||
try {
|
try {
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
if(!started) return;
|
if(!started) return;
|
||||||
addr = getLocalSocketAddress();
|
addr = getLocalSocketAddress();
|
||||||
ss = createServerSocket();
|
ss = createServerSocket();
|
||||||
|
if(addr == null || ss == null) return;
|
||||||
}
|
}
|
||||||
if(addr == null || ss == null) return;
|
|
||||||
ss.bind(addr);
|
ss.bind(addr);
|
||||||
if(LOG.isLoggable(Level.INFO)) {
|
if(LOG.isLoggable(Level.INFO)) {
|
||||||
LOG.info("Bound to " + ss.getInetAddress().getHostAddress() +
|
LOG.info("Bound to " + ss.getInetAddress().getHostAddress() +
|
||||||
@@ -69,6 +69,14 @@ abstract class SocketPlugin extends AbstractPlugin implements StreamPlugin {
|
|||||||
}
|
}
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||||
|
if(ss != null) {
|
||||||
|
try {
|
||||||
|
ss.close();
|
||||||
|
} catch(IOException e1) {
|
||||||
|
if(LOG.isLoggable(Level.WARNING))
|
||||||
|
LOG.warning(e1.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
@@ -109,6 +117,12 @@ abstract class SocketPlugin extends AbstractPlugin implements StreamPlugin {
|
|||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
// This is expected when the socket is closed
|
// This is expected when the socket is closed
|
||||||
if(LOG.isLoggable(Level.INFO)) LOG.info(e.getMessage());
|
if(LOG.isLoggable(Level.INFO)) LOG.info(e.getMessage());
|
||||||
|
try {
|
||||||
|
ss.close();
|
||||||
|
} catch(IOException e1) {
|
||||||
|
if(LOG.isLoggable(Level.WARNING))
|
||||||
|
LOG.warning(e1.getMessage());
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SocketTransportConnection conn = new SocketTransportConnection(s);
|
SocketTransportConnection conn = new SocketTransportConnection(s);
|
||||||
@@ -152,10 +166,11 @@ abstract class SocketPlugin extends AbstractPlugin implements StreamPlugin {
|
|||||||
if(!started) return null;
|
if(!started) return null;
|
||||||
addr = getRemoteSocketAddress(c);
|
addr = getRemoteSocketAddress(c);
|
||||||
s = createClientSocket();
|
s = createClientSocket();
|
||||||
|
if(addr == null || s == null) return null;
|
||||||
}
|
}
|
||||||
if(addr == null || s == null) return null;
|
|
||||||
s.connect(addr);
|
s.connect(addr);
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
|
if(LOG.isLoggable(Level.INFO)) LOG.info(e.getMessage());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return new SocketTransportConnection(s);
|
return new SocketTransportConnection(s);
|
||||||
|
|||||||
Reference in New Issue
Block a user