Example #1
0
void
parseAddresses (OutputSequence& out, InputIterator first, InputIterator last,
    beast::Journal::Stream stream = beast::Journal::Stream ())
{
    while (first != last)
    {
        auto const str (*first);
        ++first;
        {
            beast::IP::Endpoint const addr (
                beast::IP::Endpoint::from_string (str));
            if (! is_unspecified (addr))
            {
                out.push_back (addr);
                continue;
            }
        }
        {
            beast::IP::Endpoint const addr (
                beast::IP::Endpoint::from_string_altform (str));
            if (! is_unspecified (addr))
            {
                out.push_back (addr);
                continue;
            }
        }
        if (stream) stream <<
            "Config: \"" << str << "\" is not a valid IP address.";
    }
}
    void testEndpoint ()
    {
        testcase ("Endpoint");

        {
            std::pair <Endpoint, bool> result (
                Endpoint::from_string_checked ("1.2.3.4"));
            expect (result.second);
            if (expect (result.first.address().is_v4 ()))
            {
                expect (result.first.address().to_v4() ==
                    AddressV4 (1, 2, 3, 4));
                expect (result.first.port() == 0);
                expect (to_string (result.first) == "1.2.3.4");
            }
        }

        {
            std::pair <Endpoint, bool> result (
                Endpoint::from_string_checked ("1.2.3.4:5"));
            expect (result.second);
            if (expect (result.first.address().is_v4 ()))
            {
                expect (result.first.address().to_v4() ==
                    AddressV4 (1, 2, 3, 4));
                expect (result.first.port() == 5);
                expect (to_string (result.first) == "1.2.3.4:5");
            }
        }

        Endpoint ep;

        ep = Endpoint (AddressV4 (127,0,0,1), 80);
        expect (! is_unspecified (ep));
        expect (! is_public (ep));
        expect (  is_private (ep));
        expect (! is_multicast (ep));
        expect (  is_loopback (ep));
        expect (to_string (ep) == "127.0.0.1:80");

        ep = Endpoint (AddressV4 (10,0,0,1));
        expect (AddressV4::get_class (ep.to_v4()) == 'A');
        expect (! is_unspecified (ep));
        expect (! is_public (ep));
        expect (  is_private (ep));
        expect (! is_multicast (ep));
        expect (! is_loopback (ep));
        expect (to_string (ep) == "10.0.0.1");

        ep = Endpoint (AddressV4 (166,78,151,147));
        expect (! is_unspecified (ep));
        expect (  is_public (ep));
        expect (! is_private (ep));
        expect (! is_multicast (ep));
        expect (! is_loopback (ep));
        expect (to_string (ep) == "166.78.151.147");
    }
 void fetch (Results& results, beast::Journal journal)
 {
     results.addresses.resize (0);
     results.addresses.reserve (m_strings.size());
     for (int i = 0; i < m_strings.size (); ++i)
     {
         beast::IP::Endpoint ep (beast::IP::Endpoint::from_string (m_strings [i]));
         if (is_unspecified (ep))
             ep = beast::IP::Endpoint::from_string_altform (m_strings [i]);
         if (! is_unspecified (ep))
             results.addresses.push_back (ep);
     }
 }
Example #4
0
    void testAddressV4 ()
    {
        testcase ("AddressV4");

        expect (AddressV4().value == 0);
        expect (is_unspecified (AddressV4()));
        expect (AddressV4(0x01020304).value == 0x01020304);
        expect (AddressV4(1, 2, 3, 4).value == 0x01020304);

        unexpected (is_unspecified (AddressV4(1, 2, 3, 4)));

        AddressV4 const v1 (1);
        expect (AddressV4(v1).value == 1);

        {
            AddressV4 v;
            v = v1;
            expect (v.value == v1.value);
        }

        {
            AddressV4 v;
            v [0] = 1;
            v [1] = 2;
            v [2] = 3;
            v [3] = 4;
            expect (v.value == 0x01020304);
        }

        expect (to_string (AddressV4(0x01020304)) == "1.2.3.4");

        shouldParseV4 ("1.2.3.4", 0x01020304);
        shouldParseV4 ("255.255.255.255", 0xffffffff);
        shouldParseV4 ("0.0.0.0", 0);

        failParseV4 (".");
        failParseV4 ("..");
        failParseV4 ("...");
        failParseV4 ("....");
        failParseV4 ("1");
        failParseV4 ("1.");
        failParseV4 ("1.2");
        failParseV4 ("1.2.");
        failParseV4 ("1.2.3");
        failParseV4 ("1.2.3.");
        failParseV4 ("256.0.0.0");
        failParseV4 ("-1.2.3.4");
    }
    void setup (beast::Journal journal)
    {
        if (! getConfig ().getRpcIP().empty () &&
              getConfig ().getRpcPort() != 0)
        {
            beast::IP::Endpoint ep (beast::IP::Endpoint::from_string (getConfig().getRpcIP()));
            if (! is_unspecified (ep))
            {
                HTTP::Port port;
                port.addr = ep.at_port(0);
                if (getConfig ().getRpcPort() != 0)
                    port.port = getConfig ().getRpcPort();
                else
                    port.port = ep.port();
                port.context = m_context.get ();

                HTTP::Ports ports;
                ports.push_back (port);
                m_server.setPorts (ports);
            }
        }
        else
        {
            journal.info << "RPC interface: disabled";
        }
    }
