<!-- page21.html,v 1.15 2003/08/19 15:08:26 schmidt Exp --> <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="Author" CONTENT="James CE Johnson"> <TITLE>ACE Tutorial 015</TITLE> </HEAD> <BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F"> <CENTER><B><FONT SIZE=+2>ACE Tutorial 015</FONT></B></CENTER> <CENTER><B><FONT SIZE=+2>Building a protocol stream</FONT></B></CENTER> <P> <HR WIDTH="100%"> The encryption implementation isn't any smarter than that of the compressor. Still, the hooks are there for you to insert your favorite library. <HR> <PRE> <font color=red>// page21.html,v 1.15 2003/08/19 15:08:26 schmidt Exp</font> <font color=blue>#include</font> "<font color=green>Crypt.h</font>" <font color=blue>#include</font> "<A HREF="../../../ace/SOCK_Stream.h">ace/SOCK_Stream.h</A>" <font color=red>/* The expected constructor... */</font> <font color=#008888>Crypt::Crypt</font>( void ) : Protocol_Task() { } <font color=#008888>Crypt::~Crypt</font>(void) { } <font color=red>/* To send the data we'll apply a signature and encryption. */</font> int <font color=#008888>Crypt::send</font>(ACE_Message_Block *message, ACE_Time_Value *timeout) { ACE_UNUSED_ARG(timeout); ACE_DEBUG ((LM_INFO, "<font color=green>(%P|%t) <font color=#008888>Crypt::send</font>() encrypting (%s)\n</font>", message->rd_ptr() )); <font color=red>// I suspect that some encryptors might change the data size.</font> <font color=red>// It probably isn't safe to create a same-size destination buffer.</font> ACE_Message_Block * encrypted = new ACE_Message_Block( message->size() +16 ); <font color=red>// Perform a bogus encryption algorithm and add our safety</font> <font color=red>// signature. Adding the original data size is also probably</font> <font color=red>// a good idea that I haven't encorporated here.</font> <font color=#008888>ACE_OS::sprintf</font>( encrypted->wr_ptr(), "<font color=green>ED:%s</font>", message->rd_ptr() ); encrypted->wr_ptr( strlen(encrypted->wr_ptr())+1 ); <font color=red>// Send the encrypted data down the stream to the next module</font> this->put_next( encrypted ); <font color=red>// We're done here.</font> message->release(); return( 0 ); } <font color=red>/* The upstream movement requires that we decrypt what the peer has given us. */</font> int <font color=#008888>Crypt::recv</font>(ACE_Message_Block *message, ACE_Time_Value *timeout) { ACE_UNUSED_ARG(timeout); ACE_DEBUG ((LM_INFO, "<font color=green>(%P|%t) <font color=#008888>Crypt::recv</font>() decrypting (%s)\n</font>", message->rd_ptr() )); <font color=red>// Create a destination for the decrypted data. The same</font> <font color=red>// block size caveat exists of course.</font> ACE_Message_Block * decrypted = new ACE_Message_Block( message->size() +16 ); <font color=red>// Check the signature as expected.</font> if( <font color=#008888>ACE_OS::strncmp</font>( message->rd_ptr(), "<font color=green>ED:</font>", 3 ) ) { ACE_DEBUG ((LM_INFO, "<font color=green>(%P|%t) Improperly encrypted data.\n</font>" )); message->release(); return(-1); } <font color=red>// Don't forget to skip past the signature before decrypting</font> <font color=red>// or things will be quite exciting!</font> message->rd_ptr( 3 ); <font color=red>// Perform a bogus decryption algorithm</font> <font color=#008888>ACE_OS::sprintf</font>( decrypted->wr_ptr(), "<font color=green>%s</font>", message->rd_ptr() ); decrypted->wr_ptr( strlen(decrypted->wr_ptr())+1 ); <font color=red>// Send the decrypted data down the stream to the next module</font> this->put_next( decrypted ); <font color=red>// We're done here.</font> message->release(); return( 0 ); } </PRE> <P><HR WIDTH="100%"> <CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page22.html">Continue This Tutorial</A>]</CENTER>