Beispiel #1
0
 /// <summary>
 /// Initializes a new instance of the IpPacket class using the specified
 /// values.
 /// </summary>
 /// <param name="destination">The IPv4 address of the destination
 /// host.</param>
 /// <param name="source">The IPv4 address of the sending host.</param>
 /// <param name="type">The type of the transport protocol encapsulated in
 /// the IP packet's data section.</param>
 /// <param name="fragmentOffset">The fragment offset of the packet.</param>
 /// <param name="data">The transport data to transfer as part of the IP
 /// packet.</param>
 /// <param name="ihl">The Internet Header Length.</param>
 /// <param name="dscp">The Differentiated Services Code Point.</param>
 /// <param name="ttl">The time-to-live of the IP packet.</param>
 /// <param name="identification">The idenfication used for uniquely identifying
 /// fragments of a fragmented IP packet.</param>
 /// <param name="flags">The flags set on the IP packet.</param>
 /// <exception cref="ArgumentNullException">Thrown if any of the arguments
 /// is null.</exception>
 public IpPacket(IpAddress destination, IpAddress source,
                 IpProtocol type, byte ihl, byte dscp, byte ttl, ushort identification,
                 IpFlag flags, ushort fragmentOffset, byte[] data)
 {
     destination.ThrowIfNull("destination");
     source.ThrowIfNull("source");
     data.ThrowIfNull("data");
     if (ihl > 0x0F)
     {
         throw new ArgumentException("The Internet Header Length field must " +
                                     "be in the range from 0 to 15.", nameof(ihl));
     }
     if (fragmentOffset > 0x1FFF)
     {
         throw new ArgumentException("The Fragment Offset field must be in " +
                                     "the range from 0 to 8191.", nameof(fragmentOffset));
     }
     Version        = IpVersion.Ipv4;
     Ihl            = ihl;
     Dscp           = dscp;
     Identification = identification;
     Flags          = flags;
     FragmentOffset = fragmentOffset;
     TimeToLive     = ttl;
     Protocol       = type;
     Source         = source;
     Destination    = destination;
     Data           = data;
     TotalLength    = (ushort)(20 + Data.Length);
     Checksum       = ComputeChecksum(this);
 }
Beispiel #2
0
 /// <summary>
 /// Private constructor used for deserialization.
 /// </summary>
 private IpPacket(IpVersion version, byte ihl, byte dscp, ushort totalLength,
                  ushort identification, IpFlag flags, ushort fragmentOffset,
                  byte ttl, IpProtocol protocol, ushort checksum, IpAddress source,
                  IpAddress destination)
 {
     destination.ThrowIfNull("destination");
     source.ThrowIfNull("source");
     if (ihl > 0x0F)
     {
         throw new ArgumentException("The Internet Header Length field must " +
                                     "be in the range from 0 to 15.", nameof(ihl));
     }
     if (fragmentOffset > 0x1FFF)
     {
         throw new ArgumentException("The Fragment Offset field must be in " +
                                     "the range from 0 to 8191.", nameof(fragmentOffset));
     }
     Version        = version;
     Ihl            = ihl;
     Dscp           = dscp;
     TotalLength    = totalLength;
     Identification = identification;
     Flags          = flags;
     FragmentOffset = fragmentOffset;
     TimeToLive     = ttl;
     Protocol       = protocol;
     Checksum       = checksum;
     Source         = source;
     Destination    = destination;
     Data           = new byte[0];
 }
Beispiel #3
0
        private void PacketArrival(object sender, EthernetPacket eth)
        {
            var myInterface = (Interface)sender;

            var arp = eth.Extract <ArpPacket>();

            if (arp != null)
            {
                var protocol = new ArpProtocol(this, CurrentApp.Logging);
                protocol.Process(myInterface, arp);
                RouterChange?.Invoke(this, null);
                return;
            }

            if (!eth.DestinationHardwareAddress.Equals(myInterface.MacAddress) && !eth.DestinationHardwareAddress.Equals(PhysicalAddress.Parse("01-00-5E-00-00-09")))
            {
                return;
            }

            var ip = eth.Extract <IPPacket>();

            if (ip != null)
            {
                var protocol = new IpProtocol(this, CurrentApp.Logging);
                protocol.Process(myInterface, ip);
            }
        }
Beispiel #4
0
        public override async Task <Mapping> GetSpecificMappingAsync(IpProtocol protocol, int publicPort)
        {
            var message  = new GetSpecificPortMappingEntryMessage(protocol, publicPort, this);
            var response = await SendMessageAsync(message).ConfigureAwait(false);

            if (!(response is GetSpecificPortMappingEntryResponseMessage msg))
            {
                throw new MappingException(ErrorCode.Unknown, "Invalid response received when getting the specific mapping", null);
            }
            return(new Mapping(protocol, msg.InternalPort, publicPort, msg.LeaseDuration, msg.PortMappingDescription));
        }
