00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using System.IO;
00005 using System.Net;
00006 using System.Net.Sockets;
00007
00008 namespace DCPlusPlus
00009 {
00010
00011
00012
00013
00014
00015
00016
00022 public abstract class Connection
00023 {
00024 protected bool is_extended_protocol = false;
00029 public bool IsExtendedProtocol
00030 {
00031 get
00032 {
00033 return (is_extended_protocol);
00034 }
00035
00036 }
00037 protected string nick = "unknown";
00042 public string Nick
00043 {
00044 get
00045 {
00046 return (nick);
00047 }
00048 set
00049 {
00050 nick = value;
00051 }
00052 }
00053
00054 protected bool is_connected = false;
00058 public bool IsConnected
00059 {
00060 get
00061 {
00062 return (is_connected);
00063 }
00064
00065 }
00066 protected bool is_connecting = false;
00070 public bool IsConnecting
00071 {
00072 get
00073 {
00074 return (is_connecting);
00075 }
00076
00077 }
00078 protected string ip = "";
00082 public string IP
00083 {
00084 get
00085 {
00086 return (ip);
00087 }
00088 set
00089 {
00090 ip = value;
00091 }
00092 }
00093 protected int port = 0;
00097 public int Port
00098 {
00099 get
00100 {
00101 return (port);
00102 }
00103 set
00104 {
00105 port = value;
00106 }
00107 }
00111 protected Socket socket = null;
00115 protected byte[] receive_buffer = null;
00120 public abstract void Disconnect();
00126 public void SendCommand(string command)
00127 {
00128 SendCommand(command, "");
00129 }
00136 public void SendCommand(string command, string parameter)
00137 {
00138 if (!string.IsNullOrEmpty(parameter))
00139 SendCommand(command, parameter.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
00140 else
00141 SendCommand(command, new string[0]);
00142 }
00149 public void SendCommand(string command, string[] parameters)
00150 {
00151 if (socket != null)
00152 {
00153 if (!socket.Connected) return;
00154 string send_string = "$" + command;
00155 for (int i = 0; i < parameters.Length; i++)
00156 send_string = send_string + " " + parameters[i];
00157 send_string = send_string + "|";
00158 try
00159 {
00160
00161
00162 byte[] send_bytes = System.Text.Encoding.Default.GetBytes(send_string);
00163 socket.BeginSend(send_bytes, 0, send_bytes.Length, SocketFlags.None, new AsyncCallback(SendCommandCallback), socket);
00164 }
00165 catch (Exception e)
00166 {
00167 Console.WriteLine("Error sending command to peer: " + e.Message);
00168 error_code = ErrorCodes.Exception;
00169 Disconnect();
00170 }
00171 }
00172 }
00178 protected void SendCommandCallback(IAsyncResult ar)
00179 {
00180 Socket send_command_socket = (Socket)ar.AsyncState;
00181 try
00182 {
00183 int bytes = send_command_socket.EndSend(ar);
00184 }
00185 catch (Exception ex)
00186 {
00187 Console.WriteLine("exception during send of command: " + ex.Message);
00188 error_code = ErrorCodes.Exception;
00189 Disconnect();
00190 }
00191 }
00192 protected string[] supports=new string[0];
00196 public string[] Supports
00197 {
00198 get
00199 {
00200 return (supports);
00201 }
00202 }
00212 public bool CheckForExtension(string extension)
00213 {
00214 foreach (string supported_extension in supports)
00215 {
00216 if (supported_extension == extension)
00217 return (true);
00218 }
00219 return (false);
00220 }
00224 public enum ErrorCodes
00225 {
00226 UnableToConnect, Exception,UnknownException,NoFreeSlots,
00227 FileNotAvailable,Kicked,Banned,Disconnected,
00228 UnableToResolve,UrlNotFound,ProtocolError,
00229 ConnectionTimedOut,UserDisconnect,NoErrorYet,
00230 QueueEntryInUse
00231
00232 }
00233 protected Connection.ErrorCodes error_code = Connection.ErrorCodes.NoErrorYet;
00237 public Connection.ErrorCodes ErrorCode
00238 {
00239 get
00240 {
00241 return (error_code);
00242 }
00243 }
00244
00245 #region LockToKey
00246
00247
00248
00249
00250
00251 public string L2K(string lck)
00252 {
00253
00254
00255
00256
00257 int[] arrChar = new int[lck.Length + 1];
00258 int[] arrRet = new int[lck.Length + 1];
00259 arrChar[1] = lck[0];
00260 for (int i = 2; i < lck.Length + 1; i++)
00261 {
00262 arrChar[i] = lck[i - 1];
00263 arrRet[i] = arrChar[i] ^ arrChar[i - 1];
00264 }
00265 arrRet[1] = arrChar[1] ^ arrChar[lck.Length] ^ arrChar[lck.Length - 1] ^ 5;
00266 string sKey = "";
00267 for (int n = 1; n < lck.Length + 1; n++)
00268 {
00269 arrRet[n] = ((arrRet[n] * 16) & 240) | ((arrRet[n] / 16) & 15);
00270 int j = arrRet[n];
00271 switch (j)
00272 {
00273 case 0:
00274 case 5:
00275 case 36:
00276 case 96:
00277 case 124:
00278 case 126:
00279 sKey += "/%DCN"
00280 + ((string)("00" + j.ToString())).Substring(j.ToString().Length - 1)
00281 + "%/";
00282 break;
00283 default:
00284 sKey += Chr(Convert.ToByte((char)j));
00285 break;
00286 }
00287 }
00288 return sKey;
00289 }
00290
00291 public static char Chr(byte src)
00292 {
00293 return (Encoding.Default.GetChars(new byte[] { src })[0]);
00294 }
00295
00296 public static string LockToKey2(string lockStr)
00297 {
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 if (lockStr.Length < 3)
00333 {
00334 return string.Empty;
00335 }
00336
00337 char[] temp = new char[lockStr.Length];
00338 char v1;
00339
00340 v1 = (char)(lockStr[0] ^ 5);
00341 v1 = (char)(((v1 >> 4) | (v1 << 4)) & 0xff);
00342 temp[0] = v1;
00343
00344 int i;
00345
00346 for (i = 1; i < lockStr.Length; i++)
00347 {
00348 v1 = (char)(lockStr[i] ^ lockStr[i - 1]);
00349 v1 = (char)(((v1 >> 4) | (v1 << 4)) & 0xff);
00350 temp[i] = v1;
00351 }
00352
00353 temp[0] = (char)(temp[0] ^ temp[lockStr.Length - 1]);
00354
00355 return KeySubst(temp, lockStr.Length);
00356 }
00357
00358 static string KeySubst(char[] aKey, int len)
00359 {
00360 StringBuilder key = new StringBuilder(100);
00361
00362 for (int i = 0; i < len; i++)
00363 {
00364 if (IsExtra(aKey[i]))
00365 {
00366 key.Append("/%DCN");
00367 key.Append(string.Format("{0:000}", (int)aKey[i]));
00368 key.Append("%/");
00369 }
00370 else
00371 {
00372 key.Append(aKey[i]);
00373 }
00374 }
00375
00376 return key.ToString();
00377 }
00378
00379 static bool IsExtra(char b)
00380 {
00381 return (b == 0 || b == 5 || b == 124 || b == 96 || b == 126 || b == 36);
00382 }
00383
00384
00385
00392 protected string LockToKey(string lck)
00393 {
00394 string Key = "";
00395 for (int i = 0, j; lck.Length > i; i++)
00396 {
00397 if (i == 0) j = lck[0] ^ 5;
00398 else j = lck[i] ^ lck[i - 1];
00399 for (j += ((j % 17) * 15); j > 255; j -= 255) ;
00400 switch (j)
00401 {
00402 case 0:
00403 case 5:
00404 case 36:
00405 case 96:
00406 case 124:
00407 case 126:
00408 Key += "/%DCN" + ((string)("00" + j.ToString())).Substring(j.ToString().Length - 1) + "%/";
00409 break;
00410 default:
00411 Key += (char)j;
00412 break;
00413 }
00414 }
00415 return (char)(Key[0] ^ Key[Key.Length - 1]) + Key.Substring(1);
00416 }
00417
00418
00419 private static string escapeChars(string key)
00420 {
00421 System.Text.StringBuilder builder =
00422 new System.Text.StringBuilder(key.Length);
00423
00424 for (int index = 0; index < key.Length; index++)
00425 {
00426 int code = (int)key[index];
00427 if (code == 0 || code == 5 || code == 36 || code == 96
00428 || code == 124 || code == 126)
00429 builder.AppendFormat("/%DCN{0:000}%/", code);
00430 else
00431 builder.Append(key[index]);
00432 }
00433
00434 return builder.ToString();
00435 }
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00471 public string MyLockToKey(string key)
00472 {
00473 byte[] decoded_key_buffer = new byte[key.Length];
00474 for (int i = 1; i < key.Length; i++)
00475 decoded_key_buffer[i] = (byte)((char)key[i] ^ (char)key[i - 1]);
00476 decoded_key_buffer[0] = (byte)((char)key[0] ^ (char)key[key.Length - 1] ^ (char)key[key.Length - 2] ^ 5);
00477 for (int i = 0; i < key.Length; i++)
00478 decoded_key_buffer[i] = (byte)((((char)decoded_key_buffer[i] << 4) & 240) | (((char)decoded_key_buffer[i] >> 4) & 15));
00479
00480
00481 string decoded_key = Encoding.ASCII.GetString(decoded_key_buffer);
00482 for (int i = 0; i < decoded_key.Length; i++)
00483 {
00484 if (decoded_key[i] == (char)0)
00485 {
00486 decoded_key = decoded_key.Remove(i, 1);
00487 decoded_key = decoded_key.Insert(i, "/%DCN000%/");
00488 }
00489 if (decoded_key[i] == (char)5)
00490 {
00491 decoded_key = decoded_key.Remove(i, 1);
00492 decoded_key = decoded_key.Insert(i, "/%DCN005%/");
00493 }
00494 if (decoded_key[i] == (char)36)
00495 {
00496 decoded_key = decoded_key.Remove(i, 1);
00497 decoded_key = decoded_key.Insert(i, "/%DCN036%/");
00498 }
00499 if (decoded_key[i] == (char)96)
00500 {
00501 decoded_key = decoded_key.Remove(i, 1);
00502 decoded_key = decoded_key.Insert(i, "/%DCN096%/");
00503 }
00504 if (decoded_key[i] == (char)124)
00505 {
00506 decoded_key = decoded_key.Remove(i, 1);
00507 decoded_key = decoded_key.Insert(i, "/%DCN124%/");
00508 }
00509 if (decoded_key[i] == (char)126)
00510 {
00511 decoded_key = decoded_key.Remove(i, 1);
00512 decoded_key = decoded_key.Insert(i, "/%DCN126%/");
00513 }
00514
00515 }
00516
00517 return (decoded_key);
00518 }
00519 #endregion
00528 protected string CreateKey(bool extended)
00529 {
00530 string key = "";
00531 string pk = "";
00532 string id = "EXTENDEDPROTOCOL";
00533 Random rnd = new Random();
00534
00535 int key_len = id.Length + rnd.Next(14);
00536
00537 if (extended)
00538 {
00539 key_len -= id.Length;
00540 key = id;
00541 }
00542 for (int i = 0; i < key_len; i++)
00543 {
00544 key += (char)(rnd.Next(94) + 33);
00545 }
00546
00547 for (int i = 0; i < 16; i++)
00548 {
00549 pk += (char)(rnd.Next(94) + 33);
00550 }
00551
00552
00553 key = key + " Pk=" + pk;
00554
00555 for (int i = 0; i < key.Length; i++)
00556 {
00557 if (key[i] == (char)0)
00558 {
00559 key = key.Remove(i, 1);
00560 key = key.Insert(i, "/%DCN000%/");
00561 }
00562 if (key[i] == (char)5)
00563 {
00564 key = key.Remove(i, 1);
00565 key = key.Insert(i, "/%DCN005%/");
00566 }
00567 if (key[i] == (char)36)
00568 {
00569 key = key.Remove(i, 1);
00570 key = key.Insert(i, "/%DCN036%/");
00571 }
00572 if (key[i] == (char)96)
00573 {
00574 key = key.Remove(i, 1);
00575 key = key.Insert(i, "/%DCN096%/");
00576 }
00577 if (key[i] == (char)124)
00578 {
00579 key = key.Remove(i, 1);
00580 key = key.Insert(i, "/%DCN124%/");
00581 }
00582 if (key[i] == (char)126)
00583 {
00584 key = key.Remove(i, 1);
00585 key = key.Insert(i, "/%DCN126%/");
00586 }
00587
00588
00589 }
00590
00591 key = "EXTENDEDPROTOCOLABCABCABCABCABCABC Pk=DCPLUSPLUS0.698ABCABC";
00592 return (key);
00593 }
00594 }
00595 }