public void GetIpV4BytesTest() { XorMappedAddress target = new XorMappedAddress(TurnMessageRfc.Rfc3489) { IpAddress = IPAddress.Parse("192.168.1.1"), Port = 0x15b3, }; byte[] expected = new byte[] { 0xee, 0xee, 0xee, 0xee, 0x00, 0x20, 0x00, 0x08, 0x00, 0x01, 0x34, 0xa1, 0xe1, 0xba, 0xa5, 0x43, }; byte[] actual = new byte[] { 0xee, 0xee, 0xee, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; int startIndex = 4; target.GetBytes(actual, ref startIndex, GetTransactionId()); Assert.AreEqual(16, startIndex); Helpers.AreArrayEqual(expected, actual); }
public void ParseIp4Test() { byte[] bytes = new byte[] { 0x00, 0x01, 0x00, 0x08, 0xff, 0x01, 0x34, 0xa1, 0xe1, 0xba, 0xa5, 0x43, }; int startIndex = 0; XorMappedAddress target = new XorMappedAddress(TurnMessageRfc.Rfc3489); target.Parse(bytes, ref startIndex, GetTransactionId()); Assert.AreEqual(12, startIndex); Assert.AreEqual(AttributeType.XorMappedAddressStun, target.AttributeType); Assert.AreEqual(0x15b3, target.Port); Assert.AreEqual(@"192.168.1.1", target.IpAddress.ToString()); }
private void ParseAttributes(byte[] bytes, int startIndex, int length, TurnMessageRfc rfc) { int currentIndex = startIndex + HeaderLength; int endIndex = startIndex + length; while (currentIndex < endIndex) { UInt16 attributeType1 = bytes.BigendianToUInt16(currentIndex); if (Enum.IsDefined(typeof(AttributeType), (Int32)attributeType1) == false) { throw new TurnMessageException(ErrorCode.UnknownAttribute); } AttributeType attributeType = (AttributeType)attributeType1; if (rfc == TurnMessageRfc.Rfc3489) { if (IsRfc3489(attributeType)) { throw new TurnMessageException(ErrorCode.UnknownAttribute); } } if (attributeType == AttributeType.Fingerprint) { if (Fingerprint == null) { if (storedBytes == null) { storedBytes = new byte[length]; Array.Copy(bytes, startIndex, storedBytes, 0, length); } fingerprintStartOffset = currentIndex - startIndex; Fingerprint = new Fingerprint(); Fingerprint.Parse(bytes, ref currentIndex); } else { Attribute.Skip(bytes, ref currentIndex); } } else if (MessageIntegrity != null) { Attribute.Skip(bytes, ref currentIndex); } else { switch (attributeType) { case AttributeType.AlternateServer: AlternateServer = new AlternateServer(); AlternateServer.Parse(bytes, ref currentIndex); break; case AttributeType.Bandwidth: Bandwidth = new Bandwidth(); Bandwidth.Parse(bytes, ref currentIndex); break; case AttributeType.Data: Data = new Data(); Data.Parse(bytes, ref currentIndex); break; case AttributeType.DestinationAddress: DestinationAddress = new DestinationAddress(); DestinationAddress.Parse(bytes, ref currentIndex); break; case AttributeType.ErrorCode: ErrorCodeAttribute = new ErrorCodeAttribute(); ErrorCodeAttribute.Parse(bytes, ref currentIndex); break; case AttributeType.Fingerprint: break; case AttributeType.Lifetime: Lifetime = new Lifetime(); Lifetime.Parse(bytes, ref currentIndex); break; case AttributeType.MagicCookie: MagicCookie = new MagicCookie(); MagicCookie.Parse(bytes, ref currentIndex); break; case AttributeType.MappedAddress: MappedAddress = new MappedAddress(); MappedAddress.Parse(bytes, ref currentIndex); break; case AttributeType.MessageIntegrity: messageIntegrityStartOffset = currentIndex - startIndex; if (storedBytes == null) { if (rfc == TurnMessageRfc.MsTurn) { storedBytes = new byte[GetPadded64(messageIntegrityStartOffset)]; Array.Copy(bytes, startIndex, storedBytes, 0, messageIntegrityStartOffset); } else { storedBytes = new byte[length]; Array.Copy(bytes, startIndex, storedBytes, 0, length); } } MessageIntegrity = new MessageIntegrity(); MessageIntegrity.Parse(bytes, ref currentIndex); break; case AttributeType.MsVersion: MsVersion = new MsVersion(); MsVersion.Parse(bytes, ref currentIndex); break; case AttributeType.MsSequenceNumber: MsSequenceNumber = new MsSequenceNumber(); MsSequenceNumber.Parse(bytes, ref currentIndex); break; case AttributeType.RemoteAddress: RemoteAddress = new RemoteAddress(); RemoteAddress.Parse(bytes, ref currentIndex); break; case AttributeType.Software: Software = new Software(); Software.Parse(bytes, ref currentIndex); break; case AttributeType.UnknownAttributes: UnknownAttributes = new UnknownAttributes(); // Not Implemented UnknownAttributes.Parse(bytes, ref currentIndex); break; case AttributeType.Username: if (MsVersion != null) { MsUsername = new MsUsername(); MsUsername.Parse(bytes, ref currentIndex); } else { Username = new Username(); Username.Parse(bytes, ref currentIndex); } break; // ietf-mmusic-ice case AttributeType.Priority: case AttributeType.UseCandidate: case AttributeType.IceControlled: case AttributeType.IceControlling: Attribute.Skip(bytes, ref currentIndex); break; // rfc3489 case AttributeType.ChangedAddress: ChangedAddress = new ChangedAddress(); ChangedAddress.Parse(bytes, ref currentIndex); break; case AttributeType.ChangeRequest: ChangeRequest = new ChangeRequest(); ChangeRequest.Parse(bytes, ref currentIndex); break; case AttributeType.ResponseAddress: ResponseAddress = new ResponseAddress(); ResponseAddress.Parse(bytes, ref currentIndex); break; case AttributeType.SourceAddress: SourceAddress = new SourceAddress(); SourceAddress.Parse(bytes, ref currentIndex); break; case AttributeType.ReflectedFrom: ReflectedFrom = new ReflectedFrom(); ReflectedFrom.Parse(bytes, ref currentIndex); break; case AttributeType.Password: Password = new Password(); Password.Parse(bytes, ref currentIndex); break; default: if (rfc == TurnMessageRfc.MsTurn) { switch (attributeType) { case AttributeType.Nonce: Nonce = new Nonce(rfc); Nonce.Parse(bytes, ref currentIndex); break; case AttributeType.Realm: Realm = new Realm(rfc); Realm.Parse(bytes, ref currentIndex); break; case AttributeType.XorMappedAddress: XorMappedAddress = new XorMappedAddress(rfc); XorMappedAddress.Parse(bytes, ref currentIndex, TransactionId); break; default: throw new NotImplementedException(); } } else { switch (attributeType) { case AttributeType.NonceStun: Nonce = new Nonce(rfc); Nonce.Parse(bytes, ref currentIndex); break; case AttributeType.RealmStun: Realm = new Realm(rfc); Realm.Parse(bytes, ref currentIndex); break; case AttributeType.XorMappedAddressStun: XorMappedAddress = new XorMappedAddress(rfc); XorMappedAddress.Parse(bytes, ref currentIndex, TransactionId); break; default: throw new NotImplementedException(); } } break; } } if (rfc != TurnMessageRfc.MsTurn) { if (currentIndex % 4 > 0) { currentIndex += 4 - currentIndex % 4; } } } }
public void XorMappedAddressConstructorTest() { XorMappedAddress target = new XorMappedAddress(TurnMessageRfc.Rfc3489); Assert.AreEqual(AttributeType.XorMappedAddressStun, target.AttributeType); }
private void ParseAttributes(byte[] bytes, int startIndex, int length, TurnMessageRfc rfc) { int currentIndex = startIndex + HeaderLength; int endIndex = startIndex + length; while (currentIndex < endIndex) { UInt16 attributeType1 = bytes.BigendianToUInt16(currentIndex); if (Enum.IsDefined(typeof(AttributeType), (Int32)attributeType1) == false) throw new TurnMessageException(ErrorCode.UnknownAttribute); AttributeType attributeType = (AttributeType)attributeType1; if (rfc == TurnMessageRfc.Rfc3489) if (IsRfc3489(attributeType)) throw new TurnMessageException(ErrorCode.UnknownAttribute); if (attributeType == AttributeType.Fingerprint) { if (Fingerprint == null) { if (storedBytes == null) { storedBytes = new byte[length]; Array.Copy(bytes, startIndex, storedBytes, 0, length); } fingerprintStartOffset = currentIndex - startIndex; Fingerprint = new Fingerprint(); Fingerprint.Parse(bytes, ref currentIndex); } else { Attribute.Skip(bytes, ref currentIndex); } } else if (MessageIntegrity != null) { Attribute.Skip(bytes, ref currentIndex); } else { switch (attributeType) { case AttributeType.AlternateServer: AlternateServer = new AlternateServer(); AlternateServer.Parse(bytes, ref currentIndex); break; case AttributeType.Bandwidth: Bandwidth = new Bandwidth(); Bandwidth.Parse(bytes, ref currentIndex); break; case AttributeType.Data: Data = new Data(); Data.Parse(bytes, ref currentIndex); break; case AttributeType.DestinationAddress: DestinationAddress = new DestinationAddress(); DestinationAddress.Parse(bytes, ref currentIndex); break; case AttributeType.ErrorCode: ErrorCodeAttribute = new ErrorCodeAttribute(); ErrorCodeAttribute.Parse(bytes, ref currentIndex); break; case AttributeType.Fingerprint: break; case AttributeType.Lifetime: Lifetime = new Lifetime(); Lifetime.Parse(bytes, ref currentIndex); break; case AttributeType.MagicCookie: MagicCookie = new MagicCookie(); MagicCookie.Parse(bytes, ref currentIndex); break; case AttributeType.MappedAddress: MappedAddress = new MappedAddress(); MappedAddress.Parse(bytes, ref currentIndex); break; case AttributeType.MessageIntegrity: messageIntegrityStartOffset = currentIndex - startIndex; if (storedBytes == null) { if (rfc == TurnMessageRfc.MsTurn) { storedBytes = new byte[GetPadded64(messageIntegrityStartOffset)]; Array.Copy(bytes, startIndex, storedBytes, 0, messageIntegrityStartOffset); } else { storedBytes = new byte[length]; Array.Copy(bytes, startIndex, storedBytes, 0, length); } } MessageIntegrity = new MessageIntegrity(); MessageIntegrity.Parse(bytes, ref currentIndex); break; case AttributeType.MsVersion: MsVersion = new MsVersion(); MsVersion.Parse(bytes, ref currentIndex); break; case AttributeType.MsSequenceNumber: MsSequenceNumber = new MsSequenceNumber(); MsSequenceNumber.Parse(bytes, ref currentIndex); break; case AttributeType.RemoteAddress: RemoteAddress = new RemoteAddress(); RemoteAddress.Parse(bytes, ref currentIndex); break; case AttributeType.Software: Software = new Software(); Software.Parse(bytes, ref currentIndex); break; case AttributeType.UnknownAttributes: UnknownAttributes = new UnknownAttributes(); // Not Implemented UnknownAttributes.Parse(bytes, ref currentIndex); break; case AttributeType.Username: if (MsVersion != null) { MsUsername = new MsUsername(); MsUsername.Parse(bytes, ref currentIndex); } else { Username = new Username(); Username.Parse(bytes, ref currentIndex); } break; // ietf-mmusic-ice case AttributeType.Priority: case AttributeType.UseCandidate: case AttributeType.IceControlled: case AttributeType.IceControlling: Attribute.Skip(bytes, ref currentIndex); break; // rfc3489 case AttributeType.ChangedAddress: ChangedAddress = new ChangedAddress(); ChangedAddress.Parse(bytes, ref currentIndex); break; case AttributeType.ChangeRequest: ChangeRequest = new ChangeRequest(); ChangeRequest.Parse(bytes, ref currentIndex); break; case AttributeType.ResponseAddress: ResponseAddress = new ResponseAddress(); ResponseAddress.Parse(bytes, ref currentIndex); break; case AttributeType.SourceAddress: SourceAddress = new SourceAddress(); SourceAddress.Parse(bytes, ref currentIndex); break; case AttributeType.ReflectedFrom: ReflectedFrom = new ReflectedFrom(); ReflectedFrom.Parse(bytes, ref currentIndex); break; case AttributeType.Password: Password = new Password(); Password.Parse(bytes, ref currentIndex); break; default: if (rfc == TurnMessageRfc.MsTurn) { switch (attributeType) { case AttributeType.Nonce: Nonce = new Nonce(rfc); Nonce.Parse(bytes, ref currentIndex); break; case AttributeType.Realm: Realm = new Realm(rfc); Realm.Parse(bytes, ref currentIndex); break; case AttributeType.XorMappedAddress: XorMappedAddress = new XorMappedAddress(rfc); XorMappedAddress.Parse(bytes, ref currentIndex, TransactionId); break; default: throw new NotImplementedException(); } } else { switch (attributeType) { case AttributeType.NonceStun: Nonce = new Nonce(rfc); Nonce.Parse(bytes, ref currentIndex); break; case AttributeType.RealmStun: Realm = new Realm(rfc); Realm.Parse(bytes, ref currentIndex); break; case AttributeType.XorMappedAddressStun: XorMappedAddress = new XorMappedAddress(rfc); XorMappedAddress.Parse(bytes, ref currentIndex, TransactionId); break; default: throw new NotImplementedException(); } } break; } } if (rfc != TurnMessageRfc.MsTurn) { if (currentIndex % 4 > 0) currentIndex += 4 - currentIndex % 4; } } }