Fixes warnings, javadocs, formatting. Simplifies a few instances of cloning arrays.

This commit is contained in:
lubomir.marinov 2010-06-25 06:47:47 +00:00
parent 5dd7a39257
commit 316e524a0f
20 changed files with 155 additions and 136 deletions

View File

@ -96,9 +96,11 @@ public class StunException extends Exception
}
/**
* Creates a StunException, setting an error message an error id and a cause.
* @param message an error message.
* Creates a StunException, setting an error message an error id and a
* cause.
*
* @param id an error id.
* @param message an error message.
* @param cause the error that caused this exception.
*/
public StunException(int id, String message, Throwable cause)

View File

@ -6,8 +6,6 @@
*/
package org.ice4j.attribute;
import org.ice4j.*;
/**
* <tt>ContentDependentAttribute</tt>s have a value that depend on the content
* of the message. The {@link MessageIntegrityAttribute} and {@link

View File

@ -6,10 +6,6 @@
*/
package org.ice4j.attribute;
import java.util.*;
import org.ice4j.*;
/**
* The ERROR-CODE attribute is present in the Binding Error Response and
* Shared Secret Error Response. It is a numeric value in the range of
@ -142,7 +138,6 @@ public class ErrorCodeAttribute extends Attribute
return (char)(getErrorClass()*100 + getErrorNumber());
}
/**
* Sets this attribute's error number.
* @param errorNumber the error number to assign this attribute.
@ -190,7 +185,6 @@ public class ErrorCodeAttribute extends Attribute
return errorClass;
}
/**
* Returns a default reason phrase corresponding to the specified error
* code, as described by rfc 3489.
@ -371,6 +365,4 @@ public class ErrorCodeAttribute extends Attribute
setReasonPhrase(new String(reasonBytes));
}
}

View File

@ -6,14 +6,11 @@
*/
package org.ice4j.attribute;
import java.nio.channels.*;
import java.security.*;
import java.util.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import org.ice4j.*;
import org.ice4j.stack.*;
/**

View File

@ -11,7 +11,7 @@ import java.util.*;
import org.ice4j.*;
/**
* The NONCE attribute is used for authentification.
* The NONCE attribute is used for authentication.
*
* @author Sebastien Vincent
*/
@ -45,7 +45,8 @@ public class NonceAttribute extends Attribute
* @param length the length of the binary array.
* @throws StunException if attributeValue contains invalid data.
*/
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws StunException
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{
nonce = new byte[length];
System.arraycopy(attributeValue, offset, nonce, 0, length);
@ -58,7 +59,8 @@ public class NonceAttribute extends Attribute
public byte[] encode()
{
char type = getAttributeType();
byte binValue[] = new byte[HEADER_LENGTH + getDataLength() + (getDataLength() % 4)];
byte binValue[]
= new byte[HEADER_LENGTH + getDataLength() + (getDataLength() % 4)];
//Type
binValue[0] = (byte)(type>>8);
@ -93,18 +95,13 @@ public class NonceAttribute extends Attribute
}
/**
* Returns a (cloned) byte array containg the data value of the nonce
* Returns a (cloned) byte array containing the data value of the nonce
* attribute.
* @return the binary array containing the nonce.
*/
public byte[] getNonce()
{
if (nonce == null)
return null;
byte[] copy = new byte[nonce.length];
System.arraycopy(nonce, 0, copy, 0, nonce.length);
return copy;
return (nonce == null) ? null : nonce.clone();
}
/**
@ -114,14 +111,7 @@ public class NonceAttribute extends Attribute
*/
public void setNonce(byte[] nonce)
{
if (nonce == null)
{
this.nonce = null;
return;
}
this.nonce = new byte[nonce.length];
System.arraycopy(nonce, 0, this.nonce, 0, nonce.length);
this.nonce = (nonce == null) ? null : nonce.clone();
}
/**
@ -132,19 +122,16 @@ public class NonceAttribute extends Attribute
*/
public boolean equals(Object obj)
{
if (! (obj instanceof NonceAttribute) || obj == null)
return false;
if (obj == this)
return true;
NonceAttribute att = (NonceAttribute) obj;
if (att.getAttributeType() != getAttributeType()
|| att.getDataLength() != getDataLength()
|| !Arrays.equals( att.nonce, nonce))
if (! (obj instanceof NonceAttribute))
return false;
return true;
NonceAttribute att = (NonceAttribute) obj;
return
(att.getAttributeType() == getAttributeType()
&& att.getDataLength() == getDataLength()
&& Arrays.equals(att.nonce, nonce));
}
}

