void L2Socket::HandleAccept( tcp::socket& _socket, boost::system::error_code _ec ) { if (!acceptor->is_open() ) { LOG( "HandleAccept acceptor !is_open()!!!" ); return ; } if( _ec ) { LOG( "HandleAccept error: "+ _ec.message() ); Disconnect(_socket); return; } LOG( "HandleAccept New Connect ip: "+_socket.remote_endpoint().address().to_string() + " port: " + _socket.remote_endpoint().port()); //tcp::no_delay option(true); //_socket.set_option(option); shared_ptr<L2Actor> _act = L2_GET_UNUSED_OBJ(L2Actor); SERVER->PushActor( _act->OID ); _act->GetAgent()->SetConnect(true); sockAry.push_back(&_socket); agentOIDAry.push_back( _act->GetAgent()->OID ); if( bWebSocket ) OnHandshake(_socket,_act->GetAgent()->OID); else OnReceive( _socket,_act->GetAgent()->OID ); //THREAD->ThreadSleepFPS(); OnAccept(); }
void handle_accept(tcp::socket &client_socket, const boost::system::error_code& error) { if (!error &&client_socket.remote_endpoint().address().to_string()!="") { cout << client_socket.remote_endpoint().address().to_string() + " connected" << endl; session_ptr->start(); session_ptr.reset(new session(g_io_service)); } else { session_ptr->close_client(); session_ptr->close_server(); cout << __FUNCTION__ << "´íÎó£º" << error << endl; } start_accept(); }
Client(int id_given, tcp::socket* tcp_socket_given) { id = id_given; queue = ""; queue_active = false; connected = false; active = false; broken = false; expected_datagram = 0; min_fifo_size = max_fifo_size = 0; tcp_socket = tcp_socket_given; try { address = tcp_socket->remote_endpoint().address().to_string(); port = tcp_socket->remote_endpoint().port(); } catch (bs::system_error e) { broken = true; } }
void CMD_bye::run(tcp::socket& s) { boost::asio::ip::udp::endpoint p; p.address(s.remote_endpoint().address()); p.port(43201); vector<Observer>::iterator it = find(observers.begin(), observers.end(), Observer(p, id)); it->reg(false); it->set_id(-1); reply(s, "OK BYE"); }
void L2Socket::HandleResolve( tcp::socket& _socket, size_t _agentOID, const boost::system::error_code &_ec, tcp::resolver::iterator _it) { if (_ec) { LOG( "HandleResolve error: "+ _ec.message()); Disconnect(_socket); return; } LOG( "HandleResolve start ConnectHttp ip: "+_socket.remote_endpoint().address().to_string()+ " port: "+_socket.remote_endpoint().port()); async_connect(_socket,_it,bind(&L2Socket::HandleConnect,this,ref(_socket),_agentOID,_1,_2)); }
void L2Socket::Disconnect(tcp::socket& _socket,size_t _agentOID) { //return; if( _agentOID != -1 ) { UIntAry::iterator _it = find( agentOIDAry.begin(),agentOIDAry.end(),_agentOID ); if( _it != agentOIDAry.end() ) { shared_ptr<L2Agent> agt = L2_FIND_OBJ(L2Agent, _agentOID); agt->Disconnect(); agentOIDAry.erase( _it ); LOG( "Disconnect !!! ip: "+_socket.remote_endpoint().address().to_string()+" port: "+ _socket.remote_endpoint().port() ); } } else LOG( "Disconnect !!!" ); _socket.close(); SocketAry::iterator _it = find( sockAry.begin(),sockAry.end(),&_socket ); if( _it != sockAry.end() ) sockAry.erase( _it ); }
void session_thread(tcp::socket sock) { std::stringstream threadName; threadName << sock.remote_endpoint(); loguru::set_thread_name(threadName.str().c_str()); auto db = Storage::Mongo::MongoBackend{ "mongodb://localhost", "directory", "rootdn", "dc=mongodb,dc=com" }; bool noAuthentication = false; // Put this into its own scope so the YAML nodes get cleaned up. { auto check = config["noAuthentication"]; if (check && check.as<bool>() == true) noAuthentication = true; } bool userBound = false; std::string userBoundDN; for (;;) { std::vector<uint8_t> header(2);; size_t length; asio::error_code error; length = sock.read_some(asio::buffer(header), error); if (error) { if (error == asio::error::eof) break; else throw asio::system_error(error); } if (length != header.size()) { LOG_F(ERROR, "Client sent malformed BER header"); break; } std::vector<uint8_t> reqBuffer; reqBuffer.reserve(1024); if ((header[1] & 128) != 0) { header[1] -= 128; reqBuffer.resize(header[1]); length = sock.read_some(asio::buffer(reqBuffer), error); if (length != reqBuffer.size()) { LOG_F(ERROR, "Client sent mal-formed BER size header"); break; } auto decodedReqSize = Ber::decodeInteger(reqBuffer.cbegin(), reqBuffer.cend()); reqBuffer.resize(decodedReqSize, 0); } else { reqBuffer.resize(header[1], 0); } length = sock.read_some(asio::buffer(reqBuffer), error); if (length != reqBuffer.size()) { LOG_F(ERROR, "Client sent fewer bytes than expected"); break; } auto ber = Ber::Packet::decode(header[0], reqBuffer); auto messageId = static_cast<uint64_t>(ber.children[0]); auto messageType = Ldap::MessageTag { static_cast<Ldap::MessageTag>(ber.children[1].tag) }; Ldap::MessageTag errorResponseType; switch (messageType) { case Ldap::MessageTag::SearchRequest: errorResponseType = Ldap::MessageTag::SearchResDone; break; default: errorResponseType = static_cast<Ldap::MessageTag>(static_cast<uint8_t>(messageType) + 1); break; } try { if (messageType == Ldap::MessageTag::BindRequest) { Ldap::Bind::Request bindReq(ber.children[1]); if (bindReq.type == Ldap::Bind::Request::Type::Sasl) { // TODO SUPPORT SASL BINDS! throw Ldap::Exception(Ldap::ErrorCode::authMethodNotSupported); } bool passOkay = false; if (noAuthentication) { LOG_S(INFO) << "Authentication is disabled, sending bogus bind for " << bindReq.dn; passOkay = true; } else { LOG_S(INFO) << "Authenticating " << bindReq.dn; try { auto entry = db.findEntry(bindReq.dn); for (const auto& pass: entry->attributes.at("userPassword")) { passOkay = Password::checkPassword(bindReq.simple, pass); if (passOkay) break; } } catch (Ldap::Exception e) { LOG_S(ERROR) << "Error during authentication " << e.what(); if (e == Ldap::ErrorCode::noSuchObject) { throw Ldap::Exception(Ldap::ErrorCode::invalidCredentials); } throw; } } Ldap::ErrorCode respCode; if (passOkay) { respCode = Ldap::ErrorCode::success; userBound = true; userBoundDN = bindReq.dn; } else { respCode = Ldap::ErrorCode::invalidCredentials; userBound = false; userBoundDN = ""; } Ldap::Bind::Response bindResp(Ldap::buildLdapResult(respCode, bindReq.dn, "", Ldap::MessageTag::BindResponse)); sendResponse(sock, messageId, bindResp.response); } else if (messageType == Ldap::MessageTag::SearchRequest) { Ldap::Search::Request searchReq(ber.children[1]); auto cursor = db.findEntries(searchReq); for (const auto& entry: *cursor) { sendResponse(sock, messageId, Ldap::Search::generateResult(entry)); } sendResponse(sock, messageId, Ldap::buildLdapResult(Ldap::ErrorCode::success, "", "", Ldap::MessageTag::SearchResDone)); } else if (messageType == Ldap::MessageTag::AddRequest) { Ldap::Entry entry = Ldap::Add::parseRequest(ber.children[1]); db.saveEntry(entry, true); sendResponse(sock, messageId, Ldap::buildLdapResult(Ldap::ErrorCode::success, "", "", Ldap::MessageTag::AddResponse)); } else if (messageType == Ldap::MessageTag::ModifyRequest) { Ldap::Modify::Request req(ber.children[1]); auto entry = db.findEntry(req.dn); if (entry == nullptr) { throw Ldap::Exception(Ldap::ErrorCode::noSuchObject); } for (const auto& mod: req.mods) { using ModType = Ldap::Modify::Modification::Type; switch(mod.type) { case ModType::Add: for (const auto& v: mod.values) { entry->appendValue(mod.name, v); } break; case ModType::Delete: if (mod.values.size() == 0) { if (entry->attributes.erase(mod.name) == 0) { throw Ldap::Exception(Ldap::ErrorCode::noSuchAttribute); } } else { try { auto curVals = entry->attributes.at(mod.name); std::set<std::string> finalVals(curVals.begin(), curVals.end()); for (const auto& v: mod.values) { if (finalVals.erase(v) == 0) { throw Ldap::Exception(Ldap::ErrorCode::noSuchAttribute); } } curVals.clear(); std::copy(finalVals.begin(), finalVals.end(), std::back_inserter(curVals)); } catch(std::out_of_range) { throw Ldap::Exception(Ldap::ErrorCode::noSuchAttribute); } } break; case ModType::Replace: if (mod.values.size() == 0) { entry->attributes.erase(mod.name); } else { entry->attributes[mod.name] = mod.values; } break; } } db.saveEntry(*entry, false); sendResponse(sock, messageId, Ldap::buildLdapResult(Ldap::ErrorCode::success, "", "", Ldap::MessageTag::ModifyResponse)); } else if (messageType == Ldap::MessageTag::DelRequest) { std::string dn = Ldap::Delete::parseRequest(ber.children[1]); db.deleteEntry(dn); sendResponse(sock, messageId, Ldap::buildLdapResult(Ldap::ErrorCode::success, "", "", Ldap::MessageTag::DelResponse)); } } catch (const Ldap::Exception& e) { auto resPacket = Ldap::buildLdapResult(e, "", e.what(), errorResponseType); sendResponse(sock, messageId, resPacket); break; } catch (const std::exception& e) { auto resPacket = Ldap::buildLdapResult(Ldap::ErrorCode::other, "", e.what(), errorResponseType); sendResponse(sock, messageId, resPacket); break; } catch (...) { auto resPacket = Ldap::buildLdapResult(Ldap::ErrorCode::other, "", "Unknown error occurred", errorResponseType); sendResponse(sock, messageId, resPacket); break; } } }