feat: Verifies the remove address of packets passed on to the application.

This commit is contained in:
Boris Grozev 2016-11-30 15:17:33 -06:00
parent d0afb0a55b
commit 633f05b4e5
3 changed files with 207 additions and 5 deletions

View File

@ -152,7 +152,7 @@ public class Component
try try
{ {
mergingDatagramSocket = new MergingDatagramSocket(); mergingDatagramSocket = new ComponentSocket(this);
mergingDatagramSocketWrapper mergingDatagramSocketWrapper
= new IceUdpSocketWrapper( = new IceUdpSocketWrapper(
new MultiplexingDatagramSocket( new MultiplexingDatagramSocket(

View File

@ -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<SocketAddress> authorizedAddresses
= Collections.newSetFromMap(
new ConcurrentHashMap<SocketAddress, Boolean>());
/**
* 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}
* </p>
* 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<SocketAddress> newSet
= Collections.newSetFromMap(
new ConcurrentHashMap<SocketAddress, Boolean>());
newSet.addAll(authorizedAddresses);
newSet.add(address);
authorizedAddresses = newSet;
}
}
/**
* {@inheritDoc}
* </p>
* 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;
}
}
}

View File

@ -78,13 +78,22 @@ public class MergingDatagramSocket
* The {@link SocketContainer} considered active, i.e. the one which should * The {@link SocketContainer} considered active, i.e. the one which should
* be used for sending. * be used for sending.
*/ */
private SocketContainer active = null; protected SocketContainer active = null;
/** /**
* The flag which indicates whether this socket is closed. * The flag which indicates whether this socket is closed.
*/ */
private boolean closed = false; 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. * Initializes a new {@link MergingDatagramSocket} instance.
* @throws SocketException * @throws SocketException
@ -108,6 +117,7 @@ public class MergingDatagramSocket
@Override @Override
public void close() public void close()
{ {
logger.info("Closing.");
if (isClosed()) if (isClosed())
{ {
return; return;
@ -371,7 +381,7 @@ public class MergingDatagramSocket
/** /**
* TODO * TODO
*/ */
private SocketContainer getActiveSocket() protected SocketContainer getActiveSocket()
{ {
return this.active; return this.active;
} }
@ -418,6 +428,20 @@ public class MergingDatagramSocket
activeSocket == null ? null : activeSocket.getLocalSocketAddress(); 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} * {@inheritDoc}
* </p> * </p>
@ -470,11 +494,28 @@ public class MergingDatagramSocket
} }
} }
// If a packet is available read it // If a packet is available, receive it
if (socketToReceiveFrom != null) if (socketToReceiveFrom != null)
{ {
socketToReceiveFrom.receive(p); 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. // Otherwise wait on receiveLock.
else else