/** * Sends a binding request to the specified server address with only change * port flag set to true and change IP flag - to false. * @param serverAddress the address where to send the bindingRequest. * @return The returned message encapsulating event or null if no message * was received. * @throws StunException if an exception occurs while sending the messge */ private StunMessageEvent doTestIII(StunAddress serverAddress) { Request request = MessageFactory.CreateBindingRequest(); ChangeRequestAttribute changeRequest = (ChangeRequestAttribute)request.GetAttribute(Attribute.CHANGE_REQUEST); changeRequest.SetChangeIpFlag(false); changeRequest.SetChangePortFlag(true); StunMessageEvent evt = requestSender.SendRequestAndWaitForResponse(request, serverAddress); #if false if (evt != null) { System.oout.println("Test III res=" + evt.getRemoteAddress().toString() + " - " + evt.getRemoteAddress().getHostName()); } else { Console.WriteLine("NO RESPONSE received to Test III."); } #endif return(evt); }
/** * Compares two STUN Attributes. Attributeas are considered equal when their * type, length, and all data are the same. * * @param obj the object to compare this attribute with. * @return true if the attributes are equal and false otherwise. */ public override bool Equals(Object obj) { if (!(obj is ChangeRequestAttribute) || obj == null) { return(false); } if (obj == this) { return(true); } ChangeRequestAttribute att = (ChangeRequestAttribute)obj; if (att.GetAttributeType() != GetAttributeType() || att.GetDataLength() != GetDataLength() //compare data || att.GetChangeIpFlag() != GetChangeIpFlag() || att.GetChangePortFlag() != GetChangePortFlag() ) { return(false); } return(true); }
CreateChangeRequestAttribute(bool changeIP, bool changePort) { ChangeRequestAttribute attribute = new ChangeRequestAttribute(); attribute.SetChangeIpFlag(changeIP); attribute.SetChangePortFlag(changePort); return(attribute); }
/** * Creates a default binding request. The request contains a ChangeReqeust * attribute with zero change ip and change port flags. * @return a default binding request. */ public static Request CreateBindingRequest() { Request bindingRequest = new Request(); try { bindingRequest.SetMessageType(Message.BINDING_REQUEST); } catch (StunException ex) { //there should be no exc here since we're the creators. Console.WriteLine("Exception {0} {1}", ex.Message, ex.StackTrace); } //add a change request attribute ChangeRequestAttribute attribute = AttributeFactory.CreateChangeRequestAttribute(); bindingRequest.AddAttribute(attribute); return(bindingRequest); }
/** * Creates a ChangeRequestAttribute with the specified flag values. * @param changeIP the value of the changeIP flag. * @param changePort the value of the changePort flag. * @return the newly created ChangeRequestAttribute. */ public static ChangeRequestAttribute CreateChangeRequestAttribute(bool changeIP, bool changePort) { ChangeRequestAttribute attribute = new ChangeRequestAttribute(); attribute.SetChangeIpFlag(changeIP); attribute.SetChangePortFlag(changePort); return attribute; }
/** * Decodes the specified binary array and returns the corresponding * attribute object. * @param bytes the binary array that should be decoded. * @param offset the index where the message starts. * @param length the number of bytes that the message is long. * @return An object representing the attribute encoded in bytes or null if * the attribute was not recognized. * @throws StunException if bytes does is not a valid STUN attribute. */ public static Attribute decode(byte[] bytes, int offset, int length) { if(bytes == null || bytes.Length < Attribute.HEADER_LENGTH) throw new StunException(StunException.ILLEGAL_ARGUMENT, "Could not decode the specified binary array."); //Discover attribute type int attributeTypec = (int)(((bytes[offset]<<8)&0xff00)|(bytes[offset + 1] & 0xff)); int attributeType = (int) attributeTypec; int attributeLengthc = (int)(((bytes[offset + 2]<<8)&0xff00)|(bytes[offset + 3]&0xff)); int attributeLength = (int) attributeLengthc; if(attributeLength > bytes.Length - offset ) { throw new StunException(StunException.ILLEGAL_ARGUMENT, "The indicated attribute length ("+attributeLength+") " +"does not match the length of the passed binary array"); } Attribute decodedAttribute = null; switch(attributeType) { case Attribute.CHANGE_REQUEST: decodedAttribute = new ChangeRequestAttribute(); break; case Attribute.CHANGED_ADDRESS: decodedAttribute = new ChangedAddressAttribute(); break; case Attribute.MAPPED_ADDRESS: decodedAttribute = new MappedAddressAttribute(); break; case Attribute.ERROR_CODE: decodedAttribute = new ErrorCodeAttribute(); break; case Attribute.MESSAGE_INTEGRITY: throw new StunException( "The MESSAGE-INTEGRITY Attribute is not yet implemented."); case Attribute.PASSWORD: throw new StunException( "The PASSWORD Attribute is not yet implemented."); case Attribute.REFLECTED_FROM: decodedAttribute = new ReflectedFromAttribute(); break; case Attribute.RESPONSE_ADDRESS: decodedAttribute = new ResponseAddressAttribute(); break; case Attribute.SOURCE_ADDRESS: decodedAttribute = new SourceAddressAttribute(); break; case Attribute.UNKNOWN_ATTRIBUTES: decodedAttribute = new UnknownAttributesAttribute(); break; case Attribute.USERNAME: throw new StunException( "The USERNAME Attribute is not yet implemented."); //According to rfc3489 we should silently ignore unknown attributes. default: decodedAttribute = new UnknownAttributesAttribute(); break; //return null; } decodedAttribute.SetAttributeType(attributeType); decodedAttribute.DecodeAttributeBody(bytes, (Attribute.HEADER_LENGTH + offset), attributeLength); return decodedAttribute; }
/** * Decodes the specified binary array and returns the corresponding * attribute object. * @param bytes the binary array that should be decoded. * @param offset the index where the message starts. * @param length the number of bytes that the message is long. * @return An object representing the attribute encoded in bytes or null if * the attribute was not recognized. * @throws StunException if bytes does is not a valid STUN attribute. */ public static Attribute decode(byte[] bytes, int offset, int length) { if (bytes == null || bytes.Length < Attribute.HEADER_LENGTH) { throw new StunException(StunException.ILLEGAL_ARGUMENT, "Could not decode the specified binary array."); } //Discover attribute type int attributeTypec = (int)(((bytes[offset] << 8) & 0xff00) | (bytes[offset + 1] & 0xff)); int attributeType = (int)attributeTypec; int attributeLengthc = (int)(((bytes[offset + 2] << 8) & 0xff00) | (bytes[offset + 3] & 0xff)); int attributeLength = (int)attributeLengthc; if (attributeLength > bytes.Length - offset) { throw new StunException(StunException.ILLEGAL_ARGUMENT, "The indicated attribute length (" + attributeLength + ") " + "does not match the length of the passed binary array"); } Attribute decodedAttribute = null; switch (attributeType) { case Attribute.CHANGE_REQUEST: decodedAttribute = new ChangeRequestAttribute(); break; case Attribute.CHANGED_ADDRESS: decodedAttribute = new ChangedAddressAttribute(); break; case Attribute.MAPPED_ADDRESS: decodedAttribute = new MappedAddressAttribute(); break; case Attribute.ERROR_CODE: decodedAttribute = new ErrorCodeAttribute(); break; case Attribute.MESSAGE_INTEGRITY: throw new StunException( "The MESSAGE-INTEGRITY Attribute is not yet implemented."); case Attribute.PASSWORD: throw new StunException( "The PASSWORD Attribute is not yet implemented."); case Attribute.REFLECTED_FROM: decodedAttribute = new ReflectedFromAttribute(); break; case Attribute.RESPONSE_ADDRESS: decodedAttribute = new ResponseAddressAttribute(); break; case Attribute.SOURCE_ADDRESS: decodedAttribute = new SourceAddressAttribute(); break; case Attribute.UNKNOWN_ATTRIBUTES: decodedAttribute = new UnknownAttributesAttribute(); break; case Attribute.USERNAME: throw new StunException( "The USERNAME Attribute is not yet implemented."); //According to rfc3489 we should silently ignore unknown attributes. default: decodedAttribute = new UnknownAttributesAttribute(); break; //return null; } decodedAttribute.SetAttributeType(attributeType); decodedAttribute.DecodeAttributeBody(bytes, (Attribute.HEADER_LENGTH + offset), attributeLength); return(decodedAttribute); }