~ubuntu-branches/ubuntu/trusty/jsch/trusty-proposed

« back to all changes in this revision

Viewing changes to src/main/java/com/jcraft/jsch/ChannelDirectTCPIP.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-06 10:50:04 UTC
  • mfrom: (4.2.1 experimental) (6.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140106105004-9al5p968hmaaj78t
Tags: 0.1.50-1ubuntu1
* Resync with Debian unstable.
* Drop dependency on libjzlib-java for continued main inclusion. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
 
2
/*
 
3
Copyright (c) 2002-2012 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
  static private final byte[] _type = Util.str2byte("direct-tcpip");
 
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
    type = _type;
 
48
    setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
 
49
    setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
 
50
    setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
 
51
  }
 
52
 
 
53
  void init (){
 
54
    io=new IO();
 
55
  }
 
56
 
 
57
  public void connect(int connectTimeout) throws JSchException{
 
58
    this.connectTimeout=connectTimeout;
 
59
    try{
 
60
      Session _session=getSession();
 
61
      if(!_session.isConnected()){
 
62
        throw new JSchException("session is down");
 
63
      }
 
64
 
 
65
      if(io.in!=null){
 
66
        thread=new Thread(this);
 
67
        thread.setName("DirectTCPIP thread "+_session.getHost());
 
68
        if(_session.daemon_thread){
 
69
          thread.setDaemon(_session.daemon_thread);
 
70
        }
 
71
        thread.start();
 
72
      }
 
73
      else {
 
74
        sendChannelOpen();
 
75
      }
 
76
    }
 
77
    catch(Exception e){
 
78
      io.close();
 
79
      io=null;
 
80
      Channel.del(this);
 
81
      if (e instanceof JSchException) {
 
82
        throw (JSchException) e;
 
83
      }
 
84
    }
 
85
  }
 
86
 
 
87
  public void run(){
 
88
 
 
89
    try{
 
90
      sendChannelOpen();
 
91
 
 
92
      Buffer buf=new Buffer(rmpsize);
 
93
      Packet packet=new Packet(buf);
 
94
      Session _session=getSession();
 
95
      int i=0;
 
96
 
 
97
      while(isConnected() &&
 
98
            thread!=null && 
 
99
            io!=null && 
 
100
            io.in!=null){
 
101
        i=io.in.read(buf.buffer, 
 
102
                     14, 
 
103
                     buf.buffer.length-14
 
104
                     -Session.buffer_margin
 
105
                     );
 
106
        if(i<=0){
 
107
          eof();
 
108
          break;
 
109
        }
 
110
        packet.reset();
 
111
        buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
 
112
        buf.putInt(recipient);
 
113
        buf.putInt(i);
 
114
        buf.skip(i);
 
115
        synchronized(this){
 
116
          if(close)
 
117
            break;
 
118
          _session.write(packet, this, i);
 
119
        }
 
120
      }
 
121
    }
 
122
    catch(Exception e){
 
123
    }
 
124
    disconnect();
 
125
  }
 
126
 
 
127
  public void setInputStream(InputStream in){
 
128
    io.setInputStream(in);
 
129
  }
 
130
  public void setOutputStream(OutputStream out){
 
131
    io.setOutputStream(out);
 
132
  }
 
133
 
 
134
  public void setHost(String host){this.host=host;}
 
135
  public void setPort(int port){this.port=port;}
 
136
  public void setOrgIPAddress(String foo){this.originator_IP_address=foo;}
 
137
  public void setOrgPort(int foo){this.originator_port=foo;}
 
138
 
 
139
  protected Packet genChannelOpenPacket(){
 
140
    Buffer buf = new Buffer(50 + // 6 + 4*8 + 12
 
141
                            host.length() + originator_IP_address.length() +
 
142
                            Session.buffer_margin);
 
143
    Packet packet = new Packet(buf);
 
144
    // byte   SSH_MSG_CHANNEL_OPEN(90)
 
145
    // string channel type         //
 
146
    // uint32 sender channel       // 0
 
147
    // uint32 initial window size  // 0x100000(65536)
 
148
    // uint32 maxmum packet size   // 0x4000(16384)
 
149
    packet.reset();
 
150
    buf.putByte((byte)90);
 
151
    buf.putString(this.type);
 
152
    buf.putInt(id);
 
153
    buf.putInt(lwsize);
 
154
    buf.putInt(lmpsize);
 
155
    buf.putString(Util.str2byte(host));
 
156
    buf.putInt(port);
 
157
    buf.putString(Util.str2byte(originator_IP_address));
 
158
    buf.putInt(originator_port);
 
159
    return packet;
 
160
  }
 
161
}