public override void ReadRequest(NetworkPacket packet) { segmentLen = 0; startPos = packet.ReadUInt(); //first stream segment if (startPos == 0) { uint totalBytes = packet.ReadUInt(); command = packet.ReadString(); byteData = new byte[totalBytes]; segmentLen = packet.ReadUInt(); byte[] bytes = packet.ReadBytes((int)segmentLen); SetBuffer(bytes, 0); return; } //subsequent stream segments segmentLen = packet.ReadUInt(); byteData = packet.ReadBytes((int)segmentLen); }
public override void ReadResponse(NetworkPacket packet) { methodId = (ushort)((uint)packet.ReadUShort() & 0x3FFF); //0x3F (0011 1111) sets left most 2 bits to 00 methodLength = packet.ReadUShort(); transactionID = packet.ReadBytes(16); header.ackkey = BitConverter.ToUInt32(transactionID, 0); method = (STUNMethod)methodId; //Console.WriteLine("STUN Response address: " + packet.remoteEndPoint.ToString()); //Console.WriteLine("STUN Response Method: " + Enum.GetName(typeof(STUNMethod), method)); //Console.WriteLine("STUN Response Length: " + methodLength); while (packet.bytePos < packet.byteLength) { STUNAddress address; STUNAttribute attrType = (STUNAttribute)packet.ReadUShort(); attributeTypes.Add(attrType); //Console.WriteLine("STUN Attr Type: " + Enum.GetName(typeof(STUNAttribute), attrType)); switch (attrType) { case STUNAttribute.MappedAddress: case STUNAttribute.SourceAddress: case STUNAttribute.ChangedAddress: address = ReadMappedAddress(packet); response.Add(attrType, address); break; case STUNAttribute.XorMappedAddress: case STUNAttribute.XorRelayedAddress: address = ReadXorMappedAddress(packet); response.Add(attrType, address); break; case STUNAttribute.ErrorCode: response.Add(attrType, ReadErrorCode(packet)); break; case STUNAttribute.UnknownAttribute: response.Add(attrType, ReadUnknownAttributes(packet)); break; case STUNAttribute.ServerName: response.Add(attrType, ReadString(packet)); break; case STUNAttribute.Realm: response.Add(attrType, ReadString(packet)); break; case STUNAttribute.Username: response.Add(attrType, ReadString(packet)); break; default: ushort attrLen = packet.ReadUShort(); byte[] bytes = packet.ReadBytes(attrLen); response.Add(attrType, bytes); while (((attrLen++) % 4) != 0) { packet.ReadByte(); } break; } } }