void LocalSync_TagReceived(ulong user, byte[] tag) { if (tag.Length == 0) { return; } uint version = 0; OpVersionedFile file = GetFile(user); if (file != null) { version = CompactNum.ToUInt32(tag, 0, tag.Length); // version old, so we need the latest localSync file // wont cause loop because localsync's fileAquired will only fire on new version of localSync if (version < file.Header.Version) { Core.Sync.Research(user); } } // if newer file on network, or this node is in our cache area, find it if ((file != null && version > file.Header.Version) || (file == null && ((!GlobalIM && Network.Routing.InCacheArea(user)) || (GlobalIM && Core.Buddies.BuddyList.SafeContainsKey(user))))) { StartSearch(user, version); // this could be called from a patch given to another user, direct connect not gauranteed } }
public static CommData Decode(G2Header root) { CommData data = new CommData(); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_Service: data.Service = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_DataType: data.DataType = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Data: data.Data = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; } } return(data); }
public static SearchReq Decode(G2ReceivedPacket packet) { SearchReq req = new SearchReq(); G2Header child = new G2Header(packet.Root.Data); while (G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_Source: req.Source = DhtSource.ReadPacket(child); break; case Packet_SearchID: req.SearchID = BitConverter.ToUInt32(child.Data, child.PayloadPos); break; case Packet_Target: req.TargetID = BitConverter.ToUInt64(child.Data, child.PayloadPos); break; case Packet_Service: req.Service = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_DataType: req.DataType = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Parameters: req.Parameters = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Nodes: req.Nodes = BitConverter.ToBoolean(child.Data, child.PayloadPos); break; case Packet_EndSearch: req.EndProxySearch = true; break; } } return(req); }
void Search_Local(ulong key, byte[] parameters, List <byte[]> results) { uint version = CompactNum.ToUInt32(parameters, 0, parameters.Length); // if local version equal to or greater, send back signed packet if (Core.Context.SignedUpdate == null || !Core.Context.SignedUpdate.Loaded || Core.Context.SignedUpdate.SequentialVersion < version) { return; } results.Add(Core.Context.SignedUpdate.Encode(Core.Network.Protocol)); }
void Store_Patch(DhtAddress source, byte[] data) { if (data.Length < 9) { return; } ulong user = BitConverter.ToUInt64(data, 0); if (!Network.Routing.InCacheArea(user)) { return; } uint version = CompactNum.ToUInt32(data, 8, data.Length - 8); OpVersionedFile vfile = GetFile(user); if (vfile != null && vfile.Header != null) { if (vfile.Header.Version > version) { Store.Send_StoreReq(source, null, new DataReq(null, vfile.UserID, Service, DataType, vfile.SignedHeader)); return; } vfile.Unique = false; // network has current or newer version if (vfile.Header.Version == version) { return; } // else our version is old, download below } if (Network.Established) { Network.Searches.SendDirectRequest(source, user, Service, DataType, BitConverter.GetBytes(version)); } else { DownloadLater[user] = version; } }
public static PatchTag FromBytes(byte[] data, int pos, int size) { PatchTag tag = new PatchTag(); byte control = data[pos]; int sLength = (control & 0x38) >> 3; int dLength = (control & 0x07); tag.Service = CompactNum.ToUInt32(data, pos + 1, sLength); tag.DataType = CompactNum.ToUInt32(data, pos + 1 + sLength, dLength); int dataPos = 1 + sLength + dLength; tag.Tag = Utilities.ExtractBytes(data, pos + dataPos, size - dataPos); return(tag); }
public static SearchAck Decode(G2ReceivedPacket packet) { SearchAck ack = new SearchAck(); G2Header child = new G2Header(packet.Root.Data); while (G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD) { if (child.Name == Packet_Proxied) { ack.Proxied = true; continue; } if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_Source: ack.Source = DhtSource.ReadPacket(child); break; case Packet_SearchID: ack.SearchID = BitConverter.ToUInt32(child.Data, child.PayloadPos); break; case Packet_Contacts: ack.ContactList.Add(DhtContact.ReadPacket(child)); break; case Packet_Service: ack.Service = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Values: ack.ValueList.Add(Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize)); break; } } return(ack); }
public static StoreReq Decode(G2ReceivedPacket packet) { StoreReq req = new StoreReq(); G2Header child = new G2Header(packet.Root.Data); while (G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_Source: req.Source = DhtSource.ReadPacket(child); break; case Packet_Key: req.Key = BitConverter.ToUInt64(child.Data, child.PayloadPos); break; case Packet_Service: req.Service = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_DataType: req.DataType = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Data: req.Data = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_TTL: req.TTL = BitConverter.ToUInt16(child.Data, child.PayloadPos); break; } } return(req); }
public static VersionedFileHeader Decode(G2Header root) { VersionedFileHeader header = new VersionedFileHeader(); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_Key: header.Key = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); header.KeyID = Utilities.KeytoID(header.Key); break; case Packet_Version: header.Version = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_FileHash: header.FileHash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_FileSize: header.FileSize = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_FileKey: header.FileKey = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Extra: header.Extra = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; } } return(header); }
void Locations_TagReceived(DhtAddress address, ulong user, byte[] tag) { // if user not cached, we only active search their info if in local cache area if (tag.Length == 0) { return; } uint version = 0; OpVersionedFile file = Cache.GetFile(user); if (file != null) { version = CompactNum.ToUInt32(tag, 0, tag.Length); if (version < file.Header.Version) { Store.Send_StoreReq(address, null, new DataReq(null, file.UserID, ServiceID, DataTypeSync, file.SignedHeader)); } } // get new version of local sync file if ((file != null && version > file.Header.Version) || (file == null && ((!GlobalIM && Network.Routing.InCacheArea(user)) || (GlobalIM && Core.Buddies.BuddyList.SafeContainsKey(user))))) { Cache.Research(user); } // ensure we have the lastest versions of the user's services if (file != null) { CheckTags(file.UserID); } }
public static FileDetails Decode(G2Header root) { FileDetails packet = new FileDetails(); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_Service: packet.Service = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_DataType: packet.DataType = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Hash: packet.Hash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Size: packet.Size = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Extra: packet.Extra = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; } } return(packet); }
public static LocationPing Decode(G2Header root) { LocationPing ping = new LocationPing(); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_RemoteVersion: ping.RemoteVersion = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; } } return(ping); }
public static Pong Decode(G2ReceivedPacket packet) { Pong po = new Pong(); G2Header child = new G2Header(packet.Root.Data); while (G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { if (child.Name == Packet_Direct) { po.Direct = true; } continue; } switch (child.Name) { case Packet_Source: po.Source = DhtSource.ReadPacket(child); break; case Packet_RemoteIP: po.RemoteIP = new IPAddress(Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize)); break; case Packet_Version: po.Version = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; } } return(po); }
public static LocationData Decode(byte[] data) { G2Header root = new G2Header(data); G2Protocol.ReadPacket(root); if (root.Name != LocationPacket.Data) { return(null); } LocationData loc = new LocationData(); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_Key: loc.Key = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); loc.UserID = Utilities.KeytoID(loc.Key); break; case Packet_Source: loc.Source = DhtSource.ReadPacket(child); loc.UserID = loc.Source.UserID; // encode light doesnt send full key break; case Packet_IP: loc.IP = new IPAddress(Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize)); break; case Packet_Proxies: loc.Proxies.Add(DhtAddress.ReadPacket(child)); break; case Packet_Name: loc.Name = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Place: loc.Place = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Version: loc.Version = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_GMTOffset: loc.GmtOffset = BitConverter.ToInt32(child.Data, child.PayloadPos); break; case Packet_Away: loc.Away = BitConverter.ToBoolean(child.Data, child.PayloadPos); break; case Packet_AwayMsg: loc.AwayMessage = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Tag: loc.Tags.Add(PatchTag.FromBytes(child.Data, child.PayloadPos, child.PayloadSize)); break; case Packet_TunnelClient: loc.TunnelClient = TunnelAddress.FromBytes(child.Data, child.PayloadPos); break; case Packet_TunnelServers: loc.TunnelServers.Add(DhtAddress.ReadPacket(child)); break; } } return(loc); }
public static UpdateInfo Decode(G2Header root) { // embedded public key of source we allow updates from RSACryptoServiceProvider UpdateSourcePublicKey = new RSACryptoServiceProvider(); //UpdateSourcePublicKey.FromXmlString("<RSAKeyValue><Modulus>pTmHLSxyM9TDOM4tZzI5dld9JvPsHlHC/M5i0+Qtjid1DiefGAVubPToEhK9Im4Ohy37h5Ax6J3vt2pxLG4rnIDuKBJt70YH6W6XrJewQ6tid5BvVnNEzPUOIJHGMpOnyi0VjPpzZzWgp4JK6Yuh6LtsYwCyqIIJIBt9iQ/9XN0=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"); UpdateInfo info = new UpdateInfo(); if (G2Protocol.ReadPayload(root)) { info.Embedded = Utilities.ExtractBytes(root.Data, root.PayloadPos, root.PayloadSize); } G2Protocol.ResetPacket(root); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (G2Protocol.ReadPayload(child)) { if (child.Name == Packet_Signature) { info.Signature = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); } } } // verify signature byte[] pubKey = UpdateSourcePublicKey.ExportParameters(false).Modulus; if (!Utilities.CheckSignedData(pubKey, info.Embedded, info.Signature)) { return(null); } root = new G2Header(info.Embedded); if (!G2Protocol.ReadPacket(root)) { return(null); } child = new G2Header(info.Embedded); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_Name: info.Name = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Key: info.Key = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Size: info.Size = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Hash: info.Hash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Notes: info.Notes = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Beginning: info.Beginning = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_DottedVersion: info.DottedVersion = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_SequentialVersion: info.SequentialVersion = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; } } info.TempName = Utilities.ToBase64String(info.Hash); return(info); }