/// <summary> /// Close connection. /// </summary> /// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception> /// <exception cref="Exception">Thrown if TCP Status is CLOSED.</exception> public void Close() { if (StateMachine.Status == Status.CLOSED) { throw new Exception("Client already closed."); } if (StateMachine.Status == Status.ESTABLISHED) { var packet = new TCPPacket(StateMachine.source, StateMachine.destination, (ushort)StateMachine.localPort, (ushort)StateMachine.destinationPort, StateMachine.SequenceNumber, StateMachine.AckNumber, 20, (byte)(Flags.FIN | Flags.ACK), 0xFAF0, 0); OutgoingBuffer.AddPacket(packet); NetworkStack.Update(); StateMachine.SequenceNumber++; StateMachine.Status = Status.FIN_WAIT1; if (StateMachine.WaitStatus(Status.CLOSED, 5000) == false) { throw new Exception("Failed to close TCP connection!"); } } if (clients.ContainsKey((uint)StateMachine.localPort)) { clients.Remove((uint)StateMachine.localPort); } }
/// <summary> /// Send data to client. /// </summary> /// <param name="data">Data array to send.</param> /// <exception cref="Exception">Thrown if destination is null or destinationPort is 0.</exception> /// <exception cref="ArgumentException">Thrown on fatal error (contact support).</exception> /// <exception cref="OverflowException">Thrown if data array length is greater than Int32.MaxValue.</exception> /// <exception cref="Sys.IO.IOException">Thrown on IO error.</exception> /// <exception cref="Exception">Thrown if TCP Status is not ESTABLISHED.</exception> public void Send(byte[] data) { if ((StateMachine.destination == null) || (StateMachine.destinationPort == 0)) { throw new InvalidOperationException("Must establish a default remote host by calling Connect() before using this Send() overload"); } if (StateMachine.Status != Status.ESTABLISHED) { throw new Exception("Client must be connected before sending data."); } if (data.Length > 536) { var chunks = ArrayHelper.ArraySplit(data, 536); for (int i = 0; i < chunks.Length; i++) { var packet = new TCPPacket(StateMachine.source, StateMachine.destination, (ushort)StateMachine.localPort, (ushort)StateMachine.destinationPort, StateMachine.SequenceNumber, StateMachine.AckNumber, 20, i == chunks.Length - 2 ? (byte)(Flags.PSH | Flags.ACK) : (byte)(Flags.ACK), 0xFAF0, 0, chunks[i]); OutgoingBuffer.AddPacket(packet); NetworkStack.Update(); StateMachine.SequenceNumber += (uint)chunks[i].Length; } } else { var packet = new TCPPacket(StateMachine.source, StateMachine.destination, (ushort)StateMachine.localPort, (ushort)StateMachine.destinationPort, StateMachine.SequenceNumber, StateMachine.AckNumber, 20, (byte)(Flags.PSH | Flags.ACK), 0xFAF0, 0, data); OutgoingBuffer.AddPacket(packet); NetworkStack.Update(); StateMachine.SequenceNumber += (uint)data.Length; } StateMachine.WaitingAck = true; }
/// <summary> /// Connect to client. /// </summary> /// <param name="dest">Destination address.</param> /// <param name="destPort">Destination port.</param> /// <exception cref="Exception">Thrown if TCP Status is not CLOSED.</exception> public void Connect(Address dest, int destPort, int timeout = 5000) { if (StateMachine.Status != Status.CLOSED) { throw new Exception("Client must be closed before setting a new connection."); } StateMachine.destination = dest; StateMachine.destinationPort = destPort; StateMachine.source = IPConfig.FindNetwork(dest); //Generate Random Sequence Number var rnd = new Random(); StateMachine.SequenceNumber = (uint)((rnd.Next(0, Int32.MaxValue)) << 32) | (uint)(rnd.Next(0, Int32.MaxValue)); // Flags=0x02 -> Syn var packet = new TCPPacket(StateMachine.source, StateMachine.destination, (ushort)StateMachine.localPort, (ushort)destPort, StateMachine.SequenceNumber, 0, 20, (byte)Flags.SYN, 0xFAF0, 0); OutgoingBuffer.AddPacket(packet); NetworkStack.Update(); StateMachine.Status = Status.SYN_SENT; if (StateMachine.WaitStatus(Status.ESTABLISHED, timeout) == false) { throw new Exception("Failed to open TCP connection!"); } }
/// <summary> /// Send data. /// </summary> /// <param name="data">Data array.</param> /// <param name="dest">Destination address.</param> /// <param name="destPort">Destination port.</param> /// <exception cref="ArgumentException">Thrown on fatal error (contact support).</exception> /// <exception cref="OverflowException">Thrown if data array length is greater than Int32.MaxValue.</exception> /// <exception cref="Sys.IO.IOException">Thrown on IO error.</exception> public void Send(byte[] data, Address dest, int destPort) { Address source = IPConfig.FindNetwork(dest); var packet = new UDPPacket(source, dest, (ushort)localPort, (ushort)destPort, data); OutgoingBuffer.AddPacket(packet); }
/// <summary> /// Send data to client. /// </summary> /// <param name="data">Data array to send.</param> /// <exception cref="Exception">Thrown if destination is null or destinationPort is 0.</exception> /// <exception cref="ArgumentException">Thrown on fatal error (contact support).</exception> /// <exception cref="OverflowException">Thrown if data array length is greater than Int32.MaxValue.</exception> /// <exception cref="Sys.IO.IOException">Thrown on IO error.</exception> /// <exception cref="Exception">Thrown if TCP Status is not ESTABLISHED.</exception> public void Send(byte[] data) { if ((StateMachine.RemoteEndPoint.Address == null) || (StateMachine.RemoteEndPoint.Port == 0)) { throw new InvalidOperationException("Must establish a default remote host by calling Connect() before using this Send() overload"); } if (StateMachine.Status != Status.ESTABLISHED) { throw new Exception("Client must be connected before sending data."); } if (data.Length > 536) { var chunks = ArrayHelper.ArraySplit(data, 536); for (int i = 0; i < chunks.Length; i++) { var packet = new TCPPacket(StateMachine.LocalEndPoint.Address, StateMachine.RemoteEndPoint.Address, StateMachine.LocalEndPoint.Port, StateMachine.RemoteEndPoint.Port, StateMachine.TCB.SndNxt, StateMachine.TCB.RcvNxt, 20, i == chunks.Length - 2 ? (byte)(Flags.PSH | Flags.ACK) : (byte)(Flags.ACK), StateMachine.TCB.SndWnd, 0, chunks[i]); OutgoingBuffer.AddPacket(packet); NetworkStack.Update(); StateMachine.TCB.SndNxt += (uint)chunks[i].Length; } } else { var packet = new TCPPacket(StateMachine.LocalEndPoint.Address, StateMachine.RemoteEndPoint.Address, StateMachine.LocalEndPoint.Port, StateMachine.RemoteEndPoint.Port, StateMachine.TCB.SndNxt, StateMachine.TCB.RcvNxt, 20, (byte)(Flags.PSH | Flags.ACK), StateMachine.TCB.SndWnd, 0, data); OutgoingBuffer.AddPacket(packet); NetworkStack.Update(); StateMachine.TCB.SndNxt += (uint)data.Length; } }
/// <summary> /// Send acknowledgement packet /// </summary> private void SendEmptyPacket(Flags flag) { var packet = new TCPPacket(source, destination, (ushort)localPort, (ushort)destinationPort, SequenceNumber, AckNumber, 20, (byte)flag, 0xFAF0, 0); OutgoingBuffer.AddPacket(packet); NetworkStack.Update(); }
/// <summary> /// Send a request to apply the new IP configuration /// </summary> public static void SendRequestPacket(Address RequestedAddress, Address DHCPServerAddress) { foreach (NetworkDevice networkDevice in NetworkDevice.Devices) { DHCPRequest dhcp_request = new DHCPRequest(networkDevice.MACAddress, RequestedAddress, DHCPServerAddress); OutgoingBuffer.AddPacket(dhcp_request); NetworkStack.Update(); } }
/// <summary> /// Send TCP packet. /// </summary> private void SendPacket(TCPPacket packet) { OutgoingBuffer.AddPacket(packet); NetworkStack.Update(); if (packet.SYN || packet.FIN) { TCB.SndNxt++; } }
/// <summary> /// Send a request to apply the new IP configuration /// </summary> /// <returns>time value (-1 = timeout)</returns> private int SendRequestPacket(Address RequestedAddress) { foreach (NetworkDevice networkDevice in NetworkDevice.Devices) { var dhcp_request = new DHCPRequest(networkDevice.MACAddress, RequestedAddress); OutgoingBuffer.AddPacket(dhcp_request); NetworkStack.Update(); } return(Receive()); }
/// <summary> /// Send a packet to find the DHCP server and tell that we want a new IP address /// </summary> public static void SendDiscoverPacket() { foreach (NetworkDevice networkDevice in NetworkDevice.Devices) { DHCPDiscover dhcp_discover = new DHCPDiscover(networkDevice.MACAddress); OutgoingBuffer.AddPacket(dhcp_discover); NetworkStack.Update(); DHCPAsked = true; } }
public void Ask(string url) { Settings settings = new Settings(@"0:\System\" + NetworkInterfaces.Interface("eth0") + ".conf"); Address gateway = Address.Parse(settings.Get("dns01")); Address source = Config.FindNetwork(gateway); askpacket = new DNSPacketAsk(source, gateway, 0x1234, 0x0100, 1, url); OutgoingBuffer.AddPacket(askpacket); NetworkStack.Update(); }
/// <summary> /// Send DNS Ask for Domain Name string /// </summary> /// <param name="url">Domain Name string.</param> public void SendAsk(string url) { Address source = IPConfig.FindNetwork(destination); queryurl = url; var askpacket = new DNSPacketAsk(source, destination, url); OutgoingBuffer.AddPacket(askpacket); NetworkStack.Update(); }
/// <summary> /// Send a packet to find the DHCP server and tell that we want a new IP address /// </summary> public static void SendDiscoverPacket() { NetworkStack.RemoveAllConfigIP(); foreach (NetworkDevice networkDevice in NetworkDevice.Devices) { NetworkInit.Enable(networkDevice, new Network.IPV4.Address(0, 0, 0, 0), new Network.IPV4.Address(0, 0, 0, 0), new Network.IPV4.Address(0, 0, 0, 0)); DHCPDiscover dhcp_discover = new DHCPDiscover(networkDevice.MACAddress); OutgoingBuffer.AddPacket(dhcp_discover); NetworkStack.Update(); DHCPAsked = true; } }
/// <summary> /// Send a packet to the DHCP server to make the address available again /// </summary> public void SendReleasePacket() { foreach (NetworkDevice networkDevice in NetworkDevice.Devices) { Address source = IPConfig.FindNetwork(DHCPServerAddress(networkDevice)); var dhcp_release = new DHCPRelease(source, DHCPServerAddress(networkDevice), networkDevice.MACAddress); OutgoingBuffer.AddPacket(dhcp_release); NetworkStack.Update(); NetworkStack.RemoveAllConfigIP(); IPConfig.Enable(networkDevice, new Address(0, 0, 0, 0), new Address(0, 0, 0, 0), new Address(0, 0, 0, 0)); } Close(); }
/// <summary> /// Send a packet to find the DHCP server and tell that we want a new IP address /// </summary> /// <returns>time value (-1 = timeout)</returns> public int SendDiscoverPacket() { NetworkStack.RemoveAllConfigIP(); foreach (NetworkDevice networkDevice in NetworkDevice.Devices) { IPConfig.Enable(networkDevice, new Address(0, 0, 0, 0), new Address(0, 0, 0, 0), new Address(0, 0, 0, 0)); var dhcp_discover = new DHCPDiscover(networkDevice.MACAddress); OutgoingBuffer.AddPacket(dhcp_discover); NetworkStack.Update(); asked = true; } return(Receive()); }
public bool Send(bool isdata) { Apps.System.Debugger.debugger.Send("Sending TCP packet..."); if (isdata) { TCPPacket packet = new TCPPacket(source, dest, localPort, destPort, data, sequencenumber, acknowledgmentnb, 0x50, Flags, WSValue, 0x0000, false, false); OutgoingBuffer.AddPacket(packet); } else { TCPPacket packet = new TCPPacket(source, dest, localPort, destPort, data, sequencenumber, acknowledgmentnb, 0x50, Flags, WSValue, 0x0000, true, true); OutgoingBuffer.AddPacket(packet); } NetworkStack.Update(); Apps.System.Debugger.debugger.Send("Sent!"); return(true); }
/// <summary> /// Send a packet to the DHCP server to make the address available again /// </summary> public static void SendReleasePacket() { foreach (NetworkDevice networkDevice in NetworkDevice.Devices) { Address source = Config.FindNetwork(DHCPServerAddress(networkDevice)); DHCPRelease dhcp_release = new DHCPRelease(source, DHCPServerAddress(networkDevice)); OutgoingBuffer.AddPacket(dhcp_release); NetworkStack.Update(); NetworkStack.RemoveAllConfigIP(); Settings settings = new Settings(@"0:\" + networkDevice.Name + ".conf"); settings.EditValue("ipaddress", "0.0.0.0"); settings.EditValue("subnet", "0.0.0.0"); settings.EditValue("gateway", "0.0.0.0"); settings.EditValue("dns01", "0.0.0.0"); settings.PushValues(); NetworkInit.Enable(); } }
/// <summary> /// CommandEcho /// </summary> /// <param name="arguments">Arguments</param> public override ReturnInfo Execute(List <string> arguments) { if (NetworkStack.ConfigEmpty()) { return(new ReturnInfo(this, ReturnCode.ERROR, "No network configuration detected! Use ipconfig /set.")); } int PacketSent = 0; int PacketReceived = 0; int PacketLost = 0; int PercentLoss = 0; Address destination = Address.Parse(arguments[0]); Address source = Config.FindNetwork(destination); string IPdest = ""; if (destination == null) { return(new ReturnInfo(this, ReturnCode.ERROR, "Can't parse IP addresses (make sure they are well formated).")); } else { try { IPdest = destination.ToString(); int _deltaT = 0; int second; Console.WriteLine("Sending ping to " + destination.ToString()); for (int i = 0; i < 4; i++) { second = 0; try { ICMPEchoRequest request = new ICMPEchoRequest(source, destination, 0x0001, 0x50); //this is working OutgoingBuffer.AddPacket(request); //Aura doesn't work when this is called. NetworkStack.Update(); } catch (Exception ex) { return(new ReturnInfo(this, ReturnCode.ERROR, ex.ToString())); } PacketSent++; while (true) { if (ICMPPacket.recvd_reply != null) { if (second < 1) { Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time < 1s"); } else if (second >= 1) { Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time " + second + "s"); } PacketReceived++; ICMPPacket.recvd_reply = null; break; } if (second >= 5) { Console.WriteLine("Destination host unreachable."); PacketLost++; break; } if (_deltaT != Cosmos.HAL.RTC.Second) { second++; _deltaT = Cosmos.HAL.RTC.Second; } } } } catch { return(new ReturnInfo(this, ReturnCode.ERROR, "Ping process error.")); } PercentLoss = 25 * PacketLost; Console.WriteLine(); Console.WriteLine("Ping statistics for " + IPdest + ":"); Console.WriteLine(" Packets: Sent = " + PacketSent + ", Received = " + PacketReceived + ", Lost = " + PacketLost + " (" + PercentLoss + "% loss)"); return(new ReturnInfo(this, ReturnCode.OK)); } }
/// <summary> /// c = command, c_Ping /// </summary> /// <param name="arg">IP Address</param> /// /// <param name="startIndex">The start index for remove.</param> /// <param name="count">The count index for remove.</param> public static void c_Ping(string arg, short startIndex = 0, short count = 5) { if (Misc.IsIpv4Address(arg)) { string[] items = arg.Split('.'); int PacketSent = 0; int PacketReceived = 0; int PacketLost = 0; int PercentLoss = 0; try { Address destination = new Address((byte)int.Parse(items[0]), (byte)int.Parse(items[1]), (byte)int.Parse(items[2]), (byte)int.Parse(items[3])); Address source = Config.FindNetwork(destination); int _deltaT = 0; int second; for (int i = 0; i < 4; i++) { second = 0; Console.WriteLine("Sending ping to " + destination.ToString() + "..."); System.Utilities.WaitSeconds(1); try { ICMPEchoRequest request = new ICMPEchoRequest(source, destination, 0x0001, 0x50); //this is working OutgoingBuffer.AddPacket(request); //Aura doesn't work when this is called. NetworkStack.Update(); } catch (Exception ex) { Console.WriteLine("Error on ping " + i + ": " + ex); } PacketSent++; while (true) { if (ICMPPacket.recvd_reply != null) { //if (ICMPPacket.recvd_reply.SourceIP == destination) //{ if (second < 1) { Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time < 1s"); } else if (second >= 1) { Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time " + second + "s"); } PacketReceived++; ICMPPacket.recvd_reply = null; break; //} } if (second >= 5) { Console.WriteLine("Destination host unreachable."); PacketLost++; break; } if (_deltaT != Cosmos.HAL.RTC.Second) { second++; _deltaT = Cosmos.HAL.RTC.Second; } } } } catch { Console.WriteLine("Internal error."); } finally { PercentLoss = 25 * PacketLost; Console.WriteLine(); Console.WriteLine("Ping statistics for " + arg + ":"); Console.WriteLine(" Packets: Sent = " + PacketSent + ", Received = " + PacketReceived + ", Lost = " + PacketLost + " (" + PercentLoss + "% loss)"); } } else { Network.IPV4.UDP.DNS.DNSClient DNSRequest = new Network.IPV4.UDP.DNS.DNSClient(53); DNSRequest.Ask(arg); int _deltaT = 0; int second = 0; while (!DNSRequest.ReceivedResponse) { if (_deltaT != Cosmos.HAL.RTC.Second) { second++; _deltaT = Cosmos.HAL.RTC.Second; } if (second >= 4) { //Apps.System.Debugger.debugger.Send("No response in 4 secondes..."); break; } } DNSRequest.Close(); c_Ping(" " + DNSRequest.address.ToString()); } }
/// <summary> /// c = command, c_Ping /// </summary> /// <param name="arg">IP Address</param> /// /// <param name="startIndex">The start index for remove.</param> /// <param name="count">The count index for remove.</param> public static void c_Ping(string arg, short startIndex = 0, short count = 5) { string str = arg.Remove(startIndex, count); string[] items = str.Split('.'); if (System.Utils.Misc.IsIpv4Address(items)) { string IPdest = ""; int PacketSent = 0; int PacketReceived = 0; int PacketLost = 0; int PercentLoss = 0; try { Address destination = new Address((byte)(Int32.Parse(items[0])), (byte)(Int32.Parse(items[1])), (byte)(Int32.Parse(items[2])), (byte)(Int32.Parse(items[3]))); Address source = Config.FindNetwork(destination); IPdest = destination.ToString(); int _deltaT = 0; int second; Console.WriteLine("Sending ping to " + destination.ToString()); for (int i = 0; i < 4; i++) { second = 0; //CustomConsole.WriteLineInfo("Sending ping to " + destination.ToString() + "..."); try { //replace address by source //System.Network.IPV4.Address address = new System.Network.IPV4.Address(192, 168, 1, 70); ICMPEchoRequest request = new ICMPEchoRequest(source, destination, 0x0001, 0x50); //this is working OutgoingBuffer.AddPacket(request); //Aura doesn't work when this is called. NetworkStack.Update(); } catch (Exception ex) { CustomConsole.WriteLineError(ex.ToString()); } PacketSent++; while (true) { if (ICMPPacket.recvd_reply != null) { //if (ICMPPacket.recvd_reply.SourceIP == destination) //{ if (second < 1) { Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time < 1s"); } else if (second >= 1) { Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time " + second + "s"); } PacketReceived++; ICMPPacket.recvd_reply = null; break; //} } if (second >= 5) { Console.WriteLine("Destination host unreachable."); PacketLost++; break; } if (_deltaT != Cosmos.HAL.RTC.Second) { second++; _deltaT = Cosmos.HAL.RTC.Second; } } } } catch { L.Text.Display("notcorrectaddress"); } finally { PercentLoss = 25 * PacketLost; Console.WriteLine(); Console.WriteLine("Ping statistics for " + IPdest + ":"); Console.WriteLine(" Packets: Sent = " + PacketSent + ", Received = " + PacketReceived + ", Lost = " + PacketLost + " (" + PercentLoss + "% loss)"); } } else { System.Network.IPV4.UDP.DNS.DNSClient DNSRequest = new System.Network.IPV4.UDP.DNS.DNSClient(53); DNSRequest.Ask(str); int _deltaT = 0; int second = 0; while (!DNSRequest.ReceivedResponse) { if (_deltaT != Cosmos.HAL.RTC.Second) { second++; _deltaT = Cosmos.HAL.RTC.Second; } if (second >= 4) { Apps.System.Debugger.debugger.Send("No response in 4 secondes..."); break; } } DNSRequest.Close(); c_Ping(" " + DNSRequest.address.ToString()); } }
/// <summary> /// CommandEcho /// </summary> /// <param name="arguments">Arguments</param> public override ReturnInfo Execute(List <string> arguments) { int PacketSent = 0; int PacketReceived = 0; int PacketLost = 0; int PercentLoss; Address source; Address destination = Address.Parse(arguments[0]); if (destination != null) { source = Config.FindNetwork(destination); } else //Make a DNS request if it's not an IP { var xClient = new DnsClient(); xClient.Connect(NetworkConfig.CurrentConfig.Value.DefaultDNSServer); xClient.SendAsk(arguments[0]); destination = xClient.Receive(); xClient.Close(); if (destination == null) { return(new ReturnInfo(this, ReturnCode.ERROR, "Unable to get URL for " + arguments[0])); } source = Config.FindNetwork(destination); } try { int _deltaT = 0; int second; Console.WriteLine("Sending ping to " + destination.ToString()); for (int i = 0; i < 4; i++) { second = 0; try { ICMPEchoRequest request = new ICMPEchoRequest(source, destination, 0x0001, 0x50); //this is working OutgoingBuffer.AddPacket(request); //Aura doesn't work when this is called. NetworkStack.Update(); } catch (Exception ex) { return(new ReturnInfo(this, ReturnCode.ERROR, ex.ToString())); } PacketSent++; while (true) { if (ICMPPacket.recvd_reply != null) { if (second < 1) { Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time < 1s"); } else if (second >= 1) { Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time " + second + "s"); } PacketReceived++; ICMPPacket.recvd_reply = null; break; } if (second >= 5) { Console.WriteLine("Destination host unreachable."); PacketLost++; break; } if (_deltaT != Cosmos.HAL.RTC.Second) { second++; _deltaT = Cosmos.HAL.RTC.Second; } } } } catch { return(new ReturnInfo(this, ReturnCode.ERROR, "Ping process error.")); } PercentLoss = 25 * PacketLost; Console.WriteLine(); Console.WriteLine("Ping statistics for " + destination.ToString() + ":"); Console.WriteLine(" Packets: Sent = " + PacketSent + ", Received = " + PacketReceived + ", Lost = " + PacketLost + " (" + PercentLoss + "% loss)"); return(new ReturnInfo(this, ReturnCode.OK)); }