]> WPIA git - cassiopeia.git/blob - src/io/record.cpp
c8fda58c98dd942c6e7d41b11efb97cf13c28b26
[cassiopeia.git] / src / io / record.cpp
1 #include "record.h"
2
3 #include <iomanip>
4 #include <iostream>
5 #include <memory>
6 #include <sstream>
7
8 #include "io/bios.h"
9 #include "io/opensslBIO.h"
10 #include "log/logger.hpp"
11
12 std::string toHexAndChecksum( const std::string& src ) {
13     char checksum = 0;
14     std::stringstream ss;
15     ss << ':' << std::hex << std::setfill( '0' ) << std::uppercase;
16
17     for( auto c : src ) {
18         ss << std::setw( 2 ) << ( ( ( uint32_t ) c ) & 0xFF );
19         checksum += c;
20     }
21
22     ss << std::setw( 2 ) << ( ( ( uint32_t )( ~checksum ) ) & 0xFF );
23     ss << '\n';
24     return ss.str();
25 }
26
27 void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptr<OpensslBIO> bio, std::shared_ptr<std::ostream> log ) {
28     head.payloadLength = data.size();
29     std::string s;
30     s += head.packToString();
31     s += data;
32
33     std::string res = toHexAndChecksum( s );
34
35     if( log ) {
36         logger::debug( "FINE: RECORD output: ", res );
37     }
38
39     bio->write( res.data(), res.size() );
40 }
41
42 int32_t fromHexDigit( char c ) {
43     int32_t res = -1;
44
45     if( c >= '0' && c <= '9' ) {
46         res = c - '0';
47     }
48
49     if( c >= 'A' && c <= 'F' ) {
50         res = c - 'A' + 10;
51     }
52
53     return res;
54 }
55
56 std::string parseCommand( RecordHeader& head, const std::string& input, std::shared_ptr<std::ostream> log ) {
57     if( log ) {
58         logger::debug( "FINE: RECORD input: ", input );
59     }
60
61     int32_t dlen = ( input.size() - 2 ) / 2;
62     char checksum = 0;
63     bool error = false;
64
65     std::string str( std::max( dlen, RECORD_HEADER_SIZE ), 0 );
66     str.append( dlen, '\0' );
67
68     for( int i = 0; i < dlen; i++ ) {
69         int32_t digit;
70         int32_t accum;
71         digit = fromHexDigit( input[i * 2 + 1] );
72         error |= digit == -1;
73         accum = digit;
74         accum <<= 4;
75         digit = fromHexDigit( input[i * 2 + 2] );
76         error |= digit == -1;
77         accum += digit;
78         str[i] = accum;
79         checksum += str[i];
80     }
81
82     head.unpackFromString( str.substr( 0, RECORD_HEADER_SIZE ) );
83     uint32_t len = head.payloadLength;
84     uint32_t expectedTotalLength = ( RECORD_HEADER_SIZE + len + 1 /*checksum*/ ) * 2 + 2;
85     std::string data = str.substr( RECORD_HEADER_SIZE, str.size() - RECORD_HEADER_SIZE );
86
87     if( checksum != -1 || expectedTotalLength != input.size() || error || dlen < RECORD_HEADER_SIZE ) {
88         throw "Error, invalid checksum";
89     }
90
91     data.pop_back();
92
93     return data;
94 }
95
96 /*
97 int main( int argc, char* argv[] ) {
98     OpensslBIOWrapper *bio = new OpensslBIOWrapper(BIO_new_fd(0, 0));
99     std::string data = "halloPayload";
100     RecordHeader head;
101     head.command = 0x7;
102     head.flags = 1;
103     head.sessid = 13;
104     head.command_count = 0xA0B;
105     head.totalLength = 9;
106     sendCommand( head, data, std::shared_ptr<OpensslBIO>(bio) );
107     head.command = 0x8;
108
109     try {
110         std::string c = parseCommand( head, ":0700010D0000000B0A0900000000000C0068616C6C6F5061796C6F6164E6\n" );
111
112         std::cout << "res: " << std::endl;
113         std::cout << head.payloadLength << std::endl;
114         std::cout << c << std::endl;
115     } catch( char const* c ) {
116         std::cout << "err: " << c << std::endl;
117     }
118
119
120     return 0;
121 }
122 */