Beispiel #5
0
 /// <summary>
 /// Hands up the data part of the specified IP packet to the transport
 /// layer.
 /// </summary>
 /// <param name="data">The data to hand up.</param>
 /// <param name="protocol">The protocol of the data.</param>
 static void HandUp(byte[] data, IpProtocol protocol)
 {
     // ICMP is a special case since it's not a tranport protocol
     // but is implemented as part of the network layer.
     if (protocol == IpProtocol.Icmp)
     {
         WriteLine("Handling ICMP message");
     }
     else
     {
         WriteLine("Handing up IP data to Transport Layer: " + data.Length);
     }
 }
Beispiel #6
0
 /// <summary>
 /// Initializes a new instance of the IpPacket class using the specified
 /// values.
 /// </summary>
 /// <param name="destination">The IPv4 address of the destination
 /// host.</param>
 /// <param name="source">The IPv4 address of the sending host.</param>
 /// <param name="type">The type of the transport protocol encapsulated in
 /// the IP packet's data section.</param>
 /// <param name="data">The transport data to transfer as part of the IP
 /// packet.</param>
 /// <exception cref="ArgumentNullException">Thrown if any of the arguments
 /// is null.</exception>
 public IpPacket(IpAddress destination, IpAddress source,
                 IpProtocol type, byte[] data)
 {
     destination.ThrowIfNull("destination");
     source.ThrowIfNull("source");
     data.ThrowIfNull("data");
     Destination = destination;
     Source      = source;
     Protocol    = type;
     Data        = data;
     TimeToLive  = 64;
     Ihl         = 5;
     TotalLength = (ushort)(20 + Data.Length);
     Version     = IpVersion.Ipv4;
     Checksum    = ComputeChecksum(this);
 }
Beispiel #7
0
        public Task <GenericService> Locate(string serviceName, IpProtocol protocol)
        {
            if (string.IsNullOrWhiteSpace(serviceName))
            {
                throw new ArgumentNullException(nameof(serviceName));
            }

            // get name of service to resolve
            string         sProtocol = protocol.ToString().ToLowerInvariant();
            ServiceDnsName name      = new ServiceDnsName
            {
                Domain      = ServiceDomain,
                ServiceName = serviceName,
                Protocol    = sProtocol,
                DnsName     = string.IsNullOrEmpty(ServiceDomain)
                    ? serviceName.ToLowerInvariant()
                    : $"{serviceName.ToLowerInvariant()}._{sProtocol}.{ServiceDomain}"
            };

            return(Locate(name, _ => ServiceLookup.SrvTxt(name, MyServiceFactory)));
        }
Beispiel #8
0
        /// <summary>
        /// Create a port mapping so traffic sent to the <paramref name="publicPort"/> on the WAN device is sent to the <paramref name="privatePort"/> on the LAN device.
        /// </summary>
        /// <param name="protocol">The protocol used by the port mapping.</param>
        /// <param name="privatePort">The internal/LAN port which will receive traffic sent to the <paramref name="publicPort"/> on the WAN device.</param>
        /// <param name="publicPort">Traffic sent to this public/WAN port is forwarded to the <paramref name="privatePort"/> on the LAN device.</param>
        /// <param name="lifetime">The lifetime of the port mapping in seconds. If a lifetime of '0' is specified then the protocol default lifetime is used. uPnP defaults to 'indefinite' whereas NAT-PMP defaults to 7,200 seconds.</param>
        /// <param name="description">The text description for the port mapping.</param>
        public Mapping(IpProtocol protocol, int privatePort, int publicPort, int lifetime, string description)
        {
            Protocol    = protocol;
            PrivatePort = privatePort;
            PublicPort  = publicPort;
            Lifetime    = lifetime;
            Description = description;

            if (lifetime == int.MaxValue)
            {
                Expiration = DateTime.MaxValue;
            }
            else if (lifetime == 0)
            {
                Expiration = DateTime.Now;
            }
            else
            {
                Expiration = DateTime.Now.AddSeconds(lifetime);
            }
        }
