示例#1
0
        private bool PrivateIcmpInput(IPFrame packet)
        {
            IcmpFrame frame = IcmpLayer.ParseFrame(packet, false);

            if (frame == null)
            {
                return(false);
            }
            NATContext context = this.m_contextsIcmp.PrivateInput(frame.Source, frame.Destination, frame.Identification);

            if (context == null)
            {
                return(false);
            }
            IcmpFrame convertional = null;
            int       ttl          = frame.Ttl - 1;

            if (ttl <= 0)
            {
                convertional = new IcmpFrame(this.m_ethernetAddress, frame.Source, IPv4Layer.ToArray(packet))
                {
                    Ttl            = packet.Ttl,
                    Type           = IcmpType.ICMP_TE,
                    Code           = 0,
                    Sequence       = 0,
                    Identification = 0,
                };
                IPFrame replay = CopyFrameHeaderParts(IcmpLayer.ToIPFrame(convertional), packet);
                if (replay == null)
                {
                    return(false);
                }
                return(this.OnPrivateOutput(replay));
            }
            else
            {
                convertional = new IcmpFrame(this.m_ethernetAddress, frame.Destination, frame.Payload)
                {
                    Ttl            = ttl,
                    Type           = frame.Type,
                    Code           = frame.Code,
                    Sequence       = frame.Sequence,
                    Identification = frame.Identification /*unchecked((ushort)context.Destination.Port)*/,
                };
            }
            IPFrame ip = CopyFrameHeaderParts(IcmpLayer.ToIPFrame(convertional), packet);

            if (ip == null)
            {
                return(false);
            }
            return(this.OnPublicOutput(ip));
        }
示例#2
0
 protected virtual bool PrivateOutput(IPFrame packet)
 {
     if (packet == null)
     {
         return(false);
     }
     return(this.Send(packet.Destination, () =>
                      new SkylakeNATMessage(IPv4Layer.ToArray(packet))
     {
         Commands = Commands.NATCommands_kEthernetInput,
     }));
 }
示例#3
0
        protected virtual ILayer ParseFrameToLayer(IPFrame frame)
        {
            BufferSegment packet = IPv4Layer.ToArray(frame);

            if (packet == null)
            {
                return(null);
            }

            return(new PayloadLayer()
            {
                Data = new Datagram(packet.Buffer, packet.Offset, packet.Length)
            });
        }
示例#4
0
        protected virtual void NetifLevel2LayerIPv4(EthernetDatagram datagram, IpV4Datagram ip)
        {
            byte[] packet_data   = g_getIPv4DatagramBuffer(ip);
            int    packet_offset = g_getIPv4DatagramOffset(ip);
            int    packet_size   = ip.Length;

            if (packet_size > 0)
            {
                IPFrame frame = IPv4Layer.ParseFrame(new BufferSegment(packet_data, packet_offset, packet_size), true);
                if (frame != null)
                {
                    frame.SourceMacAddress      = datagram.Source;
                    frame.DestinationMacAddress = datagram.Destination;
                    this.OnSniffer(frame);
                }
            }
        }
示例#5
0
        public virtual bool Output(IPFrame frame)
        {
            if (frame == null)
            {
                return(false);
            }
            lock (this.m_syncobj)
            {
                if (this.m_disposed)
                {
                    return(false);
                }
            }
            if (this.SupportTwoLayerLinks)
            {
#if NO_USAGE_PCAP_NET
                return(false);
#else
                IList <ILayer> layers = ParseFrameToLayers(frame);
                if (layers == null || layers.Count <= 0)
                {
                    return(false);
                }

                MacAddress sources     = frame.SourceMacAddress;
                MacAddress destination = frame.DestinationMacAddress;
                if (sources == MinMacAddress)
                {
                    sources = SelectMacAddress(frame.Source);
                }
                if (destination == MinMacAddress)
                {
                    destination = SelectMacAddress(frame.Destination);
                }

                return(this.SendNetifLevel2Output(layers: layers,
                                                  source: sources,
                                                  destination: destination));
#endif
            }
            else
            {
                return(this.SendNetifLevel3Output(packet: IPv4Layer.ToArray(frame)));
            }
        }
示例#6
0
        protected virtual IList <ILayer> ParseFrameToLayers(IPFrame frame)
        {
            if (frame == null)
            {
                throw new ArgumentNullException(nameof(frame));
            }
            IList <ILayer> layers = new List <ILayer>();
            BufferSegment  packet = IPv4Layer.ToArray(frame);

            if (packet != null)
            {
                layers.Add(new PayloadLayer()
                {
                    Data = new Datagram(packet.Buffer, packet.Offset, packet.Length)
                });
            }
            return(layers);
        }
