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

« back to all changes in this revision

Viewing changes to src/com/jcraft/jsch/ChannelForwardedTCPIP.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-12-04 03:11:50 UTC
  • mfrom: (2.1.4 edgy)
  • Revision ID: james.westby@ubuntu.com-20061204031150-kg8n1b0utae11phk
Tags: 0.1.28-2
* Upload to unstable.
* Fix typo in copyright. Closes: #391988.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*-mode:java; c-basic-offset:2; -*- */
 
1
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2
2
/*
3
 
Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved.
 
3
Copyright (c) 2002,2003,2004,2005,2006 ymnk, JCraft,Inc. All rights reserved.
4
4
 
5
5
Redistribution and use in source and binary forms, with or without
6
6
modification, are permitted provided that the following conditions are met:
32
32
import java.net.*;
33
33
import java.io.*;
34
34
 
35
 
class ChannelForwardedTCPIP extends Channel{
 
35
public class ChannelForwardedTCPIP extends Channel{
36
36
 
37
37
  static java.util.Vector pool=new java.util.Vector();
38
38
 
39
39
  static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
 
40
//static private final int LOCAL_WINDOW_SIZE_MAX=0x100000;
40
41
  static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
41
42
 
42
 
  String host;
 
43
  SocketFactory factory=null;
 
44
  String target;
43
45
  int lport;
44
46
  int rport;
45
47
 
52
54
 
53
55
  void init (){
54
56
    try{ 
55
 
      Socket socket = new Socket(host, lport);
56
 
      socket.setTcpNoDelay(true);
57
57
      io=new IO();
58
 
      io.setInputStream(socket.getInputStream());
59
 
      io.setOutputStream(socket.getOutputStream());
 
58
      if(lport==-1){
 
59
        Class c=Class.forName(target);
 
60
        ForwardedTCPIPDaemon daemon=(ForwardedTCPIPDaemon)c.newInstance();
 
61
        daemon.setChannel(this);
 
62
        Object[] foo=getPort(session, rport);
 
63
        daemon.setArg((Object[])foo[3]);
 
64
        new Thread(daemon).start();
 
65
        connected=true;
 
66
        return;
 
67
      }
 
68
      else{
 
69
        Socket socket=(factory==null) ? 
 
70
          new Socket(target, lport) : 
 
71
          factory.createSocket(target, lport);
 
72
        socket.setTcpNoDelay(true);
 
73
        io.setInputStream(socket.getInputStream());
 
74
        io.setOutputStream(socket.getOutputStream());
 
75
        connected=true;
 
76
      }
60
77
    }
61
78
    catch(Exception e){
62
79
      System.out.println(e);
64
81
  }
65
82
 
66
83
  public void run(){
67
 
    thread=this;
 
84
    thread=Thread.currentThread();
68
85
    Buffer buf=new Buffer(rmpsize);
69
86
    Packet packet=new Packet(buf);
70
87
    int i=0;
71
88
    try{
72
89
      while(thread!=null && io!=null && io.in!=null){
73
90
        i=io.in.read(buf.buffer, 
74
 
                     14, 
75
 
                     buf.buffer.length-14
76
 
                     -16 -20 // padding and mac
77
 
                     );
78
 
        if(i<=0){
79
 
          eof();
 
91
                     14, 
 
92
                     buf.buffer.length-14
 
93
                     -32 -20 // padding and mac
 
94
                     );
 
95
        if(i<=0){
 
96
          eof();
80
97
          break;
81
 
        }
 
98
        }
82
99
        packet.reset();
83
 
        if(close)break;
 
100
        if(close)break;
84
101
        buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
85
102
        buf.putInt(recipient);
86
103
        buf.putInt(i);
87
104
        buf.skip(i);
88
 
        session.write(packet, this, i);
 
105
        session.write(packet, this, i);
89
106
      }
90
107
    }
91
108
    catch(Exception e){
95
112
    //eof();
96
113
    disconnect();
97
114
  }
98
 
  public void disconnect(){
99
 
    close();
100
 
    thread=null;
101
 
    try{
102
 
      if(io!=null){
103
 
      if(io.in!=null)io.in.close();
104
 
      if(io.out!=null)io.out.close();
105
 
      }
106
 
    }
107
 
    catch(Exception e){
108
 
      //e.printStackTrace();
109
 
    }
110
 
    io=null;
111
 
    Channel.del(this);
112
 
  }
113
115
  void getData(Buffer buf){
114
116
    setRecipient(buf.getInt());
115
117
    setRemoteWindowSize(buf.getInt());
132
134
        if(foo[0]!=session) continue;
133
135
        if(((Integer)foo[1]).intValue()!=port) continue;
134
136
        this.rport=port;
135
 
        this.host=(String)foo[2];
136
 
        this.lport=((Integer)foo[3]).intValue();
 
137
        this.target=(String)foo[2];
 
138
        if(foo[3]==null || (foo[3] instanceof Object[])){ this.lport=-1; }
 
139
        else{ this.lport=((Integer)foo[3]).intValue(); }
 
140
        if(foo.length>=5){
 
141
          this.factory=((SocketFactory)foo[4]);
 
142
        }
137
143
        break;
138
144
      }
139
 
      if(host==null){
140
 
        System.out.println("??");
 
145
      if(target==null){
 
146
        System.out.println("??");
141
147
      }
142
148
    }
143
149
  }
144
150
 
145
151
  static Object[] getPort(Session session, int rport){
146
152
    synchronized(pool){
147
 
      Object[] foo=null;
148
153
      for(int i=0; i<pool.size(); i++){
149
154
        Object[] bar=(Object[])(pool.elementAt(i));
150
155
        if(bar[0]!=session) continue;
151
156
        if(((Integer)bar[1]).intValue()!=rport) continue;
152
 
        return bar;
 
157
        return bar;
153
158
      }
154
159
      return null;
155
160
    }
161
166
      for(int i=0; i<pool.size(); i++){
162
167
        Object[] bar=(Object[])(pool.elementAt(i));
163
168
        if(bar[0]!=session) continue;
164
 
        foo.addElement(bar[1]+":"+bar[2]+":"+bar[3]);
 
169
        if(bar[3]==null){ foo.addElement(bar[1]+":"+bar[2]+":"); }
 
170
        else{ foo.addElement(bar[1]+":"+bar[2]+":"+bar[3]); }
165
171
      }
166
172
    }
167
173
    String[] bar=new String[foo.size()];
171
177
    return bar;
172
178
  }
173
179
 
174
 
  static void addPort(Session session, int port, String host, int lport) throws JSchException{
 
180
  static void addPort(Session session, int port, String target, int lport, SocketFactory factory) throws JSchException{
 
181
    synchronized(pool){
 
182
      if(getPort(session, port)!=null){
 
183
        throw new JSchException("PortForwardingR: remote port "+port+" is already registered.");
 
184
      }
 
185
      Object[] foo=new Object[5];
 
186
      foo[0]=session; foo[1]=new Integer(port);
 
187
      foo[2]=target; foo[3]=new Integer(lport);
 
188
      foo[4]=factory;
 
189
      pool.addElement(foo);
 
190
    }
 
191
  }
 
192
  static void addPort(Session session, int port, String daemon, Object[] arg) throws JSchException{
175
193
    synchronized(pool){
176
194
      if(getPort(session, port)!=null){
177
195
        throw new JSchException("PortForwardingR: remote port "+port+" is already registered.");
178
196
      }
179
197
      Object[] foo=new Object[4];
180
198
      foo[0]=session; foo[1]=new Integer(port);
181
 
      foo[2]=host; foo[3]=new Integer(lport);
 
199
      foo[2]=daemon; foo[3]=arg;
182
200
      pool.addElement(foo);
183
201
    }
184
202
  }
226
244
    synchronized(pool){
227
245
      rport=new int[pool.size()];
228
246
      for(int i=0; i<pool.size(); i++){
229
 
        Object[] bar=(Object[])(pool.elementAt(i));
230
 
        if(bar[0]==session) {
231
 
          rport[count++]=((Integer)bar[1]).intValue();
232
 
        }
 
247
        Object[] bar=(Object[])(pool.elementAt(i));
 
248
        if(bar[0]==session) {
 
249
          rport[count++]=((Integer)bar[1]).intValue();
 
250
        }
233
251
      }
234
252
    }
235
253
    for(int i=0; i<count; i++){
236
254
      delPort(session, rport[i]);
237
255
    }
238
256
  }
 
257
  public int getRemotePort(){return rport;}
 
258
  void setSocketFactory(SocketFactory factory){
 
259
    this.factory=factory;
 
260
  }
239
261
}