#include "login_session.h" #include #include namespace ava2 { using namespace wire; namespace { constexpr std::uint8_t sequences[]={ #include "sequence_table.inc" }; void text(Bytes& out,const std::string& value,std::size_t max) { auto start=out.size(); out.resize(start+max+1,0); std::copy(value.begin(),value.end(),out.begin()+start); } std::string string_at(std::span b,std::size_t offset,std::size_t width) { auto end=std::find(b.begin()+offset,b.begin()+offset+width,0); return {b.begin()+offset,end}; } } LoginSession::LoginSession(std::string account,std::string password,Key client) :account_(std::move(account)),password_(std::move(password)),client_key_(client),receive_key_(derive_receive_key(client)),tx_(locale_key()),rx_(locale_key()) { if(account_.empty() || account_.size()>30 || password_.empty() || password_.size()>16 || account_.find('\0')!=std::string::npos || password_.find('\0')!=std::string::npos) throw std::invalid_argument("AVA2 account/password must fit 30/16-byte fields without NUL"); } LoginSession::~LoginSession() { clear_password(); } void LoginSession::clear_password() { volatile char* p=password_.empty()?nullptr:password_.data(); for(std::size_t i=0;i LoginSession::take_output() { auto out=std::move(output_); output_.clear(); return out; } void LoginSession::feed(std::span input) { if(state_==State::Failed) return; if(input.size()>receive_space()) { fail("receive buffer limit exceeded"); return; } raw_.insert(raw_.end(),input.begin(),input.end()); while(!raw_.empty() && state_!=State::Failed) { std::size_t n=encrypted_?8:1; if(raw_.size()128*1024) { fail("pack metadata limit exceeded");return false; } // Retained for a future encrypted-pack backend; decoded assets require no IV application. pack_metadata_.push_back(std::move(packet));return true; } if(header==255) { auto time=read32(packet,5),delta=read32(packet,9); Bytes reply{255};append32(reply,read32(packet,1));append32(reply,time+delta+delta);append32(reply,0);emit(std::move(reply),false); } else if(header==44) { emit({254},encrypted_); } else if(header==7) { fail(string_at(packet,1,9)); } else if(header==253) { auto phase=packet[1]; if(phase==0) { fail("server closed connection"); } else if(phase==1 && state_==State::Handshake) { handshake_phase_entered_=true; } else if(phase==10 && !game_ && state_==State::Handshake) { encrypted_=true; state_=State::Authenticating; Bytes login{111};text(login,account_,30);text(login,password_,16); for(auto v:client_key_) append32(login,v); clear_password();emit(std::move(login),true); } else if(phase==2 && game_ && state_==State::Handshake) { encrypted_=true; state_=State::LoggingIn; game_login_sent_=true; Bytes login{109};text(login,account_,30);append32(login,login_key_);for(auto v:client_key_) append32(login,v); emit(std::move(login),true);tx_=client_key_;rx_=receive_key_; } else if(phase==3 && game_ && state_==State::LoggingIn) { select_phase_received_=true; if(roster_received_) state_=State::Selecting; } else if(phase==4 && game_ && state_==State::Entering) { state_=State::Loading; } else { fail("unexpected login phase "+std::to_string(phase)); } } else if(header==150) { if(game_ || state_!=State::Authenticating) fail("authentication result in wrong state"); else if(!packet[5] || read32(packet,1)==0) fail("BESAMEKEY"); else { login_key_=read32(packet,1);state_=State::Authenticated; } } else if(header==32) { if(!game_ || state_!=State::LoggingIn) { fail("character roster in wrong state"); return false; } characters_.clear(); for(std::size_t i=0;i<5;++i) { std::size_t o=1+i*70; Character c; c.id=read32(packet,o);c.name=string_at(packet,o+4,25);c.job=packet[o+29];c.level=packet[o+30]; c.play_minutes=read32(packet,o+31);c.strength=packet[o+35];c.health=packet[o+36];c.dexterity=packet[o+37];c.intelligence=packet[o+38]; c.main_part=packet[o+39]|(std::uint16_t(packet[o+40])<<8);c.change_name=packet[o+41]; c.hair_part=packet[o+42]|(std::uint16_t(packet[o+43])<<8);c.sash_part=packet[o+44]|(std::uint16_t(packet[o+45])<<8); c.guild_id=read32(packet,351+i*4);c.guild_name=string_at(packet,371+i*13,13); for(std::size_t j=0;j<4;++j) c.sungma[j]=packet[o+66+j]; c.x=static_cast(read32(packet,o+50));c.y=static_cast(read32(packet,o+54)); c.address=read32(packet,o+58);c.port=packet[o+62]|(std::uint16_t(packet[o+63])<<8);c.skill_group=packet[o+64];c.conqueror_level=packet[o+65];characters_.push_back(std::move(c)); } roster_received_=true; if(select_phase_received_) state_=State::Selecting; } else if(header==90) { if(!game_ || (state_!=State::LoggingIn && state_!=State::Selecting)) fail("empire packet in wrong state"); else empire_=packet[1]; } return true; } wire::Bytes LoginSession::take_game_input(std::size_t max) { if(state_!=State::Loading || max==0) return {}; if(plain_.size()<=max) { auto bytes=std::move(plain_);plain_.clear();return bytes; } Bytes bytes(plain_.begin(),plain_.begin()+max);plain_.erase(plain_.begin(),plain_.begin()+max);return bytes; } void LoginSession::send_game_packet(wire::Bytes packet,bool sequence) { if(state_!=State::Loading) throw std::logic_error("game packet before loading handoff"); if(packet.empty() || packet.size()>65535) throw std::invalid_argument("invalid game packet size"); emit(std::move(packet),sequence); } void LoginSession::select(std::uint8_t slot) { if(state_!=State::Selecting) throw std::logic_error("selection requires character roster and select phase"); if(slot>=characters_.size() || characters_[slot].id==0) throw std::invalid_argument("cannot select an empty character slot"); emit(select_character(slot),true);state_=State::Entering; } }