コード例 #1
0
ファイル: Ethernet.cs プロジェクト: liulilittle/SkylakeNAT
        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);
                }
            }
        }
コード例 #2
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);
             }
         }
     }
 }
コード例 #3
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));
        }
コード例 #4
0
ファイル: Ethernet.cs プロジェクト: liulilittle/SkylakeNAT
        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);
                    }
                }
            }
        }