Exemplo n.º 1
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)
					);
			}
		}
	}
}
Exemplo n.º 2
0
 void RouterContext::UpdateNTCPV6Address (const boost::asio::ip::address& host)
 {
     bool updated = false, found = false;    
     int port = 0;
     auto& addresses = m_RouterInfo.GetAddresses ();
     for (auto& addr : addresses)
     {
         if (addr.host.is_v6 () && addr.transportStyle == i2p::data::RouterInfo::eTransportNTCP)
         {
             if (addr.host != host)
             {
                 addr.host = host;
                 updated = true;
             }
             found = true;   
         }   
         else
             port = addr.port;   
     }   
     if (!found)
     {
         // create new address
         m_RouterInfo.AddNTCPAddress (host.to_string ().c_str (), port);
         auto mtu = i2p::util::net::GetMTU (host);
         if (mtu)
         {   
             LogPrint ("Our v6 MTU=", mtu);
             if (mtu > 1472) mtu = 1472; 
         }   
         m_RouterInfo.AddSSUAddress (host.to_string ().c_str (), port, GetIdentHash (), mtu ? mtu : 1472); // TODO
         updated = true;
     }
     if (updated)
         UpdateRouterInfo ();
 }
Exemplo n.º 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);
}
Exemplo n.º 4
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);
  }
Exemplo n.º 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());
		}
Exemplo n.º 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;
}
Exemplo n.º 8
0
bool IsLoopbackAddress(const boost::asio::ip::address& addr) {

	if (addr.is_v6()) {
		return addr.to_v6().is_loopback();
	} else {
		return (addr.to_v4() == boost::asio::ip::address_v4::loopback());
	}
}
Exemplo n.º 9
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();
}
Exemplo n.º 10
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();
     }
 }
Exemplo n.º 11
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();
}
Exemplo n.º 12
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))
				;
		}
Exemplo n.º 13
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();
     }
 }
Exemplo n.º 14
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);
   }
}
Exemplo n.º 15
0
bool TcpProxyServer::GetRemoteAddressAndPort(boost::asio::ip::tcp::socket& clientSocket, boost::asio::ip::address& remoteAddress, unsigned short& remotePort)
{
	boost::asio::ip::tcp::socket::endpoint_type userAgentEnpoint = clientSocket.remote_endpoint();
    unsigned short userAgentPort = userAgentEnpoint.port();
	boost::asio::ip::address userAgentIP = userAgentEnpoint.address();
	if(userAgentIP != boost::asio::ip::address_v4::from_string("127.0.0.1")) {
		return false;
	}

	std::pair<u_long, USHORT> remoteAddressPair = WinsockHooker::GetRemoteAddressPair(userAgentPort);
	
	if(remoteAddressPair.first == 0ul) {
		return false;
	}

	boost::asio::ip::address_v4 remote_address(remoteAddressPair.first);
	unsigned short remote_port = remoteAddressPair.second;
	if(remote_address == boost::asio::ip::address_v4::from_string("127.0.0.1") && remote_port == listen_port)
	{
		return false;
	}
	remoteAddress = remote_address;
	remotePort = remote_port;

	TSINFO4CXX("Connect: IP:" << remoteAddress.to_string() << ", Port: " << remotePort);
	return true;
}
Exemplo n.º 16
0
static bool
isLoopback(const boost::asio::ip::address& addr)
{
  if (addr.is_loopback()) {
    return true;
  }
  // Workaround for loopback IPv4-mapped IPv6 addresses
  // see https://svn.boost.org/trac/boost/ticket/9084
  else if (addr.is_v6()) {
    auto addr6 = addr.to_v6();
    if (addr6.is_v4_mapped()) {
      return addr6.to_v4().is_loopback();
    }
  }

  return false;
}
Exemplo n.º 17
0
bool Server::IsBlockedAddress(const boost::asio::ip::address& address)
{
    BOOST_FOREACH(const auto& pattern, config_.blocking_address_patterns()) {
        if (network::Utils::MatchWithWildcard(pattern, address.to_string())) {
            return true;
        }
    }
    return false;
}
Exemplo n.º 18
0
  bool subsumes( Entry other )
  {
    if ( addr.is_v6() != other.addr.is_v6() )
      {
        // jeden adres jest v6, drugi v4
        // zwracamy zawsze false
        return false;
      }

    if ( addr.is_v6() )
      {
        boost::asio::ip::address_v6::bytes_type a1 = addr.to_v6().to_bytes();
        boost::asio::ip::address_v6::bytes_type a2 = other.addr.to_v6().to_bytes();
        int mask_left = mask;
        for(unsigned int i = 0; i < a1.size(); i++)
          {
            int tmp = (1 << std::max(0,std::min(mask_left,8)))-1;
            unsigned char apply_mask = tmp;
            mask_left -= 8;
            a1[i] &= apply_mask;
            a2[i] &= apply_mask;
          }
        bool val = boost::asio::ip::address_v6(a1) == boost::asio::ip::address_v6(a2);
        return val;
      }
    else
      {
        boost::asio::ip::address_v4::bytes_type a1 = addr.to_v4().to_bytes();
        boost::asio::ip::address_v4::bytes_type a2 = other.addr.to_v4().to_bytes();
        int mask_left = mask;
        for(unsigned int i = 0; i < a1.size(); i++)
          {
            int tmp = (1 << std::max(0,std::min(mask_left,8)))-1;
            unsigned char apply_mask = tmp;
            mask_left -= 8;
            a1[i] &= apply_mask;
            a2[i] &= apply_mask;
          }
        bool val = boost::asio::ip::address_v4(a1) == boost::asio::ip::address_v4(a2);
        return val;
      }

    return false;
  }
