public void Receive_CrawlAck(CrawlAck ack, G2ReceivedPacket packet) { DhtContact source = new DhtContact(ack.Source, packet.Source.IP); if (!CrawlMap.ContainsKey(source.RoutingID)) { return; } CrawlNode node = CrawlMap[source.RoutingID]; node.Ack = ack; foreach (DhtContact contact in ack.ProxyServers) { if (!CrawlMap.ContainsKey(contact.RoutingID)) { CrawlMap[contact.RoutingID] = new CrawlNode(contact); } } foreach (DhtContact contact in ack.ProxyClients) { if (!CrawlMap.ContainsKey(contact.RoutingID)) { CrawlNode newNode = new CrawlNode(contact); newNode.Proxy = node; CrawlMap[contact.RoutingID] = newNode; } } }
public CachedIP(byte name, DhtContact contact, bool bootstrap) { Name = name; LastSeen = contact.LastSeen; Contact = contact; Bootstrap = bootstrap; }
/* object TimerLock = new object(); * object[] PacketsLock = new object[4] { new object(), new object(), new object(), new object() }; * * ManualResetEvent[] WaitHandles = new ManualResetEvent[5] { new ManualResetEvent(true), new ManualResetEvent(true), * new ManualResetEvent(true), new ManualResetEvent(true), * new ManualResetEvent(true) }; * * * void OldRun() * { * int pumps = 4; * List<SimPacket> tempList = new List<SimPacket>(); * * Thread timerThread = new Thread(RunTimer); * timerThread.Start(); * * Thread[] packetsThread = new Thread[4]; * for (int i = 0; i < 4; i++) * { * packetsThread[i] = new Thread(RunPackets); * packetsThread[i].Start(i); * } * * Thread.Sleep(1000); // lets new threads reach wait( * * * while (true && !Shutdown) * { * if (Paused && !Step) * { * Thread.Sleep(250); * continue; * } * * // load users * if (Flux) * DoFlux(); * * * // instance timer * lock (TimerLock) * { * WaitHandles[0].Reset(); * Monitor.Pulse(TimerLock); * } * * // pump packets, 4 times (250ms latency * for (int i = 0; i < pumps; i++) * { * // clear out buffer by switching with in buffer * lock (PacketHandle) * { * tempList = InPackets; * InPackets = OutPackets; * OutPackets = tempList; * } * * for (int index = 0; index < 4; index++) * lock (PacketsLock[index]) * { * WaitHandles[1 + index].Reset(); * Monitor.Pulse(PacketsLock[index]); * } * * * AutoResetEvent.WaitAll(WaitHandles); * * InPackets.Clear(); * * TimeNow = TimeNow.AddMilliseconds(1000 / pumps); * * if(UpdateView != null) * UpdateView(); * * if (Step || Shutdown) * { * Step = false; * break; * } * } * * // if run sim slow * if (SleepTime > 0) * Thread.Sleep(SleepTime); * } * * * lock(TimerLock) * Monitor.Pulse(TimerLock); * * for(int i = 0; i < 4; i++) * lock(PacketsLock[i]) * Monitor.Pulse(PacketsLock[i]); * } * * void RunTimer() * { * while (true && !Shutdown) * { * lock (TimerLock) * { * Monitor.Wait(TimerLock); * * lock (Instances) * foreach (SimInstance instance in Instances) * instance.Context.SecondTimer_Tick(null, null); * * WaitHandles[0].Set(); * } * } * } * * void RunPackets(object val) * { * int index = (int)val; * * while (true && !Shutdown) * { * lock (PacketsLock[index]) * { * Monitor.Wait(PacketsLock[index]); * * // send packets * foreach (SimPacket packet in InPackets) * { * // 0 - global udp * // 1 - global tcp * // 2 - op udp * // 3 - op tcp * * if ((index == 0 && packet.Type == SimPacketType.Udp && packet.Dest.IsGlobal) || * (index == 1 && packet.Type != SimPacketType.Udp && packet.Dest.IsGlobal) || * (index == 2 && packet.Type == SimPacketType.Udp && !packet.Dest.IsGlobal) || * (index == 3 && packet.Type != SimPacketType.Udp && !packet.Dest.IsGlobal)) * { * * switch (packet.Type) * { * case SimPacketType.Udp: * packet.Dest.Core.Sim.BytesRecvd += (ulong)packet.Packet.Length; * packet.Dest.UdpControl.OnReceive(packet.Packet, packet.Packet.Length, packet.Source); * break; * case SimPacketType.TcpConnect: * TcpConnect socket = packet.Dest.TcpControl.OnAccept(null, packet.Source); * * if (socket != null) * { * TcpSourcetoDest[packet.Tcp] = socket; * TcpSourcetoDest[socket] = packet.Tcp; * * packet.Tcp.OnConnect(); * } * * break; * case SimPacketType.Tcp: * if (TcpSourcetoDest.ContainsKey(packet.Tcp)) * { * TcpConnect dest = TcpSourcetoDest[packet.Tcp]; * * dest.Core.Sim.BytesRecvd += (ulong)packet.Packet.Length; * * packet.Packet.CopyTo(dest.RecvBuffer, dest.RecvBuffSize); * dest.OnReceive(packet.Packet.Length); * } * break; * case SimPacketType.TcpClose: * if (TcpSourcetoDest.ContainsKey(packet.Tcp)) * { * TcpConnect dest = TcpSourcetoDest[packet.Tcp]; * dest.OnReceive(0); * * TcpSourcetoDest.Remove(packet.Tcp); * TcpSourcetoDest.Remove(dest); * } * break; * } * * } * } * * WaitHandles[1 + index].Set(); * } * } * } */ public void DownloadCache(DhtNetwork network) { WebCacheHits++; List <DhtNetwork> open = new List <DhtNetwork>(); // find matching networks that are potential cache entries Instances.LockReading(() => { foreach (DeOpsContext context in from i in Instances where i.RealFirewall == FirewallType.Open && i != network.Core.Sim select i.Context) { if (context.Lookup != null && context.Lookup.Network.OpID == network.OpID) { open.Add(context.Lookup.Network); } context.Cores.LockReading(() => open.AddRange(from c in context.Cores where c.Network.OpID == network.OpID select c.Network)); } }); // give back 3 random cache entries foreach (DhtNetwork net in open.OrderBy(n => RndGen.Next()).Take(3)) { DhtContact contact = net.GetLocalContact(); contact.IP = net.Core.Sim.RealIP; contact.LastSeen = net.Core.TimeNow; network.Cache.AddContact(contact); } }
public static InvitePackage OpenInvite(byte[] decrypted, G2Protocol protocol) { // if we get down here, opening invite was success MemoryStream mem = new MemoryStream(decrypted); PacketStream stream = new PacketStream(mem, protocol, FileAccess.Read); InvitePackage package = new InvitePackage(); G2Header root = null; while (stream.ReadPacket(ref root)) { if (root.Name == InvitePacket.Info) { package.Info = OneWayInvite.Decode(root); } if (root.Name == InvitePacket.Contact) { package.Contacts.Add(DhtContact.ReadPacket(root)); } if (root.Name == InvitePacket.WebCache) { package.Caches.Add(WebCache.Decode(root)); } } return(package); }
public void SecondTimer() { // ip cache lock (IPs) while (IPs.Count > MAX_CACHE) { DhtContact entry = IPs.Last.Value; IPTable.Remove(entry.CacheHash()); IPs.RemoveLast(); } // save cache if (Core.TimeNow > NextSave) { if (Network.IsLookup) { Network.Lookup.Save(Core); } else { Core.User.Save(); } NextSave = Core.TimeNow.AddMinutes(5); } // if unresponsive if (!Network.Responsive) { Retry.Timer(); if (Network.IsLookup) { LoookupBootstrap(); } else { OpBootstrap(); } } // send broadcast in lan mode every 20 secs if (Network.LanMode)//&& !IsLookup) //crit re-enable? { // if disconnected from LAN, once reconnected, establishing should be < 20 secs if (BroadcastTimeout <= 0) { Ping ping = new Ping(); ping.Source = Network.GetLocalSource(); Network.LanControl.SendTo(ping); BroadcastTimeout = 20; } else { BroadcastTimeout--; } } }
public static ProxyAck Decode(G2ReceivedPacket packet) { ProxyAck pa = new ProxyAck(); G2Header child = new G2Header(packet.Root.Data); while (G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD) { switch (child.Name) { case Packet_Source: if (G2Protocol.ReadPayload(child)) { pa.Source = DhtSource.ReadPacket(child); } break; case Packet_Accept: pa.Accept = true; break; case Packet_Contacts: if (G2Protocol.ReadPayload(child)) { pa.ContactList.Add(DhtContact.ReadPacket(child)); } break; } } return(pa); }
public static CachedIP Decode(G2Header root) { CachedIP saved = new CachedIP(); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_Contact: saved.Contact = DhtContact.ReadPacket(child); break; case Packet_LastSeen: saved.LastSeen = DateTime.FromBinary(BitConverter.ToInt64(child.Data, child.PayloadPos)); break; case Packet_Bootstrap: saved.Bootstrap = BitConverter.ToBoolean(child.Data, child.PayloadPos); break; } } saved.Contact.LastSeen = saved.LastSeen; return(saved); }
List <byte[]> Store_Replicate(DhtContact contact) { DataReq req = new DataReq(null, Core.UserID, ServiceID, 0, LocalClient.SignedData); // only replicating to open nodes directly Network.Store.Send_StoreReq(contact, null, req); return(null); }
List <byte[]> Store_Replicate(DhtContact contact) { if (GlobalIM) // cache area doesnt change with network in global IM mode { return(null); } // indicates cache area has changed, move contacts between out and in range // move in to out List <ulong> remove = new List <ulong>(); foreach (ulong user in InRange.Keys) { if (!Network.Routing.InCacheArea(user)) { OutofRange[user] = InRange[user]; remove.Add(user); } } foreach (ulong key in remove) { InRange.Remove(key); } // move out to in remove.Clear(); foreach (ulong user in OutofRange.Keys) { if (Network.Routing.InCacheArea(user)) { InRange[user] = OutofRange[user]; remove.Add(user); } } foreach (ulong key in remove) { OutofRange.Remove(key); } // invoke tags on data moving in range so all services are cached foreach (ulong key in remove) { InvokeTags(key, InRange[key]); } return(null); }
private void WebQueryResponse(WebCache cache, string response) { double timeout = 0; bool low = false; foreach (string line in response.Split('\n')) { // add to node cache if (line.StartsWith("node:")) { string[] parts = line.Substring(5).Split('/'); DhtContact contact = new DhtContact(ulong.Parse(parts[0]), 0, IPAddress.Parse(parts[1]), ushort.Parse(parts[2]), ushort.Parse(parts[3])); contact.LastSeen = Core.TimeNow; // set this so its put at front of list, and is the first to be attempted AddContact(contact); } // for use with publishing back to cache, dont set our local IP with it in case on // network with LAN nodes and web cache active, dont want to reset until we find nodes on external network else if (line.StartsWith("remoteip:")) { cache.RemoteIP = IPAddress.Parse(line.Substring(9)); } // set next publish time else if (line.StartsWith("timeout:")) { timeout = double.Parse(line.Substring(8)); } else if (line.StartsWith("load:low")) { low = true; } } cache.NextPublish = Core.TimeNow.AddMinutes(Math.Max(timeout, 15)); // dont change to min // if cache low auto publish to low cache so that network can be initialized if (low) { new Thread(WebPublish).Start(cache); } // more caches will be queried by the bootstrap if network not responsive }
public void Receive_SearchAck(SearchAck ack, G2ReceivedPacket packet) { DhtContact source = new DhtContact(ack.Source, packet.Source.IP); if (!CrawlMap.ContainsKey(source.RoutingID)) { CrawlMap[source.RoutingID] = new CrawlNode(source); } foreach (DhtContact contact in ack.ContactList) { if (!CrawlMap.ContainsKey(contact.RoutingID)) { CrawlMap[contact.RoutingID] = new CrawlNode(contact); } } }
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 CrawlAck Decode(G2ReceivedPacket packet) { CrawlAck ack = new CrawlAck(); 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: ack.Source = DhtSource.ReadPacket(child); break; case Packet_Version: ack.Version = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Uptime: ack.Uptime = BitConverter.ToInt32(child.Data, child.PayloadPos); break; case Packet_ProxyServers: ack.ProxyServers.Add(DhtContact.ReadPacket(child)); break; case Packet_ProxyClients: ack.ProxyClients.Add(DhtContact.ReadPacket(child)); break; } } return(ack); }
private void StartSearch(ulong user, uint version) { if (Core.InvokeRequired) { Core.RunInCoreAsync(delegate() { StartSearch(user, version); }); return; } byte[] parameters = BitConverter.GetBytes(version); DhtSearch search = Core.Network.Searches.Start(user, Core.GetServiceName(Service), Service, DataType, parameters, Search_Found); if (search != null) { search.TargetResults = 2; } // node is in our local cache area, so not flooding by directly connecting if ((!GlobalIM && Network.Routing.InCacheArea(user)) || (GlobalIM && Core.Buddies.BuddyList.SafeContainsKey(user))) { foreach (ClientInfo client in Core.Locations.GetClients(user)) { if (client.Data.TunnelClient == null) { Network.Searches.SendDirectRequest(new DhtAddress(client.Data.IP, client.Data.Source), user, Service, DataType, BitConverter.GetBytes(version)); } else { foreach (DhtAddress server in client.Data.TunnelServers) { DhtContact contact = new DhtContact(client.Data.Source, client.Data.IP, client.Data.TunnelClient, server); Network.Searches.SendDirectRequest(contact, user, Service, DataType, BitConverter.GetBytes(version)); } } } } }
List <byte[]> Store_Replicate(DhtContact contact) { List <byte[]> patches = new List <byte[]>(); FileMap.LockReading(delegate() { foreach (OpVersionedFile vfile in FileMap.Values) { if (Network.Routing.InCacheArea(vfile.UserID)) { byte[] id = BitConverter.GetBytes(vfile.UserID); byte[] ver = CompactNum.GetBytes(vfile.Header.Version); byte[] patch = new byte[id.Length + ver.Length]; id.CopyTo(patch, 0); ver.CopyTo(patch, 8); patches.Add(patch); } } }); return(patches); }
public static Bye Decode(G2ReceivedPacket packet) { Bye bye = new Bye(); G2Header child = new G2Header(packet.Root.Data); while (G2Protocol.ReadNextChild(packet.Root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { if (child.Name == Packet_Reconnect) { bye.Reconnect = true; } continue; } switch (child.Name) { case Packet_Source: bye.SenderID = BitConverter.ToUInt64(child.Data, child.PayloadPos); break; case Packet_Contacts: bye.ContactList.Add(DhtContact.ReadPacket(child)); break; case Packet_Message: bye.Message = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; } } return(bye); }
public void AddContact(DhtContact entry) { lock (IPs) { if (IPTable.ContainsKey(entry.CacheHash())) { entry = IPTable[entry.CacheHash()].Value; // replace entry with dupe to maintain next try info IPs.Remove(entry); } // sort nodes based on last seen LinkedListNode <DhtContact> node = null; for (node = IPs.First; node != null; node = node.Next) { if (entry.LastSeen > node.Value.LastSeen) { break; } } IPTable[entry.CacheHash()] = (node != null) ? IPs.AddBefore(node, entry) : IPs.AddLast(entry); } }
public string CreateBootstrapLink(DhtContact contact) { return(CreateBootstrapLink(contact.UserID, contact.IP, contact.TcpPort, contact.UdpPort)); }
public BootstrapItem(OpCore core, DhtContact contact) { Core = core; Contact = contact; }
public void ConsoleCommand(string command) { if (command == "clear") { ConsoleText.Clear(); } if (command == "pause") { PauseLog = !PauseLog; } ConsoleLog("> " + command); try { string[] commands = command.Split(' '); if (commands.Length == 0) { return; } if (commands[0] == "testDht" && commands.Length == 2) { int count = Convert.ToInt32(commands[1]); for (int i = 0; i < count; i++) { RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); UInt64 kid = Utilities.StrongRandUInt64(rng); // create random contact DhtContact contact = new DhtContact(kid, 7, new IPAddress(7), 7, 7); // add to routing Network.Routing.Add(contact); } } if (commands[0] == "gc") { GC.Collect(); } if (commands[0] == "killtcp") { /*ConsoleLog(TcpControl.Connections.Count.ToString() + " tcp sockets on list"); * * lock(TcpControl.Connections.SyncRoot) * foreach(TcpConnect connection in TcpControl.Connections) * connection.CleanClose("Force Disconnect");*/ } if (commands[0] == "fwstatus") { ConsoleLog("Status set to " + Firewall.ToString()); } if (commands[0] == "fwset" && commands.Length > 1) { if (commands[1] == "open") { SetFirewallType(FirewallType.Open); } if (commands[1] == "nat") { SetFirewallType(FirewallType.NAT); } if (commands[1] == "blocked") { SetFirewallType(FirewallType.Blocked); } } if (commands[0] == "listening") { /*ConsoleLog("Listening for TCP on port " + TcpControl.ListenPort.ToString()); * ConsoleLog("Listening for UDP on port " + UdpControl.ListenPort.ToString());*/ } if (commands[0] == "ping" && commands.Length > 0) { //string[] addr = commands[1].Split(':'); //GlobalNet.Send_Ping(IPAddress.Parse(addr[0]), Convert.ToUInt16(addr[1])); } if (commands[0] == "tcptest" && commands.Length > 0) { string[] addr = commands[1].Split(':'); //TcpControl.MakeOutbound(IPAddress.Parse(addr[0]), Convert.ToUInt16(addr[1]),0); } } catch (Exception ex) { ConsoleLog("Exception " + ex.Message); } }
void ConnectProxy() { // Get cloest contacts and sort by distance to us DhtContact attempt = null; // no Dht contacts, use ip cache will be used to connect tcp/udp in DoBootstrap // find if any contacts in list are worth trying (will be skipped if set already) // get closest contact that is not already connected foreach (DhtContact contact in Network.Routing.NearXor.Contacts) { // if havent tried in 10 minutes if (Core.TimeNow > contact.NextTryProxy && contact.TunnelClient == null) { bool connected = false; lock (SocketList) foreach (TcpConnect socket in SocketList) { if (contact.UserID == socket.UserID && contact.ClientID == socket.ClientID) { connected = true; break; } } if (connected) { continue; } if (attempt == null || (contact.UserID ^ Network.Local.UserID) < (attempt.UserID ^ Network.Local.UserID)) { attempt = contact; } } } if (attempt != null) { // take into account when making proxy request, disconnct furthest if (Core.Firewall == FirewallType.Blocked) { // continue attempted to test nat with pings which are small Network.Send_Ping(attempt); MakeOutbound(attempt, attempt.TcpPort, "try proxy"); } // if natted do udp proxy request first before connect else if (Core.Firewall == FirewallType.NAT) { ProxyReq request = new ProxyReq(); request.SenderID = Network.Local.UserID; request.Type = ProxyType.ClientNAT; Network.UdpControl.SendTo(attempt, request); } attempt.NextTryProxy = Core.TimeNow.AddMinutes(10); } }
public CrawlNode(DhtContact contact) { Contact = contact; }