]> WPIA git - cassiopeia.git/blob - src/io/record.cpp
upd: various logging updates
[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 ) {
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     logger::debug( "FINE: RECORD output: ", res );
36
37     bio->write( res.data(), res.size() );
38 }
39
40 int32_t fromHexDigit( char c ) {
41     int32_t res = -1;
42
43     if( c >= '0' && c <= '9' ) {
44         res = c - '0';
45     }
46
47     if( c >= 'A' && c <= 'F' ) {
48         res = c - 'A' + 10;
49     }
50
51     return res;
52 }
53
54 std::string parseCommand( RecordHeader& head, const std::string& input) {
55     logger::debug( "FINE: RECORD input: ", input );
56
57     int32_t dlen = ( input.size() - 2 ) / 2;
58     char checksum = 0;
59     bool error = false;
60
61     std::string str( std::max( dlen, RECORD_HEADER_SIZE ), 0 );
62
63     for( int i = 0; i < dlen; i++ ) {
64         int32_t digit;
65         int32_t accum;
66         digit = fromHexDigit( input[i * 2 + 1] );
67         error |= digit == -1;
68         accum = digit;
69         accum <<= 4;
70         digit = fromHexDigit( input[i * 2 + 2] );
71         error |= digit == -1;
72         accum += digit;
73         str[i] = accum;
74         checksum += str[i];
75     }
76
77     head.unpackFromString( str.substr( 0, RECORD_HEADER_SIZE ) );
78     uint32_t len = head.payloadLength;
79     uint32_t expectedTotalLength = ( RECORD_HEADER_SIZE + len + 1 /*checksum*/ ) * 2 + 2;
80     std::string data = str.substr( RECORD_HEADER_SIZE, str.size() - RECORD_HEADER_SIZE );
81
82     if( checksum != -1 || expectedTotalLength != input.size() || error || dlen < RECORD_HEADER_SIZE ) {
83         throw "Error, invalid checksum";
84     }
85
86     data.pop_back();
87
88     return data;
89 }
90
91 /*
92 int main( int argc, char* argv[] ) {
93     OpensslBIOWrapper *bio = new OpensslBIOWrapper(BIO_new_fd(0, 0));
94     std::string data = "halloPayload";
95     RecordHeader head;
96     head.command = 0x7;
97     head.flags = 1;
98     head.sessid = 13;
99     head.command_count = 0xA0B;
100     head.totalLength = 9;
101     sendCommand( head, data, std::shared_ptr<OpensslBIO>(bio) );
102     head.command = 0x8;
103
104     try {
105         std::string c = parseCommand( head, ":0700010D0000000B0A0900000000000C0068616C6C6F5061796C6F6164E6\n" );
106
107         std::cout << "res: " << std::endl;
108         std::cout << head.payloadLength << std::endl;
109         std::cout << c << std::endl;
110     } catch( char const* c ) {
111         std::cout << "err: " << c << std::endl;
112     }
113
114
115     return 0;
116 }
117 */