~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NSch/NSch/ChannelDirectTCPIP.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
 
3
 
 
4
Redistribution and use in source and binary forms, with or without
 
5
modification, are permitted provided that the following conditions are met:
 
6
 
 
7
  1. Redistributions of source code must retain the above copyright notice,
 
8
     this list of conditions and the following disclaimer.
 
9
 
 
10
  2. Redistributions in binary form must reproduce the above copyright 
 
11
     notice, this list of conditions and the following disclaimer in 
 
12
     the documentation and/or other materials provided with the distribution.
 
13
 
 
14
  3. The names of the authors may not be used to endorse or promote products
 
15
     derived from this software without specific prior written permission.
 
16
 
 
17
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 
18
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 
19
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
 
20
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
 
21
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
22
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 
23
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
24
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
25
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 
26
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
27
 
 
28
This code is based on jsch (http://www.jcraft.com/jsch).
 
29
All credit should go to the authors of jsch.
 
30
*/
 
31
 
 
32
using System;
 
33
using NSch;
 
34
using Sharpen;
 
35
 
 
36
namespace NSch
 
37
{
 
38
        public class ChannelDirectTCPIP : Channel
 
39
        {
 
40
                private const int LOCAL_WINDOW_SIZE_MAX = unchecked((int)(0x20000));
 
41
 
 
42
                private const int LOCAL_MAXIMUM_PACKET_SIZE = unchecked((int)(0x4000));
 
43
 
 
44
                internal string host;
 
45
 
 
46
                internal int port;
 
47
 
 
48
                internal string originator_IP_address = "127.0.0.1";
 
49
 
 
50
                internal int originator_port = 0;
 
51
 
 
52
                public ChannelDirectTCPIP() : base()
 
53
                {
 
54
                        SetLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
 
55
                        SetLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
 
56
                        SetLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
 
57
                }
 
58
 
 
59
                internal override void Init()
 
60
                {
 
61
                        try
 
62
                        {
 
63
                                io = new IO();
 
64
                        }
 
65
                        catch (Exception e)
 
66
                        {
 
67
                                System.Console.Error.WriteLine(e);
 
68
                        }
 
69
                }
 
70
 
 
71
                /// <exception cref="NSch.JSchException"></exception>
 
72
                public override void Connect()
 
73
                {
 
74
                        try
 
75
                        {
 
76
                                Session _session = GetSession();
 
77
                                if (!_session.IsConnected())
 
78
                                {
 
79
                                        throw new JSchException("session is down");
 
80
                                }
 
81
                                Buffer buf = new Buffer(150);
 
82
                                Packet packet = new Packet(buf);
 
83
                                // send
 
84
                                // byte   SSH_MSG_CHANNEL_OPEN(90)
 
85
                                // string channel type         //
 
86
                                // uint32 sender channel       // 0
 
87
                                // uint32 initial window size  // 0x100000(65536)
 
88
                                // uint32 maxmum packet size   // 0x4000(16384)
 
89
                                packet.Reset();
 
90
                                buf.PutByte(unchecked((byte)90));
 
91
                                buf.PutString(Util.Str2byte("direct-tcpip"));
 
92
                                buf.PutInt(id);
 
93
                                buf.PutInt(lwsize);
 
94
                                buf.PutInt(lmpsize);
 
95
                                buf.PutString(Util.Str2byte(host));
 
96
                                buf.PutInt(port);
 
97
                                buf.PutString(Util.Str2byte(originator_IP_address));
 
98
                                buf.PutInt(originator_port);
 
99
                                _session.Write(packet);
 
100
                                int retry = 1000;
 
101
                                try
 
102
                                {
 
103
                                        while (this.GetRecipient() == -1 && _session.IsConnected() && retry > 0 && !eof_remote
 
104
                                                )
 
105
                                        {
 
106
                                                //Thread.sleep(500);
 
107
                                                Sharpen.Thread.Sleep(50);
 
108
                                                retry--;
 
109
                                        }
 
110
                                }
 
111
                                catch (Exception)
 
112
                                {
 
113
                                }
 
114
                                if (!_session.IsConnected())
 
115
                                {
 
116
                                        throw new JSchException("session is down");
 
117
                                }
 
118
                                if (retry == 0 || this.eof_remote)
 
119
                                {
 
120
                                        throw new JSchException("channel is not opened.");
 
121
                                }
 
122
                                connected = true;
 
123
                                if (io.@in != null)
 
124
                                {
 
125
                                        thread = new Sharpen.Thread(this);
 
126
                                        thread.SetName("DirectTCPIP thread " + _session.GetHost());
 
127
                                        if (_session.daemon_thread)
 
128
                                        {
 
129
                                                thread.SetDaemon(_session.daemon_thread);
 
130
                                        }
 
131
                                        thread.Start();
 
132
                                }
 
133
                        }
 
134
                        catch (Exception e)
 
135
                        {
 
136
                                io.Close();
 
137
                                io = null;
 
138
                                Channel.Del(this);
 
139
                                if (e is JSchException)
 
140
                                {
 
141
                                        throw (JSchException)e;
 
142
                                }
 
143
                        }
 
144
                }
 
145
 
 
146
                public override void Run()
 
147
                {
 
148
                        Buffer buf = new Buffer(rmpsize);
 
149
                        Packet packet = new Packet(buf);
 
150
                        int i = 0;
 
151
                        try
 
152
                        {
 
153
                                Session _session = GetSession();
 
154
                                while (IsConnected() && thread != null && io != null && io.@in != null)
 
155
                                {
 
156
                                        i = io.@in.Read(buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20);
 
157
                                        // padding and mac
 
158
                                        if (i <= 0)
 
159
                                        {
 
160
                                                Eof();
 
161
                                                break;
 
162
                                        }
 
163
                                        if (close)
 
164
                                        {
 
165
                                                break;
 
166
                                        }
 
167
                                        packet.Reset();
 
168
                                        buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
 
169
                                        buf.PutInt(recipient);
 
170
                                        buf.PutInt(i);
 
171
                                        buf.Skip(i);
 
172
                                        _session.Write(packet, this, i);
 
173
                                }
 
174
                        }
 
175
                        catch (Exception)
 
176
                        {
 
177
                        }
 
178
                        Disconnect();
 
179
                }
 
180
 
 
181
                //System.err.println("connect end");
 
182
                public override void SetInputStream(InputStream @in)
 
183
                {
 
184
                        io.SetInputStream(@in);
 
185
                }
 
186
 
 
187
                public override void SetOutputStream(OutputStream @out)
 
188
                {
 
189
                        io.SetOutputStream(@out);
 
190
                }
 
191
 
 
192
                public virtual void SetHost(string host)
 
193
                {
 
194
                        this.host = host;
 
195
                }
 
196
 
 
197
                public virtual void SetPort(int port)
 
198
                {
 
199
                        this.port = port;
 
200
                }
 
201
 
 
202
                public virtual void SetOrgIPAddress(string foo)
 
203
                {
 
204
                        this.originator_IP_address = foo;
 
205
                }
 
206
 
 
207
                public virtual void SetOrgPort(int foo)
 
208
                {
 
209
                        this.originator_port = foo;
 
210
                }
 
211
        }
 
212
}