Inheritance: NetworkPacket
示例#1
0
        public IcmpPacket(Types type, short id, short seq_num)
        {
            Type           = type;
            Identifier     = id;
            SequenceNumber = seq_num;
            Code           = (byte)0;

            byte[] msg  = new byte[64];
            Random rand = new Random();

            rand.NextBytes(msg);
            msg[0] = (byte)type;
            msg[1] = Code;
            msg[2] = (byte)0;
            msg[3] = (byte)0;


            NumberSerializer.WriteShort(Identifier, msg, 4);
            NumberSerializer.WriteShort(SequenceNumber, msg, 6);

            short checksum = (short)IPPacket.GenerateChecksum(MemBlock.Reference(msg));

            NumberSerializer.WriteShort(checksum, msg, 2);

            _icpacket = MemBlock.Reference(msg);
            _packet   = MemBlock.Reference(msg);
        }
示例#2
0
文件: Dns.cs 项目: pstjuste/brunet
    /// <summary>Look up a hostname given a Dns request in the form of IPPacket
    /// </summary>
    /// <param name="in_ip">An IPPacket containing the Dns request</param>
    /// <returns>An IPPacket containing the results</returns>
    public virtual IPPacket LookUp(IPPacket in_ip)
    {
      UdpPacket in_udp = new UdpPacket(in_ip.Payload);
      DnsPacket in_dns = new DnsPacket(in_udp.Payload);
      ICopyable out_dns = null;
      string qname = string.Empty;
      bool invalid_qtype = false;

      try {
        string qname_response = String.Empty;
        qname = in_dns.Questions[0].QName;
        DnsPacket.Types type = in_dns.Questions[0].QType;

        if(type == DnsPacket.Types.A || type == DnsPacket.Types.AAAA) {
          qname_response = AddressLookUp(qname);
        } else if(type == DnsPacket.Types.Ptr) {
          qname_response = NameLookUp(qname);
        } else {
          invalid_qtype = true;
        }

        if(qname_response == null) {
          throw new Exception("Unable to resolve");
        }

        Response response = new Response(qname, in_dns.Questions[0].QType,
            in_dns.Questions[0].QClass, 1800, qname_response);
        //Host resolver will not accept if recursive is not available 
        //when it is desired
        DnsPacket res_packet = new DnsPacket(in_dns.ID, false,
            in_dns.Opcode, true, in_dns.RD, in_dns.RD,
            in_dns.Questions, new Response[] {response}, null, null);

        out_dns = res_packet.ICPacket;
      } catch(Exception e) {
        bool failed_resolve = false;
        // The above resolver failed, let's see if another resolver works
        if(_forward_queries) {
          try {
            out_dns = Resolve(_name_server, (byte[]) in_dns.Packet);
          } catch(Exception ex) {
            e = ex;
            failed_resolve = true;
          }
        }

        if(!_forward_queries || failed_resolve) {
          ProtocolLog.WriteIf(IpopLog.Dns, "Failed to resolve: " + qname + "\n\t" + e.Message);
          out_dns = DnsPacket.BuildFailedReplyPacket(in_dns, !invalid_qtype);
        }
      }

      UdpPacket out_udp = new UdpPacket(in_udp.DestinationPort,
                                         in_udp.SourcePort, out_dns);
      return new IPPacket(IPPacket.Protocols.Udp, in_ip.DestinationIP,
                                       in_ip.SourceIP,
                                       out_udp.ICPacket);
    }
