-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTcpTransport.cs
176 lines (145 loc) · 4.82 KB
/
TcpTransport.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#region
using System;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Xml;
/**/
#endregion
/*
using OpenSSL;
using OpenSSL.Core;
using OpenSSL.X509;*/
namespace EppLib
{
/// <summary>
/// Encapsulates the TCP transport
/// </summary>
public class TcpTransport : IDisposable
{
private readonly X509Certificate _clientCertificate;
private readonly X509CertificateCollection _clientCertificateCollection;
private readonly string _eppRegistryCom;
private readonly bool _loggingEnabled;
private readonly int _port;
private readonly int _readTimeout;
private readonly int _writeTimeout;
private SslStream _stream;
public TcpTransport(string host, int port, X509Certificate clientCertificate,
X509CertificateCollection clientCertificateCollection, bool loggingEnabled = false,
int readTimeout = Timeout.Infinite, int writeTimeout = Timeout.Infinite)
{
_eppRegistryCom = host;
_port = port;
_readTimeout = readTimeout;
_writeTimeout = writeTimeout;
_loggingEnabled = loggingEnabled;
_clientCertificate = clientCertificate;
_clientCertificateCollection = clientCertificateCollection;
}
public void Dispose()
{
if (_stream != null)
{
_stream.Dispose();
}
}
/// <summary>
/// Connect to the registry end point
/// </summary>
public void Connect(SslProtocols sslProtocols)
{
var client = new TcpClient(_eppRegistryCom, _port);
_stream = new SslStream(client.GetStream(), false, ValidateServerCertificate)
{
ReadTimeout = _readTimeout,
WriteTimeout = _writeTimeout
};
if (_clientCertificate != null)
{
var clientCertificates = new X509CertificateCollection {_clientCertificate};
_stream.AuthenticateAsClient(_eppRegistryCom, clientCertificates, sslProtocols, false);
}
else
{
if (_clientCertificateCollection != null)
{
_stream.AuthenticateAsClient(_eppRegistryCom, _clientCertificateCollection, sslProtocols, false);
}
else
{
_stream.AuthenticateAsClient(_eppRegistryCom);
}
}
}
private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
}
/// <summary>
/// Disconnect from the registry end point
/// </summary>
public void Disconnect()
{
_stream.Close();
}
/// <summary>
/// Read the command response
/// </summary>
/// <returns></returns>
public byte[] Read()
{
var lenghtBytes = new byte[4];
var read = 0;
while (read < 4)
{
read = read + _stream.Read(lenghtBytes, read, 4 - read);
}
Array.Reverse(lenghtBytes);
var length = BitConverter.ToInt32(lenghtBytes, 0) - 4;
if (_loggingEnabled)
{
Debug.Log("Reading " + length + " bytes.");
}
var bytes = new byte[length];
var returned = 0;
while (returned != length)
{
returned += _stream.Read(bytes, returned, length - returned);
}
if (_loggingEnabled)
{
Debug.Log("****************** Received ******************");
Debug.Log(bytes);
}
return bytes;
}
/// <summary>
/// Writes an XmlDocument to the transport stream
/// </summary>
/// <param name="s"></param>
public void Write(XmlDocument s)
{
var bytes = GetBytes(s);
var lenght = bytes.Length + 4;
var lenghtBytes = BitConverter.GetBytes(lenght);
Array.Reverse(lenghtBytes);
_stream.Write(lenghtBytes, 0, 4);
_stream.Write(bytes, 0, bytes.Length);
_stream.Flush();
if (_loggingEnabled)
{
Debug.Log("****************** Sending ******************");
Debug.Log(bytes);
}
}
private static byte[] GetBytes(XmlDocument s)
{
return Encoding.UTF8.GetBytes(s.OuterXml);
}
}
}