]>
Commit | Line | Data |
---|---|---|
0763e16d JW |
1 | /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ |
2 | /* | |
3 | Copyright (c) 2002-2010 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 host; | |
40 | int port; | |
41 | ||
42 | String 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 e){ | |
57 | System.err.println(e); | |
58 | } | |
59 | } | |
60 | ||
61 | public void connect() throws JSchException{ | |
62 | try{ | |
63 | Session _session=getSession(); | |
64 | if(!_session.isConnected()){ | |
65 | throw new JSchException("session is down"); | |
66 | } | |
67 | Buffer buf=new Buffer(150); | |
68 | Packet packet=new Packet(buf); | |
69 | // send | |
70 | // byte SSH_MSG_CHANNEL_OPEN(90) | |
71 | // string channel type // | |
72 | // uint32 sender channel // 0 | |
73 | // uint32 initial window size // 0x100000(65536) | |
74 | // uint32 maxmum packet size // 0x4000(16384) | |
75 | ||
76 | packet.reset(); | |
77 | buf.putByte((byte)90); | |
78 | buf.putString(Util.str2byte("direct-tcpip")); | |
79 | buf.putInt(id); | |
80 | buf.putInt(lwsize); | |
81 | buf.putInt(lmpsize); | |
82 | buf.putString(Util.str2byte(host)); | |
83 | buf.putInt(port); | |
84 | buf.putString(Util.str2byte(originator_IP_address)); | |
85 | buf.putInt(originator_port); | |
86 | _session.write(packet); | |
87 | ||
88 | int retry=1000; | |
89 | try{ | |
90 | while(this.getRecipient()==-1 && | |
91 | _session.isConnected() && | |
92 | retry>0 && | |
93 | !eof_remote){ | |
94 | //Thread.sleep(500); | |
95 | Thread.sleep(50); | |
96 | retry--; | |
97 | } | |
98 | } | |
99 | catch(Exception ee){ | |
100 | } | |
101 | if(!_session.isConnected()){ | |
102 | throw new JSchException("session is down"); | |
103 | } | |
104 | if(retry==0 || this.eof_remote){ | |
105 | throw new JSchException("channel is not opened."); | |
106 | } | |
107 | /* | |
108 | if(this.eof_remote){ // failed to open | |
109 | disconnect(); | |
110 | return; | |
111 | } | |
112 | */ | |
113 | ||
114 | connected=true; | |
115 | ||
116 | if(io.in!=null){ | |
117 | thread=new Thread(this); | |
118 | thread.setName("DirectTCPIP thread "+_session.getHost()); | |
119 | if(_session.daemon_thread){ | |
120 | thread.setDaemon(_session.daemon_thread); | |
121 | } | |
122 | thread.start(); | |
123 | } | |
124 | } | |
125 | catch(Exception e){ | |
126 | io.close(); | |
127 | io=null; | |
128 | Channel.del(this); | |
129 | if (e instanceof JSchException) { | |
130 | throw (JSchException) e; | |
131 | } | |
132 | } | |
133 | } | |
134 | ||
135 | public void run(){ | |
136 | ||
137 | Buffer buf=new Buffer(rmpsize); | |
138 | Packet packet=new Packet(buf); | |
139 | int i=0; | |
140 | ||
141 | try{ | |
142 | Session _session=getSession(); | |
143 | while(isConnected() && | |
144 | thread!=null && | |
145 | io!=null && | |
146 | io.in!=null){ | |
147 | i=io.in.read(buf.buffer, | |
148 | 14, | |
149 | buf.buffer.length-14 | |
150 | -32 -20 // padding and mac | |
151 | ); | |
152 | ||
153 | if(i<=0){ | |
154 | eof(); | |
155 | break; | |
156 | } | |
157 | if(close)break; | |
158 | packet.reset(); | |
159 | buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA); | |
160 | buf.putInt(recipient); | |
161 | buf.putInt(i); | |
162 | buf.skip(i); | |
163 | _session.write(packet, this, i); | |
164 | } | |
165 | } | |
166 | catch(Exception e){ | |
167 | } | |
168 | disconnect(); | |
169 | //System.err.println("connect end"); | |
170 | } | |
171 | ||
172 | public void setInputStream(InputStream in){ | |
173 | io.setInputStream(in); | |
174 | } | |
175 | public void setOutputStream(OutputStream out){ | |
176 | io.setOutputStream(out); | |
177 | } | |
178 | ||
179 | public void setHost(String host){this.host=host;} | |
180 | public void setPort(int port){this.port=port;} | |
181 | public void setOrgIPAddress(String foo){this.originator_IP_address=foo;} | |
182 | public void setOrgPort(int foo){this.originator_port=foo;} | |
183 | } |