7 #define BUFFER_SIZE 8192
9 #define SLIP_ESCAPE_CHAR ( (char) 0xDB)
10 #define SLIP_PACKET ( (char) 0xC0)
12 char hexDigit( char c ) {
28 std::string toHex( const char* buf, int len ) {
29 std::string data = "000000";
31 for( int i = 0; i < len; i++ ) {
32 data.append( 1, ' ' );
33 data.append( 1, hexDigit( ( buf[i] >> 4 ) & 0xF ) );
34 data.append( 1, hexDigit( buf[i] & 0xF ) );
41 this->buffer = std::vector<char>( BUFFER_SIZE );
42 this->decodeTarget = 0;
48 void SlipBIO::setTarget( std::shared_ptr<OpensslBIO> target ) {
49 this->target = target;
52 SlipBIO::SlipBIO( std::shared_ptr<OpensslBIO> target ) {
53 this->target = target;
55 this->buffer = std::vector<char>( BUFFER_SIZE );
56 this->decodeTarget = 0;
63 SlipBIO::~SlipBIO() {}
65 int SlipBIO::write( const char* buf, int num ) {
67 std::cout << "Out: " << toHex( buf, num ) << std::endl;
72 for( int i = 0; i < num; i++ ) {
73 if( ( buf[i] == SLIP_PACKET ) || ( buf[i] == SLIP_ESCAPE_CHAR ) ) {
78 int totalLen = num + badOnes + 1; // 2
79 char* targetPtr = ( char* ) malloc( totalLen );
85 std::shared_ptr<char> t = std::shared_ptr<char>( targetPtr, free );
88 for( int i = 0; i < num; i++ ) {
89 if( buf[i] == SLIP_PACKET ) {
90 targetPtr[j++] = SLIP_ESCAPE_CHAR;
91 targetPtr[j++] = ( char )0xDC;
92 } else if( buf[i] == SLIP_ESCAPE_CHAR ) {
93 targetPtr[j++] = SLIP_ESCAPE_CHAR;
94 targetPtr[j++] = ( char )0xDD;
96 targetPtr[j++] = buf[i];
100 targetPtr[j++] = SLIP_PACKET;
106 int dlen = target->write( targetPtr + sent, std::min( 1024, j - sent ) );
109 throw "Error, target write failed";
110 } else if( dlen == 0 ) {
125 int SlipBIO::read( char* buf, int size ) {
126 // while we have no data to decode or unmasking does not yield a full package
127 while( !packageLeft && ( decodePos >= rawPos || !unmask() ) ) {
129 // we have no data, read more
130 if( buffer.size() - rawPos < 64 ) {
131 // not enough space... package is too big
136 int len = target->read( buffer.data() + rawPos, buffer.capacity() - rawPos );
149 int len = std::min( decodeTarget, ( unsigned int ) size );
150 // a package finished, return it
151 std::copy( buffer.data(), buffer.data() + len, buf );
152 // move the buffer contents back
153 std::copy( buffer.data() + len, buffer.data() + decodeTarget, buffer.data() );
156 if( decodeTarget == 0 ) {
161 std::cout << "in: " << toHex( buf, len ) << std::endl;
167 long SlipBIO::ctrl( int cmod, long arg1, void* arg2 ) {
172 if( cmod == BIO_CTRL_RESET ) {
173 char resetSequence[] = {SLIP_ESCAPE_CHAR, 0, SLIP_PACKET};
174 target->write( resetSequence, 3 );
178 std::cout << "resetting SLIP" << std::endl;
182 return target->ctrl( cmod, arg1, arg2 );
185 const char* SlipBIO::getName() {
189 bool SlipBIO::unmask() {
190 unsigned int j = decodeTarget;
192 for( unsigned int i = decodePos; i < rawPos; i++ ) {
193 if( buffer[i] == SLIP_ESCAPE_CHAR ) {
198 buffer[decodeTarget] = buffer[i - 1];
199 decodePos = decodeTarget;
200 rawPos = decodePos + 1;
201 return 0;// no packet
202 } else if( buffer[i] == ( char )0xdc ) {
203 buffer[j++] = SLIP_PACKET;
204 } else if( buffer[i] == ( char )0xdd ) {
205 buffer[j++] = SLIP_ESCAPE_CHAR;
206 } else if( buffer[i] == SLIP_PACKET ) {
216 } else if( buffer[i] == SLIP_PACKET ) {
220 // copy rest to bufferfer i to len
228 buffer[j++] = buffer[i];