Files
spark/src/server/sparkd/cmds/CMECertCmd.cpp
T
2020-05-06 11:54:32 -05:00

179 lines
5.2 KiB
C++

#include "CMECertCmd.h"
#include "lib/orderman/actors/ilink3/IL3Reactor.h"
#include "lib/orderman/actors/ilink3/ILinkGW.h"
#include "lib/orderman/actors/ilink3/IL3Dispatcher.h"
#include "lib/utils/TimeStamp.h"
#include "lib/utils/Dispatcher.h"
#include "lib/utils/SecureLogon.h"
#include "lib/os/os.h"
#include <string>
#include <iostream>
#include <fstream>
namespace bts {
namespace cmd {
using namespace orderman::ilink3;
void beginning_of_week_logon(ILinkGW& gw) {
// Over an iLink connection, submit a Negotiate message .
// Receive and process a Negotiate Response message
// Submit an Establish message to bind connection
// Receive and process a Establishment Acknowledgment message.
// Submit a sequence message
// Receive and process a sequence message
gw.connect();
}
struct SessionSaver {
Session current;
std::string path;
SessionSaver() :
current { Session::none() },
path { os::sparkdata_path("ilink3") + "session.txt" }
{}
void save() {
std::ofstream writer(path, std::ios::binary);
writer
<< current.uuid << " "
<< current.to_sequence << " "
<< current.from_sequence;
writer.close();
}
bool load() {
std::ifstream reader(path, std::ios::binary);
if (reader) {
reader >> current.uuid
>> current.to_sequence
>> current.from_sequence;
return true;
}
return false;
}
void session_changed(const Session& s) {
current = s;
save();
}
};
int CMECert::execute(const std::vector<std::string>&) {
// Typically a timestamp
SessionSaver ss;
ss.load();
Config config { Session::none(), "NGG", "004", false };
Reactor reactor;
auto& connection = reactor.register_connection({
// local dev
TCPEndpoint { "127.0.0.1", 64530 },
// General NRCERT not AutoCert+
// {"69.50.112.184", 64530},
// {"69.50.112.185", 64530},
// NGF (AutoCert)
// TCPEndpoint { "69.50.112.199", 64529 },
// TCPEndpoint { "69.50.12.205", 64529 },
// NGG (AutoCert)
TCPEndpoint { "69.50.112.199", 64530 },
TCPEndpoint { "69.50.12.205", 64530 }
});
ILinkGW gw(connection, ss.current, config);
gw.subscribe_session([&](auto& s) { ss.session_changed(s); });
beginning_of_week_logon(gw);
auto start = utils::TimeStamp::now();
// bool flag = true;
while(gw.state() != State::ERROR) {
reactor.do_work();
auto delta = utils::TimeStamp::now() - start;
// if (delta.seconds() > 36 && flag) {
// gw.send_heartbeat();
// gw.flush();
// flag = false;
// }
if (delta.seconds() > 128) {
gw.send_terminate();
gw.flush();
break;
}
}
std::cout << std::endl << "We're done here.";
// sleep(100);
return 0;
}
std::string CMECert::help() {
return "Tool to drive CME Cert";
}
//////////////////////////////////////////
// This code worked once...
//////////////////////////////////////////
// uint64_t determine_session() {
// // this is fairly complex
// // see session_name.h
// return utils::TimeStamp::now().to_uint64();
// }
// template <class Msg, size_t Data=0, size_t Sz=4+iLinkBinary::MessageHeader::encodedLength()+Msg::sbeBlockLength()>
// std::array<char,Sz+Data> mkbuffer() {
// static_assert(sizeof(FramingHeader) == 4);
// return std::array<char,Sz+Data> {};
// }
// template <class Msg, size_t Sz>
// Msg frame_message(std::array<char,Sz>& buffer) {
// static_assert(Sz >= 4+iLinkBinary::MessageHeader::encodedLength()+Msg::sbeBlockLength());
// static_assert(sizeof(FramingHeader) == 4);
// FramingHeader* fhead = reinterpret_cast<FramingHeader*>(&buffer[0]);
// fhead->message_size = buffer.size();
// fhead->encoding_type = 0xCAFE;
// Msg msg;
// msg.wrapAndApplyHeader((char*)&buffer, sizeof(FramingHeader), buffer.size());
// return msg;
// }
// void do_negotiate() {
// // add 2 bytes for empty 'credentials' (39008) length
// auto buffer = mkbuffer<iLinkBinary::Negotiate500,2>();
// auto msg = frame_message<iLinkBinary::Negotiate500>(buffer);
// // msg.customerFlow(); // constant
// // msg.hMACVersion(); // constant
// msg.uUID(1111);
// msg.requestTimestamp(9999);
// msg.putSession("NGG");
// // sets the length of the credential block to ushort 00
// msg.putCredentials("");
// // pad with 0 at the front *and* null at the back
// const std::string_view firm = "004";
// // this must use the std::string_view overload
// // the char* overload does not pad correctly
// msg.putFirm(firm);
// const auto ld = glbx::calculate_hmac(
// msg.getSessionAsString(),
// std::to_string(msg.requestTimestamp()),
// std::to_string(msg.uUID()),
// msg.getSessionAsString(),
// firm.data()
// );
// msg.putAccessKeyID(ld.access_key);
// msg.putHMACSignature(ld.hmac);
// // connection->send({ buffer.data(), buffer.size() });
// auto fd = fopen("/home/eric/old.bin", "wb");
// if (fd) {
// fwrite(buffer.data(), 1, buffer.size(), fd);
// fclose(fd);
// }
// }
//////////////////////////////////////////
// This code worked once
//////////////////////////////////////////
}}//namespace