示例#3
0
        /**
         * <summary>If we're not creating a packet from scratch, this will keep its
         * integrity and transform UDP and TCP headers as well (due to their checksums
         * being dependent on source and destination ip addresses.</summary>
         * <param name="Packet">The packet to translate.</param>
         * <param name="SourceIP">The new source ip.</param>
         * <param name="DestinationIP">The new destination ip.</param>
         */
        public static MemBlock Translate(MemBlock Packet, MemBlock SourceIP,
                                         MemBlock DestinationIP)
        {
            byte[] new_packet = new byte[Packet.Length];
            Packet.CopyTo(new_packet, 0);
            int length = (Packet[0] & 0xF) * 4;

            SourceIP.CopyTo(new_packet, 12);
            // Do not copy over multicast addresses!
            if (new_packet[16] < 224 || new_packet[16] > 239)
            {
                DestinationIP.CopyTo(new_packet, 16);
            }
            // Zero out the checksum so we don't use it in our future calculations
            new_packet[10] = 0;
            new_packet[11] = 0;
            MemBlock header   = MemBlock.Reference(new_packet, 0, length);
            int      checksum = GenerateChecksum(header);

            new_packet[10] = (byte)((checksum >> 8) & 0xFF);
            new_packet[11] = (byte)(checksum & 0xFF);
            Protocols p = (Protocols)Packet[9];

            bool fragment = ((Packet[6] & 0x1F) | Packet[7]) != 0;

            if (p == Protocols.Udp && !fragment)
            {
                // Zero out the checksum to disable it
                new_packet[length + 6] = 0;
                new_packet[length + 7] = 0;
            }
            else if (p == Protocols.Tcp && !fragment)
            {
                // Zero out the checksum so we don't use it in our future calculations
                new_packet[length + 16] = 0;
                new_packet[length + 17] = 0;
                MemBlock payload      = MemBlock.Reference(new_packet).Slice(length);
                MemBlock pseudoheader = IPPacket.MakePseudoHeader(SourceIP,
                                                                  DestinationIP, (byte)Protocols.Tcp, (ushort)(Packet.Length - 20));
                checksum = IPPacket.GenerateChecksum(payload, pseudoheader);
                new_packet[length + 16] = (byte)((checksum >> 8) & 0xFF);
                new_packet[length + 17] = (byte)(checksum & 0xFF);
            }
            return(MemBlock.Reference(new_packet));
        }
示例#4
0
文件: DNS.cs 项目: kyungyonglee/ipop
        /// <summary>Look up a hostname given a DNS request in the form of IPPacket
        /// </summary>
        /// <param name="req_ipp">An IPPacket containing the DNS request</param>
        /// <returns>An IPPacket containing the results</returns>
        public virtual IPPacket LookUp(IPPacket req_ipp)
        {
            UDPPacket req_udpp = new UDPPacket(req_ipp.Payload);
              DNSPacket dnspacket = new DNSPacket(req_udpp.Payload);
              ICopyable rdnspacket = null;
              try {
            string qname_response = String.Empty;
            string qname = dnspacket.Questions[0].QNAME;
            if(dnspacket.Questions[0].QTYPE == DNSPacket.TYPES.A) {
              qname_response = AddressLookUp(qname);
            }
            else if(dnspacket.Questions[0].QTYPE == DNSPacket.TYPES.PTR) {
              if(!InRange(qname)) {
            throw new Exception("Address out of range.");
              }
              qname_response = NameLookUp(qname);
            }
            if(qname_response == null) {
              throw new Exception("Unable to resolve name: " + qname);
            }
            Response response = new Response(qname, dnspacket.Questions[0].QTYPE,
              dnspacket.Questions[0].QCLASS, 1800, qname_response);

            // For some reason, if RD is set and we don't have RA it Linux `host`
            // doesn't work!
            DNSPacket res_packet = new DNSPacket(dnspacket.ID, false,
              dnspacket.OPCODE, true, dnspacket.RD, dnspacket.RD,
              dnspacket.Questions, new Response[] {response}, null, null);

            rdnspacket = res_packet.ICPacket;
              }
              catch(Exception e) {
            ProtocolLog.WriteIf(IpopLog.DNS, e.Message);
            rdnspacket = DNSPacket.BuildFailedReplyPacket(dnspacket);
              }
              UDPPacket res_udpp = new UDPPacket(req_udpp.DestinationPort,
                                         req_udpp.SourcePort, rdnspacket);
              IPPacket res_ipp = new IPPacket(IPPacket.Protocols.UDP,
                                       req_ipp.DestinationIP,
                                       req_ipp.SourceIP,
                                       res_udpp.ICPacket);
              return res_ipp;
        }
示例#5
0
文件: IpopNode.cs 项目: xujyan/ipop
        ///<summary>This sends an ICMP Request to the specified address, we want
        ///him to respond to us, so we can guarantee that by pretending to be the
        ///Server (i.e. x.y.z.1).  We'll get a response in our main thread.</summary>
        ///<param name="dest_ip">Destination IP of our request.</summary>
        protected virtual void SendICMPRequest(MemBlock dest_ip)
        {
            if(_dhcp_server == null) {
            return;
              }
              MemBlock ether_addr = null;
              if(!_ip_to_ether.TryGetValue(dest_ip, out ether_addr)) {
            ether_addr = EthernetPacket.BroadcastAddress;
              }

              ICMPPacket icmp = new ICMPPacket(ICMPPacket.Types.EchoRequest);
              IPPacket ip = new IPPacket(IPPacket.Protocols.ICMP, _dhcp_server.ServerIP,
              dest_ip, icmp.Packet);
              EthernetPacket ether = new EthernetPacket(ether_addr,
              EthernetPacket.UnicastAddress, EthernetPacket.Types.IP, ip.ICPacket);
              Ethernet.Send(ether.ICPacket);
        }
示例#6
0
文件: IpopNode.cs 项目: xujyan/ipop
 /// <summary>This method is called by HandleIPOut if the destination address
 /// is within the multicast address range.  If you want Multicast, implement
 /// this method, output will most likely be sent via the SendIP() method in the
 /// IpopNode base class.</summary>
 /// <param name="ipp"> The IPPacket the contains the multicast message</param>
 /// <returns>True if implemented, false otherwise.</returns>
 protected virtual bool HandleMulticast(IPPacket ipp)
 {
     return false;
 }
示例#7
0
文件: IpopNode.cs 项目: xujyan/ipop
 /// <summary>This method is called by HandleIPOut if the packet does match any
 /// of the common mappings.</summary>
 /// <param name="ipp"> The miscellaneous IPPacket.</param>
 /// <returns>True if implemented, false otherwise.</returns>
 protected virtual bool HandleOther(IPPacket ipp)
 {
     return false;
 }
示例#8
0
文件: IpopNode.cs 项目: xujyan/ipop
        /// <summary>This is used to process a dhcp packet on the node side, that
        /// includes placing data such as the local Brunet Address, Ipop Namespace,
        /// and other optional parameters in our request to the dhcp server.  When
        /// receiving the results, if it is successful, the results are written to
        /// the TAP device.</summary>
        /// <param name="ipp"> The IPPacket that contains the DHCP Request</param>
        /// <param name="dhcp_params"> an object containing any extra parameters for 
        /// the dhcp server</param>
        /// <returns> true on if dhcp is supported.</returns>
        protected virtual bool HandleDHCP(IPPacket ipp)
        {
            UDPPacket udpp = new UDPPacket(ipp.Payload);
              DHCPPacket dhcp_packet = new DHCPPacket(udpp.Payload);
              MemBlock ether_addr = dhcp_packet.chaddr;

              if(_dhcp_config == null) {
            return true;
              }

              DHCPServer dhcp_server = CheckOutDHCPServer(ether_addr);
              if(dhcp_server == null) {
            return true;
              }

              MemBlock last_ip = null;
              _ether_to_ip.TryGetValue(ether_addr, out last_ip);
              byte[] last_ipb = (last_ip == null) ? null : (byte[]) last_ip;

              WaitCallback wcb = delegate(object o) {
            ProtocolLog.WriteIf(IpopLog.DHCPLog, String.Format(
            "Attemping DHCP for: {0}", Utils.MemBlockToString(ether_addr, '.')));

            DHCPPacket rpacket = null;
            try {
              rpacket = dhcp_server.ProcessPacket(dhcp_packet,
              Brunet.Address.ToString(), last_ipb);
            } catch(Exception e) {
              ProtocolLog.WriteIf(IpopLog.DHCPLog, e.Message);
              CheckInDHCPServer(dhcp_server);
              return;
            }

            /* Check our allocation to see if we're getting a new address */
            MemBlock new_addr = rpacket.yiaddr;
            UpdateMapping(ether_addr, new_addr);

            MemBlock destination_ip = ipp.SourceIP;
            if(destination_ip.Equals(IPPacket.ZeroAddress)) {
              destination_ip = IPPacket.BroadcastAddress;
            }

            UDPPacket res_udpp = new UDPPacket(_dhcp_server_port, _dhcp_client_port, rpacket.Packet);
            IPPacket res_ipp = new IPPacket(IPPacket.Protocols.UDP, rpacket.siaddr,
            destination_ip, res_udpp.ICPacket);
            EthernetPacket res_ep = new EthernetPacket(ether_addr, EthernetPacket.UnicastAddress,
            EthernetPacket.Types.IP, res_ipp.ICPacket);
            Ethernet.Send(res_ep.ICPacket);
            CheckInDHCPServer(dhcp_server);
              };

              ThreadPool.QueueUserWorkItem(wcb);
              return true;
        }
示例#9
0
文件: IpopNode.cs 项目: xujyan/ipop
        /// <summary>This method handles IPPackets that come from the TAP Device, i.e.,
        /// local system.</summary>
        /// <remarks>Currently this supports HandleMulticast (ip[0] >= 244 &&
        /// ip[0]<=239), HandleDNS (dport = 53 and ip[3] == 1), dhcp (sport 68 and
        /// dport 67.</remarks>
        /// <param name="packet">The packet from the TAP device</param>
        /// <param name="from"> This should always be the tap device</param>
        protected virtual void HandleIPOut(EthernetPacket packet, ISender ret)
        {
            IPPacket ipp = new IPPacket(packet.Payload);
              if(IpopLog.PacketLog.Enabled) {
            ProtocolLog.Write(IpopLog.PacketLog, String.Format(
                          "Outgoing {0} packet::IP src: {1}, IP dst: {2}",
                          ipp.Protocol, ipp.SSourceIP, ipp.SDestinationIP));
              }

              if(!IsLocalIP(ipp.SourceIP)) {
            HandleNewStaticIP(packet.SourceAddress, ipp.SourceIP);
            return;
              }

              if(ipp.DestinationIP[0] >= 224 && ipp.DestinationIP[0] <= 239) {
            if(HandleMulticast(ipp)) {
              return;
            }
              }

              switch(ipp.Protocol) {
            case IPPacket.Protocols.UDP:
              UDPPacket udpp = new UDPPacket(ipp.Payload);
              if(udpp.SourcePort == _dhcp_client_port && udpp.DestinationPort == _dhcp_server_port) {
            if(HandleDHCP(ipp)) {
              return;
            }
              } else if(udpp.DestinationPort == 53 &&ipp.DestinationIP.Equals(_dhcp_server.ServerIP)) {
            if(HandleDNS(ipp)) {
              return;
            }
              }
              break;
              }

              if(HandleOther(ipp)) {
            return;
              }

              if(_dhcp_server == null || ipp.DestinationIP.Equals(_dhcp_server.ServerIP)) {
            return;
              }

              AHAddress target = (AHAddress) _address_resolver.Resolve(ipp.DestinationIP);
              if (target != null) {
            if(IpopLog.PacketLog.Enabled) {
              ProtocolLog.Write(IpopLog.PacketLog, String.Format(
                            "Brunet destination ID: {0}", target));
            }
            SendIP(target, packet.Payload);
              }
        }
示例#10
0
 /// <summary>
 /// This method handles multicast packets (not yet implemented)
 /// </summary>
 /// <param name="ipp">A multicast packet to be processed</param>
 /// <returns></returns>
 protected override bool HandleMulticast(IPPacket ipp) {
   foreach(Address addr in _marad.mcast_addr) {
     SendIP(addr, ipp.Packet);
   }
   return true;
 }
示例#11
0
 /// <summary>Calls HandleMulticast.</summary>
 protected override bool HandleBroadcast(IPPacket ipp)
 {
     return HandleMulticast(ipp);
 }
示例#12
0
 /// <summary>This method is called by HandleIPOut if the destination address
 /// is the broadcast address.  If you want Broadcast, implement
 /// this method, output will most likely be sent via the SendIP() method in the
 /// IpopNode base class.</summary>
 /// <param name="ipp"> The IPPacket the contains the broadcast message</param>
 /// <returns>True if implemented, false otherwise.</returns>
 protected virtual bool HandleBroadcast(IPPacket ipp) {
   return false;
 }
    /**
    <summary>Implements the ITranslator portion for ManagedAddress..., takes an
    IP Packet, based upon who the originating Brunet Sender was, changes who
    the packet was sent from and then switches the destination address to the
    local nodes address. Takes incomming packets only.</summary>
    <param name="packet">The IP Packet to translate.</param>
    <param name="from">The Brunet address the packet was sent from.</param>
    <returns>The translated IP Packet.</returns>
    */
    public MemBlock Translate(MemBlock packet, Address from) {
      UpdateCounter(_rx_counters, from);
      MemBlock source_ip = _addr_ip[from];
      if(source_ip == null) {
        throw new Exception("Invalid mapping " + from + ".");
      }

      // Attempt to translate a packet
      IPPacket ipp = new IPPacket(packet);
      // hdr is everything in the packet up to the source IP, dest IP, "options"
      //   and data
      MemBlock hdr = packet.Slice(0,12);
      // Pull the "fragment" info from the header. If it is not fragmented, we
      // can deal with the packet. DNS packets should never be sent as fragments
      bool fragment = ((hdr[6] & 0x1F) | hdr[7]) != 0;
      //should there be a field in the IPPacket class for this?
      
      MemBlock dest_ip = ipp.DestinationIP;
      byte dest_ip_first_byte = dest_ip[0];
      bool is_multicast = dest_ip_first_byte > 223 && dest_ip_first_byte < 240;
      //multicast addresses are 224.0.0.0 through 239.255.255.255
      
      if(ipp.Protocol == IPPacket.Protocols.Udp && !fragment) { //simple UDP
        UdpPacket udpp = new UdpPacket(ipp.Payload);
        foreach(IProtocolTranslator<UdpPacket> i in _udp_translators) {
          if(i.MatchesProtocol(udpp)) {
            udpp = i.Translate(udpp, source_ip,
                               ipp.SourceIP, ipp.DestinationIP);
            return new IPPacket(ipp.Protocol, source_ip,
                                is_multicast?
                                  ipp.DestinationIP:
                                  _local_ip,
                                hdr, udpp.ICPacket).Packet;
          }
        }
      }
      
      //fallback
      return IPPacket.Translate(packet, source_ip,
                                is_multicast?
                                  ipp.DestinationIP:
                                  _local_ip
      );
    }
示例#14
0
    /// <summary>This method handles IPPackets that come from Brunet, i.e.,
    /// abroad.  </summary>
    /// <param name="packet"> The packet from Brunet.</param>
    /// <param name="ret">An ISender to send data to the Brunet node that sent
    /// the packet.</param>
    public virtual void HandleIPIn(MemBlock packet, ISender ret) {
      Address addr = _conn_handler.GetAddress(ret);

      if(_translator != null) {
        try {
          packet = _translator.Translate(packet, addr);
        }
        catch (Exception e) {
          if(ProtocolLog.Exceptions.Enabled) {
            ProtocolLog.Write(ProtocolLog.Exceptions, e.ToString());
          }
          return;
        }
      }

      IPPacket ipp = new IPPacket(packet);

      if(!_address_resolver.Check(ipp.SourceIP, addr)) {
        Address other = _address_resolver.Resolve(ipp.SourceIP);
        if(other == null) {
          MissedMapping(ipp.SSourceIP, addr);
          return;
        } else if(other != addr) {
          ProtocolLog.WriteIf(IpopLog.ResolverLog, String.Format(
              "IP:P2P Mismatch IP: {0}, Claimed P2P: {1}, Local P2P: {2}",
              ipp.SourceIP, addr, other));
          return;
        }
      }

      if(IpopLog.PacketLog.Enabled) {
        ProtocolLog.Write(IpopLog.PacketLog, String.Format(
                          "Incoming packet:: IP src: {0}, IP dst: {1}, p2p " +
                              "from: {2}, size: {3}", ipp.SSourceIP, ipp.SDestinationIP,
                              ret, packet.Length));
      }

      WriteIP(packet);
    }
