public byte[] getBytes() throws UtilityException { byte[] result = new byte[8]; // message attribute header // type System.arraycopy(Utility.integerToTwoBytes(typeToInteger(type)), 0, result, 0, 2); // length System.arraycopy(Utility.integerToTwoBytes(4), 0, result, 2, 2); // change request header if (changeIP) result[7] = Utility.integerToOneByte(4); if (changePort) result[7] = Utility.integerToOneByte(2); if (changeIP && changePort) result[7] = Utility.integerToOneByte(6); return result; }
public Address(byte[] address) throws UtilityException { if (address.length < 4) { throw new UtilityException("4 bytes are required."); } firstOctet = Utility.oneByteToInteger(address[0]); secondOctet = Utility.oneByteToInteger(address[1]); thirdOctet = Utility.oneByteToInteger(address[2]); fourthOctet = Utility.oneByteToInteger(address[3]); }
public void generateTransactionID() throws UtilityException { System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 0, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 2, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 4, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 6, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 8, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 10, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 12, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 14, 2); }
protected static MappedResponseChangedSourceAddressReflectedFrom parse(MappedResponseChangedSourceAddressReflectedFrom ma, byte[] data) throws MessageAttributeParsingException { try { if (data.length < 8) { throw new MessageAttributeParsingException("Data array too short"); } int family = Utility.oneByteToInteger(data[1]); if (family != 0x01) throw new MessageAttributeParsingException("Family " + family + " is not supported"); byte[] portArray = new byte[2]; System.arraycopy(data, 2, portArray, 0, 2); ma.setPort(Utility.twoBytesToInteger(portArray)); int firstOctet = Utility.oneByteToInteger(data[4]); int secondOctet = Utility.oneByteToInteger(data[5]); int thirdOctet = Utility.oneByteToInteger(data[6]); int fourthOctet = Utility.oneByteToInteger(data[7]); ma.setAddress(new Address(firstOctet, secondOctet, thirdOctet, fourthOctet)); return ma; } catch (UtilityException ue) { throw new MessageAttributeParsingException("Parsing error"); } catch (MessageAttributeException mae) { throw new MessageAttributeParsingException("Port parsing error"); } }
public void parseAttributes(byte[] data) throws MessageAttributeParsingException { try { byte[] lengthArray = new byte[2]; System.arraycopy(data, 2, lengthArray, 0, 2); int length = Utility.twoBytesToInteger(lengthArray); System.arraycopy(data, 4, id, 0, 16); byte[] cuttedData; int offset = 20; while (length > 0) { cuttedData = new byte[length]; System.arraycopy(data, offset, cuttedData, 0, length); MessageAttribute ma = MessageAttribute.parseCommonHeader(cuttedData); addMessageAttribute(ma); length -= ma.getLength(); offset += ma.getLength(); } } catch (UtilityException ue) { throw new MessageAttributeParsingException("Parsing error"); } }
public byte[] getBytes() throws UtilityException { byte[] result = new byte[4]; result[0] = Utility.integerToOneByte(firstOctet); result[1] = Utility.integerToOneByte(secondOctet); result[2] = Utility.integerToOneByte(thirdOctet); result[3] = Utility.integerToOneByte(fourthOctet); return result; }
public static UnknownAttribute parse(byte[] data) throws MessageAttributeParsingException { try { UnknownAttribute result = new UnknownAttribute(); if (data.length % 4 != 0) throw new MessageAttributeParsingException("Data array too short"); for (int i = 0; i < data.length; i += 4) { byte[] temp = new byte[4]; System.arraycopy(data, i, temp, 0, 4); long attri = Utility.fourBytesToLong(temp); result.addAttribute(MessageAttribute.intToType(attri)); } return result; } catch (UtilityException ue) { throw new MessageAttributeParsingException("Parsing error"); } } }
public void generateTransactionID() throws UtilityException { System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 0, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 2, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 4, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 6, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 8, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 10, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 12, 2); System.arraycopy(Utility.integerToTwoBytes((int)(Math.random() * 65536)), 0, id, 14, 2); }
protected static MappedResponseChangedSourceAddressReflectedFrom parse(MappedResponseChangedSourceAddressReflectedFrom ma, byte[] data) throws MessageAttributeParsingException { try { if (data.length < 8) { throw new MessageAttributeParsingException("Data array too short"); } int family = Utility.oneByteToInteger(data[1]); if (family != 0x01) throw new MessageAttributeParsingException("Family " + family + " is not supported"); byte[] portArray = new byte[2]; System.arraycopy(data, 2, portArray, 0, 2); ma.setPort(Utility.twoBytesToInteger(portArray)); int firstOctet = Utility.oneByteToInteger(data[4]); int secondOctet = Utility.oneByteToInteger(data[5]); int thirdOctet = Utility.oneByteToInteger(data[6]); int fourthOctet = Utility.oneByteToInteger(data[7]); ma.setAddress(new Address(firstOctet, secondOctet, thirdOctet, fourthOctet)); return ma; } catch (UtilityException ue) { throw new MessageAttributeParsingException("Parsing error"); } catch (MessageAttributeException mae) { throw new MessageAttributeParsingException("Port parsing error"); } }
public void parseAttributes(byte[] data) throws MessageAttributeParsingException { try { byte[] lengthArray = new byte[2]; System.arraycopy(data, 2, lengthArray, 0, 2); int length = Utility.twoBytesToInteger(lengthArray); System.arraycopy(data, 4, id, 0, 16); byte[] cuttedData; int offset = 20; while (length > 0) { cuttedData = new byte[length]; System.arraycopy(data, offset, cuttedData, 0, length); MessageAttribute ma = MessageAttribute.parseCommonHeader(cuttedData); addMessageAttribute(ma); length -= ma.getLength(); offset += ma.getLength(); } } catch (UtilityException ue) { throw new MessageAttributeParsingException("Parsing error"); } }
public byte[] getBytes() throws UtilityException { byte[] result = new byte[4]; result[0] = Utility.integerToOneByte(firstOctet); result[1] = Utility.integerToOneByte(secondOctet); result[2] = Utility.integerToOneByte(thirdOctet); result[3] = Utility.integerToOneByte(fourthOctet); return result; }
public static UnknownAttribute parse(byte[] data) throws MessageAttributeParsingException { try { UnknownAttribute result = new UnknownAttribute(); if (data.length % 4 != 0) throw new MessageAttributeParsingException("Data array too short"); for (int i = 0; i < data.length; i += 4) { byte[] temp = new byte[4]; System.arraycopy(data, i, temp, 0, 4); long attri = Utility.fourBytesToLong(temp); result.addAttribute(MessageAttribute.intToType(attri)); } return result; } catch (UtilityException ue) { throw new MessageAttributeParsingException("Parsing error"); } } }
public byte[] getBytes() throws UtilityException { byte[] result = new byte[8]; // message attribute header // type System.arraycopy(Utility.integerToTwoBytes(typeToInteger(type)), 0, result, 0, 2); // length System.arraycopy(Utility.integerToTwoBytes(4), 0, result, 2, 2); // change request header if (changeIP) result[7] = Utility.integerToOneByte(4); if (changePort) result[7] = Utility.integerToOneByte(2); if (changeIP && changePort) result[7] = Utility.integerToOneByte(6); return result; }
public byte[] getBytes() throws UtilityException { byte[] result = new byte[lengthValue + 4]; // message attribute header // type System.arraycopy(Utility.integerToTwoBytes(typeToInteger(type)), 0, result, 0, 2); // length System.arraycopy(Utility.integerToTwoBytes(lengthValue), 0, result, 2, 2); return result; }
public Address(byte[] address) throws UtilityException { if (address.length < 4) { throw new UtilityException("4 bytes are required."); } firstOctet = Utility.oneByteToInteger(address[0]); secondOctet = Utility.oneByteToInteger(address[1]); thirdOctet = Utility.oneByteToInteger(address[2]); fourthOctet = Utility.oneByteToInteger(address[3]); }
public static MessageHeader parseHeader(byte[] data) throws MessageHeaderParsingException { try { MessageHeader mh = new MessageHeader(); byte[] typeArray = new byte[2]; System.arraycopy(data, 0, typeArray, 0, 2); int type = Utility.twoBytesToInteger(typeArray); switch (type) { case BINDINGREQUEST: mh.setType(MessageHeaderType.BindingRequest); LOGGER.debug("Binding Request received."); break; case BINDINGRESPONSE: mh.setType(MessageHeaderType.BindingResponse); LOGGER.debug("Binding Response received."); break; case BINDINGERRORRESPONSE: mh.setType(MessageHeaderType.BindingErrorResponse); LOGGER.debug("Binding Error Response received."); break; case SHAREDSECRETREQUEST: mh.setType(MessageHeaderType.SharedSecretRequest); LOGGER.debug("Shared Secret Request received."); break; case SHAREDSECRETRESPONSE: mh.setType(MessageHeaderType.SharedSecretResponse); LOGGER.debug("Shared Secret Response received."); break; case SHAREDSECRETERRORRESPONSE: mh.setType(MessageHeaderType.SharedSecretErrorResponse); LOGGER.debug("Shared Secret Error Response received.");break; default: throw new MessageHeaderParsingException("Message type " + type + "is not supported"); } return mh; } catch (UtilityException ue) { throw new MessageHeaderParsingException("Parsing error"); } } }
public InetAddress getInetAddress() throws UtilityException, UnknownHostException { byte[] address = new byte[4]; address[0] = Utility.integerToOneByte(firstOctet); address[1] = Utility.integerToOneByte(secondOctet); address[2] = Utility.integerToOneByte(thirdOctet); address[3] = Utility.integerToOneByte(fourthOctet); return InetAddress.getByAddress(address); }
public byte[] getBytes() throws UtilityException { byte[] result = new byte[12]; // message attribute header // type System.arraycopy(Utility.integerToTwoBytes(typeToInteger(type)), 0, result, 0, 2); // length System.arraycopy(Utility.integerToTwoBytes(8), 0, result, 2, 2); // mappedaddress header // family result[5] = Utility.integerToOneByte(0x01); // port System.arraycopy(Utility.integerToTwoBytes(port), 0, result, 6, 2); // address System.arraycopy(address.getBytes(), 0, result, 8, 4); return result; }
public byte[] getBytes() throws UtilityException { byte[] result = new byte[lengthValue + 4]; // message attribute header // type System.arraycopy(Utility.integerToTwoBytes(typeToInteger(type)), 0, result, 0, 2); // length System.arraycopy(Utility.integerToTwoBytes(lengthValue), 0, result, 2, 2); return result; }
public static ErrorCode parse(byte[] data) throws MessageAttributeParsingException { try { if (data.length < 4) { throw new MessageAttributeParsingException("Data array too short"); } byte classHeaderByte = data[3]; int classHeader = Utility.oneByteToInteger(classHeaderByte); if ((classHeader < 1) || (classHeader > 6)) throw new MessageAttributeParsingException("Class parsing error"); byte numberByte = data[4]; int number = Utility.oneByteToInteger(numberByte); if ((number < 0) || (number > 99)) throw new MessageAttributeParsingException("Number parsing error"); int responseCode = (classHeader * 100) + number; ErrorCode result = new ErrorCode(); result.setResponseCode(responseCode); return result; } catch (UtilityException ue) { throw new MessageAttributeParsingException("Parsing error"); } catch (MessageAttributeException mae) { throw new MessageAttributeParsingException("Parsing error"); } } }