Пример #1
0
            void resolve_no_block(
                implementation_type & impl, 
                NetName const & name, 
                error_code & ec)
            {
                Endpoint svc;
                svc_cache_.find(name, svc, ec);
                if (ec) {
                    impl->ec = ec;
                    impl->state = ResolveTask::finished;
                    return;
                }
                // 可能service只有一种
                impl->name = name;
                impl->name.protocol((NetName::ProtocolEnum)svc.protocol());
                impl->endpoints.clear();
                impl->ec.clear();
                bool found = host_cache_.find(impl->name, impl->endpoints);
                if (found) {
                    for (size_t i = 0; i < impl->endpoints.size(); ++i) {
                        impl->endpoints[i].port(svc.port());
                    }
                } else {
                    ec = boost::asio::error::would_block;
                }
#ifndef FRAMEWORK_NETWORK_WITH_SERVICE_CACHE
                impl->name.svc(format(svc.port()));
#endif
                impl->state = ResolveTask::waiting;
                tasks_.push_back(impl);
                if (tasks_.size() == 1 && tasks2_.empty()) {
                    sync_data_->cond.notify_one();
                }
            }
Пример #2
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;
}
Пример #3
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();

}
Пример #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
//------------------------------------------------------------------------------
//!
RCP<ConnectedSocketDevice>
Socket::connect( const Endpoint& ep )
{
#if BASE_SOCKET_USE_BSD || BASE_SOCKET_USE_WINSOCK
   state( STATE_CONNECTING );

   addrinfo* candidates = getAddrInfo( *this, ep.hostname().cstr(), ep.port() );
   if( candidates == NULL )
   {
      return NULL;
   }

   Family   family   = fromAPI_family  ( candidates->ai_family   );
   Type     type     = fromAPI_type    ( candidates->ai_socktype );
   Protocol protocol = fromAPI_protocol( candidates->ai_protocol );

   RCP<Socket>  socket = new Socket( family, type, protocol );

   int err = ::connect( socket->_impl->_sock, candidates->ai_addr, SocketSSizeT(candidates->ai_addrlen) );
   if( err != 0 )
   {
      printAPIError( "ERROR - Socket::connect() - connect failed" );
      freeaddrinfo( candidates );
      return NULL;
   }

   RCP<ConnectedSocketDevice>  device = new ConnectedSocketDevice( socket.ptr() );
   device->_remoteEndpoint = ep;

   freeaddrinfo( candidates );

   state( STATE_CONNECTED );

   return device;
#else
   return NULL;
#endif
}
Пример #6
0
bool DelegateService::deliver(const Endpoint& src, const Endpoint& dst, MemoryReference data) const {
    // Check from most to least specific

    PortMap const* pm = getPortMap(dst.spaceObject());
    if (pm != NULL) {
        PortMap::const_iterator it = pm->find(dst.port());

        if (it != pm->end())
        {
            DelegatePort* port = it->second;
            bool delivered = port->deliver(src, dst, data);
            if (delivered)
                return true;
        }
    }

    // And finally, the default handler
    if (mDefaultHandler != 0) {
        mDefaultHandler(src, dst, data);
        return true;
    }

    return false;
}
Пример #7
0
		void write_endpoint(Endpoint const& e, OutIt& out)
		{
			write_address(e.address(), out);
			write_uint16(e.port(), out);
		}
Пример #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());
}
Пример #9
0
ostream& operator<<(ostream& os, const Peer& p) {
    /// when NTP implemented, change to just nTime = GetAdjustedTime()
    int64_t nTime = (p._inbound ? GetAdjustedTime() : UnixTime::s());
    //    Endpoint remote = _socket.remote_endpoint();
    Endpoint local = p._socket.local_endpoint();
    Endpoint addrYou = (p._proxy ? Endpoint("0.0.0.0") : p.addr);
    Endpoint addrMe = (p._proxy ? Endpoint("0.0.0.0") : local);
    // hack to avoid serializing the time
    if (p._version)
        os << const_binary<int>(p._version);
    else // not initialized - we assume we are sending the version info
        os << const_binary<int>(p._chain.protocol_version());
    os << const_binary<uint64_t>(NODE_NETWORK) << const_binary<int64_t>(nTime);
    os << const_binary<uint64_t>(addrYou.getServices()) << const_binary<boost::asio::ip::address_v6::bytes_type>(addrYou.getIPv6()) << const_binary<unsigned short>(htons(addrYou.port()));
    os << const_binary<uint64_t>(addrMe.getServices()) << const_binary<boost::asio::ip::address_v6::bytes_type>(addrMe.getIPv6()) << const_binary<unsigned short>(htons(addrMe.port()));
    os << const_binary<uint64_t>(p._nonce) << const_varstr(p._sub_version);
    os << const_binary<int>(p._peerManager.getBestHeight());
    os << const_binary<bool>(true);
    return os;
}