Example #6
0
// {
//   ip: <string>,
//   port: <number>
// }
// XXX Might allow domain for manual connections.
Json::Value doConnect (RPC::Context& context)
{
    auto lock = beast::make_lock(getApp().getMasterMutex());
    if (getConfig ().RUN_STANDALONE)
        return "cannot connect in standalone mode";

    if (!context.params.isMember (jss::ip))
        return RPC::missing_field_error (jss::ip);

    if (context.params.isMember (jss::port) &&
        !context.params[jss::port].isConvertibleTo (Json::intValue))
    {
        return rpcError (rpcINVALID_PARAMS);
    }

    int iPort;

    if(context.params.isMember (jss::port))
        iPort = context.params[jss::port].asInt ();
    else
        iPort = 6561;

    auto ip = beast::IP::Endpoint::from_string(
        context.params[jss::ip].asString ());

    if (! is_unspecified (ip))
        getApp().overlay ().connect (ip.at_port(iPort));

    return RPC::makeObjectValue ("connecting");
}
Example #7
0
// VFALCO NOTE This is a hack to support legacy data format
//
Endpoint Endpoint::from_string_altform (std::string const& s)
{
    // Accept the regular form if it parses
    {
        Endpoint ep (Endpoint::from_string (s));
        if (! is_unspecified (ep))
            return ep;
    }

    // Now try the alt form
    std::stringstream is (s);

    AddressV4 v4;
    is >> v4;
    if (! is.fail())
    {
        Endpoint ep (v4);

        if (is.rdbuf()->in_avail()>0)
        {
            if (! IP::detail::expect (is, ' '))
                return Endpoint();

            while (is.rdbuf()->in_avail()>0)
            {
                char c;
                is.get(c);
                if (c != ' ')
                {
                    is.unget();
                    break;
                }
            }

            Port port;
            is >> port;
            if (is.fail())
                return Endpoint();

            return ep.at_port (port);
        }
        else
        {
            // Just an address with no port
            return ep;
Example #8
0
bool address::parse(const char *str, bool multicast, bool addport) {
	char tmp[128];
	strncpy(tmp, str, sizeof(tmp));

	char *port = strchr(tmp, '/');
	if (port) {
		*port = 0;
		port ++;
	} else if (addport) {
		port = (char *)defaultPort;
	}

	int cres;
	addrinfo hint, *rres, *res;
	memset(&hint, 0, sizeof(hint));

	hint.ai_family = forceFamily;
	hint.ai_socktype = SOCK_DGRAM;

	if ((cres = getaddrinfo(*tmp ? tmp : 0, port, &hint, &rres)) != 0) {
		info("getaddrinfo failed: %s", gai_strerror(cres));
		return false;
	}

	for (res = rres; res; res = res->ai_next) {
		set(res->ai_addr);
		if (multicast) {
			if (is_multicast())
				break;
		} else if (!is_unspecified())
			break;
	}

	freeaddrinfo(rres);

	if (!res) {
		info("Failed to resolve %s", tmp);
		return false;
	}

	return true;
}
Example #9
0
BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV6, UdpFactoryMcastFixture)
{
#ifdef __linux__
  // need superuser privileges to create multicast faces on Linux
  SKIP_IF_NOT_SUPERUSER();
#endif // __linux__
  SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1);

  auto multicastFace1  = createMulticastFace("::1", "ff02::114", 20070);
  auto multicastFace1a = createMulticastFace("::1", "ff02::114", 20070);
  auto multicastFace2  = createMulticastFace("::1", "ff02::114", 20030);
  BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
  BOOST_CHECK_NE(multicastFace1, multicastFace2);

  auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V6);
  if (!address.is_unspecified()) {
    auto multicastFace3  = createMulticastFace(address.to_string(), "ff02::114", 20070);
    BOOST_CHECK_NE(multicastFace1, multicastFace3);
    BOOST_CHECK_NE(multicastFace2, multicastFace3);
  }

  // create with a local endpoint already used by a channel
  auto channel = createChannel("::1", 20071);
  BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::114", 20071), UdpFactory::Error,
                        [] (const UdpFactory::Error& e) {
                          return strcmp(e.what(),
                                        "Cannot create UDP multicast face on [::1]:20071, "
                                        "endpoint already allocated for a UDP channel") == 0;
                        });

  // create with a local endpoint already used by a multicast face on a different multicast group
  BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::42", 20070), UdpFactory::Error,
                        [] (const UdpFactory::Error& e) {
                          return strcmp(e.what(),
                                        "Cannot create UDP multicast face on [::1]:20070, "
                                        "endpoint already allocated for a different UDP multicast face") == 0;
                        });
}
Example #10
0
    void testEndpoint ()
    {
        testcase ("Endpoint");

        {
            std::pair <Endpoint, bool> result (
                Endpoint::from_string_checked ("1.2.3.4"));
            expect (result.second);
            if (expect (result.first.address().is_v4 ()))
            {
                expect (result.first.address().to_v4() ==
                    AddressV4 (1, 2, 3, 4));
                expect (result.first.port() == 0);
                expect (to_string (result.first) == "1.2.3.4");
            }
        }

        {
            std::pair <Endpoint, bool> result (
                Endpoint::from_string_checked ("1.2.3.4:5"));
            expect (result.second);
            if (expect (result.first.address().is_v4 ()))
            {
                expect (result.first.address().to_v4() ==
                    AddressV4 (1, 2, 3, 4));
                expect (result.first.port() == 5);
                expect (to_string (result.first) == "1.2.3.4:5");
            }
        }

        Endpoint ep;

        ep = Endpoint (AddressV4 (127,0,0,1), 80);
        expect (! is_unspecified (ep));
        expect (! is_public (ep));
        expect (  is_private (ep));
        expect (! is_multicast (ep));
        expect (  is_loopback (ep));
        expect (to_string (ep) == "127.0.0.1:80");

        ep = Endpoint (AddressV4 (10,0,0,1));
        expect (AddressV4::get_class (ep.to_v4()) == 'A');
        expect (! is_unspecified (ep));
        expect (! is_public (ep));
        expect (  is_private (ep));
        expect (! is_multicast (ep));
        expect (! is_loopback (ep));
        expect (to_string (ep) == "10.0.0.1");

        ep = Endpoint (AddressV4 (166,78,151,147));
        expect (! is_unspecified (ep));
        expect (  is_public (ep));
        expect (! is_private (ep));
        expect (! is_multicast (ep));
        expect (! is_loopback (ep));
        expect (to_string (ep) == "166.78.151.147");

        {
            ep = Endpoint::from_string ("192.0.2.112");
            expect (! is_unspecified (ep));
            expect (ep == Endpoint::from_string_altform ("192.0.2.112"));

            auto const ep1 = Endpoint::from_string ("192.0.2.112:2016");
            expect (! is_unspecified (ep1));
            expect (ep.address() == ep1.address());
            expect (ep1.port() == 2016);

            auto const ep2 =
                Endpoint::from_string_altform ("192.0.2.112:2016");
            expect (! is_unspecified (ep2));
            expect (ep.address() == ep2.address());
            expect (ep2.port() == 2016);
            expect (ep1 == ep2);

            auto const ep3 =
                Endpoint::from_string_altform ("192.0.2.112 2016");
            expect (! is_unspecified (ep3));
            expect (ep.address() == ep3.address());
            expect (ep3.port() == 2016);
            expect (ep2 == ep3);

            auto const ep4 =
                Endpoint::from_string_altform ("192.0.2.112     2016");
            expect (! is_unspecified (ep4));
            expect (ep.address() == ep4.address());
            expect (ep4.port() == 2016);
            expect (ep3 == ep4);

            expect (to_string(ep1) == to_string(ep2));
            expect (to_string(ep1) == to_string(ep3));
            expect (to_string(ep1) == to_string(ep4));
        }

        // Failures:
        expect (is_unspecified (
            Endpoint::from_string ("192.0.2.112:port")));
        expect (is_unspecified (
            Endpoint::from_string_altform ("192.0.2.112:port")));
        expect (is_unspecified (
            Endpoint::from_string_altform ("192.0.2.112 port")));

        expect (is_unspecified (
            Endpoint::from_string ("ip:port")));
        expect (is_unspecified (
            Endpoint::from_string_altform ("ip:port")));
        expect (is_unspecified (
            Endpoint::from_string_altform ("ip port")));

        expect (is_unspecified (
            Endpoint::from_string("")));
        expect (is_unspecified (
            Endpoint::from_string_altform("")));

        expect (is_unspecified (
            Endpoint::from_string("255")));
        expect (is_unspecified (
            Endpoint::from_string_altform("255")));

        expect (is_unspecified (
            Endpoint::from_string("512")));
        expect (is_unspecified (
            Endpoint::from_string_altform("512")));

        expect (is_unspecified (
            Endpoint::from_string("1.2.3.256")));
        expect (is_unspecified (
            Endpoint::from_string_altform("1.2.3.256")));

        expect (is_unspecified (
            Endpoint::from_string("1.2.3:80")));
        expect (is_unspecified (
            Endpoint::from_string_altform("1.2.3:80")));
        expect (is_unspecified (
            Endpoint::from_string_altform("1.2.3 80")));

        expect (is_unspecified (
            Endpoint::from_string("1.2.3.4:65536")));
        expect (is_unspecified (
            Endpoint::from_string_altform("1.2.3:65536")));
        expect (is_unspecified (
            Endpoint::from_string_altform("1.2.3 65536")));

        expect (is_unspecified (
            Endpoint::from_string("1.2.3.4:89119")));
        expect (is_unspecified (
            Endpoint::from_string_altform("1.2.3:89119")));
        expect (is_unspecified (
            Endpoint::from_string_altform("1.2.3 89119")));
    }