138 lines
8.6 KiB
C++
138 lines
8.6 KiB
C++
#include "login_session.h"
|
|
#include <algorithm>
|
|
#include <stdexcept>
|
|
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<const std::uint8_t> 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<password_.size();++i) p[i]=0; password_.clear(); }
|
|
void LoginSession::fail(std::string reason) { failure_=std::move(reason); state_=State::Failed; clear_password(); output_.clear(); }
|
|
void LoginSession::begin_game() {
|
|
if(state_!=State::Authenticated) throw std::logic_error("game connection requires successful authentication");
|
|
game_=true; handshake_phase_entered_=false; encrypted_=false; sequence_=0; raw_.clear(); plain_.clear(); output_.clear(); tx_=rx_=locale_key(); state_=State::Handshake;
|
|
}
|
|
void LoginSession::reconnect_game() {
|
|
if(state_!=State::Selecting) throw std::logic_error("DirectEnter requires a selected account session");
|
|
handshake_phase_entered_=false;encrypted_=false; sequence_=0; raw_.clear(); plain_.clear(); output_.clear();
|
|
tx_=rx_=locale_key(); state_=State::Handshake;
|
|
roster_received_=select_phase_received_=game_login_sent_=false;
|
|
}
|
|
void LoginSession::emit(Bytes bytes,bool sequence) {
|
|
if(sequence) { bytes.push_back(sequences[sequence_]); sequence_=(sequence_+1)%std::size(sequences); }
|
|
output_.push_back(encrypted_?encrypt(bytes,tx_):std::move(bytes));
|
|
}
|
|
std::vector<Bytes> LoginSession::take_output() { auto out=std::move(output_); output_.clear(); return out; }
|
|
void LoginSession::feed(std::span<const std::uint8_t> input) {
|
|
if(state_==State::Failed) return;
|
|
if(input.size()>128*1024 || raw_.size()+plain_.size()+input.size()>128*1024) { 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()<n) break;
|
|
Bytes next(raw_.begin(),raw_.begin()+n); raw_.erase(raw_.begin(),raw_.begin()+n);
|
|
if(encrypted_) next=decrypt(next,rx_);
|
|
plain_.insert(plain_.end(),next.begin(),next.end());
|
|
while(state_!=State::Failed && process_packet()) {}
|
|
}
|
|
}
|
|
bool LoginSession::process_packet() {
|
|
if(plain_.empty() || state_==State::Loading) return false;
|
|
auto header=plain_[0]; std::size_t size=0;
|
|
switch(header) {
|
|
case 0:size=1;break; case 253:size=2;break; case 255:size=13;break; case 44:size=1;break;
|
|
case 254:size=7;break; // legacy UDP binding, not used by this TCP bootstrap
|
|
case 151:size=289;break; // Panama pack IV metadata
|
|
case 152: case 153:
|
|
if(plain_.size()<7) return false;
|
|
size=plain_[1]|(std::size_t(plain_[2])<<8);
|
|
if(size<7 || read32(plain_,3)!=size-7) { fail("invalid pack metadata packet length");return false; }
|
|
break;
|
|
case 150:size=6;break; case 7:size=10;break; case 90:size=2;break;
|
|
case 32:size=444;break; // 5 * (70-byte player + 4-byte guild id + 13-byte guild name) + 9
|
|
default:fail("unsupported login packet header "+std::to_string(header));return false;
|
|
}
|
|
if(plain_.size()<size) return false;
|
|
Bytes packet(plain_.begin(),plain_.begin()+size); plain_.erase(plain_.begin(),plain_.begin()+size);
|
|
if(header==0 || header==254) return true;
|
|
if(header==151 || header==152 || header==153) {
|
|
std::size_t stored=0; for(const auto& metadata:pack_metadata_) stored+=metadata.size();
|
|
if(stored+packet.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<std::int32_t>(read32(packet,o+50));c.y=static_cast<std::int32_t>(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() {
|
|
if(state_!=State::Loading) return {};
|
|
auto bytes=std::move(plain_);plain_.clear();return bytes;
|
|
}
|
|
void LoginSession::send_game_packet(wire::Bytes packet) {
|
|
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),true);
|
|
}
|
|
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;
|
|
}
|
|
}
|