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