Beispiel #9
0
        public static ResponseMessage Decode(byte [] data)
        {
            if (data.Length < 16)
            {
                throw new MappingException(ErrorCode.Unknown, $"The received message was too short, only {data.Length} bytes", null);
            }

            if (data [0] != PmpConstants.Version)
            {
                throw new MappingException(ErrorCode.Unknown, $"The received message was unsupported version {data[0]}", null);
            }

            byte opCode = (byte)(data [1] & (byte)127);

            IpProtocol protocol = IpProtocol.Tcp;

            if (opCode == PmpConstants.OperationCodeUdp)
            {
                protocol = IpProtocol.Udp;
            }

            var  resultCode = (ErrorCode)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, 2));
            uint epoch      = (uint)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data, 4));

            int privatePort = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, 8));
            int publicPort  = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, 10));

            uint lifetime = (uint)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data, 12));

            if (publicPort < 0 || privatePort < 0 || resultCode != ErrorCode.Success)
            {
                throw new MappingException((ErrorCode)resultCode, "Could not modify the port map", null);
            }

            var mapping = new Mapping(protocol, privatePort, publicPort, (int)lifetime, null);

            return(new MappingResponseMessage(mapping));
        }
Beispiel #10
0
        /// <summary>
        /// Wraps the specified higher-level data into IP packets and
        /// transmits them to the specified destination.
        /// </summary>
        /// <param name="ifc">The interface through which to output the data.</param>
        /// <param name="destination">The logical address of the destination
        /// host.</param>
        /// <param name="data">The higher-level data to transmit.</param>
        /// <param name="type">The type of the higher-level data.</param>
        /// <exception cref="ArgumentNullException">Thrown if any of the arguments
        /// is null.</exception>
        /// <remarks>This API is exposed to the next higher-up layer.</remarks>
        public void Output(Interface ifc, IpAddress destination, byte[] data,
                           IpProtocol type)
        {
            ifc.ThrowIfNull("ifc");
            destination.ThrowIfNull("destination");
            data.ThrowIfNull("data");
            // Construct IP packets of the size of the MTU of the data-link.
            var maxDataSize = ifc.MaximumTransmissionUnit - 20;
            var numPackets  = (int)Math.Ceiling(data.Length / (double)maxDataSize);
            var sameSubnet  = (destination & ifc.Netmask) == (ifc.IpAddress & ifc.Netmask);

            for (int i = 0, index = 0; i < numPackets; i++)
            {
                var numBytes   = Math.Min(maxDataSize, data.Length - index);
                var packetData = new byte[numBytes];
                Array.Copy(data, index, packetData, 0, numBytes);
                index = index + numBytes;
                // Construct the packet.
                var packet = new IpPacket(destination, ifc.IpAddress, type, packetData);
                // If source and destination are in the same subnet, we can deliver the
                // packet directly. Otherwise send it to the configured default gateway.
                Output(ifc, sameSubnet ? destination : ifc.Gateway, packet);
            }
        }
Beispiel #11
0
		/// <summary>
		/// Private constructor used for deserialization.
		/// </summary>
		private IpPacket(IpVersion version, byte ihl, byte dscp, ushort totalLength,
			ushort identification, IpFlag flags, ushort fragmentOffset,
			byte ttl, IpProtocol protocol, ushort checksum, IpAddress source,
			IpAddress destination) {
			destination.ThrowIfNull("destination");
			source.ThrowIfNull("source");
			if (ihl > 0x0F)
				throw new ArgumentException("The Internet Header Length field must " +
					"be in the range from 0 to 15.", nameof(ihl));
			if (fragmentOffset > 0x1FFF)
				throw new ArgumentException("The Fragment Offset field must be in " +
					"the range from 0 to 8191.", nameof(fragmentOffset));
			Version = version;
			Ihl = ihl;
			Dscp = dscp;
			TotalLength = totalLength;
			Identification = identification;
			Flags = flags;
			FragmentOffset = fragmentOffset;
			TimeToLive = ttl;
			Protocol = protocol;
			Checksum = checksum;
			Source = source;
			Destination = destination;
			Data = new byte[0];
		}
