diff --git a/src/main/java/org/ice4j/ice/Component.java b/src/main/java/org/ice4j/ice/Component.java
index 9ffdc2e..e8147b6 100644
--- a/src/main/java/org/ice4j/ice/Component.java
+++ b/src/main/java/org/ice4j/ice/Component.java
@@ -152,7 +152,7 @@ public class Component
try
{
- mergingDatagramSocket = new MergingDatagramSocket();
+ mergingDatagramSocket = new ComponentSocket(this);
mergingDatagramSocketWrapper
= new IceUdpSocketWrapper(
new MultiplexingDatagramSocket(
diff --git a/src/main/java/org/ice4j/ice/ComponentSocket.java b/src/main/java/org/ice4j/ice/ComponentSocket.java
new file mode 100644
index 0000000..6760320
--- /dev/null
+++ b/src/main/java/org/ice4j/ice/ComponentSocket.java
@@ -0,0 +1,161 @@
+/*
+ * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
+ *
+ * Copyright @ 2015-2016 Atlassian Pty Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.ice4j.ice;
+
+import org.ice4j.socket.*;
+
+import java.beans.*;
+import java.net.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.logging.*;
+
+/**
+ * Extends {@link MergingDatagramSocket} with functionality specific to
+ * an ICE {@link Component}.
+ */
+class ComponentSocket
+ extends MergingDatagramSocket
+ implements PropertyChangeListener
+{
+ /**
+ * The {@link Logger} used by the {@link MergingDatagramSocket} class and
+ * its instances for logging output.
+ */
+ private static final Logger logger
+ = Logger.getLogger(MergingDatagramSocket.class.getName());
+
+ /**
+ * Controls access to {@link #authorizedAddresses}.
+ */
+ private final Object authorizedAddressesSyncRoot = new Object();
+
+ /**
+ * The set of remote addresses, which this socket is allowed to receive
+ * from. These should be the addresses which we have confirmed (e.g. by
+ * having received a STUN message with correct authentication fields).
+ */
+ private Set authorizedAddresses
+ = Collections.newSetFromMap(
+ new ConcurrentHashMap());
+
+ /**
+ * The owning {@link Component}.
+ */
+ private Component component;
+
+ /**
+ * Initializes a new {@link MergingDatagramSocket} instance.
+ * @throws SocketException
+ */
+ ComponentSocket(Component component)
+ throws SocketException
+ {
+ this.component = component;
+ component.getParentStream().addPairChangeListener(this);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * Verifies that the source of the packet is an authorized remote address.
+ */
+ protected boolean accept(DatagramPacket p)
+ {
+ return authorizedAddresses.contains(p.getSocketAddress());
+ }
+
+ /**
+ * Adds a specific address to the list of authorized remote addresses.
+ * @param address the address to add.
+ */
+ private void addAuthorizedAddress(SocketAddress address)
+ {
+ synchronized (authorizedAddressesSyncRoot)
+ {
+ if (authorizedAddresses.contains(address))
+ {
+ return;
+ }
+
+ logger.info("Adding allowed address: " + address);
+
+ Set newSet
+ = Collections.newSetFromMap(
+ new ConcurrentHashMap());
+ newSet.addAll(authorizedAddresses);
+ newSet.add(address);
+
+ authorizedAddresses = newSet;
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * Handles property change events coming from ICE pairs.
+ * @param event
+ */
+ @Override
+ public void propertyChange(PropertyChangeEvent event)
+ {
+ String propertyName = event.getPropertyName();
+ if (!(event.getSource() instanceof CandidatePair))
+ {
+ return;
+ }
+
+ CandidatePair pair = (CandidatePair) event.getSource();
+ if (!pair.getParentComponent().equals(component))
+ {
+ // Events are fired by the IceMediaStream, which might have
+ // multiple components. Make sure that we only handle events for
+ // our own component.
+ return;
+ }
+
+ if (IceMediaStream.PROPERTY_PAIR_STATE_CHANGED.equals(
+ propertyName))
+ {
+ CandidatePairState newState
+ = (CandidatePairState) event.getNewValue();
+
+ if (CandidatePairState.SUCCEEDED.equals(newState))
+ {
+ addAuthorizedAddress(
+ pair.getRemoteCandidate().getTransportAddress());
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void close()
+ {
+ try
+ {
+ super.close();
+ }
+ finally
+ {
+ component = null;
+ }
+ }
+}
diff --git a/src/main/java/org/ice4j/socket/MergingDatagramSocket.java b/src/main/java/org/ice4j/socket/MergingDatagramSocket.java
index e994feb..ae50d50 100644
--- a/src/main/java/org/ice4j/socket/MergingDatagramSocket.java
+++ b/src/main/java/org/ice4j/socket/MergingDatagramSocket.java
@@ -78,13 +78,22 @@ public class MergingDatagramSocket
* The {@link SocketContainer} considered active, i.e. the one which should
* be used for sending.
*/
- private SocketContainer active = null;
+ protected SocketContainer active = null;
/**
* The flag which indicates whether this socket is closed.
*/
private boolean closed = false;
+ /**
+ * The number of packets which were read from an underlying socket, but were
+ * discarded because they were not accepted by
+ * {@link #accept(DatagramPacket)}.
+ * Access to this field should be protected by {@link #receiveLock}.
+ */
+ private int numDiscardedPackets = 0;
+
+
/**
* Initializes a new {@link MergingDatagramSocket} instance.
* @throws SocketException
@@ -108,6 +117,7 @@ public class MergingDatagramSocket
@Override
public void close()
{
+ logger.info("Closing.");
if (isClosed())
{
return;
@@ -371,7 +381,7 @@ public class MergingDatagramSocket
/**
* TODO
*/
- private SocketContainer getActiveSocket()
+ protected SocketContainer getActiveSocket()
{
return this.active;
}
@@ -418,6 +428,20 @@ public class MergingDatagramSocket
activeSocket == null ? null : activeSocket.getLocalSocketAddress();
}
+ /**
+ * Checks whether a particular {@link DatagramPacket}, received from one of
+ * the underlying sockets of thins merging socket, should be accepted and
+ * provided for reception from this {@link MergingDatagramSocket}.
+ * @param p the packet for which to decide whether to accept it or not.
+ * @return {@code true} iff {@code p} should be accepted.
+ */
+ protected boolean accept(DatagramPacket p)
+ {
+ // By default we accept all packets, and allow extending classes to
+ // override
+ return true;
+ }
+
/**
* {@inheritDoc}
*
@@ -470,11 +494,28 @@ public class MergingDatagramSocket
}
}
- // If a packet is available read it
+ // If a packet is available, receive it
if (socketToReceiveFrom != null)
{
socketToReceiveFrom.receive(p);
- return;
+
+ if (accept(p))
+ {
+ return;
+ }
+ else
+ {
+ numDiscardedPackets++;
+ if (numDiscardedPackets % 100 == 1)
+ {
+ logger.info("Discarded " + numDiscardedPackets
+ + " packets. Last remote address:"
+ + p.getSocketAddress());
+ }
+
+ // Go on and receive the next packet in p.
+ continue;
+ }
}
// Otherwise wait on receiveLock.
else