📄 ecproto.cs
字号:
} public ecTagMD5(ECTagNames name, byte[] hash_data) : base(name, EcTagTypes.EC_TAGTYPE_HASH16) { m_val = hash_data; m_size = 16; } public ecTagMD5(ECTagNames name, BinaryReader br, LinkedList<ecTag> subtags) : base(name, EcTagTypes.EC_TAGTYPE_HASH16, subtags) { m_size = 16; m_val = br.ReadBytes(16); } public override void Write(BinaryWriter wr) { base.Write(wr); wr.Write(m_val); } public ecMD5 ValueMD5() { return new ecMD5(m_val); } } public class ecTagIPv4 : ecTag { Int32 m_addr; Int16 m_port; public ecTagIPv4(ECTagNames name, BinaryReader br, LinkedList<ecTag> subtags) : base(name, EcTagTypes.EC_TAGTYPE_IPV4, subtags) { m_size = 4+2; m_addr = System.Net.IPAddress.NetworkToHostOrder(br.ReadInt32()); m_port = System.Net.IPAddress.NetworkToHostOrder(br.ReadInt16()); } } public class ecTagCustom : ecTag { byte[] m_val; public ecTagCustom(ECTagNames n, Int32 tag_size, BinaryReader br, LinkedList<ecTag> subtags) : base(n, EcTagTypes.EC_TAGTYPE_CUSTOM, subtags) { m_val= br.ReadBytes(tag_size); m_size = tag_size; } public byte [] Value() { return m_val; } } public class ecTagString : ecTag { byte[] m_val; public ecTagString(ECTagNames n, string s) : base(n, EcTagTypes.EC_TAGTYPE_STRING) { m_val = System.Text.Encoding.UTF8.GetBytes(s); m_size = m_val.GetLength(0) + 1; } public ecTagString(ECTagNames n, Int32 tag_size, BinaryReader br, LinkedList<ecTag> subtags) : base(n, EcTagTypes.EC_TAGTYPE_STRING, subtags) { byte[] buf = br.ReadBytes(tag_size-1); // discard trailing '0' br.ReadBytes(1); m_size = tag_size; m_val = buf; } public override void Write(BinaryWriter wr) { base.Write(wr); wr.Write(m_val); byte zero_byte = 0; wr.Write(zero_byte); } public string StringValue() { Encoding u8 = Encoding.UTF8; string s = u8.GetString(m_val); return s; } } public class ecPacket : ecTag { // since I have no zlib here, effectively disable compression const int MaxUncompressedPacket = 0x6666; private ECOpCodes m_opcode; protected Int32 m_flags; // // Parsing ctor // ecTag ReadTag(BinaryReader br) { ecTag t = null; Int16 tag_name16 = System.Net.IPAddress.NetworkToHostOrder(br.ReadInt16()); bool have_subtags = ((tag_name16 & 1) != 0); ECTagNames tag_name = (ECTagNames)(tag_name16 >> 1); byte tag_type8 = br.ReadByte(); Int32 tag_size32 = System.Net.IPAddress.NetworkToHostOrder(br.ReadInt32()); LinkedList<ecTag> subtags = null; if ( have_subtags ) { subtags = ReadSubtags(br); } EcTagTypes tag_type = (EcTagTypes)tag_type8; switch (tag_type) { case EcTagTypes.EC_TAGTYPE_UNKNOWN: break; case EcTagTypes.EC_TAGTYPE_CUSTOM: t = new ecTagCustom(tag_name, tag_size32, br, subtags); break; case EcTagTypes.EC_TAGTYPE_UINT8: t = new ecTagInt(tag_name, 1, br, subtags); break; case EcTagTypes.EC_TAGTYPE_UINT16: t = new ecTagInt(tag_name, 2, br, subtags); break; case EcTagTypes.EC_TAGTYPE_UINT32: t = new ecTagInt(tag_name, 4, br, subtags); break; case EcTagTypes.EC_TAGTYPE_UINT64: t = new ecTagInt(tag_name, 8, br, subtags); break; case EcTagTypes.EC_TAGTYPE_STRING: t = new ecTagString(tag_name, tag_size32, br, subtags); break; case EcTagTypes.EC_TAGTYPE_DOUBLE: break; case EcTagTypes.EC_TAGTYPE_IPV4: t = new ecTagIPv4(tag_name, br, subtags); break; case EcTagTypes.EC_TAGTYPE_HASH16: t = new ecTagMD5(tag_name, br, subtags); break; default: break; } if ( t == null ) { throw new Exception("Unexpected tag type"); } return t; } LinkedList<ecTag> ReadSubtags(BinaryReader br) { Int16 count16 = System.Net.IPAddress.NetworkToHostOrder(br.ReadInt16()); LinkedList<ecTag> taglist = new LinkedList<ecTag>(); for (int i = 0; i < count16;i++) { ecTag st = ReadTag(br); taglist.AddLast(st); } return taglist; } public ecPacket() { m_flags = 0x20; } public ecPacket(BinaryReader br) { m_flags = System.Net.IPAddress.NetworkToHostOrder(br.ReadInt32()); Int32 packet_size = System.Net.IPAddress.NetworkToHostOrder(br.ReadInt32()); m_opcode = (ECOpCodes)br.ReadByte(); Int16 tags_count = System.Net.IPAddress.NetworkToHostOrder(br.ReadInt16()); if ( tags_count != 0 ) { for (int i = 0; i < tags_count; i++) { ecTag t = ReadTag(br); AddSubtag(t); } } } // // Default ctor - for tx packets public ecPacket(ECOpCodes cmd) { m_flags = 0x20; m_opcode = cmd; } public ecPacket(ECOpCodes cmd, EC_DETAIL_LEVEL detail_level) { m_flags = 0x20; m_opcode = cmd; if ( detail_level != EC_DETAIL_LEVEL.EC_DETAIL_FULL ) { AddSubtag(new ecTagInt(ECTagNames.EC_TAG_DETAIL_LEVEL, (Int64)detail_level)); } } // // Size of data for TX, not of payload // public int PacketSize() { int packet_size = Size(); if ((m_flags & (UInt32)ECFlags.EC_FLAG_ACCEPTS) != 0) { packet_size += 4; } // 1 (command) + 2 (tag count) + 4 (flags) + 4 (total size) return packet_size + 1 + 2 + 4 + 4; } public ECOpCodes Opcode() { return m_opcode; } public override void Write(BinaryWriter wr) { // 1 (command) + 2 (tag count) int packet_size = Size() + 1 + 2; if ( packet_size > MaxUncompressedPacket ) { m_flags |= (Int32)ECFlags.EC_FLAG_ZLIB; } if ((m_flags & (UInt32)ECFlags.EC_FLAG_ZLIB) != 0) { throw new NotImplementedException("no zlib compression yet"); } wr.Write(System.Net.IPAddress.HostToNetworkOrder((Int32)(m_flags))); if ((m_flags & (UInt32)ECFlags.EC_FLAG_ACCEPTS) != 0) { wr.Write(System.Net.IPAddress.HostToNetworkOrder((Int32)(m_flags))); } wr.Write(System.Net.IPAddress.HostToNetworkOrder((Int32)(packet_size))); wr.Write((byte)m_opcode); if ( m_subtags.Count != 0 ) { WriteSubtags(wr); } else { wr.Write((Int16)(0)); } } } // // Specific - purpose tags // public class ecLoginPacket : ecPacket { public ecLoginPacket(string client_name, string version, string pass) : base(ECOpCodes.EC_OP_AUTH_REQ) { m_flags |= 0x20 | (Int32)ECFlags.EC_FLAG_ACCEPTS; AddSubtag(new ecTagString(ECTagNames.EC_TAG_CLIENT_NAME, client_name)); AddSubtag(new ecTagString(ECTagNames.EC_TAG_CLIENT_VERSION, version)); AddSubtag(new ecTagInt(ECTagNames.EC_TAG_PROTOCOL_VERSION, (Int64)ProtocolVersion.EC_CURRENT_PROTOCOL_VERSION)); AddSubtag(new ecTagMD5(ECTagNames.EC_TAG_PASSWD_HASH, pass, false)); // discussion is ongoing //AddSubtag(new ecTagMD5(ECTagNames.EC_TAG_VERSION_ID, EC_VERSION_ID, true)); } } public class ecDownloadsInfoReq : ecPacket { public ecDownloadsInfoReq() : base(ECOpCodes.EC_OP_GET_DLOAD_QUEUE) { } } // // Class exists only for parsing purpose // public class ecConnStateTag { ecTagInt m_tag; Int32 m_tag_val; public ecConnStateTag(ecTagInt tag) { m_tag = tag; //m_tag_val = (Int32)tag.Value64(); m_tag_val = 0xfff; } //public static explicit operator ecConnStateTag(ecTagInt t) //{ // return new ecConnStateTag(t.ValueInt64()); //} public bool IsConnected() { return IsConnectedED2K() || IsConnectedKademlia(); } public bool IsConnectedED2K() { return (m_tag_val & 0x01) != 0; } public bool IsConnectingED2K() { return (m_tag_val & 0x02) != 0; } public bool IsConnectedKademlia() { return (m_tag_val & 0x04) != 0; } public bool IsKadFirewalled() { return (m_tag_val & 0x08) != 0; } public bool IsKadRunning() { return (m_tag_val & 0x10) != 0; } public ecProto.ecTag Server() { return m_tag.SubTag(ECTagNames.EC_TAG_SERVER); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -