Пример #1
0
 /// Construct an endpoint using a port number and an IP address. This
 /// constructor may be used for accepting connections on a specific interface
 /// or for making a connection to a remote endpoint.
 basic_endpoint(const boost::asio::ip::address& addr, unsigned short port_num)
   : data_()
 {
   using namespace std; // For memcpy.
   if (addr.is_v4())
   {
     data_.v4.sin_family = AF_INET;
     data_.v4.sin_port =
       boost::asio::detail::socket_ops::host_to_network_short(port_num);
     data_.v4.sin_addr.s_addr =
       boost::asio::detail::socket_ops::host_to_network_long(
           addr.to_v4().to_ulong());
   }
   else
   {
     data_.v6.sin6_family = AF_INET6;
     data_.v6.sin6_port =
       boost::asio::detail::socket_ops::host_to_network_short(port_num);
     data_.v6.sin6_flowinfo = 0;
     boost::asio::ip::address_v6 v6_addr = addr.to_v6();
     boost::asio::ip::address_v6::bytes_type bytes = v6_addr.to_bytes();
     memcpy(data_.v6.sin6_addr.s6_addr, bytes.elems, 16);
     data_.v6.sin6_scope_id = v6_addr.scope_id();
   }
 }
  void
  initialize(ip::address address)
  {
    ip::address mcastAddr;
    if (address.is_v4()) {
      // the administratively scoped group 224.0.0.254 is reserved for experimentation (RFC 4727)
      mcastAddr = ip::address_v4(0xE00000FE);
    }
    else {
      // the group FF0X::114 is reserved for experimentation at all scope levels (RFC 4727)
      auto v6Addr = ip::address_v6::from_string("FF01::114");
      v6Addr.scope_id(address.to_v6().scope_id());
      mcastAddr = v6Addr;
    }
    mcastEp = udp::endpoint(mcastAddr, 7373);
    remoteMcastEp = udp::endpoint(mcastAddr, 8383);

    MulticastUdpTransport::openRxSocket(remoteSockRx, mcastEp, address);
    MulticastUdpTransport::openTxSocket(remoteSockTx, udp::endpoint(address, 0), nullptr, true);

    udp::socket sockRx(g_io);
    udp::socket sockTx(g_io);
    MulticastUdpTransport::openRxSocket(sockRx, remoteMcastEp, address);
    MulticastUdpTransport::openTxSocket(sockTx, udp::endpoint(address, txPort), nullptr, true);

    face = make_unique<Face>(make_unique<DummyLinkService>(),
                             make_unique<MulticastUdpTransport>(mcastEp, std::move(sockRx), std::move(sockTx),
                                                                ndn::nfd::LINK_TYPE_MULTI_ACCESS));
    transport = static_cast<MulticastUdpTransport*>(face->getTransport());
    receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;

    BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
  }
Пример #3
0
void PacketLog::LogPacket(WorldPacket const& packet, Direction direction, boost::asio::ip::address const& addr, uint16 port)
{
    std::lock_guard<std::mutex> lock(_logPacketLock);

    PacketHeader header;
    *reinterpret_cast<uint32*>(header.Direction) = direction == CLIENT_TO_SERVER ? 0x47534d43 : 0x47534d53;
    header.ConnectionId = 0;
    header.ArrivalTicks = getMSTime();

    header.OptionalDataSize = sizeof(header.OptionalData);
    memset(header.OptionalData.SocketIPBytes, 0, sizeof(header.OptionalData.SocketIPBytes));
    if (addr.is_v4())
    {
        auto bytes = addr.to_v4().to_bytes();
        memcpy(header.OptionalData.SocketIPBytes, bytes.data(), bytes.size());
    }
    else if (addr.is_v6())
    {
        auto bytes = addr.to_v6().to_bytes();
        memcpy(header.OptionalData.SocketIPBytes, bytes.data(), bytes.size());
    }

    header.OptionalData.SocketPort = port;
    header.Length = packet.size() + sizeof(header.Opcode);
    header.Opcode = packet.GetOpcode();

    fwrite(&header, sizeof(header), 1, _file);
    if (!packet.empty())
        fwrite(packet.contents(), 1, packet.size(), _file);

    fflush(_file);
}
Пример #4
0
bool PeerID::Certificate::checkAltNames(boost::asio::ip::address& address) const
{
	GENERAL_NAMES* gens = static_cast<GENERAL_NAMES*>(
		X509_get_ext_d2i(x509_, NID_subject_alt_name, 0, 0));
	BOOST_SCOPE_EXIT(gens){
		GENERAL_NAMES_free(gens);
	}BOOST_SCOPE_EXIT_END;
	for (int i = 0; i < sk_GENERAL_NAME_num(gens); ++i)
	{
		GENERAL_NAME* gen = sk_GENERAL_NAME_value(gens, i);
		if (gen->type == GEN_IPADD)
		{			
			ASN1_OCTET_STRING* ip = gen->d.iPAddress;
			if (ip->type == V_ASN1_OCTET_STRING && ip->data)
			{
				return (
					(address.is_v4() && ip->length == 4 &&
						std::memcmp(address.to_v4().to_bytes().data(), ip->data, 4) == 0) ||
					(address.is_v6() && ip->length == 16 &&
						std::memcmp(address.to_v6().to_bytes().data(), ip->data, 16) == 0)
					);
			}
		}
	}
}
Пример #5
0
		bool is_allowed(const boost::asio::ip::address &address, std::list<std::string> &errors) {
			return (entries_v4.empty()&&entries_v6.empty())
				|| (address.is_v4() && is_allowed_v4(address.to_v4().to_bytes(), errors))
				|| (address.is_v6() && is_allowed_v6(address.to_v6().to_bytes(), errors))
				|| (address.is_v6() && address.to_v6().is_v4_compatible() && is_allowed_v4(address.to_v6().to_v4().to_bytes(), errors))
				|| (address.is_v6() && address.to_v6().is_v4_mapped() && is_allowed_v4(address.to_v6().to_v4().to_bytes(), errors))
				;
		}
Пример #6
0
		bool contains(const boost::asio::ip::address & addr)
		{
			if(addr.is_v4() == false)
				return false;

			hippolib::subnet s(m_network, m_netmask);
			return s.containsIp(addr.to_v4());
		}
Пример #7
0
bool is_link_local(const boost::asio::ip::address& addr) {
    if (addr.is_v6() && addr.to_v6().is_link_local())
        return true;
    if (addr.is_v4() &&
        (mask_address(addr, 16) == address::from_string("169.254.0.0")))
        return true;
    return false;
}
Пример #8
0
bool gateway::address_is_any(const boost::asio::ip::address & addr)
{
    if (addr.is_v4())
    {
        return addr.to_v4() == boost::asio::ip::address_v4::any();
    }


    return addr.to_v6() == boost::asio::ip::address_v6::any();
}
Пример #9
0
 /**
  * If true the address is any.
  */
 static bool address_is_any(const boost::asio::ip::address & addr)
 {
     if (addr.is_v4())
     {
         return addr.to_v4() == boost::asio::ip::address_v4::any();
     }
     else
     {
         return addr.to_v6() == boost::asio::ip::address_v6::any();
     }
 }
Пример #10
0
bool gateway::address_is_multicast(
    const boost::asio::ip::address & addr
    )
{
    if (addr.is_v4())
    {
        return addr.to_v4().is_multicast();
    }

    return addr.to_v6().is_multicast();
}
Пример #11
0
 /**
  * If true the address is multicast.
  */
 static bool address_is_multicast(
     const boost::asio::ip::address & addr
     )
 {
     if (addr.is_v4())
     {
         return addr.to_v4().is_multicast();
     }
     else
     {
         return addr.to_v6().is_multicast();
     }
 }
Пример #12
0
static void addMatchSubnet(struct match* match,
                           const boost::asio::ip::address& ip,
                           uint8_t prefixLen, bool src,
                           uint16_t& ethType) {
   if (ip.is_v4()) {
       switch (ethType) {
       case 0:
           ethType = eth::type::IP;
           /* fall through */
       case eth::type::IP:
       case eth::type::ARP:
           break;
       default:
           return;
       }

       if (prefixLen > 32) prefixLen = 32;
       uint32_t mask = (prefixLen != 0)
           ? (~((uint32_t)0) << (32 - prefixLen))
           : 0;
       uint32_t addr = ip.to_v4().to_ulong() & mask;
       match_set_dl_type(match, htons(ethType));
       if (src)
           match_set_nw_src_masked(match, htonl(addr), htonl(mask));
       else
           match_set_nw_dst_masked(match, htonl(addr), htonl(mask));
   } else {
       switch (ethType) {
       case 0:
           ethType = eth::type::IPV6;
           /* fall through */
       case eth::type::IPV6:
           break;
       default:
           return;
       }

       if (prefixLen > 128) prefixLen = 128;
       struct in6_addr mask;
       struct in6_addr addr;
       network::compute_ipv6_subnet(ip.to_v6(), prefixLen, &mask, &addr);

       match_set_dl_type(match, htons(ethType));
       if (src)
           match_set_ipv6_src_masked(match, &addr, &mask);
       else
           match_set_ipv6_dst_masked(match, &addr, &mask);
   }
}
Пример #13
0
bool ClientAllowed(const boost::asio::ip::address& address)
{
    // Make sure that IPv4-compatible and IPv4-mapped IPv6 addresses are treated as IPv4 addresses
    if (address.is_v6()
     && (address.to_v6().is_v4_compatible()
      || address.to_v6().is_v4_mapped()))
        return ClientAllowed(address.to_v6().to_v4());

    if (address == asio::ip::address_v4::loopback()
     || address == asio::ip::address_v6::loopback()
     || (address.is_v4()
         // Check whether IPv4 addresses match 127.0.0.0/8 (loopback subnet)
      && (address.to_v4().to_ulong() & 0xff000000) == 0x7f000000))
        return true;

    const string strAddress = address.to_string();
    const vector<string>& vAllow = SysCfg().GetMultiArgs("-rpcallowip");
    for (auto strAllow : vAllow)
        if (WildcardMatch(strAddress, strAllow))
            return true;
    return false;
}
Пример #14
0
bool prefix_match(const boost::asio::ip::address& addr,
                  uint32_t srcPfxLen,
                  const boost::asio::ip::address& targetAddr,
                  uint32_t tgtPfxLen,
                  bool &is_exact_match) {
    if (addr.is_v4()) {
        if (srcPfxLen > 32) srcPfxLen = 32;
        uint32_t mask = get_subnet_mask_v4(srcPfxLen);
        uint32_t netAddr = addr.to_v4().to_ulong() & mask;
        uint32_t rtAddr = targetAddr.to_v4().to_ulong() & mask;
        if ((netAddr == rtAddr) &&
            (srcPfxLen <= tgtPfxLen)) {
            if(srcPfxLen == tgtPfxLen)
                is_exact_match=true;
            return true;
        }
    } else {
        if (srcPfxLen > 128) srcPfxLen = 128;
        struct in6_addr mask;
        struct in6_addr netAddr;
        struct in6_addr rtAddr;
        memcpy(&rtAddr, targetAddr.to_v6().to_bytes().data(),
               sizeof(rtAddr));
        network::compute_ipv6_subnet(addr.to_v6(), srcPfxLen,
                                     &mask, &netAddr);

        ((uint64_t*)&rtAddr)[0] &= ((uint64_t*)&mask)[0];
        ((uint64_t*)&rtAddr)[1] &= ((uint64_t*)&mask)[1];

        if ((((uint64_t*)&rtAddr)[0] == ((uint64_t*)&netAddr)[0]) &&
            (((uint64_t*)&rtAddr)[1] == ((uint64_t*)&netAddr)[1]) &&
            (srcPfxLen <= tgtPfxLen)) {
            if(srcPfxLen == tgtPfxLen)
                is_exact_match=true;
            return true;
        }
    }
    return false;
}
Пример #15
0
		bool is_allowed(const boost::asio::ip::address &address, std::list<std::string> &errors) {
			return (address.is_v4() && is_allowed_v4(address.to_v4().to_ulong(), errors))
				|| (address.is_v6() && address.to_v6().is_v4_compatible() && is_allowed_v4(address.to_v6().to_v4().to_ulong(), errors))
				|| (address.is_v6() && address.to_v6().is_v4_mapped() && is_allowed_v4(address.to_v6().to_v4().to_ulong(), errors));
		}
Пример #16
0
 bool
 operator()(const boost::asio::ip::address& address)
 {
   return address.is_v4();
 }