Пример #1
0
bool operator<(const Endpoint& a, const Endpoint& b)
{
    int ret = memcmp(a._ipv6, b._ipv6, sizeof(a._ipv6));
    if (ret < 0)
        return true;
    else if (ret == 0)
    {
        if (ntohl(a.address().to_v4().to_ulong()) < ntohl(b.address().to_v4().to_ulong()))
            return true;
        else if (a.address() == b.address())
            return ntohs(a.port()) < ntohs(b.port());
    }
    return false;
}
Пример #2
0
void SaslConnection::close()
{
    Endpoint client = peer();
    Connection::close();

    if ( !u || logged ||
         !client.valid() || client.protocol() == Endpoint::Unix )
        return;

    logged = true;

    Query * q = new Query(
        "insert into connections "
        "(username,address,port,mechanism,authfailures,"
        "syntaxerrors,started_at,ended_at,userid) "
        "values ($1,$2,$3,$4,$5,$6,"
        "$7::interval + 'epoch'::timestamptz,"
        "$8::interval + 'epoch'::timestamptz,$9)", 0
    );

    q->bind( 1, u->login() );
    q->bind( 2, client.address() );
    q->bind( 3, client.port() );
    q->bind( 4, m );
    q->bind( 5, af );
    q->bind( 6, sf );
    q->bind( 7, s );
    q->bind( 8, (uint)time( 0 ) );
    q->bind( 9, u->id() );
    q->execute();

}
Пример #3
0
    int bind(descriptor const& desc, Endpoint const& endpoint)
    {
        error_code ec;

        system::call(::bind, desc.c_descriptor(), 
                     static_cast<sockaddr const*>(endpoint.address().first), 
                     endpoint.address().second,
                     ec,
                     -1
                    );
        if(ec)
        {
            report_error re;
            return re("socket::bind", ec);
        }

        return error_code::success;
    }
Пример #4
0
int Connection::listen( const Endpoint &e, bool silent )
{
    if ( !e.valid() )
        return -1;

    if ( !valid() ) {
        init( socket( e.protocol() ) );
        if ( !valid() )
            return -1;
    }

    int i = 1;
    ::setsockopt( d->fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof (int) );

    if ( e.protocol() == Endpoint::Unix )
        unlink( File::chrooted( e.address() ).cstr() );

    int retcode = ::bind( d->fd, e.sockaddr(), e.sockaddrSize() );
    if ( retcode < 0 ) {
        if ( errno == EADDRINUSE ) {
            if ( !silent )
                log( "Cannot listen to " +
                     e.address() + " port " + fn( e.port() ) +
                     " because another process is occupying it", Log::Error );
            return -1;
        }
        if ( !silent )
            log( "bind( " + fn( d->fd ) + ", " +
                 e.address() + " port " + fn( e.port() ) +
                 " ) returned errno " + fn( errno ), Log::Debug );
        return -1;
    }
    if ( ::listen( d->fd, 64 ) < 0 ) {
        if ( !silent )
            log( "listen( " + fn( d->fd ) + ", 64 ) for address " +
                 e.address() + " port " + fn( e.port() ) +
                 " ) returned errno " + fn( errno ), Log::Debug );
        return -1;
    }

    setState( Listening );
    d->self = e;
    return 1;
}
Пример #5
0
std::size_t send_to(descriptor const& desc, mutable_buffer_container buffer, Endpoint const& endpoint)
{
    error_code ec;
    std::size_t ret;

    ret = system::call(::sendto, desc.c_descriptor(),
                       buffer_cast<char const*>(buffer),
                       buffer_size(buffer),
                       0,
                       static_cast<sockaddr const*>(endpoint.address().first), 
                       endpoint.address().second,
                       ec,
                       -1
                      );
    if(ec)
    {
        report_error re;
        return re("socket::send_to", ec);
    }

    return ret;
}
Пример #6
0
		void write_endpoint(Endpoint const& e, OutIt& out)
		{
			write_address(e.address(), out);
			write_uint16(e.port(), out);
		}
Пример #7
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")));
    }
Пример #8
0
bool operator==(const Endpoint& a, const Endpoint& b)
{
    return (memcmp(a._ipv6, b._ipv6, sizeof(a._ipv6)) == 0 &&
            a.address()   == b.address() &&
            a.port() == b.port());
}