Moves check list management to the Agent rather than the media stream since checks need to be managed in a global way.
This commit is contained in:
parent
4b7b2e6687
commit
f279a7dcaa
@ -58,6 +58,18 @@ public class Agent
|
|||||||
private final List<CandidateHarvester> harvesters
|
private final List<CandidateHarvester> harvesters
|
||||||
= new ArrayList<CandidateHarvester>();
|
= new ArrayList<CandidateHarvester>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the System property that allows us to set a custom maximum
|
||||||
|
* for check list sizes.
|
||||||
|
*/
|
||||||
|
public static final String PNAME_MAX_CHECK_LIST_SIZE
|
||||||
|
= "org.ice4j.MAX_CHECK_LIST_SIZE";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default maximum size for check lists.
|
||||||
|
*/
|
||||||
|
public static final int DEFAULT_MAX_CHECK_LIST_SIZE = 100;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We use the <tt>FoundationsRegistry</tt> to keep track of the foundations
|
* We use the <tt>FoundationsRegistry</tt> to keep track of the foundations
|
||||||
* we assign within a session (i.e. the entire life time of an
|
* we assign within a session (i.e. the entire life time of an
|
||||||
@ -65,6 +77,17 @@ public class Agent
|
|||||||
*/
|
*/
|
||||||
private FoundationsRegistry foundationsRegistry = new FoundationsRegistry();
|
private FoundationsRegistry foundationsRegistry = new FoundationsRegistry();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The CHECK-LIST for this agent described in the ICE specification. Note
|
||||||
|
* that even though this may sound a bit unclear in the spec,
|
||||||
|
* <tt>checkList</tt>s are supposed to be created on a per-agent basis so
|
||||||
|
* that we could, for example, freeze candidates with common foundations
|
||||||
|
* across streams and not only across components, and so that we could
|
||||||
|
* limit the total number of candidate pairs for all streams.
|
||||||
|
*/
|
||||||
|
private final List<CandidatePair> checkList
|
||||||
|
= new LinkedList<CandidatePair>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The user fragment that we should use for the ice-ufrag attribute.
|
* The user fragment that we should use for the ice-ufrag attribute.
|
||||||
*/
|
*/
|
||||||
@ -220,13 +243,90 @@ public class Agent
|
|||||||
*/
|
*/
|
||||||
public void startChecks()
|
public void startChecks()
|
||||||
{
|
{
|
||||||
List<IceMediaStream> streams = getStreams();
|
//first init the check list.
|
||||||
|
synchronized(checkList)
|
||||||
for(IceMediaStream stream : streams)
|
|
||||||
{
|
{
|
||||||
stream.initCheckList();
|
checkList.clear();
|
||||||
|
List<IceMediaStream> streams = getStreams();
|
||||||
|
|
||||||
|
for(IceMediaStream stream : streams)
|
||||||
|
{
|
||||||
|
stream.createCheckList(checkList);
|
||||||
|
}
|
||||||
|
|
||||||
|
orderCheckList();
|
||||||
|
pruneCheckList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Orders this stream's pair check list in decreasing order of pair
|
||||||
|
* priority. If two pairs have identical priority, the ordering amongst
|
||||||
|
* them is arbitrary.
|
||||||
|
*/
|
||||||
|
private void orderCheckList()
|
||||||
|
{
|
||||||
|
Collections.sort(checkList, CandidatePair.comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes or, as per the ICE spec, "prunes" pairs that we don't need to
|
||||||
|
* run checks for. For example, since we cannot send requests directly
|
||||||
|
* from a reflexive candidate, but only from its base, we go through the
|
||||||
|
* sorted list of candidate pairs and in every pair where the local
|
||||||
|
* candidate is server reflexive, we replace the local server reflexive
|
||||||
|
* candidate with its base. Once this has been done, we remove each pair
|
||||||
|
* where the local and remote candidates are identical to the local and
|
||||||
|
* remote candidates of a pair higher up on the priority list.
|
||||||
|
* <p/>
|
||||||
|
* In addition, in order to limit the attacks described in Section 18.5.2
|
||||||
|
* of the ICE spec, we limit the total number of pairs and hence
|
||||||
|
* (connectivity checks) to a specific value, (100 by default).
|
||||||
|
*/
|
||||||
|
private void pruneCheckList()
|
||||||
|
{
|
||||||
|
//a list that we only use for storing pairs that we've already gone
|
||||||
|
//through. The list is destroyed at the end of this method.
|
||||||
|
List<CandidatePair> tmpCheckList
|
||||||
|
= new ArrayList<CandidatePair>(checkList.size());
|
||||||
|
|
||||||
|
Iterator<CandidatePair> ckListIter = checkList.iterator();
|
||||||
|
|
||||||
|
int maxCheckListSize = Integer.getInteger(
|
||||||
|
PNAME_MAX_CHECK_LIST_SIZE, DEFAULT_MAX_CHECK_LIST_SIZE);
|
||||||
|
|
||||||
|
while(ckListIter.hasNext())
|
||||||
|
{
|
||||||
|
CandidatePair pair = ckListIter.next();
|
||||||
|
|
||||||
|
//drop all pairs above MAX_CHECK_LIST_SIZE.
|
||||||
|
if(tmpCheckList.size() > maxCheckListSize)
|
||||||
|
{
|
||||||
|
ckListIter.remove();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//replace local server reflexive candidates with their base.
|
||||||
|
LocalCandidate localCnd = pair.getLocalCandidate();
|
||||||
|
if( localCnd.getType()
|
||||||
|
== CandidateType.SERVER_REFLEXIVE_CANDIDATE)
|
||||||
|
{
|
||||||
|
pair.setLocalCandidate(localCnd.getBase());
|
||||||
|
|
||||||
|
//if the new pair corresponds to another one with a higher
|
||||||
|
//priority, then remove it.
|
||||||
|
if(tmpCheckList.contains(pair))
|
||||||
|
{
|
||||||
|
ckListIter.remove();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpCheckList.add(pair);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes and sets the foundations foundation for all <tt>Candidate</tt>s
|
* Computes and sets the foundations foundation for all <tt>Candidate</tt>s
|
||||||
* currently found in <tt>component</tt>.
|
* currently found in <tt>component</tt>.
|
||||||
|
|||||||
@ -52,12 +52,15 @@ public class CandidatePair
|
|||||||
public static final PairComparator comparator = new PairComparator();
|
public static final PairComparator comparator = new PairComparator();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Each candidate pair has a a state that is assigned once the check list
|
* Each candidate pair has a state that is assigned once the check list
|
||||||
* for each media stream has been computed. The ICE RFC defines five
|
* for each media stream has been computed. The ICE RFC defines five
|
||||||
* potential values that the state can have and they are all represented
|
* potential values that the state can have and they are all represented
|
||||||
* in the <tt>CandidatePairState</tt> enumeration.
|
* in the <tt>CandidatePairState</tt> enumeration. The ICE spec stipulates
|
||||||
|
* that the first step of the state initialization process is: The agent
|
||||||
|
* sets all of the pairs in each check list to the Frozen state, and hence
|
||||||
|
* our default state.
|
||||||
*/
|
*/
|
||||||
private CandidatePairState state = CandidatePairState.WAITING;
|
private CandidatePairState state = CandidatePairState.FROZEN;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a <tt>CandidatePair</tt> instance mapping <tt>localCandidate</tt>
|
* Creates a <tt>CandidatePair</tt> instance mapping <tt>localCandidate</tt>
|
||||||
|
|||||||
@ -48,24 +48,6 @@ public class IceMediaStream
|
|||||||
*/
|
*/
|
||||||
private int lastComponentID = 0;
|
private int lastComponentID = 0;
|
||||||
|
|
||||||
/**
|
|
||||||
* The CHECK-LIST for this stream as described in the ICE specification.
|
|
||||||
*/
|
|
||||||
private final List<CandidatePair> checkList
|
|
||||||
= new LinkedList<CandidatePair>();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The name of the System property that allows us to set a custom maximum
|
|
||||||
* for check list sizes.
|
|
||||||
*/
|
|
||||||
public static final String PNAME_MAX_CHECK_LIST_SIZE
|
|
||||||
= "org.ice4j.MAX_CHECK_LIST_SIZE";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The default maximum size for check lists.
|
|
||||||
*/
|
|
||||||
public static final int DEFAULT_MAX_CHECK_LIST_SIZE = 100;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The agent that this media stream belongs to.
|
* The agent that this media stream belongs to.
|
||||||
*/
|
*/
|
||||||
@ -265,19 +247,17 @@ public class IceMediaStream
|
|||||||
/**
|
/**
|
||||||
* Creates, initializes and orders the list of candidate pairs that would
|
* Creates, initializes and orders the list of candidate pairs that would
|
||||||
* be used for the connectivity checks for all components in this stream.
|
* be used for the connectivity checks for all components in this stream.
|
||||||
|
*
|
||||||
|
* @param checkList the list that we need to update with the new pairs.
|
||||||
*/
|
*/
|
||||||
protected void initCheckList()
|
protected void createCheckList(List<CandidatePair> checkList)
|
||||||
{
|
{
|
||||||
synchronized(checkList)
|
|
||||||
{
|
|
||||||
checkList.clear();
|
|
||||||
List<Component> componentsList = getComponents();
|
List<Component> componentsList = getComponents();
|
||||||
|
|
||||||
for(Component cmp : componentsList)
|
for(Component cmp : componentsList)
|
||||||
{
|
{
|
||||||
createCheckList(cmp, checkList);
|
createCheckList(cmp, checkList);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -306,83 +286,6 @@ public class IceMediaStream
|
|||||||
checkList.add(pair);
|
checkList.add(pair);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
orderCheckList();
|
|
||||||
pruneCheckList();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Orders this stream's pair check list in decreasing order of pair
|
|
||||||
* priority. If two pairs have identical priority, the ordering amongst
|
|
||||||
* them is arbitrary.
|
|
||||||
*/
|
|
||||||
private void orderCheckList()
|
|
||||||
{
|
|
||||||
synchronized(checkList)
|
|
||||||
{
|
|
||||||
Collections.sort(checkList, CandidatePair.comparator);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes or, as per the ICE spec, "prunes" pairs that we don't need to
|
|
||||||
* run checks for. For example, since we cannot send requests directly
|
|
||||||
* from a reflexive candidate, but only from its base, we go through the
|
|
||||||
* sorted list of candidate pairs and in every pair where the local
|
|
||||||
* candidate is server reflexive, we replace the local server reflexive
|
|
||||||
* candidate with its base. Once this has been done, we remove each pair
|
|
||||||
* where the local and remote candidates are identical to the local and
|
|
||||||
* remote candidates of a pair higher up on the priority list.
|
|
||||||
* <p/>
|
|
||||||
* In addition, in order to limit the attacks described in Section 18.5.2
|
|
||||||
* of the ICE spec, we limit the total number of pairs and hence
|
|
||||||
* (connectivity checks) to a specific value, (100 by default).
|
|
||||||
*/
|
|
||||||
private void pruneCheckList()
|
|
||||||
{
|
|
||||||
synchronized(checkList)
|
|
||||||
{
|
|
||||||
//a list that we only use for storing pairs that we've already gone
|
|
||||||
//through. The list is destroyed at the end of this method.
|
|
||||||
List<CandidatePair> tmpCheckList
|
|
||||||
= new ArrayList<CandidatePair>(checkList.size());
|
|
||||||
|
|
||||||
Iterator<CandidatePair> ckListIter = checkList.iterator();
|
|
||||||
|
|
||||||
int maxCheckListSize = Integer.getInteger(
|
|
||||||
PNAME_MAX_CHECK_LIST_SIZE, DEFAULT_MAX_CHECK_LIST_SIZE);
|
|
||||||
|
|
||||||
while(ckListIter.hasNext())
|
|
||||||
{
|
|
||||||
CandidatePair pair = ckListIter.next();
|
|
||||||
|
|
||||||
//drop all pairs above MAX_CHECK_LIST_SIZE.
|
|
||||||
if(tmpCheckList.size() > maxCheckListSize)
|
|
||||||
{
|
|
||||||
ckListIter.remove();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
//replace local server reflexive candidates with their base.
|
|
||||||
LocalCandidate localCnd = pair.getLocalCandidate();
|
|
||||||
if( localCnd.getType()
|
|
||||||
== CandidateType.SERVER_REFLEXIVE_CANDIDATE)
|
|
||||||
{
|
|
||||||
pair.setLocalCandidate(localCnd.getBase());
|
|
||||||
|
|
||||||
//if the new pair corresponds to another one with a higher
|
|
||||||
//priority, then remove it.
|
|
||||||
if(tmpCheckList.contains(pair))
|
|
||||||
{
|
|
||||||
ckListIter.remove();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpCheckList.add(pair);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -33,11 +33,28 @@ public class NetworkUtils
|
|||||||
*/
|
*/
|
||||||
public static final String IN6_ADDR_ANY = "::0";
|
public static final String IN6_ADDR_ANY = "::0";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The length of IPv6 addresses.
|
||||||
|
*/
|
||||||
|
private final static int IN6_ADDR_SIZE = 16;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The size of the tokens in a <tt>String</tt> representation of IPv6
|
||||||
|
* addresses.
|
||||||
|
*/
|
||||||
|
//private final static int IN6_ADDR_TOKEN_SIZE = 16;
|
||||||
|
private final static int IN6_ADDR_TOKEN_SIZE = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A string containing the "any" local address for IPv4.
|
* A string containing the "any" local address for IPv4.
|
||||||
*/
|
*/
|
||||||
public static final String IN4_ADDR_ANY = "0.0.0.0";
|
public static final String IN4_ADDR_ANY = "0.0.0.0";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The length of IPv4 addresses.
|
||||||
|
*/
|
||||||
|
private final static int IN4_ADDR_SIZE = 4;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A string containing the "any" local address.
|
* A string containing the "any" local address.
|
||||||
*/
|
*/
|
||||||
@ -162,7 +179,7 @@ public class NetworkUtils
|
|||||||
*/
|
*/
|
||||||
public static boolean isIPv4Address(String address)
|
public static boolean isIPv4Address(String address)
|
||||||
{
|
{
|
||||||
return sun.net.util.IPAddressUtil.isIPv4LiteralAddress(address);
|
return strToIPv4(address) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -175,7 +192,7 @@ public class NetworkUtils
|
|||||||
*/
|
*/
|
||||||
public static boolean isIPv6Address(String address)
|
public static boolean isIPv6Address(String address)
|
||||||
{
|
{
|
||||||
return sun.net.util.IPAddressUtil.isIPv6LiteralAddress(address);
|
return strToIPv6(address) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -217,12 +234,11 @@ public class NetworkUtils
|
|||||||
byte[] addr = null;
|
byte[] addr = null;
|
||||||
|
|
||||||
// see if it is IPv4 address
|
// see if it is IPv4 address
|
||||||
addr = sun.net.util.IPAddressUtil.textToNumericFormatV4(address);
|
addr = strToIPv4(address);
|
||||||
// if not, see if it is IPv6 address
|
// if not, see if it is IPv6 address
|
||||||
if (addr == null)
|
if (addr == null)
|
||||||
{
|
{
|
||||||
addr = sun.net.util.IPAddressUtil
|
addr = strToIPv6(address);
|
||||||
.textToNumericFormatV6(address);
|
|
||||||
}
|
}
|
||||||
// if IPv4 is found when IPv6 is expected
|
// if IPv4 is found when IPv6 is expected
|
||||||
else if (ipv6Expected)
|
else if (ipv6Expected)
|
||||||
@ -241,6 +257,308 @@ public class NetworkUtils
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a byte array containing the specified <tt>ipv4AddStr</tt>.
|
||||||
|
*
|
||||||
|
* @param ipv4AddrStr a <tt>String</tt> containing an IPv4 address.
|
||||||
|
*
|
||||||
|
* @return a byte array containing the four bytes of the address represented
|
||||||
|
* by ipv4AddrStr or <tt>null</tt> if <tt>ipv4AddrStr</tt> does not contain
|
||||||
|
* a valid IPv4 address string.
|
||||||
|
*/
|
||||||
|
public static byte[] strToIPv4(String ipv4AddrStr)
|
||||||
|
{
|
||||||
|
if (ipv4AddrStr.length() == 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
byte[] address = new byte[IN4_ADDR_SIZE];
|
||||||
|
String[] tokens = ipv4AddrStr.split("\\.", -1);
|
||||||
|
long currentTkn;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
switch(tokens.length)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
//If the address was specified as a single String we can
|
||||||
|
//directly copy it into the byte array.
|
||||||
|
currentTkn = Long.parseLong(tokens[0]);
|
||||||
|
if (currentTkn < 0 || currentTkn > 0xffffffffL)
|
||||||
|
return null;
|
||||||
|
address[0] = (byte) ((currentTkn >> 24) & 0xff);
|
||||||
|
address[1] = (byte) (((currentTkn & 0xffffff) >> 16) & 0xff);
|
||||||
|
address[2] = (byte) (((currentTkn & 0xffff) >> 8) & 0xff);
|
||||||
|
address[3] = (byte) (currentTkn & 0xff);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
// If the address was passed in two parts (e.g. when dealing
|
||||||
|
// with a Class A address representation), we place the
|
||||||
|
// first one in the leftmost byte and the rest in the three
|
||||||
|
// remaining bytes of the address array.
|
||||||
|
currentTkn = Integer.parseInt(tokens[0]);
|
||||||
|
|
||||||
|
if (currentTkn < 0 || currentTkn > 0xff)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
address[0] = (byte) (currentTkn & 0xff);
|
||||||
|
currentTkn = Integer.parseInt(tokens[1]);
|
||||||
|
|
||||||
|
if (currentTkn < 0 || currentTkn > 0xffffff)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
address[1] = (byte) ((currentTkn >> 16) & 0xff);
|
||||||
|
address[2] = (byte) (((currentTkn & 0xffff) >> 8) &0xff);
|
||||||
|
address[3] = (byte) (currentTkn & 0xff);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
// If the address was passed in three parts (e.g. when
|
||||||
|
// dealing with a Class B address representation), we place
|
||||||
|
// the first two parts in the two leftmost bytes and the
|
||||||
|
// rest in the two remaining bytes of the address array.
|
||||||
|
for (int i = 0; i < 2; i++)
|
||||||
|
{
|
||||||
|
currentTkn = Integer.parseInt(tokens[i]);
|
||||||
|
|
||||||
|
if (currentTkn < 0 || currentTkn > 0xff)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
address[i] = (byte) (currentTkn & 0xff);
|
||||||
|
}
|
||||||
|
|
||||||
|
currentTkn = Integer.parseInt(tokens[2]);
|
||||||
|
|
||||||
|
if (currentTkn < 0 || currentTkn > 0xffff)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
address[2] = (byte) ((currentTkn >> 8) & 0xff);
|
||||||
|
address[3] = (byte) (currentTkn & 0xff);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
// And now for the most common - four part case. This time
|
||||||
|
// there's a byte for every part :). Yuppiee! :)
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
currentTkn = Integer.parseInt(tokens[i]);
|
||||||
|
|
||||||
|
if (currentTkn < 0 || currentTkn > 0xff)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
address[i] = (byte) (currentTkn & 0xff);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(NumberFormatException e)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a byte array containing the specified <tt>ipv6AddStr</tt>.
|
||||||
|
*
|
||||||
|
* @param ipv6AddrStr a <tt>String</tt> containing an IPv6 address.
|
||||||
|
*
|
||||||
|
* @return a byte array containing the four bytes of the address represented
|
||||||
|
* by <tt>ipv6AddrStr</tt> or <tt>null</tt> if <tt>ipv6AddrStr</tt> does
|
||||||
|
* not contain a valid IPv6 address string.
|
||||||
|
*/
|
||||||
|
public static byte[] strToIPv6(String ipv6AddrStr)
|
||||||
|
{
|
||||||
|
// Bail out if the string is shorter than "::"
|
||||||
|
if (ipv6AddrStr.length() < 2)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
int colonIndex;
|
||||||
|
char currentChar;
|
||||||
|
boolean sawtDigit;
|
||||||
|
int currentTkn;
|
||||||
|
char[] addrBuff = ipv6AddrStr.toCharArray();
|
||||||
|
byte[] dst = new byte[IN6_ADDR_SIZE];
|
||||||
|
|
||||||
|
int srcb_length = addrBuff.length;
|
||||||
|
int scopeID = ipv6AddrStr.indexOf ("%");
|
||||||
|
|
||||||
|
if (scopeID == srcb_length -1)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if (scopeID != -1)
|
||||||
|
srcb_length = scopeID;
|
||||||
|
|
||||||
|
colonIndex = -1;
|
||||||
|
int i = 0, j = 0;
|
||||||
|
// Starting : mean we need to have at least one more.
|
||||||
|
if (addrBuff[i] == ':')
|
||||||
|
if (addrBuff[++i] != ':')
|
||||||
|
return null;
|
||||||
|
|
||||||
|
int curtok = i;
|
||||||
|
sawtDigit = false;
|
||||||
|
currentTkn = 0;
|
||||||
|
while (i < srcb_length)
|
||||||
|
{
|
||||||
|
currentChar = addrBuff[i++];
|
||||||
|
int chval = Character.digit(currentChar, 16);
|
||||||
|
if (chval != -1)
|
||||||
|
{
|
||||||
|
currentTkn <<= 4;
|
||||||
|
currentTkn |= chval;
|
||||||
|
if (currentTkn > 0xffff)
|
||||||
|
return null;
|
||||||
|
sawtDigit = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentChar == ':')
|
||||||
|
{
|
||||||
|
curtok = i;
|
||||||
|
|
||||||
|
if (!sawtDigit)
|
||||||
|
{
|
||||||
|
if (colonIndex != -1)
|
||||||
|
return null;
|
||||||
|
colonIndex = j;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if (i == srcb_length)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (j + IN6_ADDR_TOKEN_SIZE > IN6_ADDR_SIZE)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
dst[j++] = (byte) ((currentTkn >> 8) & 0xff);
|
||||||
|
dst[j++] = (byte) (currentTkn & 0xff);
|
||||||
|
sawtDigit = false;
|
||||||
|
currentTkn = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentChar == '.' && ((j + IN4_ADDR_SIZE) <= IN6_ADDR_SIZE))
|
||||||
|
{
|
||||||
|
String ia4 = ipv6AddrStr.substring(curtok, srcb_length);
|
||||||
|
// check this IPv4 address has 3 dots, ie. A.B.C.D
|
||||||
|
int dot_count = 0, index=0;
|
||||||
|
while ((index = ia4.indexOf ('.', index)) != -1)
|
||||||
|
{
|
||||||
|
dot_count ++;
|
||||||
|
index ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dot_count != 3)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
byte[] v4addr = strToIPv4(ia4);
|
||||||
|
if (v4addr == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
for (int k = 0; k < IN4_ADDR_SIZE; k++)
|
||||||
|
{
|
||||||
|
dst[j++] = v4addr[k];
|
||||||
|
}
|
||||||
|
|
||||||
|
sawtDigit = false;
|
||||||
|
break; /* '\0' was seen by inet_pton4(). */
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sawtDigit)
|
||||||
|
{
|
||||||
|
if (j + IN6_ADDR_TOKEN_SIZE > IN6_ADDR_SIZE)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
dst[j++] = (byte) ((currentTkn >> 8) & 0xff);
|
||||||
|
dst[j++] = (byte) (currentTkn & 0xff);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (colonIndex != -1)
|
||||||
|
{
|
||||||
|
int n = j - colonIndex;
|
||||||
|
|
||||||
|
if (j == IN6_ADDR_SIZE)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
for (i = 1; i <= n; i++)
|
||||||
|
{
|
||||||
|
dst[IN6_ADDR_SIZE - i] = dst[colonIndex + n - i];
|
||||||
|
dst[colonIndex + n - i] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
j = IN6_ADDR_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (j != IN6_ADDR_SIZE)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
byte[] newdst = mappedIPv4ToRealIPv4(dst);
|
||||||
|
|
||||||
|
if (newdst != null)
|
||||||
|
{
|
||||||
|
return newdst;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an IPv4 address matching the one mapped in the IPv6
|
||||||
|
* <tt>addr</tt>. Both input and returned value are in network order.
|
||||||
|
*
|
||||||
|
* @param addr a String representing an IPv4-Mapped address in textual
|
||||||
|
* format
|
||||||
|
*
|
||||||
|
* @return a byte array numerically representing the IPv4 address
|
||||||
|
*/
|
||||||
|
public static byte[] mappedIPv4ToRealIPv4(byte[] addr)
|
||||||
|
{
|
||||||
|
if (isMappedIPv4Addr(addr))
|
||||||
|
{
|
||||||
|
byte[] newAddr = new byte[IN4_ADDR_SIZE];
|
||||||
|
System.arraycopy(addr, 12, newAddr, 0, IN6_ADDR_SIZE);
|
||||||
|
return newAddr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility method to check if the specified <tt>address</tt> is an IPv4
|
||||||
|
* mapped IPv6 address.
|
||||||
|
*
|
||||||
|
* @param address the address that we'd like to determine as an IPv4 mapped
|
||||||
|
* one or not.
|
||||||
|
*
|
||||||
|
* @return <tt>true</tt> if address is an IPv4 mapped IPv6 address and
|
||||||
|
* <tt>false</tt> otherwise.
|
||||||
|
*/
|
||||||
|
private static boolean isMappedIPv4Addr(byte[] address)
|
||||||
|
{
|
||||||
|
if (address.length < IN6_ADDR_SIZE)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((address[0] == 0x00) && (address[1] == 0x00)
|
||||||
|
&& (address[2] == 0x00) && (address[3] == 0x00)
|
||||||
|
&& (address[4] == 0x00) && (address[5] == 0x00)
|
||||||
|
&& (address[6] == 0x00) && (address[7] == 0x00)
|
||||||
|
&& (address[8] == 0x00) && (address[9] == 0x00)
|
||||||
|
&& (address[10] == (byte)0xff)
|
||||||
|
&& (address[11] == (byte)0xff))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an InetAddress from the specified <tt>hostAddress</tt>. The point
|
* Creates an InetAddress from the specified <tt>hostAddress</tt>. The point
|
||||||
* of using the method rather than creating the address by yourself is that
|
* of using the method rather than creating the address by yourself is that
|
||||||
@ -293,14 +611,12 @@ public class NetworkUtils
|
|||||||
byte[] addr = null;
|
byte[] addr = null;
|
||||||
|
|
||||||
// attempt parse as IPv4 address
|
// attempt parse as IPv4 address
|
||||||
addr = sun.net.util.IPAddressUtil
|
addr = strToIPv4(hostAddress);
|
||||||
.textToNumericFormatV4(hostAddress);
|
|
||||||
|
|
||||||
// if not IPv4, parse as IPv6 address
|
// if not IPv4, parse as IPv6 address
|
||||||
if (addr == null)
|
if (addr == null)
|
||||||
{
|
{
|
||||||
addr = sun.net.util.IPAddressUtil
|
addr = strToIPv6(hostAddress);
|
||||||
.textToNumericFormatV6(hostAddress);
|
|
||||||
}
|
}
|
||||||
return InetAddress.getByAddress(hostAddress, addr);
|
return InetAddress.getByAddress(hostAddress, addr);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user