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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
|
<?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::Service::Forward::Driver</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::Service::Forward::Driver</td>
<td align="right">
<table cellspacing=0 cellpadding=2>
<tr valign="top">
<td>In:</td>
<td>
<a href="../../../../../files/lib/net/ssh/service/forward/driver_rb.html">lib/net/ssh/service/forward/driver.rb</a>
</td>
</tr>
<tr>
<td>Parent:</td>
<td>
Object
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- banner header -->
<div id="bodyContent">
<div id="content">
<div class="description"><p>
The <a href="Driver.html">Forward::Driver</a> class manages ports that have
been forwarded. It will open a socket on each <a
href="Driver.html#M000306">local</a> port and listen for <a
href="Driver.html#M000306">local</a> connections, forwarding those
connections over an <a href="../../../SSH.html">SSH</a> channel to the
other end.
</p>
</div>
<div class="sectiontitle">Methods</div>
<ul>
<li><a href="#M000309">active_locals</a></li>
<li><a href="#M000310">active_remotes</a></li>
<li><a href="#M000311">cancel_local</a></li>
<li><a href="#M000312">cancel_remote</a></li>
<li><a href="#M000305">direct_channel</a></li>
<li><a href="#M000313">do_open_channel</a></li>
<li><a href="#M000306">local</a></li>
<li><a href="#M000304">new</a></li>
<li><a href="#M000307">remote</a></li>
<li><a href="#M000308">remote_to</a></li>
</ul>
<div class="sectiontitle">Attributes</div>
<table border='0' cellpadding='5'>
<tr valign='top'>
<td class='attr-rw'>
[R]
</td>
<td class='attr-name'>direct_channel_count</td>
<td class='attr-desc'>
The number of direct (<a href="Driver.html#M000306">local</a>-to-<a
href="Driver.html#M000307">remote</a>) channels that have been opened.
</td>
</tr>
<tr valign='top'>
<td class='attr-rw'>
[R]
</td>
<td class='attr-name'>open_direct_channel_count</td>
<td class='attr-desc'>
The number of direct (<a href="Driver.html#M000306">local</a>-to-<a
href="Driver.html#M000307">remote</a>) channels that are currently open.
</td>
</tr>
</table>
<div class="sectiontitle">Public Class methods</div>
<div class="method">
<div class="title">
<a name="M000304"></a><b>new</b>( connection, buffers, log, handlers )
</div>
<div class="description">
<p>
Create a <a href="Driver.html#M000304">new</a> <a
href="Driver.html">Driver</a> instance.
</p>
</div>
<div class="sourcecode">
<p class="source-link">[ <a href="javascript:toggleSource('M000304_source')" id="l_M000304_source">show source</a> ]</p>
<div id="M000304_source" class="dyn-source">
<pre>
<span class="ruby-comment cmt"># File lib/net/ssh/service/forward/driver.rb, line 39</span>
39: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>( <span class="ruby-identifier">connection</span>, <span class="ruby-identifier">buffers</span>, <span class="ruby-identifier">log</span>, <span class="ruby-identifier">handlers</span> )
40: <span class="ruby-ivar">@connection</span> = <span class="ruby-identifier">connection</span>
41: <span class="ruby-ivar">@buffers</span> = <span class="ruby-identifier">buffers</span>
42: <span class="ruby-ivar">@log</span> = <span class="ruby-identifier">log</span>
43: <span class="ruby-ivar">@handlers</span> = <span class="ruby-identifier">handlers</span>
44:
45: <span class="ruby-ivar">@local_forwards</span> = <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span>
46: <span class="ruby-ivar">@remote_forwards</span> = <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span>
47: <span class="ruby-ivar">@direct_channel_count</span> = <span class="ruby-value">0</span>
48: <span class="ruby-ivar">@open_direct_channel_count</span> = <span class="ruby-value">0</span>
49:
50: <span class="ruby-ivar">@connection</span>.<span class="ruby-identifier">add_channel_open_handler</span>(
51: <span class="ruby-value str">"forwarded-tcpip"</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">method</span>(<span class="ruby-identifier">:do_open_channel</span>) )
52: <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="M000309"></a><b>active_locals</b>()
</div>
<div class="description">
<p>
Return an array of the active forwarded <a
href="Driver.html#M000306">local</a> connections. Each element of the array
is another array containing the <a href="Driver.html#M000306">local</a>
port, and the <a href="Driver.html#M000307">remote</a> host and port of the
connection.
</p>
</div>
<div class="sourcecode">
<p class="source-link">[ <a href="javascript:toggleSource('M000309_source')" id="l_M000309_source">show source</a> ]</p>
<div id="M000309_source" class="dyn-source">
<pre>
<span class="ruby-comment cmt"># File lib/net/ssh/service/forward/driver.rb, line 229</span>
229: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">active_locals</span>
230: <span class="ruby-ivar">@local_forwards</span>.<span class="ruby-identifier">keys</span>
231: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<div class="method">
<div class="title">
<a name="M000310"></a><b>active_remotes</b>()
</div>
<div class="description">
<p>
Return an array of the active forwarded <a
href="Driver.html#M000307">remote</a> connections. Each element of the
array is an integer representing the port number of the <a
href="Driver.html#M000307">remote</a> host that is being forwarded to the
<a href="Driver.html#M000306">local</a> client.
</p>
</div>
<div class="sourcecode">
<p class="source-link">[ <a href="javascript:toggleSource('M000310_source')" id="l_M000310_source">show source</a> ]</p>
<div id="M000310_source" class="dyn-source">
<pre>
<span class="ruby-comment cmt"># File lib/net/ssh/service/forward/driver.rb, line 236</span>
236: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">active_remotes</span>
237: <span class="ruby-ivar">@remote_forwards</span>.<span class="ruby-identifier">keys</span>
238: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<div class="method">
<div class="title">
<a name="M000311"></a><b>cancel_local</b>( local_port, bind_address="127.0.0.1" )
</div>
<div class="description">
<p>
Cease forwarding connections from the given <a
href="Driver.html#M000306">local</a> port (and interface). The parameters
must match those given to a prior call to <a
href="Driver.html#M000306">local</a>. Existing forwarded connections are
not affected by this call, but no more connections will be accepted on the
<a href="Driver.html#M000306">local</a> port.
</p>
</div>
<div class="sourcecode">
<p class="source-link">[ <a href="javascript:toggleSource('M000311_source')" id="l_M000311_source">show source</a> ]</p>
<div id="M000311_source" class="dyn-source">
<pre>
<span class="ruby-comment cmt"># File lib/net/ssh/service/forward/driver.rb, line 245</span>
245: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">cancel_local</span>( <span class="ruby-identifier">local_port</span>, <span class="ruby-identifier">bind_address</span>=<span class="ruby-value str">"127.0.0.1"</span> )
246: <span class="ruby-identifier">key</span> = [ <span class="ruby-identifier">local_port</span>.<span class="ruby-identifier">to_i</span>, <span class="ruby-identifier">bind_address</span> ]
247:
248: <span class="ruby-identifier">forward</span> = <span class="ruby-ivar">@local_forwards</span>[ <span class="ruby-identifier">key</span> ]
249: <span class="ruby-ivar">@local_forwards</span>.<span class="ruby-identifier">delete</span> <span class="ruby-identifier">key</span>
250:
251: <span class="ruby-identifier">forward</span>[ <span class="ruby-identifier">:socket</span> ].<span class="ruby-identifier">shutdown</span>
252: <span class="ruby-identifier">forward</span>[ <span class="ruby-identifier">:thread</span> ].<span class="ruby-identifier">terminate</span>
253:
254: <span class="ruby-keyword kw">true</span>
255: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<div class="method">
<div class="title">
<a name="M000312"></a><b>cancel_remote</b>( remote_port, remote_host="127.0.0.1" )
</div>
<div class="description">
<p>
Cease forwarding connections from the given <a
href="Driver.html#M000307">remote</a> port. The <tt>remote_host</tt>
parameter must match the corresponding parameter that was passed to <a
href="Driver.html#M000307">remote</a>. Existing forwarded connections are
not affected by this call, but no more connections will be forwarded from
the <a href="Driver.html#M000307">remote</a> host to the <a
href="Driver.html#M000306">local</a> host via that port.
</p>
</div>
<div class="sourcecode">
<p class="source-link">[ <a href="javascript:toggleSource('M000312_source')" id="l_M000312_source">show source</a> ]</p>
<div id="M000312_source" class="dyn-source">
<pre>
<span class="ruby-comment cmt"># File lib/net/ssh/service/forward/driver.rb, line 262</span>
262: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">cancel_remote</span>( <span class="ruby-identifier">remote_port</span>, <span class="ruby-identifier">remote_host</span>=<span class="ruby-value str">"127.0.0.1"</span> )
263: <span class="ruby-identifier">writer</span> = <span class="ruby-ivar">@buffers</span>.<span class="ruby-identifier">writer</span>
264: <span class="ruby-identifier">writer</span>.<span class="ruby-identifier">write_string</span> <span class="ruby-identifier">remote_host</span>
265: <span class="ruby-identifier">writer</span>.<span class="ruby-identifier">write_long</span> <span class="ruby-identifier">remote_port</span>.<span class="ruby-identifier">to_i</span>
266:
267: <span class="ruby-ivar">@connection</span>.<span class="ruby-identifier">global_request</span>( <span class="ruby-value str">"cancel-tcpip-forward"</span>, <span class="ruby-identifier">writer</span>
268: ) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">success</span>, <span class="ruby-identifier">response</span><span class="ruby-operator">|</span>
269: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">success</span>
270: <span class="ruby-ivar">@remote_forwards</span>.<span class="ruby-identifier">delete</span> <span class="ruby-identifier">remote_port</span>
271: <span class="ruby-keyword kw">else</span>
272: <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>,
273: <span class="ruby-value str">"could not cancel remote forward request on "</span> <span class="ruby-operator">+</span>
274: <span class="ruby-node">"(#{remote_port},#{remote_host})"</span>
275: <span class="ruby-keyword kw">end</span>
276: <span class="ruby-keyword kw">end</span>
277: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<div class="method">
<div class="title">
<a name="M000305"></a><b>direct_channel</b>( local_port, remote_host, remote_port, handler, *data )
</div>
<div class="description">
<p>
Open a direct "tcpip" channel to the <a
href="Driver.html#M000307">remote</a> machine, which will then forward the
connection to the given <a href="Driver.html#M000307">remote</a>-host and
<a href="Driver.html#M000307">remote</a>-port. The connection will appear
to have come from the given port on the <a
href="Driver.html#M000306">local</a> machine.
</p>
<p>
The handler object may respond to the following messages, in order to
respond to requests over the channel:
</p>
<ul>
<li>on_receive( channel, data ): when data is received over the channel (from
the <a href="Driver.html#M000307">remote</a> machine), this method will be
invoked.
</li>
<li>on_eof( channel ): when the <a href="Driver.html#M000307">remote</a>
machine will no longer send data, this method will be invoked. The client
may continue to send data over the channel, however.
</li>
<li>on_close( channel ): when the channel has been closed and is no longer
valid for passing data, this method will be invoked.
</li>
<li>confirm( channel, local_port, remote_host, remote_port, *data ): when the
channel has been opened and the <a href="Driver.html#M000307">remote</a>
machine has confirmed it, this method will be invoked. The <tt>data</tt>
parameters are the same parameters as were passed to the <a
href="Driver.html#M000305">direct_channel</a> method.
</li>
<li>process( channel ): invoked after the channel is confirmed, to process the
channel. It is invoked in a <a href="Driver.html#M000304">new</a> Thread.
</li>
</ul>
<p>
Only the <tt>process</tt> method is required—the others will only be
invoked if the handler responds to them.
</p>
</div>
<div class="sourcecode">
<p class="source-link">[ <a href="javascript:toggleSource('M000305_source')" id="l_M000305_source">show source</a> ]</p>
<div id="M000305_source" class="dyn-source">
<pre>
<span class="ruby-comment cmt"># File lib/net/ssh/service/forward/driver.rb, line 79</span>
79: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">direct_channel</span>( <span class="ruby-identifier">local_port</span>, <span class="ruby-identifier">remote_host</span>, <span class="ruby-identifier">remote_port</span>,
80: <span class="ruby-identifier">handler</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">data</span> )
81: <span class="ruby-comment cmt"># begin</span>
82: <span class="ruby-identifier">writer</span> = <span class="ruby-ivar">@buffers</span>.<span class="ruby-identifier">writer</span>
83: <span class="ruby-identifier">writer</span>.<span class="ruby-identifier">write_string</span> <span class="ruby-identifier">remote_host</span>
84: <span class="ruby-identifier">writer</span>.<span class="ruby-identifier">write_long</span> <span class="ruby-identifier">remote_port</span>.<span class="ruby-identifier">to_i</span>
85: <span class="ruby-identifier">writer</span>.<span class="ruby-identifier">write_string</span> <span class="ruby-value str">"127.0.0.1"</span>
86: <span class="ruby-identifier">writer</span>.<span class="ruby-identifier">write_long</span> <span class="ruby-identifier">local_port</span>.<span class="ruby-identifier">to_i</span>
87:
88: <span class="ruby-ivar">@direct_channel_count</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
89: <span class="ruby-ivar">@open_direct_channel_count</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
90:
91: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug?</span>
92: <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug</span> <span class="ruby-value str">"opening direct channel for "</span> <span class="ruby-operator">+</span>
93: <span class="ruby-node">"#{local_port}:#{remote_host}:#{remote_port}"</span>
94: <span class="ruby-keyword kw">end</span>
95:
96: <span class="ruby-identifier">c</span> = <span class="ruby-ivar">@connection</span>.<span class="ruby-identifier">open_channel</span>( <span class="ruby-value str">'direct-tcpip'</span>, <span class="ruby-identifier">writer</span> ) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">channel</span><span class="ruby-operator">|</span>
97: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug?</span>
98: <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug</span> <span class="ruby-value str">"direct channel confirmed for "</span> <span class="ruby-operator">+</span>
99: <span class="ruby-node">"#{local_port}:#{remote_host}:#{remote_port}"</span>
100: <span class="ruby-keyword kw">end</span>
101:
102: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>( <span class="ruby-identifier">:on_receive</span> )
103: <span class="ruby-identifier">channel</span>.<span class="ruby-identifier">on_data</span>( <span class="ruby-operator">&</span><span class="ruby-identifier">handler</span>.<span class="ruby-identifier">method</span>(<span class="ruby-identifier">:on_receive</span>) )
104: <span class="ruby-keyword kw">end</span>
105:
106: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>( <span class="ruby-identifier">:on_eof</span> )
107: <span class="ruby-identifier">channel</span>.<span class="ruby-identifier">on_eof</span>( <span class="ruby-operator">&</span><span class="ruby-identifier">handler</span>.<span class="ruby-identifier">method</span>(<span class="ruby-identifier">:on_eof</span>) )
108: <span class="ruby-keyword kw">end</span>
109:
110: <span class="ruby-identifier">channel</span>.<span class="ruby-identifier">on_close</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">ch</span><span class="ruby-operator">|</span>
111: <span class="ruby-ivar">@open_direct_channel_count</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span>
112: <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">on_close</span>( <span class="ruby-identifier">ch</span> ) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>( <span class="ruby-identifier">:on_close</span> )
113: <span class="ruby-keyword kw">end</span>
114:
115: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>( <span class="ruby-identifier">:confirm</span> )
116: <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">confirm</span>( <span class="ruby-identifier">channel</span>, <span class="ruby-identifier">local_port</span>, <span class="ruby-identifier">remote_host</span>,
117: <span class="ruby-identifier">remote_port</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">data</span> )
118: <span class="ruby-keyword kw">end</span>
119:
120: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>( <span class="ruby-identifier">:process</span> )
121: <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> { <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">process</span>( <span class="ruby-identifier">channel</span> ) }
122: <span class="ruby-keyword kw">end</span>
123: <span class="ruby-keyword kw">end</span>
124:
125: <span class="ruby-identifier">c</span>.<span class="ruby-identifier">on_confirm_failed</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">channel</span>, <span class="ruby-identifier">code</span>,<span class="ruby-identifier">desc</span>,<span class="ruby-identifier">lang</span><span class="ruby-operator">|</span>
126: <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">"could not open direct channel for "</span> <span class="ruby-operator">+</span>
127: <span class="ruby-node">"#{local_port}:#{remote_host}:#{remote_port} (#{code}, #{desc})"</span>
128: <span class="ruby-keyword kw">end</span>
129:
130: <span class="ruby-keyword kw">nil</span>
131: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<div class="method">
<div class="title">
<a name="M000313"></a><b>do_open_channel</b>( connection, channel, data )
</div>
<div class="description">
<p>
open a <a href="Driver.html#M000304">new</a> channel as requested by the
server
</p>
</div>
<div class="sourcecode">
<p class="source-link">[ <a href="javascript:toggleSource('M000313_source')" id="l_M000313_source">show source</a> ]</p>
<div id="M000313_source" class="dyn-source">
<pre>
<span class="ruby-comment cmt"># File lib/net/ssh/service/forward/driver.rb, line 280</span>
280: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">do_open_channel</span>( <span class="ruby-identifier">connection</span>, <span class="ruby-identifier">channel</span>, <span class="ruby-identifier">data</span> )
281: <span class="ruby-identifier">connected_address</span> = <span class="ruby-identifier">data</span>.<span class="ruby-identifier">read_string</span>
282: <span class="ruby-identifier">connected_port</span> = <span class="ruby-identifier">data</span>.<span class="ruby-identifier">read_long</span>
283: <span class="ruby-identifier">originator_address</span> = <span class="ruby-identifier">data</span>.<span class="ruby-identifier">read_string</span>
284: <span class="ruby-identifier">originator_port</span> = <span class="ruby-identifier">data</span>.<span class="ruby-identifier">read_long</span>
285:
286: <span class="ruby-identifier">forward_data</span> = <span class="ruby-ivar">@remote_forwards</span>[ <span class="ruby-identifier">connected_port</span> ]
287: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">forward_data</span>
288: <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>,
289: <span class="ruby-value str">"recieved invalid channel-open command for a port forward "</span> <span class="ruby-operator">+</span>
290: <span class="ruby-value str">"that was never requested"</span>
291: <span class="ruby-keyword kw">end</span>
292:
293: <span class="ruby-identifier">handler</span> = <span class="ruby-identifier">forward_data</span>[<span class="ruby-identifier">:handler</span>]
294:
295: <span class="ruby-identifier">forward_data</span>[<span class="ruby-identifier">:channel</span>] = <span class="ruby-identifier">channel</span>
296:
297: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>( <span class="ruby-identifier">:on_open</span> )
298: <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">on_open</span>( <span class="ruby-identifier">channel</span>,
299: <span class="ruby-identifier">connected_address</span>, <span class="ruby-identifier">connected_port</span>,
300: <span class="ruby-identifier">originator_address</span>, <span class="ruby-identifier">originator_port</span> )
301: <span class="ruby-keyword kw">end</span>
302:
303: <span class="ruby-identifier">channel</span>.<span class="ruby-identifier">on_data</span>( <span class="ruby-operator">&</span><span class="ruby-identifier">handler</span>.<span class="ruby-identifier">method</span>(<span class="ruby-identifier">:on_receive</span>) )
304:
305: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>( <span class="ruby-identifier">:on_close</span> )
306: <span class="ruby-identifier">channel</span>.<span class="ruby-identifier">on_close</span>( <span class="ruby-operator">&</span><span class="ruby-identifier">handler</span>.<span class="ruby-identifier">method</span>(<span class="ruby-identifier">:on_close</span>) )
307: <span class="ruby-keyword kw">end</span>
308:
309: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>( <span class="ruby-identifier">:on_eof</span> )
310: <span class="ruby-identifier">channel</span>.<span class="ruby-identifier">on_eof</span>( <span class="ruby-operator">&</span><span class="ruby-identifier">handler</span>.<span class="ruby-identifier">method</span>(<span class="ruby-identifier">:on_eof</span>) )
311: <span class="ruby-keyword kw">end</span>
312: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<div class="method">
<div class="title">
<a name="M000306"></a><b>local</b>( *args )
</div>
<div class="description">
<p>
<a href="../Forward.html">Forward</a> connections on the given <a
href="Driver.html#M000306">local</a> port, to the given <a
href="Driver.html#M000307">remote</a> host and <a
href="Driver.html#M000307">remote</a> port.
</p>
<p>
This method will return immediately, forwarding the connections
asynchronously.
</p>
</div>
<div class="sourcecode">
<p class="source-link">[ <a href="javascript:toggleSource('M000306_source')" id="l_M000306_source">show source</a> ]</p>
<div id="M000306_source" class="dyn-source">
<pre>
<span class="ruby-comment cmt"># File lib/net/ssh/service/forward/driver.rb, line 138</span>
138: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">local</span>( <span class="ruby-operator">*</span><span class="ruby-identifier">args</span> )
139: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator"><</span> <span class="ruby-value">3</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">></span> <span class="ruby-value">4</span>
140: <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>,
141: <span class="ruby-node">"expected 3 or 4 parameters, got #{args.length+1}"</span>
142: <span class="ruby-keyword kw">end</span>
143:
144: <span class="ruby-identifier">bind_address</span> = <span class="ruby-value str">"127.0.0.1"</span>
145: <span class="ruby-identifier">bind_address</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">is_a?</span> <span class="ruby-constant">String</span>
146:
147: <span class="ruby-identifier">local_port</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span>.<span class="ruby-identifier">to_i</span>
148: <span class="ruby-identifier">remote_host</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span>
149: <span class="ruby-identifier">remote_port</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span>
150:
151: <span class="ruby-identifier">key</span> = [ <span class="ruby-identifier">local_port</span>.<span class="ruby-identifier">to_i</span>, <span class="ruby-identifier">bind_address</span> ]
152:
153: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug?</span>
154: <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug</span> <span class="ruby-value str">"starting local forwarding server on "</span> <span class="ruby-operator">+</span>
155: <span class="ruby-identifier">key</span>.<span class="ruby-identifier">inspect</span>
156: <span class="ruby-keyword kw">end</span>
157:
158: <span class="ruby-identifier">socket</span> = <span class="ruby-constant">TCPServer</span>.<span class="ruby-identifier">new</span>( <span class="ruby-identifier">bind_address</span>, <span class="ruby-identifier">local_port</span> )
159:
160: <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword kw">do</span>
161: <span class="ruby-keyword kw">begin</span>
162: <span class="ruby-ivar">@local_forwards</span>[ <span class="ruby-identifier">key</span> ] = { <span class="ruby-identifier">:thread</span> =<span class="ruby-operator">></span> <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>,
163: <span class="ruby-identifier">:socket</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">socket</span> }
164:
165: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug?</span>
166: <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug</span> <span class="ruby-node">"listening for connections on #{key.inspect}"</span>
167: <span class="ruby-keyword kw">end</span>
168:
169: <span class="ruby-keyword kw">while</span> ( <span class="ruby-identifier">client</span> = <span class="ruby-identifier">socket</span>.<span class="ruby-identifier">accept</span> )
170: <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug</span> <span class="ruby-node">"#{key.inspect} received connection"</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">debug?</span>
171:
172: <span class="ruby-identifier">direct_channel</span>( <span class="ruby-identifier">local_port</span>,
173: <span class="ruby-identifier">remote_host</span>,
174: <span class="ruby-identifier">remote_port</span>,
175: <span class="ruby-ivar">@handlers</span>[<span class="ruby-identifier">:local</span>].<span class="ruby-identifier">call</span>( <span class="ruby-identifier">client</span> ) )
176: <span class="ruby-keyword kw">end</span>
177:
178: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
179: <span class="ruby-ivar">@log</span>.<span class="ruby-identifier">error</span> <span class="ruby-value str">"error forwarding local connection: "</span> <span class="ruby-operator">+</span>
180: <span class="ruby-node">"#{e.class} (#{e.message})\n "</span> <span class="ruby-operator">+</span>
181: <span class="ruby-identifier">e</span>.<span class="ruby-identifier">backtrace</span>.<span class="ruby-identifier">join</span>( <span class="ruby-value str">"\n "</span> )
182: <span class="ruby-keyword kw">end</span>
183: <span class="ruby-keyword kw">end</span>
184: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<div class="method">
<div class="title">
<a name="M000307"></a><b>remote</b>( handler, remote_port, remote_host="127.0.0.1" )
</div>
<div class="description">
<p>
Initiate forwarding of the given <a href="Driver.html#M000307">remote</a>
port on the connected host. Forwarded packets will be passed to the given
block as they are recieved. The <a
href="Driver.html#M000307">remote</a>-host represents the address that
should be bound on the <a href="Driver.html#M000307">remote</a> host, and
defaults to ‘127.0.0.1’.
</p>
</div>
<div class="sourcecode">
<p class="source-link">[ <a href="javascript:toggleSource('M000307_source')" id="l_M000307_source">show source</a> ]</p>
<div id="M000307_source" class="dyn-source">
<pre>
<span class="ruby-comment cmt"># File lib/net/ssh/service/forward/driver.rb, line 190</span>
190: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">remote</span>( <span class="ruby-identifier">handler</span>, <span class="ruby-identifier">remote_port</span>, <span class="ruby-identifier">remote_host</span>=<span class="ruby-value str">"127.0.0.1"</span> )
191: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@remote_forwards</span>[ <span class="ruby-identifier">remote_port</span> ]
192: <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-node">"#{remote_port} is already forwarded"</span>
193: <span class="ruby-keyword kw">end</span>
194:
195: <span class="ruby-identifier">writer</span> = <span class="ruby-ivar">@buffers</span>.<span class="ruby-identifier">writer</span>
196: <span class="ruby-identifier">writer</span>.<span class="ruby-identifier">write_string</span> <span class="ruby-identifier">remote_host</span>
197: <span class="ruby-identifier">writer</span>.<span class="ruby-identifier">write_long</span> <span class="ruby-identifier">remote_port</span>.<span class="ruby-identifier">to_i</span>
198:
199: <span class="ruby-ivar">@connection</span>.<span class="ruby-identifier">global_request</span>( <span class="ruby-value str">"tcpip-forward"</span>, <span class="ruby-identifier">writer</span>
200: ) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">success</span>, <span class="ruby-identifier">response</span><span class="ruby-operator">|</span>
201: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">success</span>
202: <span class="ruby-identifier">remote_port</span> = <span class="ruby-identifier">response</span>.<span class="ruby-identifier">read_long</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">remote_port</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
203: <span class="ruby-ivar">@remote_forwards</span>[ <span class="ruby-identifier">remote_port</span> ] = { <span class="ruby-identifier">:port</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">remote_port</span>,
204: <span class="ruby-identifier">:handler</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">handler</span> }
205: <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">setup</span>( <span class="ruby-identifier">remote_port</span> ) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>( <span class="ruby-identifier">:setup</span> )
206: <span class="ruby-keyword kw">else</span>
207: <span class="ruby-identifier">msg</span> = <span class="ruby-node">"remote port #{remote_port} could not be forwarded "</span> <span class="ruby-operator">+</span>
208: <span class="ruby-value str">"to local host"</span>
209: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>( <span class="ruby-identifier">:error</span> )
210: <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">error</span>( <span class="ruby-identifier">msg</span> )
211: <span class="ruby-keyword kw">else</span>
212: <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-identifier">msg</span>
213: <span class="ruby-keyword kw">end</span>
214: <span class="ruby-keyword kw">end</span>
215: <span class="ruby-keyword kw">end</span>
216: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<div class="method">
<div class="title">
<a name="M000308"></a><b>remote_to</b>( port, host, remote_port, remote_host="127.0.0.1" )
</div>
<div class="description">
<p>
A convenience method for setting up a forwarded connection from the given
port on the <a href="Driver.html#M000307">remote</a> host, to the given
host and port (<a href="Driver.html#M000306">local</a>).
</p>
</div>
<div class="sourcecode">
<p class="source-link">[ <a href="javascript:toggleSource('M000308_source')" id="l_M000308_source">show source</a> ]</p>
<div id="M000308_source" class="dyn-source">
<pre>
<span class="ruby-comment cmt"># File lib/net/ssh/service/forward/driver.rb, line 220</span>
220: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">remote_to</span>( <span class="ruby-identifier">port</span>, <span class="ruby-identifier">host</span>, <span class="ruby-identifier">remote_port</span>, <span class="ruby-identifier">remote_host</span>=<span class="ruby-value str">"127.0.0.1"</span> )
221: <span class="ruby-comment cmt"># begin</span>
222: <span class="ruby-identifier">remote</span>( <span class="ruby-ivar">@handlers</span>[<span class="ruby-identifier">:remote</span>].<span class="ruby-identifier">call</span>( <span class="ruby-identifier">port</span>, <span class="ruby-identifier">host</span> ), <span class="ruby-identifier">remote_port</span>,
223: <span class="ruby-identifier">remote_host</span> )
224: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
|