View File

@ -95,18 +95,13 @@ public class RealmAttribute extends Attribute
}
/**
* Returns a (cloned) byte array containg the data value of the realm
* Returns a (cloned) byte array containing the data value of the realm
* attribute.
* @return the binary array containing the realm.
*/
public byte[] getRealm()
{
if (realm == null)
return null;
byte[] copy = new byte[realm.length];
System.arraycopy(realm, 0, copy, 0, realm.length);
return copy;
return (realm == null) ? null : realm.clone();
}
/**

View File

@ -8,8 +8,6 @@ package org.ice4j.attribute;
import java.util.*;
import org.ice4j.*;
/**
* The SOFTWARE attribute contains a textual description of the software
* being used by the software or the client, including manufacturer and version number.

View File

@ -105,12 +105,7 @@ public class UsernameAttribute extends Attribute
*/
public byte[] getUsername()
{
if (username == null)
return null;
byte[] copy = new byte[username.length];
System.arraycopy(username, 0, copy, 0, username.length);
return copy;
return (username == null) ? null : username.clone();
}
/**

View File

@ -202,7 +202,7 @@ class ConnectivityCheckClient
else if(evt.getResponse().getMessageType()
== Response.BINDING_ERROR_RESPONSE)
{
if(! evt.getResponse().contains(Attribute.ERROR_CODE))
if(! evt.getResponse().containsAttribute(Attribute.ERROR_CODE))
{
logger.fine("Received a malformed error response.");
return; //malformed error response
@ -298,7 +298,7 @@ class ConnectivityCheckClient
CandidatePair checkedPair = ((CandidatePair)evt.getTransactionID()
.getApplicationData());
if(! response.contains(Attribute.XOR_MAPPED_ADDRESS))
if(! response.containsAttribute(Attribute.XOR_MAPPED_ADDRESS))
{
logger.fine("Received a success response with no "
+"XOR_MAPPED_ADDRESS attribute.");
@ -432,7 +432,7 @@ class ConnectivityCheckClient
//CANDIDATE attribute in the Binding request, the valid pair generated
//from that check has its nominated flag set to true.
if(parentAgent.isControlling()
&& request.contains(Attribute.USE_CANDIDATE))
&& request.containsAttribute(Attribute.USE_CANDIDATE))
{
parentAgent.nominationConfirmed( validPair );
}
@ -504,7 +504,7 @@ class ConnectivityCheckClient
if(errorCode.getErrorCode() == ErrorCodeAttribute.ROLE_CONFLICT)
{
boolean wasControlling = originalRequest
.contains(Attribute.ICE_CONTROLLING);
.containsAttribute(Attribute.ICE_CONTROLLING);
logger.finer("Swithing to isControlling=" + !wasControlling);
parentAgent.setControlling(!wasControlling);

View File

@ -13,6 +13,7 @@ import org.ice4j.attribute.*;
import org.ice4j.message.*;
import org.ice4j.security.*;
import org.ice4j.stack.*;
/**
* The class that would be handling and responding to incoming connectivity
* checks.
@ -56,7 +57,7 @@ class ConnectivityCheckServer
/**
* Handles the {@link Request} delivered in <tt>evt</tt> by possibly
* queueing a triggered check and sending a success or or error response
* queuing a triggered check and sending a success or an error response
* depending on how processing goes.
*
* @param evt the {@link StunMessageEvent} containing the {@link Request}
@ -94,16 +95,17 @@ class ConnectivityCheckServer
//detect role conflicts
if( ( parentAgent.isControlling()
&& request.contains(Attribute.ICE_CONTROLLING))
&& request.containsAttribute(Attribute.ICE_CONTROLLING))
|| ( ! parentAgent.isControlling()
&& request.contains(Attribute.ICE_CONTROLLED)))
&& request.containsAttribute(Attribute.ICE_CONTROLLED)))
{
if (!repairRoleConflict(evt))
return;
}
long priority = extractPriority(request);
boolean useCandidate = request.contains(Attribute.USE_CANDIDATE);
boolean useCandidate
= request.containsAttribute(Attribute.USE_CANDIDATE);
//tell our address handler we saw a new remote address;
parentAgent.incomingCheckReceived(evt.getRemoteAddress(),
@ -187,7 +189,7 @@ class ConnectivityCheckServer
// If the agent is in the controlling role, and the
// ICE-CONTROLLING attribute is present in the request:
if(parentAgent.isControlling()
&& req.contains(Attribute.ICE_CONTROLLING))
&& req.containsAttribute(Attribute.ICE_CONTROLLING))
{
IceControllingAttribute controlling = (IceControllingAttribute)
req.getAttribute(Attribute.ICE_CONTROLLING);
@ -234,7 +236,7 @@ class ConnectivityCheckServer
// If the agent is in the controlled role, and the ICE-CONTROLLED
// attribute is present in the request:
else if(!parentAgent.isControlling()
&& req.contains(Attribute.ICE_CONTROLLED))
&& req.containsAttribute(Attribute.ICE_CONTROLLED))
{
IceControlledAttribute controlled = (IceControlledAttribute)
req.getAttribute(Attribute.ICE_CONTROLLED);

View File

@ -42,11 +42,6 @@ public class HostCandidate extends LocalCandidate
*/
private final DatagramSocket socket;
/**
* The <tt>DatagramPacketFilter</tt> that only accepts STUN messages.
*/
private DatagramPacketFilter stunDatagramPacketFilter;
/**
* Creates a HostCandidate for the specified transport address.
*
@ -99,6 +94,7 @@ public class HostCandidate extends LocalCandidate
Throwable exception = null;
if (hostSocket instanceof MultiplexingDatagramSocket)
{
try
{
stunSocket
@ -108,12 +104,13 @@ public class HostCandidate extends LocalCandidate
}
catch (SocketException sex)//don't u just luv da name? ;)
{
logger.log( Level.SEVERE,
"Failed to acquire DatagramSocket"
+ " specific to STUN communication.",
sex);
logger.log(Level.SEVERE,
"Failed to acquire DatagramSocket"
+ " specific to STUN communication.",
sex);
exception = sex;
}
}
if (stunSocket == null)
throw new IllegalArgumentException("hostCand", exception);
else

View File

@ -13,8 +13,6 @@ import org.ice4j.*;
import org.ice4j.attribute.*;
import org.ice4j.stack.*;
import sun.tools.tree.*;
/**
* This class represents a STUN message. STUN messages are TLV (type-length-value)
* encoded using big endian (network ordered) binary. All STUN messages start
@ -308,7 +306,7 @@ public abstract class Message
* @return <tt>true</tt> if the this <tt>Message</tt> contains an attribute
* with the specified type or <tt>false</tt> otherwise.
*/
public boolean contains(char attributeType)
public boolean containsAttribute(char attributeType)
{
return attributes.containsKey(attributeType);
}
@ -491,12 +489,13 @@ public abstract class Message
{
switch (messageType)
{
case BINDING_REQUEST: return "BINDING-REQUEST";
case BINDING_SUCCESS_RESPONSE: return "BINDING-RESPONSE";
case BINDING_ERROR_RESPONSE: return "BINDING-ERROR-RESPONSE";
case SHARED_SECRET_REQUEST: return "SHARED-SECRET-REQUEST";
case SHARED_SECRET_RESPONSE: return "SHARED-SECRET-RESPONSE";
case SHARED_SECRET_ERROR_RESPONSE: return "SHARED-SECRET-ERROR-RESPONSE";
case ALLOCATE_REQUEST: return "ALLOCATE-REQUEST";
case BINDING_REQUEST: return "BINDING-REQUEST";
case BINDING_SUCCESS_RESPONSE: return "BINDING-RESPONSE";
case BINDING_ERROR_RESPONSE: return "BINDING-ERROR-RESPONSE";
case SHARED_SECRET_REQUEST: return "SHARED-SECRET-REQUEST";
case SHARED_SECRET_RESPONSE: return "SHARED-SECRET-RESPONSE";
case SHARED_SECRET_ERROR_RESPONSE: return "SHARED-SECRET-ERROR-RESPONSE";
}
return "UNKNOWN-MESSAGE";
@ -705,7 +704,7 @@ public abstract class Message
offset+=TRANSACTION_ID_LENGTH;
while(offset - Message.HEADER_LENGTH< length)
while(offset - Message.HEADER_LENGTH < length)
{
Attribute att = AttributeDecoder.decode(
binMessage, offset, (char)(length - offset));
@ -824,7 +823,6 @@ public abstract class Message
if(getAttributePresentity(i) == M && getAttribute(i) == null)
throw new IllegalStateException(
"A mandatory attribute (type=" +(int)i + ") is missing!");
}
/**
@ -886,12 +884,27 @@ public abstract class Message
*
* @return a <tt>String</tt> representation of this message.
*/
@Override
public String toString()
{
return getName()+"(0x"+Integer.toHexString(getMessageType())
+")[attrib.count=" + getAttributeCount()
+" len=" + (int) this.getDataLength()
+" tranID=" + TransactionID.toString(getTransactionID()) + "]";
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(getName());
stringBuilder.append("(0x");
stringBuilder.append(Integer.toHexString(getMessageType()));
stringBuilder.append(")[attrib.count=");
stringBuilder.append(getAttributeCount());
stringBuilder.append(" len=");
stringBuilder.append((int) this.getDataLength());
byte[] transactionID = getTransactionID();
if (transactionID != null)
{
stringBuilder.append(" tranID=");
stringBuilder.append(TransactionID.toString(transactionID));
}
stringBuilder.append("]");
return stringBuilder.toString();
}
}

