1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
<!-- $Id$ -->
<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>// $Id$</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>
|