Beispiel #12
0
        /// <summary>
        /// Initializes a new instance of the IpPacket class using the specified
        /// values.
        /// </summary>
        /// <param name="destination">The IPv4 address of the destination
        /// host.</param>
        /// <param name="source">The IPv4 address of the sending host.</param>
        /// <param name="type">The type of the transport protocol encapsulated in
        /// the IP packet's data section.</param>
        /// <param name="fragmentOffset">The fragment offset of the packet.</param>
        /// <param name="data">The transport data to transfer as part of the IP
        /// packet.</param>
        /// <param name="ihl">The Internet Header Length.</param>
        /// <param name="dscp">The Differentiated Services Code Point.</param>
        /// <param name="ttl">The time-to-live of the IP packet.</param>
        /// <param name="identification">The idenfication used for uniquely identifying
        /// fragments of a fragmented IP packet.</param>
        /// <param name="flags">The flags set on the IP packet.</param>
        /// <exception cref="ArgumentNullException">Thrown if any of the arguments
        /// is null.</exception>
        public IpPacket(IpAddress destination, IpAddress source,
			IpProtocol type, byte ihl, byte dscp, byte ttl, ushort identification,
			IpFlag flags, ushort fragmentOffset, byte[] data) {
			destination.ThrowIfNull("destination");
			source.ThrowIfNull("source");
			data.ThrowIfNull("data");
			if (ihl > 0x0F)
				throw new ArgumentException("The Internet Header Length field must " +
					"be in the range from 0 to 15.", nameof(ihl));
			if (fragmentOffset > 0x1FFF)
				throw new ArgumentException("The Fragment Offset field must be in " +
					"the range from 0 to 8191.", nameof(fragmentOffset));
			Version = IpVersion.Ipv4;
			Ihl = ihl;
			Dscp = dscp;
			Identification = identification;
			Flags = flags;
			FragmentOffset = fragmentOffset;
			TimeToLive = ttl;
			Protocol = type;
			Source = source;
			Destination = destination;
			Data = data;
			TotalLength = (ushort) (20 + Data.Length);
			Checksum = ComputeChecksum(this);
		}
Beispiel #13
0
		/// <summary>
		/// Initializes a new instance of the IpPacket class using the specified
		/// values.
		/// </summary>
		/// <param name="destination">The IPv4 address of the destination
		/// host.</param>
		/// <param name="source">The IPv4 address of the sending host.</param>
		/// <param name="type">The type of the transport protocol encapsulated in
		/// the IP packet's data section.</param>
		/// <param name="data">The transport data to transfer as part of the IP
		/// packet.</param>
		/// <exception cref="ArgumentNullException">Thrown if any of the arguments
		/// is null.</exception>
		public IpPacket(IpAddress destination, IpAddress source,
			IpProtocol type, byte[] data) {
				destination.ThrowIfNull("destination");
				source.ThrowIfNull("source");
				data.ThrowIfNull("data");
				Destination = destination;
				Source = source;
				Protocol = type;
				Data = data;
				TimeToLive = 64;
				Ihl = 5;
				TotalLength = (ushort) (20 + Data.Length);
				Version = IpVersion.Ipv4;
				Checksum = ComputeChecksum(this);
        }
Beispiel #14
0
 protected static void WriteFullElement(XmlWriter writer, string element, IpProtocol value)
 => WriteFullElement(writer, element, value == IpProtocol.Tcp ? "TCP" : "UDP");
 public PacketFilter(IpProtocol? protocol = null, PacketDirection? direction = null, string host = null)
 {
     this.Protocol = protocol;
     this.Direction = direction;
     this.Host = host;
 }
Beispiel #16
0
 public XsSocket(IpProtocol protocol, NetworkLayerProtocol ipVersion) : this(xsensdeviceapiPINVOKE.new_XsSocket__SWIG_0((int)protocol, (int)ipVersion), true)
 {
 }
Beispiel #17
0
 public GetSpecificPortMappingEntryMessage(IpProtocol protocol, int externalPort, UpnpNatRouterDevice device)
     : base(device, "GetSpecificPortMappingEntry")
 {
     Protocol     = protocol;
     ExternalPort = externalPort;
 }
Beispiel #18
0
 public XsSocket(IpProtocol protocol) : this(xsensdeviceapiPINVOKE.new_XsSocket__SWIG_1((int)protocol), true)
 {
 }
Beispiel #19
0
 public override Task <Mapping> GetSpecificMappingAsync(IpProtocol protocol, int publicPort)
 => throw new NotImplementedException("The NAT-PMP protocol does not support retrieving a specific mappings");
Beispiel #20
0
 /// <summary>
 /// Create a port mapping so traffic sent to the <paramref name="publicPort"/> on the WAN device is sent to the <paramref name="privatePort"/> on the LAN device.
 /// </summary>
 /// <param name="protocol">The protocol used by the port mapping.</param>
 /// <param name="privatePort">The internal/LAN port which will receive traffic sent to the <paramref name="publicPort"/> on the WAN device.</param>
 /// <param name="publicPort">Traffic sent to this external/WAN port is forwarded to the <paramref name="privatePort"/> on the LAN device.</param>
 public Mapping(IpProtocol protocol, int privatePort, int publicPort)
     : this(protocol, privatePort, publicPort, 0, null)
 {
 }
Beispiel #21
0
 /// <summary>
 /// Retrieves the mapping associated with this combination of public port and protocol. Throws a MappingException
 /// if there is no mapping matching the criteria.
 /// </summary>
 public abstract Task <Mapping> GetSpecificMappingAsync(IpProtocol protocol, int publicPort);