KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jsch > ChannelDirectTCPIP


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002,2003,2004,2005,2006 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

29
30 package com.jcraft.jsch;
31
32 import java.io.*;
33
34 public class ChannelDirectTCPIP extends Channel{
35
36   static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
37   static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
38
39   String JavaDoc host;
40   int port;
41
42   String JavaDoc originator_IP_address="127.0.0.1";
43   int originator_port=0;
44
45   ChannelDirectTCPIP(){
46     super();
47     setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
48     setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
49     setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
50   }
51
52   void init (){
53     try{
54       io=new IO();
55     }
56     catch(Exception JavaDoc e){
57       System.err.println(e);
58     }
59   }
60
61   public void connect() throws JSchException{
62     try{
63       if(!session.isConnected()){
64         throw new JSchException("session is down");
65       }
66       Buffer buf=new Buffer(150);
67       Packet packet=new Packet(buf);
68       // send
69
// byte SSH_MSG_CHANNEL_OPEN(90)
70
// string channel type //
71
// uint32 sender channel // 0
72
// uint32 initial window size // 0x100000(65536)
73
// uint32 maxmum packet size // 0x4000(16384)
74

75       packet.reset();
76       buf.putByte((byte)90);
77       buf.putString("direct-tcpip".getBytes());
78       buf.putInt(id);
79       buf.putInt(lwsize);
80       buf.putInt(lmpsize);
81       buf.putString(host.getBytes());
82       buf.putInt(port);
83       buf.putString(originator_IP_address.getBytes());
84       buf.putInt(originator_port);
85       session.write(packet);
86
87       int retry=1000;
88       try{
89         while(this.getRecipient()==-1 &&
90               session.isConnected() &&
91               retry>0 &&
92               !eof_remote){
93           //Thread.sleep(500);
94
Thread.sleep(50);
95           retry--;
96         }
97       }
98       catch(Exception JavaDoc ee){
99       }
100       if(!session.isConnected()){
101     throw new JSchException("session is down");
102       }
103       if(retry==0 || this.eof_remote){
104         throw new JSchException("channel is not opened.");
105       }
106       /*
107       if(this.eof_remote){ // failed to open
108         disconnect();
109         return;
110       }
111       */

112
113       connected=true;
114
115       thread=new Thread JavaDoc(this);
116       thread.start();
117     }
118     catch(Exception JavaDoc e){
119       io.close();
120       io=null;
121       Channel.del(this);
122       if (e instanceof JSchException) {
123         throw (JSchException) e;
124       }
125     }
126   }
127
128   public void run(){
129     Buffer buf=new Buffer(rmpsize);
130     Packet packet=new Packet(buf);
131     int i=0;
132     try{
133       while(isConnected() &&
134             thread!=null &&
135             io!=null &&
136             io.in!=null){
137         i=io.in.read(buf.buffer,
138                      14,
139                      buf.buffer.length-14
140                      -32 -20 // padding and mac
141
);
142
143         if(i<=0){
144           eof();
145           break;
146         }
147         if(close)break;
148         packet.reset();
149         buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
150         buf.putInt(recipient);
151         buf.putInt(i);
152         buf.skip(i);
153         session.write(packet, this, i);
154       }
155     }
156     catch(Exception JavaDoc e){
157     }
158     disconnect();
159     //System.err.println("connect end");
160
}
161
162   public void setInputStream(InputStream in){
163     io.setInputStream(in);
164   }
165   public void setOutputStream(OutputStream out){
166     io.setOutputStream(out);
167   }
168
169   public void setHost(String JavaDoc host){this.host=host;}
170   public void setPort(int port){this.port=port;}
171   public void setOrgIPAddress(String JavaDoc foo){this.originator_IP_address=foo;}
172   public void setOrgPort(int foo){this.originator_port=foo;}
173 }
174
Popular Tags