示例#7
0
 protected virtual void ProcessMessage(SkylakeNATClient socket, SkylakeNATMessage message)
 {
     if (message.Commands == Commands.NATCommands_kEthernetOutput)
     {
         IPFrame packet = IPv4Layer.ParseFrame(message.Payload, false);
         if (packet != null)
         {
             if (this._sockets.ContainsKey(packet.Source))
             {
                 this.PrivateInput(socket, packet);
             }
             else
             {
                 this.CloseManyClient(socket.Address);
             }
         }
     }
 }
示例#8
0
        private bool PublicIcmpNATOutput(NATContext context, IcmpFrame frame, IPFrame packet)
        {
            if (context == null || frame == null || packet == null)
            {
                return(false);
            }
            if (frame.Type != IcmpType.ICMP_ER) // TIME EXCEEDED
            {
                IPFrame   ipAgo   = IPv4Layer.ParseFrame(frame.Payload, false);
                IcmpFrame icmpAgo = IcmpLayer.ParseFrame(ipAgo, false);
                if (icmpAgo == null) // 这是一个伪造的ICMP数据报,正确报文应答必须要包含从本机发向对端主机的ICMP/IP数据报文
                {
                    return(false);
                }
                ipAgo = CopyFrameHeaderParts(IcmpLayer.ToIPFrame(new IcmpFrame(frame.Source, context.Sources.Address, icmpAgo.Payload)  // 重新汇编整个ICMP/IP数据报文
                {
                    Ttl            = icmpAgo.Ttl,
                    Type           = icmpAgo.Type,
                    Code           = icmpAgo.Code,
                    Sequence       = icmpAgo.Sequence,
                    Identification = unchecked ((ushort)context.Sources.Port), // 客户端ICMP协议请求时的凭证编号
                }), ipAgo);
                frame.Payload = IPv4Layer.ToArray(ipAgo);
            }
            var convertional = new IcmpFrame(frame.Source, context.Sources.Address, frame.Payload)
            {
                Ttl            = frame.Ttl - 1,
                Type           = frame.Type,
                Code           = frame.Code,
                Sequence       = frame.Sequence,
                Identification = frame.Identification,
            };
            IPFrame ip = CopyFrameHeaderParts(IcmpLayer.ToIPFrame(convertional), packet);

            if (ip == null)
            {
                return(false);
            }
            return(this.OnPrivateOutput(ip));
        }
示例#9
0
        public virtual void Listen()
        {
            while (true)
            {
                lock (this.m_syncobj)
                {
                    if (this.m_disposed)
                    {
                        break;
                    }
                }
                if (this.SupportTwoLayerLinks)
                {
#if !NO_USAGE_PCAP_NET
                    // Retrieve the packets
                    PacketCommunicatorReceiveResult result = this.m_packetCommunicator.ReceivePacket(out Packet packet);
                    switch (result)
                    {
                    case PacketCommunicatorReceiveResult.Timeout:
                        // Timeout elapsed
                        continue;

                    case PacketCommunicatorReceiveResult.Ok:
                    {
                        EthernetDatagram datagram = packet.Ethernet;         // 以太网数据报
                        switch (datagram.EtherType)
                        {
                        case EthernetType.IpV4:
                            NetifLevel2LayerIPv4(datagram, datagram.IpV4);
                            break;

                        case EthernetType.IpV6:
                            break;

                        case EthernetType.Arp:
                            NetifLevel2LayerArp(datagram, datagram.Arp);
                            break;

                        default:
                            break;
                        }
                    }
                    break;

                    default:
                        break;
                    }
#endif
                }
                else
                {
                    int packet_size = 0;
                    try
                    {
                        EndPoint localEP = this.m_socket.LocalEndPoint;
                        packet_size = this.m_socket.ReceiveFrom(this.m_buffer, 0, this.m_buffer.Length, SocketFlags.None, ref localEP);
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    if (packet_size <= 0)
                    {
                        continue;
                    }

                    IPFrame frame = IPv4Layer.ParseFrame(new BufferSegment(this.m_buffer, 0, packet_size), true);
                    if (frame != null)
                    {
                        this.OnSniffer(frame);
                    }
                }
            }
        }