View File

@ -6,6 +6,7 @@
*/
package org.ice4j.message;
import java.io.*;
import java.util.logging.*;
import org.ice4j.*;
@ -20,9 +21,8 @@ import org.ice4j.attribute.*;
*/
public class MessageFactory
{
private static final Logger logger = Logger
.getLogger(MessageFactory.class
.getName());
private static final Logger logger
= Logger.getLogger(MessageFactory.class.getName());
/**
* Creates a default binding request. The request DOES NOT contains a
@ -374,34 +374,64 @@ public class MessageFactory
}
/**
* Add the required attributes for long-term authentication.<br/>
* This will also add a MESSAGE-INTEGRITY and FINGERPRINT attribute.<br/>
* Do not add another attributes after called this method.
* Adds the <tt>Attribute</tt>s to a specific <tt>Request</tt> which support
* the STUN long-term credential mechanism.
* <p>
* <b>Warning</b>: The MESSAGE-INTEGRITY <tt>Attribute</tt> will also be
* added so <tt>Attribute</tt>s added afterwards will not be taken into
* account for the calculation of the MESSAGE-INTEGRITY value. For example,
* the FINGERPRINT <tt>Attribute</tt> may still safely be added afterwards,
* because it is known to appear after the MESSAGE-INTEGRITY.
* </p>
*
* @param request the original request
* @param username username value
* @param realm realm value
* @param nonce nonce value
* @param request the <tt>Request</tt> in which the <tt>Attribute</tt>s of
* the STUN long-term credential mechanism are to be added
* @param username the value for the USERNAME <tt>Attribute</tt> to be added
* to <tt>request</tt>
* @param realm the value for the REALM <tt>Attribute</tt> to be added to
* <tt>request</tt>
* @param nonce the value for the NONCE <tt>Attribute</tt> to be added to
* <tt>request</tt>
*
* @throws StunException in case we have a problem creating the username
* realm or nonce attributes.
* @throws StunException if anything goes wrong while adding the
* <tt>Attribute</tt>s to <tt>request</tt> which support the STUN long-term
* credential mechanism
*/
public static void addLongTermAuthentifcationAttribute(
Request request, byte username[], byte realm[],
byte nonce[]) throws StunException
public static void addLongTermCredentialAttributes(
Request request,
byte username[], byte realm[], byte nonce[])
throws StunException
{
UsernameAttribute usernameAttr = AttributeFactory
.createUsernameAttribute(username);
RealmAttribute realmAttr = AttributeFactory
.createRealmAttribute(realm);
NonceAttribute nonceAttr = AttributeFactory
.createNonceAttribute(nonce);
UsernameAttribute usernameAttribute
= AttributeFactory.createUsernameAttribute(username);
RealmAttribute realmAttribute
= AttributeFactory.createRealmAttribute(realm);
NonceAttribute nonceAttribute
= AttributeFactory.createNonceAttribute(nonce);
request.addAttribute(usernameAttr);
request.addAttribute(realmAttr);
request.addAttribute(nonceAttr);
request.addAttribute(usernameAttribute);
request.addAttribute(realmAttribute);
request.addAttribute(nonceAttribute);
/* TODO calculate MESSAGE-INTEGRITY and FINGERPRINT */
// MESSAGE-INTEGRITY
MessageIntegrityAttribute messageIntegrityAttribute;
try
{
/*
* The value of USERNAME is a variable-length value. It MUST contain
* a UTF-8 [RFC3629] encoded sequence of less than 513 bytes, and
* MUST have been processed using SASLprep [RFC4013].
*/
messageIntegrityAttribute
= AttributeFactory.createMessageIntegrityAttribute(
new String(username, "UTF-8"));
}
catch (UnsupportedEncodingException ueex)
{
throw new StunException("username", ueex);
}
request.addAttribute(messageIntegrityAttribute);
}
/**

View File

@ -13,7 +13,6 @@ package org.ice4j.message;
*
* @author Emil Ivov
*/
public class Request extends Message
{
@ -22,7 +21,6 @@ public class Request extends Message
*/
Request()
{
}
/**
@ -43,5 +41,4 @@ public class Request extends Message
super.setMessageType(requestType);
}
}

View File

@ -6,16 +6,16 @@
*/
package org.ice4j.message;
import org.ice4j.*;
/**
* A response descendant of the message class. The primary purpose of the
* Response class is to allow better functional definition of the classes in the
* stack package.
*
* @author Emil Ivov
* @author Lubomir Marinov
*/
public class Response extends Message
public class Response
extends Message
{
/**
@ -25,6 +25,28 @@ public class Response extends Message
{
}
/**
* Determines whether this instance represents a STUN error response.
*
* @return <tt>true</tt> if this instance represents a STUN error response;
* otherwise, <tt>false</tt>
*/
public boolean isErrorResponse()
{
return isErrorResponseType(getMessageType());
}
/**
* Determines whether this instance represents a STUN success response.
*
* @return <tt>true</tt> if this instance represents a STUN success
* response; otherwise, <tt>false</tt>
*/
public boolean isSuccessResponse()
{
return isSuccessResponseType(getMessageType());
}
/**
* Checks whether responseType is a valid response type and if yes sets it
* as the type of the current instance.
@ -37,8 +59,8 @@ public class Response extends Message
{
if(!isResponseType(responseType))
throw new IllegalArgumentException(
(int)(responseType)
+ " - is not a valid response type.");
Integer.toString(responseType)
+ " is not a valid response type.");
super.setMessageType(responseType);

View File

@ -9,7 +9,7 @@ package org.ice4j.security;
/**
* The {@link CredentialsAuthority} interface is implemented by applications
* in order to allow the stack to verify the integrity of incoming messages
* containing the <tt>MessageIntegrity</tt> attribute.
* containing the <tt>MessageIntegrityAttribute</tt>.
*
* @author Emil Ivov
*/

View File

@ -7,7 +7,6 @@
package org.ice4j.security;
import java.util.*;
import java.util.logging.*;
/**
* The <tt>CredentialsManager</tt> allows an application to handle verification

View File

@ -9,7 +9,6 @@ package org.ice4j.stack;
import java.util.logging.*;
import org.ice4j.*;
import org.ice4j.attribute.*;
import org.ice4j.message.*;
/**

View File

@ -9,14 +9,11 @@ package org.ice4j.stack;
import org.ice4j.*;
import org.ice4j.message.*;
import com.sun.corba.se.impl.protocol.*;
/**
* Handles incoming requests.
*
* @author Emil Ivov
*/
public interface RequestListener
{
/**

View File

@ -19,8 +19,6 @@ import org.ice4j.attribute.*;
import org.ice4j.message.*;
import org.ice4j.security.*;
import com.sun.corba.se.impl.protocol.*;
/**
* The entry point to the Stun4J stack. The class is used to start, stop and
* configure the stack.
@ -693,8 +691,9 @@ public class StunStack
}
//assert Message Integrity
MessageIntegrityAttribute msgIntAttr = (MessageIntegrityAttribute)
request.getAttribute(Attribute.MESSAGE_INTEGRITY);
MessageIntegrityAttribute msgIntAttr
= (MessageIntegrityAttribute)
request.getAttribute(Attribute.MESSAGE_INTEGRITY);
if (msgIntAttr != null)
{