source: other-projects/trunk/gs3-release-maker/tasks/sshtaskdef/src/mindbright/ssh/SSHListenChannel.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 4.1 KB
Line 
1/******************************************************************************
2 *
3 * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
4 * www.mindbright.se, [email protected]
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 *****************************************************************************
17 * $Author: mats $
18 * $Date: 2000/04/04 09:34:12 $
19 * $Name: rel1-2-1 $
20 *****************************************************************************/
21package mindbright.ssh;
22
23import java.net.*;
24import java.io.*;
25
26public class SSHListenChannel extends SSHChannel {
27 static boolean allowRemoteConnect = false;
28
29 static final int LISTEN_QUEUE_SIZE = 16;
30
31 SSHChannelController controller;
32
33 ServerSocket listenSocket;
34 String remoteHost;
35 int remotePort;
36 InetAddress localHost1;
37 InetAddress localHost2;
38
39 boolean temporaryListener;
40
41 public SSHListenChannel(String localHost, int localPort,
42 String remoteHost, int remotePort,
43 SSHChannelController controller)
44 throws IOException {
45 super(SSH.LISTEN_CHAN_NUM);
46 this.controller = controller;
47 try {
48 this.listenSocket = new ServerSocket(localPort, LISTEN_QUEUE_SIZE,
49 InetAddress.getByName(localHost));
50 } catch (IOException e) {
51 throw new IOException("Error setting up local forward on port " +
52 localPort + ", " + e.getMessage());
53 }
54 this.remoteHost = remoteHost;
55 this.remotePort = remotePort;
56
57 this.localHost1 = InetAddress.getLocalHost();
58 this.localHost2 = InetAddress.getByName("127.0.0.1");
59 }
60
61 public int getListenPort() {
62 return listenSocket.getLocalPort();
63 }
64
65 public String getListenHost() {
66 return listenSocket.getInetAddress().getHostAddress();
67 }
68
69 public static synchronized void setAllowRemoteConnect(boolean val) {
70 allowRemoteConnect = val;
71 }
72
73 static synchronized boolean getAllowRemoteConnect() {
74 return allowRemoteConnect;
75 }
76
77 public SSHTunnel newTunnel(Socket ioSocket, int channelId, int remoteChannelId,
78 SSHChannelController controller) throws IOException {
79 return new SSHTunnel(ioSocket, channelId, remoteChannelId, controller);
80 }
81
82 public void setTemporaryListener(boolean val) {
83 temporaryListener = val;
84 }
85
86 public void serviceLoop() throws IOException {
87
88 SSH.log("Starting listen-chan: " + listenSocket.getLocalPort());
89
90 try {
91 for(;;) {
92 Socket fwdSocket = listenSocket.accept();
93
94 if(!getAllowRemoteConnect() &&
95 !(fwdSocket.getInetAddress().equals(localHost1) ||
96 fwdSocket.getInetAddress().equals(localHost2))) {
97 controller.alert("Remote connect to local tunnel rejected: " + fwdSocket.getInetAddress());
98 fwdSocket.close();
99 continue;
100 }
101
102 SSHPduOutputStream respPdu = new SSHPduOutputStream(SSH.MSG_PORT_OPEN, controller.sndCipher);
103
104 int newChan = controller.newChannelId();
105 SSHTunnel tunnel = newTunnel(fwdSocket,
106 newChan, SSH.UNKNOWN_CHAN_NUM,
107 controller);
108 controller.addTunnel(tunnel);
109 tunnel.setRemoteDesc(remoteHost + ":" + remotePort);
110
111 respPdu.writeInt(newChan);
112 respPdu.writeString(remoteHost);
113 respPdu.writeInt(remotePort);
114
115 SSH.log("got connect for: " + remoteHost + " : " + remotePort + ", " + newChan);
116
117 //
118 // !!! TODO: check this !!! if(controller.haveHostInFwdOpen())
119 //
120 respPdu.writeString(fwdSocket.getInetAddress().getHostAddress());
121
122 controller.transmit(respPdu);
123
124 if(temporaryListener)
125 break;
126 }
127 } finally {
128 listenSocket.close();
129 }
130 }
131
132 public void forceClose() {
133 if(isAlive()) {
134 stop();
135 }
136 try {
137 listenSocket.close();
138 } catch (IOException e) {
139 }
140 }
141
142}
Note: See TracBrowser for help on using the repository browser.