示例#15
0
    /// <summary>This method handles IPPackets that come from the TAP Device, i.e.,
    /// local system.</summary>
    /// <remarks>Currently this supports HandleMulticast (ip[0] >= 244 &&
    /// ip[0]<=239), HandleDns (dport = 53 and ip[3] == 1), dhcp (sport 68 and
    /// dport 67.</remarks>
    /// <param name="packet">The packet from the TAP device</param>
    /// <param name="from"> This should always be the tap device</param>
    protected virtual void HandleIPOut(EthernetPacket packet, ISender ret) {
      IPPacket ipp = new IPPacket(packet.Payload);

      if(IpopLog.PacketLog.Enabled) {
        ProtocolLog.Write(IpopLog.PacketLog, String.Format(
                          "Outgoing {0} packet::IP src: {1}, IP dst: {2}", 
                          ipp.Protocol, ipp.SSourceIP, ipp.SDestinationIP));
      }

      if(!IsLocalIP(ipp.SourceIP)) {
        // This really ought to have been caught in ARP, but just in case...
        HandleNewStaticIP(packet.SourceAddress, ipp.SourceIP);
        return;
      }

      UdpPacket udpp = null;
      switch(ipp.Protocol) {
        case IPPacket.Protocols.Udp:
          udpp = new UdpPacket(ipp.Payload);
          if(udpp.SourcePort == _dhcp_client_port && udpp.DestinationPort == _dhcp_server_port) {
            if(HandleDhcp(ipp)) {
              return;
            }
          } else if(udpp.DestinationPort == 53 && ipp.DestinationIP.Equals(_dhcp_server.ServerIP)) {
            if(HandleDns(ipp)) {
              return;
            }
          }
          break;
      }

      if(ipp.DestinationIP[0] >= 224 && ipp.DestinationIP[0] <= 239) {
        // We don't want to send Brunet multicast packets over IPOP!
        if(udpp != null && udpp.DestinationPort == IPHandler.mc_port) {
          return;
        } else if(HandleMulticast(ipp)) {
          return;
        }
      }

      if(ipp.DestinationIP.Equals(IPPacket.BroadcastAddress)) {
        if(HandleBroadcast(ipp)) {
          return;
        }
      }

      if(_dhcp_server == null || ipp.DestinationIP.Equals(_dhcp_server.ServerIP)) {
        return;
      }

      Address target = null;
      try {
        target = _address_resolver.Resolve(ipp.DestinationIP) as AHAddress;
      } catch(AddressResolutionException ex) {
        if(ex.Issue != AddressResolutionException.Issues.DoesNotExist) {
          throw;
        }
        // Otherwise nothing to do, mapping doesn't exist...
      }

      if(target != null) {
        SendIP(target, packet.Payload);
      }
    }
        /**
        <summary>Implements the ITranslator portion for ManagedAddress..., takes an
        IP Packet, based upon who the originating Brunet Sender was, changes who
        the packet was sent from and then switches the destination address to the
        local nodes address</summary>
        <param name="packet">The IP Packet to translate.</param>
        <param name="from">The Brunet address the packet was sent from.</param>
        <returns>The translated IP Packet.</returns>
        */
        public MemBlock Translate(MemBlock packet, Address from)
        {
            MemBlock source_ip = (MemBlock) _addr_ip[from];
              if(source_ip == null) {
            throw new Exception("Invalid mapping " + from + ".");
              }

              // Attempt to translate a MDNS packet
              IPPacket ipp = new IPPacket(packet);
              MemBlock hdr = packet.Slice(0,12);
              bool fragment = ((packet[6] & 0x1F) | packet[7]) != 0;

              if(ipp.Protocol == IPPacket.Protocols.UDP && !fragment) {
            UDPPacket udpp = new UDPPacket(ipp.Payload);
            // MDNS runs on 5353
            if(udpp.DestinationPort == 5353) {
              DNSPacket dnsp = new DNSPacket(udpp.Payload);
              String ss_ip = DNSPacket.IPMemBlockToString(source_ip);
              bool change = mDnsTranslate(dnsp.Answers, ss_ip);
              change |= mDnsTranslate(dnsp.Authority, ss_ip);
              change |= mDnsTranslate(dnsp.Additional, ss_ip);
              // If we make a change let's make a new packet!
              if(change) {
            dnsp = new DNSPacket(dnsp.ID, dnsp.QUERY, dnsp.OPCODE, dnsp.AA,
                                 dnsp.RA, dnsp.RD, dnsp.Questions, dnsp.Answers,
                                 dnsp.Authority, dnsp.Additional);
            udpp = new UDPPacket(udpp.SourcePort, udpp.DestinationPort,
                                 dnsp.ICPacket);
            ipp = new IPPacket(ipp.Protocol, source_ip, ipp.DestinationIP,
                               hdr, udpp.ICPacket);
            return ipp.Packet;
              }
            }
            else if(udpp.DestinationPort >= 5060 && udpp.DestinationPort < 5100) {
              udpp = SIPTranslate(udpp, source_ip, ipp.SSourceIP,
                              ipp.SDestinationIP);
              ipp = new IPPacket(ipp.Protocol, source_ip, _local_ip, hdr,
                             udpp.ICPacket);
              return ipp.Packet;
            }
              }
              return IPPacket.Translate(packet, source_ip, _local_ip);
        }
示例#17
0
        /// <summary>Called by HandleIPOut if the current packet has a Multicast 
        /// address in its destination field.  This sends the multicast packet
        /// to all members of the multicast group stored in the dht.</summary>
        /// <param name="ipp">The IP Packet destined for multicast.</param>
        /// <returns>This returns true since this is implemented.</returns>
        protected override bool HandleMulticast(IPPacket ipp)
        {
            if(!_ipop_config.EnableMulticast) {
            return true;
              }

              WaitCallback wcb = delegate(object o) {
            Hashtable[] results = null;
            try {
              results = AppNode.Dht.Get(Encoding.UTF8.GetBytes(_ipop_config.IpopNamespace + ".multicast.ipop"));
            } catch {
              return;
            }
            foreach(Hashtable result in results) {
              try {
            AHAddress target = (AHAddress) AddressParser.Parse(Encoding.UTF8.GetString((byte[]) result["value"]));
            if(IpopLog.PacketLog.Enabled) {
              ProtocolLog.Write(IpopLog.PacketLog, String.Format(
                                "Brunet destination ID: {0}", target));
            }
            SendIP(target, ipp.Packet);
              }
              catch {}
            }
              };

              ThreadPool.QueueUserWorkItem(wcb, ipp);
              return true;
        }
