File: TcpConnection.cs

package info (click to toggle)
virtuoso-opensource 7.2.12%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 299,308 kB
  • sloc: ansic: 655,054; sql: 508,209; xml: 269,573; java: 84,064; javascript: 79,847; cpp: 37,662; sh: 32,429; cs: 25,702; php: 12,690; yacc: 11,666; lex: 7,933; makefile: 7,308; jsp: 4,523; awk: 1,719; perl: 1,013; ruby: 1,003; python: 326
file content (285 lines) | stat: -rw-r--r-- 8,409 bytes parent folder | download | duplicates (2)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//  
// $Id$
//
//  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
//  project.
//  
//  Copyright (C) 1998-2024 OpenLink Software
//  
//  This project is free software; you can redistribute it and/or modify it
//  under the terms of the GNU General Public License as published by the
//  Free Software Foundation; only version 2 of the License, dated June 1991.
//  
//  This program is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License along
//  with this program; if not, write to the Free Software Foundation, Inc.,
//  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//  
//  
//
//

using System;
using System.Collections;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;

#if ODBC_CLIENT
namespace OpenLink.Data.VirtuosoOdbcClient
#elif CLIENT
namespace OpenLink.Data.VirtuosoClient
#else
namespace OpenLink.Data.Virtuoso
#endif
{
	internal class TcpConnection : ManagedConnection
	{
		private static byte[] thePass = Encoding.ASCII.GetBytes ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
		private static Random rnd = new Random(unchecked((int) (DateTime.Now.Ticks)));

		private TcpSession session = null;

		internal override ISession Session
		{
			get { return session; }
		}

		public override bool IsValid ()
		{
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "TcpConnection.IsValid ()");

			if (session == null || session.IsBroken)
				return false;
			return true;
		}

		public override void Open (ConnectionOptions options)
		{
			Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			bool useRoundRobin = options.RoundRobin;
			int hostIndex = 0;
			int startIndex = 0;

	                charset_utf8 = (options.Charset != null && options.Charset.ToUpper() == "UTF-8");

			ArrayList ds_list = options.DataSourceList;
			if (ds_list != null)
			{
				if (ds_list.Count <= 1)
					useRoundRobin = false;

				if (ds_list.Count > 1 && useRoundRobin)
					startIndex = hostIndex = rnd.Next(ds_list.Count);

				while(true)
				{
			        	try {
			        		if (ds_list.Count == 0)
			        		{
							socket.Connect (GetEndPoint (null));
						}
						else
						{
							socket.Connect (GetEndPoint ((string)ds_list[hostIndex]));
						}
			        	        break;
			        	} catch (SocketException e) {
			        		hostIndex++;
			        		if (useRoundRobin)
			        		{
			        			if (ds_list.Count == hostIndex)
			        				hostIndex = 0;
			        			if (hostIndex == startIndex)
			        				throw e;
			        		}
			        		else if (ds_list.Count == hostIndex) // Failover mode last rec
			        		{
			        			throw e;
			        		}
			        	}
				}
			}

			try
			{
				session = new TcpSession (this, socket);
                		socket.NoDelay = true;

#if MONO
				Future future = new Future (Service.CallerId, new object[] { null });
#else
				Future future = new Future (Service.CallerId, (object) null); // not object[]
#endif
				future.SendRequest (session, options.ConnectionTimeout);
				object[] results = (object[]) future.GetResultSerial (session);
				peer = (string) results[1];

				object[] idOpts = null;
				if (results.Length > 2)
					idOpts = (object[]) results[2];
				int pwdClearCode = GetConnectionOption (idOpts, "SQL_ENCRYPTION_ON_PASSWORD", -1);
				Debug.WriteLineIf (Switch.Enabled, "pwdClearCode: " + pwdClearCode);

				string user = options.UserId;
				string password = null;
				if (pwdClearCode == 1)
					password = options.Password;
				else if (pwdClearCode == 2)
					password = MagicEncrypt (user, options.Password);
				else
					password = Digest (user, options.Password, peer);

				object[] info = new object[6];
				info[0] = ".NET Application";
				info[1] = 0;
				info[2] = Environment.MachineName;
				info[3] = ".NET";
				info[4] = options.Charset != null ? options.Charset.ToUpper () : "";
				info[5] = 0;
				future = new Future (Service.Connect, user, password, Values.VERSION, info);
				future.SendRequest (session, options.ConnectionTimeout);
				results = future.GetResultSerial (session) as object[];
				if (results == null)
					throw new SystemException ("Login failed.");
				switch ((AnswerTag) results[0])
				{
				case AnswerTag.QA_LOGIN:
					SetConnectionOptions (results);
					break;

				case AnswerTag.QA_ERROR:
					throw new SystemException (results[1].ToString () + " " + results[2].ToString ());

				default:
					throw new SystemException ("Bad login response.");
				}

				if (options.Database != null
					&& options.Database != String.Empty
					&& options.Database != currentCatalog)
					SetCurrentCatalog (options.Database);
			}
			catch (Exception)
			{
				Close ();
				throw;
			}
		}

		public override void Close ()
		{
			if (session != null)
			{
				session.Close ();
				session = null;
			}
			base.Close ();
		}

		private IPEndPoint GetEndPoint (string ds)
		{
			string host;
			int port;

			if (ds == null || ds == String.Empty)
			{
				host = Values.DEFAULT_HOST;
				port = Values.DEFAULT_PORT;
			}
			else
			{
				int colonIndex = ds.IndexOf (':');
				if (colonIndex < 0)
				{
					host = ds;
					port = Values.DEFAULT_PORT;
				}
				else
				{
					host = ds.Substring (0, colonIndex);
					port = Int32.Parse (ds.Substring (colonIndex + 1));
				}
			}
#if !ADONET2
			IPAddress address = Dns.Resolve (host).AddressList[0];
#else			
			IPAddress address = Dns.GetHostEntry (host).AddressList[0];
#endif			
			if (IPAddress.IsLoopback (address) || address.IsIPv6LinkLocal)
				address = IPAddress.Loopback;
			return new IPEndPoint (address, port);
		}

		internal static string Digest (string username, string password, string peername)
		{
			Encoding encoding = Encoding.GetEncoding ("iso-8859-1");
			if (encoding == null)
				throw new SystemException ("Cannot get iso-8859-1 encoding.");

			byte[] usernameBytes = encoding.GetBytes (username == null ? "" : username);
			byte[] passwordBytes = encoding.GetBytes (password == null ? "" : password);
			byte[] peernameBytes = encoding.GetBytes (peername == null ? "" : peername);

			MD5 md5 = new MD5 ();
			md5.Update (peernameBytes);
			md5.Update (usernameBytes);
			md5.Update (passwordBytes);

			byte[] digest = md5.Final ();
			return encoding.GetString (digest);
		}

		internal static string MagicEncrypt (string username, string password)
		{
			Encoding encoding = Encoding.GetEncoding ("iso-8859-1");
			if (encoding == null)
				throw new SystemException ("Cannot get iso-8859-1 encoding.");

			byte[] usernameBytes = encoding.GetBytes (username);
			byte[] passwordBytes = encoding.GetBytes (password);

			byte[] passwordBytes_1 = new byte[passwordBytes.Length + 1];
			passwordBytes_1[0] = 0;
			Array.Copy (passwordBytes, 0, passwordBytes_1, 1, passwordBytes.Length);
			XX_Encrypt (passwordBytes_1, 1, usernameBytes);

			return encoding.GetString (passwordBytes_1);
		}

		private static void XX_Encrypt (byte[] thing, int offset, byte[] username)
		{
			lock (thePass)
			{
				if (thePass[0] == 'x')
				{
					String s1 = "7rLrT7iG3kWWLuSDYdS/KIXO8JF86h12KyCTG1Mh0qxWdSZ6ezHRST0UuGl6xkbMgsXj4+eZbXNyYijRmoaaJm+hQCWSOW+0OHGCnYWB4upxi0Fogdu0gb+q4VFzyUFknEpZPg==";
					String s2 = "PCuJhpWX5eApg2mRs0bvSIdfwSDUa0kjiSdd76ORgXYyhtLbHm4Uq6afLbfROLi5pDpjKVS9Vr9aZo+F3IpyZ6Zn6m/Xf1PRtq3jdseJht4VSduxHrpocKVdRh3LixXKr6Ue6A==";
					byte[] pass1 = Encoding.ASCII.GetBytes (s1);
					byte[] pass2 = Encoding.ASCII.GetBytes (s2);
					for (int inx = 0; inx < pass2.Length; inx++)
					{
						thePass[inx] = (byte) (pass1[inx] ^ pass2[inx]);
						if (thePass[inx] == 0)
							thePass[inx] = pass1[inx];
					}
					thePass[136] = 0;
				}
			}

			MD5 md5 = new MD5();
			if (username != null && username[0] != 0)
				md5.Update (username);
			md5.Update (thePass);

			byte[] md5Bytes = md5.Final ();
			for (int thingIndex = offset, md5Index = 0; thingIndex < thing.Length; thingIndex++, md5Index++)
				thing[thingIndex] = (byte) (thing[thingIndex] ^ md5Bytes[md5Index % md5Bytes.Length]);
		}
	}
}