]> WPIA git - cassiopeia.git/blobdiff - src/slipBio.cpp
add: Plug things together so we can have TBSCertificates from the database
[cassiopeia.git] / src / slipBio.cpp
index 6f1105c96bac38efd3ab69e16fb511473a343071..6fe1ec19f74b93ef371bd4b06e4bce88bb38e08a 100644 (file)
@@ -2,6 +2,10 @@
 
 #include <iostream>
 
+#include <unistd.h>
+
+#define BUFFER_SIZE 8192
+
 char hexDigit( char c ) {
     if( c < 0 ) {
         return 'x';
@@ -19,26 +23,33 @@ char hexDigit( char c ) {
 }
 
 std::string toHex( const char* buf, int len ) {
-    char* c = ( char* ) malloc( len * 2 );
+    std::string data = "000000";
 
-    if( !c ) {
-        return "<malloc fail>";
+    for( int i = 0; i < len; i++ ) {
+        data.append( 1, ' ' );
+        data.append( 1, hexDigit( ( buf[i] >> 4 ) & 0xF ) );
+        data.append( 1, hexDigit( buf[i] & 0xF ) );
     }
 
-    std::shared_ptr<char> mem = std::shared_ptr<char>( c, free );
+    return data;
+}
 
-    for( int i = 0; i < len; i++ ) {
-        c[i * 2] = hexDigit( ( buf[i] >> 4 ) & 0xF );
-        c[i * 2 + 1] = hexDigit( buf[i] & 0xF );
-    }
+SlipBIO::SlipBIO() {
+    this->buffer = std::vector<char>( BUFFER_SIZE );
+    this->decodeTarget = 0;
+    this->decodePos = 0;
+    this->rawPos = 0;
+    this->failed = false;
+}
 
-    return std::string( mem.get(), len * 2 );
+void SlipBIO::setTarget( std::shared_ptr<OpensslBIO> target ) {
+    this->target = 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;
@@ -49,6 +60,7 @@ SlipBIO::SlipBIO( std::shared_ptr<OpensslBIO> target ) {
 SlipBIO::~SlipBIO() {}
 
 int SlipBIO::write( const char* buf, int num ) {
+    std::cout << "Out: " << toHex( buf, num ) << std::endl;
     int badOnes = 0;
 
     for( int i = 0; i < num; i++ ) {
@@ -57,7 +69,7 @@ int SlipBIO::write( const char* buf, int num ) {
         }
     }
 
-    int totalLen = num + badOnes + 2;
+    int totalLen = num + badOnes + 1; // 2
     char* targetPtr = ( char* ) malloc( totalLen );
 
     if( !targetPtr ) {
@@ -66,7 +78,8 @@ int SlipBIO::write( const char* buf, int num ) {
 
     std::shared_ptr<char> t = std::shared_ptr<char>( targetPtr, free );
     int j = 0;
-    targetPtr[j++] = ( char )0xC0;
+
+    //targetPtr[j++] = (char)0xC0;
 
     for( int i = 0; i < num; i++ ) {
         if( buf[i] == ( char )0xc0 ) {
@@ -81,22 +94,33 @@ int SlipBIO::write( const char* buf, int num ) {
     }
 
     targetPtr[j++] = ( char )0xC0;
+    int sent = 0;
 
-    if( target->write( targetPtr, j ) != j ) {
-        throw "Error, target write failed";
+    while( sent < j ) {
+
+        errno = 0;
+        int dlen = target->write( targetPtr + sent, std::min( 1024, j - sent ) );
+
+        if( dlen < 0 ) {
+            throw "Error, target write failed";
+        } else if( dlen == 0 ) {
+            // sleep
+            usleep( 50000 );
+        }
+
+        if( errno != 0 ) {
+            perror( "Error" );
+        }
+
+        sent += dlen;
     }
 
-    std::cout << toHex( targetPtr, j ) << std::endl;
     return num;
 }
 
 int SlipBIO::read( char* buf, int size ) {
-    if( ( unsigned int ) size < buffer.capacity() ) {
-        // fail...
-    }
-
     // while we have no data to decode or unmasking does not yield a full package
-    while( decodePos >= rawPos || !unmask() ) {
+    while( !packageLeft && ( decodePos >= rawPos || !unmask() ) ) {
 
         // we have no data, read more
         if( buffer.size() - rawPos < 64 ) {
@@ -110,18 +134,24 @@ int SlipBIO::read( char* buf, int size ) {
         if( len > 0 ) {
             rawPos += len;
         } else {
-            decodeTarget = 0;
-            failed = true;
+            return -1;
+            //decodeTarget = 0;
+            //failed = true;
         }
 
     }
 
+    packageLeft = true;
+    int len = std::min( decodeTarget, ( unsigned int ) size );
     // a package finished, return it
-    std::copy( buffer.data(), buffer.data() + decodeTarget, buf );
+    std::copy( buffer.data(), buffer.data() + len, buf );
     // move the buffer contents back
+    std::copy( buffer.data() + len, buffer.data() + decodeTarget, buffer.data() );
+    decodeTarget -= len;
 
-    int len = decodeTarget;
-    decodeTarget = 0;
+    if( decodeTarget == 0 ) {
+        packageLeft = false;
+    }
 
     return len;
 }
@@ -130,8 +160,8 @@ long SlipBIO::ctrl( int cmod, long arg1, void* arg2 ) {
     ( void ) cmod;
     ( void ) arg1;
     ( void ) arg2;
-
-    return 0;
+    std::cout << "SLIP crtl: " << cmod << std::endl;
+    return target->ctrl( cmod, arg1, arg2 );
 }
 
 const char* SlipBIO::getName() {