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: 1999/07/15 14:53:27 $
|
---|
19 | * $Name: rel1-2-1 $
|
---|
20 | *****************************************************************************/
|
---|
21 | package mindbright.ssh;
|
---|
22 |
|
---|
23 | import java.net.*;
|
---|
24 | import java.io.*;
|
---|
25 |
|
---|
26 | public class SSHSocketImpl extends SocketImpl {
|
---|
27 |
|
---|
28 | public final static int SO_TIMEOUT = 0x1006; // !!!
|
---|
29 |
|
---|
30 | protected SSHSocketFactory factory;
|
---|
31 |
|
---|
32 | SSHClient client;
|
---|
33 | SSHSocketTunnel tunnel;
|
---|
34 | SSHPduQueue cnQueue;
|
---|
35 |
|
---|
36 | boolean isClosed;
|
---|
37 |
|
---|
38 | protected SSHSocketImpl() {
|
---|
39 | // !!! Used to indicate to create if we have done bind() or not...
|
---|
40 | // (i.e. if we are an implementation of a SSHServerSocket or a SSHSocket)
|
---|
41 | localport = SSHSocketFactory.NOLISTENPORT;
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected void setFactory(SSHSocketFactory factory) {
|
---|
45 | this.factory = factory;
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected void create(boolean stream) {
|
---|
49 | // !!! Not used, but abstract in SocketImpl
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected void create(SSHClient client, boolean acceptor) throws IOException {
|
---|
53 | InetAddress localHost = InetAddress.getLocalHost();
|
---|
54 |
|
---|
55 | this.client = client;
|
---|
56 | client.addRef();
|
---|
57 |
|
---|
58 | if(acceptor) {
|
---|
59 | cnQueue = client.controller.getCnQueue();
|
---|
60 | } else {
|
---|
61 | tunnel = new SSHSocketTunnel(client.controller, this);
|
---|
62 | tunnel.setLocalAddress(localHost);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected void connect(String host, int port) throws IOException {
|
---|
67 | try {
|
---|
68 | address = InetAddress.getByName(host);
|
---|
69 | } catch (Exception e) {
|
---|
70 | address = InetAddress.getLocalHost();
|
---|
71 | }
|
---|
72 |
|
---|
73 | tunnel.connect(host, port);
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected void connect(InetAddress address, int port) throws IOException {
|
---|
77 | connect(address.getHostAddress(), port);
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected void bind(InetAddress host, int port) throws IOException {
|
---|
81 | localport = port;
|
---|
82 | // !!! This is done elsewhere (also, the local address is not important...)
|
---|
83 | // !!! tunnel.setLocalAddress(host);
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected void listen(int backlog) throws IOException {
|
---|
87 | cnQueue.setMaxDepth(backlog);
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected void accept(SocketImpl s) throws IOException {
|
---|
91 | SSHPduOutputStream respPdu;
|
---|
92 | SSHPduInputStream inPdu;
|
---|
93 | SSHSocketImpl aImpl = (SSHSocketImpl)s;
|
---|
94 | int remoteChannel;
|
---|
95 | inPdu = (SSHPduInputStream) cnQueue.getFirst();
|
---|
96 | if(inPdu == null)
|
---|
97 | throw new IOException("Socket closed");
|
---|
98 | remoteChannel = inPdu.readInt();
|
---|
99 | aImpl.tunnel.setRemoteChannelId(remoteChannel);
|
---|
100 | respPdu = new SSHPduOutputStream(SSH.MSG_CHANNEL_OPEN_CONFIRMATION, client.controller.sndCipher);
|
---|
101 | respPdu.writeInt(remoteChannel);
|
---|
102 | respPdu.writeInt(aImpl.tunnel.channelId);
|
---|
103 | client.controller.transmit(respPdu);
|
---|
104 | client.controller.addTunnel(aImpl.tunnel);
|
---|
105 |
|
---|
106 | /* !!!
|
---|
107 | } catch (Exception e) {
|
---|
108 | respPdu = new SSHPduOutputStream(SSH.MSG_CHANNEL_OPEN_FAILURE, client.controller.sndCipher);
|
---|
109 | respPdu.writeInt(remoteChannel);
|
---|
110 | client.controller.transmit(respPdu);
|
---|
111 | throw new IOException(e.getMessage());
|
---|
112 | }
|
---|
113 | */
|
---|
114 | }
|
---|
115 |
|
---|
116 | protected InputStream getInputStream() throws IOException {
|
---|
117 | return tunnel.in;
|
---|
118 | }
|
---|
119 |
|
---|
120 | protected OutputStream getOutputStream() throws IOException {
|
---|
121 | return tunnel.out;
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected int available() throws IOException {
|
---|
125 | return tunnel.available();
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected void close() throws IOException {
|
---|
129 | if(localport == SSHSocketFactory.NOLISTENPORT) {
|
---|
130 | if(tunnel != null && !tunnel.terminated())
|
---|
131 | tunnel.close();
|
---|
132 | } else {
|
---|
133 | factory.closePseudoUser(client, this);
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | protected void finalize() throws IOException {
|
---|
138 | if(!isClosed)
|
---|
139 | close();
|
---|
140 | }
|
---|
141 |
|
---|
142 | public void setOption(int optID, Object value) throws SocketException {
|
---|
143 | throw new SocketException("Not implemented");
|
---|
144 | }
|
---|
145 |
|
---|
146 | public Object getOption(int optID) throws SocketException {
|
---|
147 | throw new SocketException("Not implemented");
|
---|
148 | }
|
---|
149 |
|
---|
150 | protected InetAddress getInetAddress() {
|
---|
151 | return address;
|
---|
152 | }
|
---|
153 |
|
---|
154 | protected int getPort() {
|
---|
155 | return port;
|
---|
156 | }
|
---|
157 |
|
---|
158 | protected int getLocalPort() {
|
---|
159 | return localport;
|
---|
160 | }
|
---|
161 |
|
---|
162 | }
|
---|