示例#18
0
 /// <summary>This calls a Dns Lookup using ThreadPool.</summary>
 /// <param name="ipp">The IP Packet containing the Dns query.</param>
 /// <returns>Returns true since this is implemented.</returns>
 protected override bool HandleDns(IPPacket ipp)
 {
     WaitCallback wcb = delegate(object o) {
     try {
       WriteIP(_dns.LookUp(ipp).ICPacket);
     } catch {
     }
       };
       ThreadPool.QueueUserWorkItem(wcb, ipp);
       return true;
 }
示例#19
0
文件: IpopNode.cs 项目: xujyan/ipop
        /// <summary>Writes an IPPacket as is to the TAP device.</summary>
        /// <param name="packet">The IPPacket!</param>
        protected virtual void WriteIP(ICopyable packet)
        {
            MemBlock mp = packet as MemBlock;
              if(mp == null) {
            mp = MemBlock.Copy(packet);
              }

              IPPacket ipp = new IPPacket(mp);
              MemBlock dest = null;
              if(!_ip_to_ether.TryGetValue(ipp.DestinationIP, out dest)) {
            return;
              }

              EthernetPacket res_ep = new EthernetPacket(_ip_to_ether[ipp.DestinationIP],
              EthernetPacket.UnicastAddress, EthernetPacket.Types.IP, mp);
              Ethernet.Send(res_ep.ICPacket);
        }
示例#20
0
 /// <summary>
 /// This method handles multicast packets (not yet implemented)
 /// </summary>
 /// <param name="ipp">A multicast packet to be processed</param>
 /// <returns></returns>
 protected override bool HandleMulticast(IPPacket ipp) {
   foreach(Address addr in _marad.mcast_addr) {
     if(AppNode.SymphonySecurityOverlord.ContainsSender(addr)) {
       SendIP(addr, ipp.Packet);
     }
   }
   return true;
 }
示例#21
0
 /// <summary>
 /// This method handles incoming Dns Packets
 /// </summary>
 /// <param name="ipp">A Dns IPPacket to be processed</param>
 /// <returns>A boolean result</returns>
 protected override bool HandleDns(IPPacket ipp) {
   WriteIP(_dns.LookUp(ipp).ICPacket);
   return true;
 }
示例#22
0
文件: IpopNode.cs 项目: xujyan/ipop
        /// <summary>This method handles IPPackets that come from Brunet, i.e.,
        /// abroad.  </summary>
        /// <param name="packet"> The packet from Brunet.</param>
        /// <param name="ret">An ISender to send data to the Brunet node that sent
        /// the packet.</param>
        public virtual void HandleIPIn(MemBlock packet, ISender ret)
        {
            if(_secure_senders && !(ret is SecurityAssociation)) {
            return;
              }

              Address addr = null;
              if(ret is SecurityAssociation) {
            ret = ((SecurityAssociation) ret).Sender;
              }

              if(ret is AHSender) {
            addr = ((AHSender) ret).Destination;
              } else {
            ProtocolLog.Write(IpopLog.PacketLog, String.Format(
              "Incoming packet was not from an AHSender: {0}.", ret));
            return;
              }

              if(_translator != null) {
            try {
              packet = _translator.Translate(packet, addr);
            }
            catch (Exception e) {
              if(ProtocolLog.Exceptions.Enabled) {
            ProtocolLog.Write(ProtocolLog.Exceptions, e.ToString());
              }
              return;
            }
              }

              IPPacket ipp = new IPPacket(packet);

              if(!_address_resolver.Check(ipp.SourceIP, addr)) {
            return;
              }

              if(IpopLog.PacketLog.Enabled) {
            ProtocolLog.Write(IpopLog.PacketLog, String.Format(
                          "Incoming packet:: IP src: {0}, IP dst: {1}, p2p " +
                              "from: {2}, size: {3}", ipp.SSourceIP, ipp.SDestinationIP,
                              ret, packet.Length));
              }

              WriteIP(packet);
        }
