]> 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 f796341068aaab7abd98c6d6c5346fa62bd6aad8..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,27 +23,23 @@ char hexDigit( char c ) {
 }
 
 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 ) {
@@ -48,7 +48,8 @@ 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;
@@ -59,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++ ) {
@@ -92,10 +94,25 @@ int SlipBIO::write( const char* buf, int num ) {
     }
 
     targetPtr[j++] = ( char )0xC0;
+    int sent = 0;
+
+    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" );
+        }
 
-    if( target->write( targetPtr, j ) != j ) {
-        std::cout << "sent " << j << std::endl;
-        throw "Error, target write failed";
+        sent += dlen;
     }
 
     return num;
@@ -143,6 +160,7 @@ long SlipBIO::ctrl( int cmod, long arg1, void* arg2 ) {
     ( void ) cmod;
     ( void ) arg1;
     ( void ) arg2;
+    std::cout << "SLIP crtl: " << cmod << std::endl;
     return target->ctrl( cmod, arg1, arg2 );
 }