refactor: Extracts AbstractTcpListener from TcpHarvester.

This commit is contained in:
Boris Grozev 2016-09-08 21:26:52 -05:00
parent 35e603d346
commit 4216982445
3 changed files with 1369 additions and 1256 deletions

File diff suppressed because it is too large Load Diff

View File

@ -125,6 +125,80 @@ public abstract class AbstractUdpListener
return addresses;
}
/**
* Tries to parse the bytes in <tt>buf</tt> at offset <tt>off</tt> (and
* length <tt>len</tt>) as a STUN Binding Request message. If successful,
* looks for a USERNAME attribute and returns the local username fragment
* part (see RFC5245 Section 7.1.2.3).
* In case of any failure returns <tt>null</tt>.
*
* @param buf the bytes.
* @param off the offset.
* @param len the length.
* @return the local ufrag from the USERNAME attribute of the STUN message
* contained in <tt>buf</tt>, or <tt>null</tt>.
*/
static String getUfrag(byte[] buf, int off, int len)
{
// RFC5389, Section 6:
// All STUN messages MUST start with a 20-byte header followed by zero
// or more Attributes.
if (buf == null || buf.length < off + len || len < 20)
{
return null;
}
// RFC5389, Section 6:
// The magic cookie field MUST contain the fixed value 0x2112A442 in
// network byte order.
if ( !( (buf[off + 4] & 0xFF) == 0x21 &&
(buf[off + 5] & 0xFF) == 0x12 &&
(buf[off + 6] & 0xFF) == 0xA4 &&
(buf[off + 7] & 0xFF) == 0x42))
{
if (logger.isLoggable(Level.FINE))
{
logger.fine("Not a STUN packet, magic cookie not found.");
}
return null;
}
try
{
Message stunMessage
= Message.decode(buf,
(char) off,
(char) len);
if (stunMessage.getMessageType()
!= Message.BINDING_REQUEST)
{
return null;
}
UsernameAttribute usernameAttribute
= (UsernameAttribute)
stunMessage.getAttribute(Attribute.USERNAME);
if (usernameAttribute == null)
return null;
String usernameString
= new String(usernameAttribute.getUsername());
return usernameString.split(":")[0];
}
catch (Exception e)
{
// Catch everything. We are going to log, and then drop the packet
// anyway.
if (logger.isLoggable(Level.FINE))
{
logger.fine("Failed to extract local ufrag: " + e);
}
}
return null;
}
/**
* The map which keeps the known remote addresses and their associated
* candidateSockets.
@ -286,80 +360,6 @@ public abstract class AbstractUdpListener
return buf;
}
/**
* Tries to parse the bytes in <tt>buf</tt> at offset <tt>off</tt> (and
* length <tt>len</tt>) as a STUN Binding Request message. If successful,
* looks for a USERNAME attribute and returns the local username fragment
* part (see RFC5245 Section 7.1.2.3).
* In case of any failure returns <tt>null</tt>.
*
* @param buf the bytes.
* @param off the offset.
* @param len the length.
* @return the local ufrag from the USERNAME attribute of the STUN message
* contained in <tt>buf</tt>, or <tt>null</tt>.
*/
private String getUfrag(byte[] buf, int off, int len)
{
// RFC5389, Section 6:
// All STUN messages MUST start with a 20-byte header followed by zero
// or more Attributes.
if (buf == null || buf.length < off + len || len < 20)
{
return null;
}
// RFC5389, Section 6:
// The magic cookie field MUST contain the fixed value 0x2112A442 in
// network byte order.
if ( !( (buf[off + 4] & 0xFF) == 0x21 &&
(buf[off + 5] & 0xFF) == 0x12 &&
(buf[off + 6] & 0xFF) == 0xA4 &&
(buf[off + 7] & 0xFF) == 0x42))
{
if (logger.isLoggable(Level.FINE))
{
logger.fine("Not a STUN packet, magic cookie not found.");
}
return null;
}
try
{
Message stunMessage
= Message.decode(buf,
(char) off,
(char) len);
if (stunMessage.getMessageType()
!= Message.BINDING_REQUEST)
{
return null;
}
UsernameAttribute usernameAttribute
= (UsernameAttribute)
stunMessage.getAttribute(Attribute.USERNAME);
if (usernameAttribute == null)
return null;
String usernameString
= new String(usernameAttribute.getUsername());
return usernameString.split(":")[0];
}
catch (Exception e)
{
// Catch everything. We are going to log, and then drop the packet
// anyway.
if (logger.isLoggable(Level.FINE))
{
logger.fine("Failed to extract local ufrag: " + e);
}
}
return null;
}
/**
* Creates a new {@link MySocket} instance and associates it with the given
* remote address. Returns the created instance.

File diff suppressed because it is too large Load Diff