示例#23
0
 /// <summary>Sends the IP Packet to the specified target address.</summary>
 /// <param name="target"> the Brunet Address of the target</param>
 /// <param name="packet"> the data to send to the recepient</param>
 protected virtual void SendIP(Address target, MemBlock packet) {
   if(!_conn_handler.Send(target, packet)) {
     if(IpopLog.PacketLog.Enabled) {
       IPPacket ipp = new IPPacket(packet);
       ProtocolLog.Write(IpopLog.PacketLog, String.Format(
             "No connection to destination (IP / P2P): {0} / {1}",
             ipp.SourceIP, target));
     }
   }
 }
示例#24
0
文件: IpopNode.cs 项目: bakriy/brunet
    /// <summary>This method handles IPPackets that come from Brunet, i.e.,
    /// abroad.  </summary>
    /// <param name="packet"> The packet from Brunet.</param>
    /// <param name="ret">An ISender to send data to the Brunet node that sent
    /// the packet.</param>
    public virtual void HandleIPIn(MemBlock packet, ISender ret) {
      if(_secure_senders && !(ret is SecurityAssociation)) {
        return;
      }

      Address addr = null;
      if(ret is SecurityAssociation) {
        ret = ((SecurityAssociation) ret).Sender;
      }

      if(ret is AHSender) {
        addr = ((AHSender) ret).Destination;
      } else {
        ProtocolLog.Write(IpopLog.PacketLog, String.Format(
          "Incoming packet was not from an AHSender: {0}.", ret));
        return;
      }

      if(_translator != null) {
        try {
          packet = _translator.Translate(packet, addr);
        }
        catch (Exception e) {
          if(ProtocolLog.Exceptions.Enabled) {
            ProtocolLog.Write(ProtocolLog.Exceptions, e.ToString());
          }
          return;
        }
      }

      IPPacket ipp = new IPPacket(packet);

      try {
        if(!_address_resolver.Check(ipp.SourceIP, addr)) {
          return;
        }
      } catch (AddressResolutionException ex) {
        if(ex.Issue == AddressResolutionException.Issues.DoesNotExist) {
          ProtocolLog.WriteIf(IpopLog.ResolverLog, "Notifying remote node of " +
              " missing address: " + addr + ":" + ipp.SSourceIP);
          ISender sender = new AHExactSender(AppNode.Node, addr);
          AppNode.Node.Rpc.Invoke(sender, null, "Ipop.NoSuchMapping", ipp.SSourceIP);
          return;
        } else {
          throw;
        }
      }

      if(IpopLog.PacketLog.Enabled) {
        ProtocolLog.Write(IpopLog.PacketLog, String.Format(
                          "Incoming packet:: IP src: {0}, IP dst: {1}, p2p " +
                              "from: {2}, size: {3}", ipp.SSourceIP, ipp.SDestinationIP,
                              ret, packet.Length));
      }

      WriteIP(packet);
    }
示例#25
0
    /// <summary>Writes an IPPacket as is to the TAP device.</summary>
    /// <param name="packet">The IPPacket!</param>
    protected virtual void WriteIP(ICopyable packet)
    {
      MemBlock mp = packet as MemBlock;
      if(mp == null) {
        mp = MemBlock.Copy(packet);
      }

      IPPacket ipp = new IPPacket(mp);
      MemBlock dest = null;
  
      if(!_ip_to_ether.TryGetValue(ipp.DestinationIP, out dest)) {
        if(ipp.DestinationIP[0] >= 224 && ipp.DestinationIP[0] <= 239) {
          dest = EthernetPacket.GetMulticastEthernetAddress(ipp.DestinationIP);
        } else if(ipp.DestinationIP[3] == 255){
          dest = EthernetPacket.BroadcastAddress;
        } else {
          return;
        }
      }

      EthernetPacket res_ep = new EthernetPacket(dest,
            EthernetPacket.UnicastAddress, EthernetPacket.Types.IP, mp);
      Ethernet.Send(res_ep.ICPacket);
    }
示例#26
0
 /// <summary>
 /// This method handles incoming Dns Packets
 /// </summary>
 /// <param name="ipp">A Dns IPPacket to be processed</param>
 /// <returns>A boolean result</returns>
 protected override bool HandleDns(IPPacket ipp) {
   System.Threading.ThreadPool.QueueUserWorkItem(HandleDnsCallback, ipp);
   return true;
 }