#include "slipBio.h"
+#include <unistd.h>
+
#include <iostream>
-#include <unistd.h>
+#define BUFFER_SIZE 8192
+
+#define SLIP_ESCAPE_CHAR ( (char) 0xDB)
+#define SLIP_PACKET ( (char) 0xC0)
char hexDigit( char c ) {
if( c < 0 ) {
}
std::string toHex( const char* buf, int len ) {
- char* c = ( char* ) malloc( len * 2 );
-
- if( !c ) {
- return "<malloc fail>";
- }
-
- std::shared_ptr<char> mem = std::shared_ptr<char>( c, free );
+ std::string data = "000000";
for( int i = 0; i < len; i++ ) {
- c[i * 2] = hexDigit( ( buf[i] >> 4 ) & 0xF );
- c[i * 2 + 1] = hexDigit( buf[i] & 0xF );
+ data.append( 1, ' ' );
+ data.append( 1, hexDigit( ( buf[i] >> 4 ) & 0xF ) );
+ data.append( 1, hexDigit( buf[i] & 0xF ) );
}
- return std::string( mem.get(), len * 2 );
+ return data;
}
SlipBIO::SlipBIO() {
- this->buffer = std::vector<char>( 4096 );
+ this->buffer = std::vector<char>( BUFFER_SIZE );
this->decodeTarget = 0;
this->decodePos = 0;
this->rawPos = 0;
+ this->failed = false;
}
void SlipBIO::setTarget( std::shared_ptr<OpensslBIO> target ) {
SlipBIO::SlipBIO( std::shared_ptr<OpensslBIO> target ) {
this->target = target;
- this->buffer = std::vector<char>( 4096 );
+
+ this->buffer = std::vector<char>( BUFFER_SIZE );
this->decodeTarget = 0;
this->decodePos = 0;
this->rawPos = 0;
SlipBIO::~SlipBIO() {}
int SlipBIO::write( const char* buf, int num ) {
+#ifdef SLIP_IO_DEBUG
+ std::cout << "Out: " << toHex( buf, num ) << std::endl;
+#endif
+
int badOnes = 0;
for( int i = 0; i < num; i++ ) {
- if( ( buf[i] == ( char )0xc0 ) || ( buf[i] == ( char )0xDB ) ) {
+ if( ( buf[i] == SLIP_PACKET ) || ( buf[i] == SLIP_ESCAPE_CHAR ) ) {
badOnes++;
}
}
std::shared_ptr<char> t = std::shared_ptr<char>( targetPtr, free );
int j = 0;
- //targetPtr[j++] = (char)0xC0;
-
for( int i = 0; i < num; i++ ) {
- if( buf[i] == ( char )0xc0 ) {
- targetPtr[j++] = ( char )0xDB;
+ if( buf[i] == SLIP_PACKET ) {
+ targetPtr[j++] = SLIP_ESCAPE_CHAR;
targetPtr[j++] = ( char )0xDC;
- } else if( buf[i] == ( char )0xDB ) {
- targetPtr[j++] = ( char )0xDB;
+ } else if( buf[i] == SLIP_ESCAPE_CHAR ) {
+ targetPtr[j++] = SLIP_ESCAPE_CHAR;
targetPtr[j++] = ( char )0xDD;
} else {
targetPtr[j++] = buf[i];
}
}
- targetPtr[j++] = ( char )0xC0;
+ targetPtr[j++] = SLIP_PACKET;
int sent = 0;
while( sent < j ) {
packageLeft = false;
}
+#ifdef SLIP_IO_DEBUG
+ std::cout << "in: " << toHex( buf, len ) << std::endl;
+#endif
+
return len;
}
( void ) cmod;
( void ) arg1;
( void ) arg2;
- std::cout << "SLIP crtl: " << cmod << std::endl;
+
+ if( cmod == BIO_CTRL_RESET ) {
+ char resetSequence[] = {SLIP_ESCAPE_CHAR, 0, SLIP_PACKET};
+ target->write( resetSequence, 3 );
+ decodePos = 0;
+ decodeTarget = 0;
+ rawPos = 0;
+ std::cout << "resetting SLIP" << std::endl;
+ return 0;
+ }
+
return target->ctrl( cmod, arg1, arg2 );
}
unsigned int j = decodeTarget;
for( unsigned int i = decodePos; i < rawPos; i++ ) {
- if( buffer[i] == ( char ) 0xDB ) {
+ if( buffer[i] == SLIP_ESCAPE_CHAR ) {
i++;
if( i >= rawPos ) {
rawPos = decodePos + 1;
return 0;// no packet
} else if( buffer[i] == ( char )0xdc ) {
- buffer[j++] = ( char ) 0xc0;
+ buffer[j++] = SLIP_PACKET;
} else if( buffer[i] == ( char )0xdd ) {
- buffer[j++] = ( char ) 0xdb;
+ buffer[j++] = SLIP_ESCAPE_CHAR;
+ } else if( buffer[i] == SLIP_PACKET ) {
+ failed = true;
+ i--;
+ continue;
} else {
decodeTarget = 0;
failed = true;
// failed package
// error
}
- } else if( buffer[i] == ( char ) 0xc0 ) {
+ } else if( buffer[i] == SLIP_PACKET ) {
decodePos = i + 1;
decodeTarget = j;