File: IncomingPacketStream.html

package info (click to toggle)
libnet-ssh-ruby 1%3A1.1.4-1.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,532 kB
  • ctags: 2,464
  • sloc: ruby: 10,876; makefile: 3
file content (243 lines) | stat: -rw-r--r-- 12,865 bytes parent folder | download
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Class: Net::SSH::Transport::IncomingPacketStream</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" href="../../../.././rdoc-style.css" type="text/css" media="screen" />

    <script language="JavaScript" type="text/javascript">
    // <![CDATA[

        function toggleSource( id )
        {
          var elem
          var link

          if( document.getElementById )
          {
            elem = document.getElementById( id )
            link = document.getElementById( "l_" + id )
          }
          else if ( document.all )
          {
            elem = eval( "document.all." + id )
            link = eval( "document.all.l_" + id )
          }
          else
            return false;

          if( elem.style.display == "block" )
          {
            elem.style.display = "none"
            link.innerHTML = "show source"
          }
          else
          {
            elem.style.display = "block"
            link.innerHTML = "hide source"
          }
        }

        function openCode( url )
        {
          window.open( url, "SOURCE_CODE", "width=400,height=400,scrollbars=yes" )
        }
      // ]]>
    </script>
  </head>

  <body>
  <table width="100%" border='0' cellpadding='0' cellspacing='0' class='banner'><tr>
  <td class="file-title"><span class="file-title-prefix">Class</span><br />Net::SSH::Transport::IncomingPacketStream</td>
  <td align="right">
    <table cellspacing=0 cellpadding=2>
      <tr valign="top">
        <td>In:</td>
        <td>
<a href="../../../../files/lib/net/ssh/transport/packet-stream_rb.html">lib/net/ssh/transport/packet-stream.rb</a>
        </td>
      </tr>
    <tr>
      <td>Parent:</td>
      <td>
        <a href="PacketStream.html">
PacketStream
         </a>
     </td>
   </tr>
         </table>
        </td>
        </tr>
      </table>
 <!-- banner header -->

  <div id="bodyContent">
      <div id="content">

  <div class="description"><p>
Handles the decompression and dencryption of incoming packets.
</p>
</div>



  <div class="sectiontitle">Methods</div>
  <ul>
  <li><a href="#M000069">get</a></li>
  <li><a href="#M000067">new</a></li>
  <li><a href="#M000068">set_algorithms</a></li>
  </ul>





  <div class="sectiontitle">Attributes</div>
  <table border='0' cellpadding='5'>
  <tr valign='top'>
    <td class='attr-rw'>
[W]
    </td>
    <td class='attr-name'>buffers</td>
    <td class='attr-desc'>
A handle to the buffer factory to use when creating buffers

</td>
  </tr>
  <tr valign='top'>
    <td class='attr-rw'>
[W]
    </td>
    <td class='attr-name'>log</td>
    <td class='attr-desc'>
A handle to the logger instance to use for writing log messages

</td>
  </tr>
  </table>

<div class="sectiontitle">Public Class methods</div>
<div class="method">
  <div class="title">
    <a name="M000067"></a><b>new</b>( ciphers, hmacs, decompressors )
  </div>
  <div class="description">
  <p>
Create a <a href="IncomingPacketStream.html#M000067">new</a> <a
href="IncomingPacketStream.html">IncomingPacketStream</a>.
</p>
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000067_source')" id="l_M000067_source">show source</a> ]</p>
  <div id="M000067_source" class="dyn-source">
<pre>
     <span class="ruby-comment cmt"># File lib/net/ssh/transport/packet-stream.rb, line 141</span>
141:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>( <span class="ruby-identifier">ciphers</span>, <span class="ruby-identifier">hmacs</span>, <span class="ruby-identifier">decompressors</span> )
142:           <span class="ruby-keyword kw">super</span>( <span class="ruby-identifier">ciphers</span>, <span class="ruby-identifier">hmacs</span> )
143:           <span class="ruby-ivar">@decompressor</span> = <span class="ruby-identifier">decompressors</span>.<span class="ruby-identifier">fetch</span>( <span class="ruby-value str">&quot;none&quot;</span> )
144:           <span class="ruby-ivar">@mutex</span> = <span class="ruby-constant">Mutex</span>.<span class="ruby-identifier">new</span>
145:         <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
<div class="sectiontitle">Public Instance methods</div>
<div class="method">
  <div class="title">
    <a name="M000069"></a><b>get</b>()
  </div>
  <div class="description">
  <p>
Retrieve the next packet from the string, after (possibly) decrypting and
decompressing it. The packet is returned as a reader buffer.
</p>
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000069_source')" id="l_M000069_source">show source</a> ]</p>
  <div id="M000069_source" class="dyn-source">
<pre>
     <span class="ruby-comment cmt"># File lib/net/ssh/transport/packet-stream.rb, line 155</span>
155:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get</span>
156:           <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword kw">do</span>
157:             <span class="ruby-comment cmt"># get the first block of data</span>
158:             <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug?</span>
159:               <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug</span> <span class="ruby-node">&quot;reading #{@cipher.block_size} bytes from socket...&quot;</span>
160:             <span class="ruby-keyword kw">end</span>
161: 
162:             <span class="ruby-identifier">data</span> = <span class="ruby-identifier">read</span>( <span class="ruby-ivar">@cipher</span>.<span class="ruby-identifier">block_size</span> )
163: 
164:             <span class="ruby-comment cmt"># decipher it</span>
165:             <span class="ruby-identifier">reader</span> = <span class="ruby-ivar">@buffers</span>.<span class="ruby-identifier">reader</span>( <span class="ruby-ivar">@cipher</span>.<span class="ruby-identifier">update</span>( <span class="ruby-identifier">data</span> ) )
166: 
167:             <span class="ruby-comment cmt"># determine the packet length and how many bytes remain to be read</span>
168:             <span class="ruby-identifier">packet_length</span> = <span class="ruby-identifier">reader</span>.<span class="ruby-identifier">read_long</span>
169:             <span class="ruby-identifier">remaining_to_read</span> = <span class="ruby-identifier">packet_length</span> <span class="ruby-operator">+</span> <span class="ruby-value">4</span> <span class="ruby-operator">-</span> <span class="ruby-ivar">@cipher</span>.<span class="ruby-identifier">block_size</span>
170:             <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug?</span>
171:               <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug</span> <span class="ruby-node">&quot;packet length(#{packet_length}) &quot;</span> <span class="ruby-operator">+</span>
172:                 <span class="ruby-node">&quot;remaining(#{remaining_to_read})&quot;</span>
173:             <span class="ruby-keyword kw">end</span>
174: 
175:             <span class="ruby-comment cmt"># read the remainder of the packet and decrypt it.</span>
176:             <span class="ruby-identifier">data</span> = <span class="ruby-identifier">read</span>( <span class="ruby-identifier">remaining_to_read</span> )
177: 
178:             <span class="ruby-comment cmt"># get the hmac from the tail of the packet (if one exists), and</span>
179:             <span class="ruby-comment cmt"># then validate it.</span>
180:             <span class="ruby-identifier">hmac</span> = <span class="ruby-ivar">@hmac</span>.<span class="ruby-identifier">mac_length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">read</span>( <span class="ruby-ivar">@hmac</span>.<span class="ruby-identifier">mac_length</span> ) <span class="ruby-operator">:</span> <span class="ruby-value str">&quot;&quot;</span>
181: 
182:             <span class="ruby-identifier">reader</span>.<span class="ruby-identifier">append</span> <span class="ruby-ivar">@cipher</span>.<span class="ruby-identifier">update</span>( <span class="ruby-identifier">data</span> ) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">empty?</span>
183:             <span class="ruby-identifier">reader</span>.<span class="ruby-identifier">append</span> <span class="ruby-ivar">@cipher</span>.<span class="ruby-identifier">final</span>
184: 
185:             <span class="ruby-identifier">padding_length</span> = <span class="ruby-identifier">reader</span>.<span class="ruby-identifier">read_byte</span>
186: 
187:             <span class="ruby-identifier">payload</span> = <span class="ruby-identifier">reader</span>.<span class="ruby-identifier">read</span>( <span class="ruby-identifier">packet_length</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">padding_length</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span> )
188:             <span class="ruby-identifier">padding</span> = <span class="ruby-identifier">reader</span>.<span class="ruby-identifier">read</span>( <span class="ruby-identifier">padding_length</span> ) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">padding_length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>
189: 
190:             <span class="ruby-identifier">my_computed_hmac</span> = <span class="ruby-identifier">compute_hmac</span>( <span class="ruby-identifier">reader</span>.<span class="ruby-identifier">content</span> )
191:             <span class="ruby-identifier">raise</span> <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">SSH</span><span class="ruby-operator">::</span><span class="ruby-constant">Exception</span>, <span class="ruby-value str">&quot;corrupted mac detected&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">hmac</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">my_computed_hmac</span>
192: 
193:             <span class="ruby-comment cmt"># decompress the payload</span>
194:             <span class="ruby-identifier">payload</span> = <span class="ruby-ivar">@decompressor</span>.<span class="ruby-identifier">decompress</span>( <span class="ruby-identifier">payload</span> )
195: 
196:             <span class="ruby-identifier">increment_sequence_number</span>
197: 
198:             <span class="ruby-identifier">buffer</span> = <span class="ruby-ivar">@buffers</span>.<span class="ruby-identifier">reader</span>( <span class="ruby-identifier">payload</span> )
199:             <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug</span> <span class="ruby-node">&quot;received: #{buffer.content.inspect}&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug?</span>
200: 
201:             <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">buffer</span>
202:           <span class="ruby-keyword kw">end</span>
203:         <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
<div class="method">
  <div class="title">
    <a name="M000068"></a><b>set_algorithms</b>( cipher, mac, decompressor )
  </div>
  <div class="description">
  <p>
Set the cipher, mac, and decompressor algorithms to the given values.
</p>
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000068_source')" id="l_M000068_source">show source</a> ]</p>
  <div id="M000068_source" class="dyn-source">
<pre>
     <span class="ruby-comment cmt"># File lib/net/ssh/transport/packet-stream.rb, line 148</span>
148:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">set_algorithms</span>( <span class="ruby-identifier">cipher</span>, <span class="ruby-identifier">mac</span>, <span class="ruby-identifier">decompressor</span> )
149:           <span class="ruby-keyword kw">super</span>( <span class="ruby-identifier">cipher</span>, <span class="ruby-identifier">mac</span> )
150:           <span class="ruby-ivar">@decompressor</span> = <span class="ruby-identifier">decompressor</span>
151:         <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
</div>

  </div>

    </body>
</html>