Exemplo n.º 19
0
bool PeerID::Certificate::checkCommonNames(const boost::asio::ip::address& address) const
{
	std::string ipString = address.to_string();
	return checkCommonNames(
		[&ipString](char const * host, std::size_t len) -> bool
	{
		return boost::iequals(
			ipString,
			std::make_pair(host, host + len));
	});
}
Exemplo n.º 20
0
 /**
  * If true the address is private.
  */
 static bool address_is_private(
     const boost::asio::ip::address & addr
     )
 {
     if (addr.is_v6())
     {
         return addr.to_v6().is_link_local();
     }
     else
     {
         std::uint32_t ip = addr.to_v4().to_ulong();
         
         return (
             (ip & 0xff000000) == 0x0a000000 || 
             (ip & 0xfff00000) == 0xac100000 || 
             (ip & 0xffff0000) == 0xc0a80000
         );
     }
     
     return false;
 }
Exemplo n.º 21
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;
}
Exemplo n.º 22
0
Server::Server(const boost::asio::ip::address &address
		, short unsigned int &port
		, const std::string &docRoot
		, std::size_t threadCount)
	: mThreadCount(threadCount)
	, mAcceptor(mIoService)
	, mRequestHandler(docRoot)
	, mNewConnection(new Session(mIoService, mRequestHandler))
{
	// Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
	boost::asio::ip::tcp::resolver resolver(mIoService);
	boost::asio::ip::tcp::resolver::query query(address.to_string(), std::to_string(port));
	boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
	mAcceptor.open(endpoint.protocol());
	mAcceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
	mAcceptor.bind(endpoint);
	mAcceptor.listen();
	mAcceptor.async_accept(mNewConnection->socket()
			, boost::bind(&Server::handleAccept, this, boost::asio::placeholders::error));
}
Exemplo n.º 23
0
bool AccessControl::isWhiteListedNetwork(const boost::asio::ip::address& address) const
{
  boost::system::error_code ec;
  std::string ipAddress = address.to_string(ec);
  if (ec)
    return false;

  _packetCounterMutex.lock();
  for (std::set<std::string>::const_iterator iter = _networkWhiteList.begin();
    iter != _networkWhiteList.end(); iter++)
  {
    if (OSS::socket_address_cidr_verify(ipAddress, *iter))
    {
      _packetCounterMutex.unlock();
      return true;
    }
  }
  
  _packetCounterMutex.unlock();
  return false;
}
Exemplo n.º 24
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;
}
Exemplo n.º 25
0
int32_t TXMLProtocol::writeIPADDR(const boost::asio::ip::address& ipaddress) {
  return writePlain(ipaddress.to_string());
}
Exemplo n.º 26
0
  void print()
  {
        show_route_protocol_short(inner.rtmsg_ptr->rtm_protocol);
        show_route_type_short(inner.rtmsg_ptr->rtm_type);
        show_route_scope_short(inner.rtmsg_ptr->rtm_scope);

        for(int i=0; i<indent;i++) printf(" ");
        printf(" %s/%d%s%s ===> ", addr.to_string().c_str(), inner.dest_mask, 
               strlen(inner.gateway) ? " via " : "", inner.gateway);

        rtattr_ptr = (struct rtattr *) RTM_RTA(inner.rtmsg_ptr);
        rtmsg_len = inner.rtmsg_len;

        for(; RTA_OK(rtattr_ptr, rtmsg_len); rtattr_ptr = RTA_NEXT(rtattr_ptr, rtmsg_len)) {
            const char * attr_name = attr_to_name_short(rtattr_ptr->rta_type);

            switch(rtattr_ptr->rta_type) {
              // te parametry już przetworzyliśmy
            case RTA_DST:
            case RTA_GATEWAY:
              break;

            case RTA_SRC:
            case RTA_PREFSRC:
            {
                char buf[128];
                inet_ntop(rtmsg_ptr->rtm_family, RTA_DATA(rtattr_ptr), buf, 128);
                printf("%s %s ", attr_name, buf);
            }
            break;

            case RTA_IIF:
            case RTA_OIF:
            {
                printf("%s %d ", attr_name, *((int *) RTA_DATA(rtattr_ptr)));
            }
            break;

            case RTA_TABLE:
              printf("%s", attr_name);
              show_route_table_short((enum rt_class_t)rtmsg_ptr->rtm_table);
              printf(" ");
                break;

            case RTA_UNSPEC:
            case RTA_PRIORITY:
            case RTA_METRICS:
            case RTA_MULTIPATH:
            case RTA_FLOW:
            case RTA_CACHEINFO:
            case RTA_MARK:
            case RTA_PROTOINFO:
            case RTA_SESSION:
            case RTA_MP_ALGO:
                printf("%s ", attr_name);
                break;

            default:
                printf("Unkown attribute ");
                break;

            }
        }

        printf("\n");
  }
Exemplo n.º 27
0
 Client::Client(const std::string& name, const boost::asio::ip::address& ip) :
   AbstractReceivable(2 + (ip.is_v6() ? 16 : 4) + name.size()),
   its_name(name),
   its_ip(ip)
 {
 }
Exemplo n.º 28
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));
		}
Exemplo n.º 29
0
//	////////////////////////////////////////////////////////////////////////////
HostEntry HostAddressToEntry(const boost::asio::ip::address &host_address)
{
	return((host_address.is_v6()) ?
		HostAddressToEntry(host_address.to_v6()) :
		HostAddressToEntry(host_address.to_v4()));
}
Exemplo n.º 30
0
 bool
 operator()(const boost::asio::ip::address& address)
 {
   return address.is_v6();
 }