private bool CanClose() { while (MainForm.Instance.LocalAddress == null) { ; } if (MainForm.Instance.LocalAddress.ToString() == txtServer.Text) { txtServer.Text = "127.0.0.1"; } Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { ConnectionHandler.ConnectTo(s, txtServer.Text, int.Parse(tempPortTxt.Text)); } catch { MessageBox.Show("Unable to connect to the server."); return(false); } ModUpdaterNetworkStream str = new ModUpdaterNetworkStream(s); Packet.Send(new HandshakePacket { Type = HandshakePacket.SessionType.ServerList }, str); Packet p = Packet.ReadPacket(str); //The server should only return a ServerList, right? ServerListPacket sp = null; if (!(p is ServerListPacket)) //But just in case... { Packet.Send(new DisconnectPacket(), str); ConnectTo = new Server { Address = txtServer.Text, Port = int.Parse(tempPortTxt.Text), Name = "Not Yet Defined." }; str.Close(); s.Disconnect(false); return(true); } sp = (ServerListPacket)p; List <Server> servers = new List <Server>(); for (int i = 0; i < sp.Servers.Length; i++) { Server srv = new Server { Address = sp.Locations[i], Name = sp.Servers[i], Port = sp.Ports[i] }; servers.Add(srv); } SelectServerDialog dial = new SelectServerDialog(servers.ToArray()); dial.ShowDialog(); if (dial.DialogResult != System.Windows.Forms.DialogResult.OK) { return(false); } ConnectTo = dial.SelectedServer; return(true); }
public void Start() { Address = IPAddress.Loopback; try { string direction = ""; WebRequest request = WebRequest.Create("http://checkip.dyndns.org/"); using (WebResponse response = request.GetResponse()) { using (StreamReader stream = new StreamReader(response.GetResponseStream())) { direction = stream.ReadToEnd(); } } int first = direction.IndexOf("Address: ") + 9; int last = direction.LastIndexOf("</body>"); direction = direction.Substring(first, last - first); Address = IPAddress.Parse(direction); } catch (Exception e) { MinecraftModUpdater.Logger.Log(e); } MinecraftModUpdater.Logger.Log(Logger.Level.Info, "Server IP Address is: " + Address.ToString()); TcpServer.Start(); Online = true; TaskManager.AddAsyncTask(delegate { SimpleConsoleInputHandler(); }); TaskManager.AddAsyncTask(delegate { if (Config.MasterServer != "") { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string ip = Config.MasterServer.Split(':')[0].Trim(); int port = int.Parse(Config.MasterServer.Split(':')[1].Trim()); ConnectionHandler.ConnectTo(s, ip, port); PacketHandler ph = new PacketHandler(s); ph.Start(); Thread.Sleep(1000); Packet.Send(new HandshakePacket { Name = Config.ServerName, Port = Config.Port, Address = Address.ToString(), Type = HandshakePacket.SessionType.Server }, ph.Stream); } }, ThreadRole.Delayed, 1000); TaskManager.AddAsyncTask(delegate { string ver; bool api; if (Extras.CheckForUpdate("server", Program.Version, out ver, out api)) { if (!api) { MinecraftModUpdater.Logger.Log(Logger.Level.Info, "Version {0} is now available for Minecraft Mod Updater.", ver); } else { MinecraftModUpdater.Logger.Log(Logger.Level.Info, "Version {0} is now available for Minecraft Mod Updater API.", ver); } } }); Receive(); }
protected void ConnectionHandlerTest(Node node0, Node node1, ConnectionHandler ch0, ConnectionHandler ch1) { var mdh0 = new MockDataHandler(); var mdh1 = new MockDataHandler(); MemBlock zero = MemBlock.Reference(new byte[] { 0 }); EventHandler cb = delegate(object o, EventArgs ea) { Assert.AreEqual(o, zero, "Zero"); }; mdh0.HandleDataCallback += cb; mdh1.HandleDataCallback += cb; ch0.Subscribe(mdh0, null); ch1.Subscribe(mdh1, null); Assert.AreEqual(mdh0.Count, 0, "MDH0 0"); Assert.AreEqual(mdh1.Count, 0, "MDH1 0"); ch0.ConnectTo(node1.Address); Assert.IsTrue(AreConnected(node0, node1), "ConnectionHandler ConnectTo"); SimpleTimer.RunSteps(fifteen_mins * 2, false); Assert.IsFalse(Simulator.AreConnected(node0, node1)); ch0.Send(node1.Address, zero); SimpleTimer.RunSteps(fifteen_mins / 60, false); Assert.AreEqual(mdh0.Count, 0, "MDH0 1"); Assert.AreEqual(mdh1.Count, 0, "MDH1 1"); Assert.IsTrue(AreConnected(node0, node1), "ConnectionHandler ConnectTo0"); SimpleTimer.RunSteps(fifteen_mins / 3, false); ch0.Send(node1.Address, zero); SimpleTimer.RunSteps(fifteen_mins / 60, false); Assert.AreEqual(mdh0.Count, 0, "MDH0 2"); Assert.AreEqual(mdh1.Count, 1, "MDH1 2"); Assert.IsTrue(Simulator.AreConnected(node0, node1), "Continuous 0"); SimpleTimer.RunSteps(fifteen_mins / 3, false); ch0.Send(node1.Address, zero); SimpleTimer.RunSteps(fifteen_mins / 60, false); Assert.AreEqual(mdh0.Count, 0, "MDH0 3"); Assert.AreEqual(mdh1.Count, 2, "MDH1 3"); Assert.IsTrue(Simulator.AreConnected(node0, node1), "Continuous 1"); SimpleTimer.RunSteps(fifteen_mins / 3, false); ch0.Send(node1.Address, zero); SimpleTimer.RunSteps(fifteen_mins / 60, false); Assert.AreEqual(mdh0.Count, 0, "MDH0 4"); Assert.AreEqual(mdh1.Count, 3, "MDH1 4"); Assert.IsTrue(Simulator.AreConnected(node0, node1), "Continuous 2"); SimpleTimer.RunSteps(fifteen_mins / 3, false); ch1.Send(node0.Address, zero); SimpleTimer.RunSteps(fifteen_mins / 60, false); Assert.AreEqual(mdh0.Count, 1, "MDH0 5"); Assert.AreEqual(mdh1.Count, 3, "MDH1 5"); Assert.IsTrue(Simulator.AreConnected(node0, node1), "Continuous 3"); SimpleTimer.RunSteps(fifteen_mins * 2, false); Assert.IsFalse(Simulator.AreConnected(node0, node1), "Dead"); Assert.AreEqual(mdh0.Count, 1, "MDH0 6"); Assert.AreEqual(mdh1.Count, 3, "MDH1 6"); }
/// <summary>Parses Arp Packets and writes to the Ethernet the translation.</summary> /// <remarks>IpopRouter makes nodes think they are in the same Layer 2 network /// so that two nodes in the same network can communicate directly with each /// other. IpopRouter masquerades for those that are not local.</remarks> /// <param name="ep">The Ethernet packet to translate</param> protected virtual void HandleArp(MemBlock packet) { // Can't do anything until we have network connectivity! if (_dhcp_server == null) { return; } ArpPacket ap = new ArpPacket(packet); // Not in our range! if (!_dhcp_server.IPInRange((byte[])ap.TargetProtoAddress) && !_dhcp_server.IPInRange((byte[])ap.SenderProtoAddress)) { ProtocolLog.WriteIf(IpopLog.Arp, String.Format("Bad Arp request from {0} for {1}", Utils.MemBlockToString(ap.SenderProtoAddress, '.'), Utils.MemBlockToString(ap.TargetProtoAddress, '.'))); return; } if (ap.Operation == ArpPacket.Operations.Reply) { // This would be a unsolicited Arp if (ap.TargetProtoAddress.Equals(IPPacket.BroadcastAddress) && !ap.SenderHWAddress.Equals(EthernetPacket.BroadcastAddress)) { HandleNewStaticIP(ap.SenderHWAddress, ap.SenderProtoAddress); } return; } // We only support request operation hereafter if (ap.Operation != ArpPacket.Operations.Request) { return; } // Must return nothing if the node is checking availability of IPs // Or he is looking himself up. if (_ip_to_ether.ContainsKey(ap.TargetProtoAddress) || ap.SenderProtoAddress.Equals(IPPacket.BroadcastAddress) || ap.SenderProtoAddress.Equals(IPPacket.ZeroAddress)) { return; } if (!ap.TargetProtoAddress.Equals(MemBlock.Reference(_dhcp_server.ServerIP))) { // Do not return messages if there is no connection to the remote address Address baddr = null; try { baddr = _address_resolver.Resolve(ap.TargetProtoAddress); } catch (AddressResolutionException ex) { if (ex.Issue != AddressResolutionException.Issues.DoesNotExist) { throw; } // Otherwise nothing to do, mapping doesn't exist... } if (AppNode.Node.Address.Equals(baddr) || baddr == null) { ProtocolLog.WriteIf(IpopLog.Arp, String.Format("No mapping for: {0}", Utils.MemBlockToString(ap.TargetProtoAddress, '.'))); return; } if (!_conn_handler.ContainsAddress(baddr)) { ProtocolLog.WriteIf(IpopLog.Arp, String.Format( "No connection to {0} for {1}", baddr, Utils.MemBlockToString(ap.TargetProtoAddress, '.'))); _conn_handler.ConnectTo(baddr); return; } } ProtocolLog.WriteIf(IpopLog.Arp, String.Format("Sending Arp response for: {0}", Utils.MemBlockToString(ap.TargetProtoAddress, '.'))); ArpPacket response = ap.Respond(EthernetPacket.UnicastAddress); EthernetPacket res_ep = new EthernetPacket(ap.SenderHWAddress, EthernetPacket.UnicastAddress, EthernetPacket.Types.Arp, response.ICPacket); Ethernet.Send(res_ep.ICPacket); }
private void Connect() { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket = s; Debug.Assert("Creating Objects."); try { string srv = Server.Address; int port = Server.Port; if (srv == LocalAddress.ToString()) { srv = "127.0.0.1"; } ConnectionHandler.ConnectTo(s, srv, port); SplashScreen.GetScreen().Progress.PerformStep(); } catch (SocketException ex) { Debug.Assert(ex); MessageBox.Show("There was an error while connecting to the update server. I will now self destruct."); Thread.Sleep(1000); SplashScreen.UpdateStatusTextWithStatus("Boom!!!", TypeOfMessage.Error); Thread.Sleep(5000); SplashScreen.UpdateStatusTextWithStatus("That was a joke, by the way.", TypeOfMessage.Warning); Thread.Sleep(1000); SplashScreen.CloseSplashScreen(); Thread.Sleep(3000); Close(); return; } catch (Exception ex) { ExceptionHandler.HandleException(ex, this); } modImages = new ImageList(); modImages.ImageSize = new Size(230, 180); modImages.ColorDepth = ColorDepth.Depth32Bit; SplashScreen.GetScreen().Progress.PerformStep(); TaskManager.AddAsyncTask(delegate { while (s.Connected) { ; } if (!warnDisconnect) { return; } if (SplashScreen.GetScreen() != null) { SplashScreen.UpdateStatusTextWithStatus("Lost connection to server.", TypeOfMessage.Error); Thread.Sleep(5000); } else { MessageBox.Show("Lost connection to server."); } Program.RunOnUIThread(delegate { Close(); }); }); MinecraftModUpdater.Logger.Log(Logger.Level.Info, "Logging started."); ph = new PacketHandler(s); ph.Start(); for (int i = 0; i < 10; i++) { TaskManager.SpawnTaskThread(ThreadRole.Standard); } TaskManager.AddAsyncTask(delegate { ph.RegisterPacketHandler(PacketId.Metadata, ph_Metadata); ph.RegisterPacketHandler(PacketId.ModInfo, ph_ModInfo); ph.RegisterPacketHandler(PacketId.ModList, ph_ModList); ph.RegisterPacketHandler(PacketId.AllDone, ph_AllDone); ph.RegisterPacketHandler(PacketId.NextDownload, ph_NextDownload); ph.RegisterPacketHandler(PacketId.FilePart, ph_FilePart); ph.RegisterPacketHandler(PacketId.Image, ph_Image); Debug.Assert("Packet Handlers registered."); SplashScreen.GetScreen().Progress.PerformStep(); }); if ((new LoginForm()).ShowDialog() != DialogResult.OK) { MinecraftModUpdater.Logger.Log(Logger.Level.Error, "Login failed"); SplashScreen.UpdateStatusTextWithStatus("Your login failed.", TypeOfMessage.Error); Thread.Sleep(2000); SplashScreen.CloseSplashScreen(); Thread.Sleep(400); Close(); return; } Thread.Sleep(1000); SplashScreen.UpdateStatusText("Connected to server. Retreving Mod List."); Packet.Send(new HandshakePacket { Username = ProgramOptions.Username }, ph.Stream); Debug.Assert("Sent Handshake Packet."); Thread.Sleep(100); for (int i = 0; i < 5; i++) { SplashScreen.GetScreen().Progress.Value += 1; Thread.Sleep(20); } }