Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2498 - 1
/*
2
    Copyright © 2002, The KPD-Team
3
    All rights reserved.
4
    http://www.mentalis.org/
5
 
6
  Redistribution and use in source and binary forms, with or without
7
  modification, are permitted provided that the following conditions
8
  are met:
9
 
10
    - Redistributions of source code must retain the above copyright
11
       notice, this list of conditions and the following disclaimer.
12
 
13
    - Neither the name of the KPD-Team, nor the names of its contributors
14
       may be used to endorse or promote products derived from this
15
       software without specific prior written permission.
16
 
17
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21
  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28
  OF THE POSSIBILITY OF SUCH DAMAGE.
29
*/
30
 
31
using System;
32
using System.Net;
33
using System.Net.Sockets;
34
 
35
namespace Org.Mentalis.Network.ProxySocket {
36
        /// <summary>
37
        /// References the callback method to be called when the protocol negotiation is completed.
38
        /// </summary>
39
        internal delegate void HandShakeComplete(Exception error);
40
        /// <summary>
41
        /// Implements a specific version of the SOCKS protocol. This is an abstract class; it must be inherited.
42
        /// </summary>
43
        internal abstract class SocksHandler {
44
                /// <summary>
45
                /// Initilizes a new instance of the SocksHandler class.
46
                /// </summary>
47
                /// <param name="server">The socket connection with the proxy server.</param>
48
                /// <param name="user">The username to use when authenticating with the server.</param>
49
                /// <exception cref="ArgumentNullException"><c>server</c> -or- <c>user</c> is null.</exception>
50
                public SocksHandler(Socket server, string user) {
51
                        Server = server;
52
                        Username = user;
53
                }
54
                /// <summary>
55
                /// Converts a port number to an array of bytes.
56
                /// </summary>
57
                /// <param name="port">The port to convert.</param>
58
                /// <returns>An array of two bytes that represents the specified port.</returns>
59
                protected byte[] PortToBytes(int port) {
60
                        byte [] ret = new byte[2];
61
                        ret[0] = (byte)(port / 256);
62
                        ret[1] = (byte)(port % 256);
63
                        return ret;
64
                }
65
 
66
                /// <summary>
67
                /// Reads a specified number of bytes from the Server socket.
68
                /// </summary>
69
                /// <param name="count">The number of bytes to return.</param>
70
                /// <returns>An array of bytes.</returns>
71
                /// <exception cref="ArgumentException">The number of bytes to read is invalid.</exception>
72
                /// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception>
73
                /// <exception cref="ObjectDisposedException">The Socket has been closed.</exception>
74
                protected byte[] ReadBytes(int count) {
75
                        if (count <= 0)
76
                                throw new ArgumentException();
77
                        byte[] buffer = new byte[count];
78
                        int received = 0;
79
                        while(received != count) {
80
                                received += Server.Receive(buffer, received, count - received, SocketFlags.None);
81
                        }
82
                        return buffer;
83
                }
84
                /// <summary>
85
                /// Gets or sets the socket connection with the proxy server.
86
                /// </summary>
87
                /// <value>A Socket object that represents the connection with the proxy server.</value>
88
                /// <exception cref="ArgumentNullException">The specified value is null.</exception>
89
                protected Socket Server {
90
                        get {
91
                                return m_Server;
92
                        }
93
                        set {
94
                                if (value == null)
95
                                        throw new ArgumentNullException();
96
                                m_Server = value;
97
                        }
98
                }
99
                /// <summary>
100
                /// Gets or sets the username to use when authenticating with the proxy server.
101
                /// </summary>
102
                /// <value>A string that holds the username to use when authenticating with the proxy server.</value>
103
                /// <exception cref="ArgumentNullException">The specified value is null.</exception>
104
                protected string Username {
105
                        get {
106
                                return m_Username;
107
                        }
108
                        set {
109
                                if (value == null)
110
                                        throw new ArgumentNullException();
111
                                m_Username = value;
112
                        }
113
                }
114
                /// <summary>
115
                /// Gets or sets the return value of the BeginConnect call.
116
                /// </summary>
117
                /// <value>An IAsyncProxyResult object that is the return value of the BeginConnect call.</value>
118
                protected IAsyncProxyResult AsyncResult {
119
                        get {
120
                                return m_AsyncResult;
121
                        }
122
                        set {
123
                                m_AsyncResult = value;
124
                        }
125
                }
126
                /// <summary>
127
                /// Gets or sets a byte buffer.
128
                /// </summary>
129
                /// <value>An array of bytes.</value>
130
                protected byte[] Buffer {
131
                        get {
132
                                return m_Buffer;
133
                        }
134
                        set {
135
                                m_Buffer = value;
136
                        }
137
                }
138
                /// <summary>
139
                /// Gets or sets the number of bytes that have been received from the remote proxy server.
140
                /// </summary>
141
                /// <value>An integer that holds the number of bytes that have been received from the remote proxy server.</value>
142
                protected int Received {
143
                        get {
144
                                return m_Received;
145
                        }
146
                        set {
147
                                m_Received = value;
148
                        }
149
                }
150
                // private variables
151
                /// <summary>Holds the value of the Server property.</summary>
152
                private Socket m_Server;
153
                /// <summary>Holds the value of the Username property.</summary>
154
                private string m_Username;
155
                /// <summary>Holds the value of the AsyncResult property.</summary>
156
                private IAsyncProxyResult m_AsyncResult;
157
                /// <summary>Holds the value of the Buffer property.</summary>
158
                private byte[] m_Buffer;
159
                /// <summary>Holds the value of the Received property.</summary>
160
                private int m_Received;
161
                /// <summary>Holds the address of the method to call when the SOCKS protocol has been completed.</summary>
162
                protected HandShakeComplete ProtocolComplete;
163
                /// <summary>
164
                /// Starts negotiating with a SOCKS proxy server.
165
                /// </summary>
166
                /// <param name="host">The remote server to connect to.</param>
167
                /// <param name="port">The remote port to connect to.</param>
168
                public abstract void Negotiate(string host, int port);
169
                /// <summary>
170
                /// Starts negotiating with a SOCKS proxy server.
171
                /// </summary>
172
                /// <param name="remoteEP">The remote endpoint to connect to.</param>
173
                public abstract void Negotiate(IPEndPoint remoteEP);
174
                /// <summary>
175
                /// Starts negotiating asynchronously with a SOCKS proxy server.
176
                /// </summary>
177
                /// <param name="remoteEP">An IPEndPoint that represents the remote device. </param>
178
                /// <param name="callback">The method to call when the connection has been established.</param>
179
                /// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param>
180
                /// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
181
                public abstract IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, IPEndPoint proxyEndPoint);
182
                /// <summary>
183
                /// Starts negotiating asynchronously with a SOCKS proxy server.
184
                /// </summary>
185
                /// <param name="host">The remote server to connect to.</param>
186
                /// <param name="port">The remote port to connect to.</param>
187
                /// <param name="callback">The method to call when the connection has been established.</param>
188
                /// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param>
189
                /// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
190
                public abstract IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, IPEndPoint proxyEndPoint);
191
        }
192
}