DCPlusPlus.Connection Class Reference

Basic Class for Peer and Hub Connections Contains properties and methods used by both to reduce redundancy. More...

Inheritance diagram for DCPlusPlus.Connection:

Inheritance graph
[legend]
Collaboration diagram for DCPlusPlus.Connection:

Collaboration graph
[legend]
List of all members.

Public Types

enum  ErrorCodes {
  UnableToConnect, Exception, UnknownException, NoFreeSlots,
  FileNotAvailable, Kicked, Banned, Disconnected,
  UnableToResolve, UrlNotFound, ProtocolError, ConnectionTimedOut,
  UserDisconnect, NoErrorYet, QueueEntryInUse
}
 Enumeration of possible Connection ErrorCodes. More...

Public Member Functions

bool CheckForExtension (string extension)
 Check if an extension is supported by the remote side.
abstract void Disconnect ()
 Prototype for the Disconnect method used in the Peer/Hub classes.
string L2K (string lck)
string MyLockToKey (string key)
 Convert a Lock to a Key (personal version) (broken somehow).
void SendCommand (string command, string[] parameters)
 Send a command with parameters (for example $SR filename filesize etc).
void SendCommand (string command, string parameter)
 Send a command with a parameter (for example $Nick test).
void SendCommand (string command)
 Send a command without parameters (for example $Send).

Static Public Member Functions

static char Chr (byte src)
static string LockToKey2 (string lockStr)

Protected Member Functions

string CreateKey (bool extended)
 Creates a key used during handshake.
string LockToKey (string lck)
 Convert a Lock to a Key shamelessly grabbed from the dcpp dev wiki.
void SendCommandCallback (IAsyncResult ar)
 Async Callback for SendCommand (gets called when the send is completed).

Protected Attributes

Connection.ErrorCodes error_code = Connection.ErrorCodes.NoErrorYet
string ip = ""
bool is_connected = false
bool is_connecting = false
bool is_extended_protocol = false
string nick = "unknown"
int port = 0
byte[] receive_buffer = null
 The Receive buffer used by socket.
Socket socket = null
 The Socket used for all Peer/Hub communications.
string[] supports = new string[0]

Properties

Connection.ErrorCodes ErrorCode [get]
 Get the error code of a connection.
string IP [get, set]
 Get/Set ip address of the remote end of the connection.
bool IsConnected [get]
 Returns TRUE if Peer/Hub is connected.
bool IsConnecting [get]
 Returns TRUE if Peer/Hub is still connecting.
bool IsExtendedProtocol [get]
 Returns TRUE if the other side supports DC++ Extensions ($support comand).
string Nick [get, set]
 Get/Set your own Nickname (gets transfered via $Nick during handshake of the connection).
int Port [get, set]
 Get/Set port of the remote end of the connection.
string[] Supports [get]
 Array of supported extensions by the remote side.

Static Private Member Functions

static string escapeChars (string key)
static bool IsExtra (char b)
static string KeySubst (char[] aKey, int len)

Detailed Description

Basic Class for Peer and Hub Connections Contains properties and methods used by both to reduce redundancy.

Definition at line 22 of file Connection.cs.


Member Enumeration Documentation

enum DCPlusPlus::Connection::ErrorCodes

Enumeration of possible Connection ErrorCodes.

Enumerator:
UnableToConnect 
Exception 
UnknownException 
NoFreeSlots 
FileNotAvailable 
Kicked 
Banned 
Disconnected 
UnableToResolve 
UrlNotFound 
ProtocolError 
ConnectionTimedOut 
UserDisconnect 
NoErrorYet 
QueueEntryInUse 

Definition at line 224 of file Connection.cs.


Member Function Documentation

bool DCPlusPlus.Connection.CheckForExtension ( string  extension  ) 

Check if an extension is supported by the remote side.

Parameters:
extension String containing the extension you want to check for
Returns:
Returns TRUE if the extension is supported

Definition at line 212 of file Connection.cs.

Referenced by DCPlusPlus.Peer.GetFileList(), DCPlusPlus.Peer.GetTTHL(), DCPlusPlus.Peer.StartUpload(), and DCPlusPlus.Peer.WriteDataToFile().

00213         {
00214             foreach (string supported_extension in supports)
00215             {
00216                 if (supported_extension == extension)
00217                     return (true);
00218             }
00219             return (false);
00220         }

Here is the caller graph for this function:

static char DCPlusPlus.Connection.Chr ( byte  src  )  [static]

Definition at line 291 of file Connection.cs.

00292         {
00293             return (Encoding.Default.GetChars(new byte[] { src })[0]);
00294         }

string DCPlusPlus.Connection.CreateKey ( bool  extended  )  [protected]

Creates a key used during handshake.

Parameters:
extended TRUE if you want to tell the remote side that we are supporting DC++ extensions
Returns:
String containing a valid key

Definition at line 528 of file Connection.cs.

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         }

abstract void DCPlusPlus.Connection.Disconnect (  )  [pure virtual]

Prototype for the Disconnect method used in the Peer/Hub classes.

Implemented in DCPlusPlus.Hub, and DCPlusPlus.Peer.

Referenced by DCPlusPlus.Connection.SendCommand().

Here is the caller graph for this function:

static string DCPlusPlus.Connection.escapeChars ( string  key  )  [static, private]

Definition at line 419 of file Connection.cs.

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         }

static bool DCPlusPlus.Connection.IsExtra ( char  b  )  [static, private]

Definition at line 379 of file Connection.cs.

00380         {
00381             return (b == 0 || b == 5 || b == 124 || b == 96 || b == 126 || b == 36);
00382         }

static string DCPlusPlus.Connection.KeySubst ( char[]  aKey,
int  len 
) [static, private]

Definition at line 358 of file Connection.cs.

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         }

string DCPlusPlus.Connection.L2K ( string  lck  ) 

Definition at line 251 of file Connection.cs.

00252         {
00253             /*lck = lck.Replace("$Lock ", "");
00254             int iPos = lck.IndexOf(" Pk=", 1);
00255             if (iPos > 0) lck = lck.Substring(0, iPos);
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         }

string DCPlusPlus.Connection.LockToKey ( string  lck  )  [protected]

Convert a Lock to a Key shamelessly grabbed from the dcpp dev wiki.

Parameters:
lck the connections lock
Returns:
a hopefully valid key

Definition at line 392 of file Connection.cs.

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         }

static string DCPlusPlus.Connection.LockToKey2 ( string  lockStr  )  [static]

Definition at line 296 of file Connection.cs.

00297         {
00298             //string lockStr = GetLock(sLock);
00299 
00300             //                  int j = Lock.IndexOf(" Pk=");
00301             //                  string lockStr, pk;
00302             //                  if(j > 0)
00303             //                  {
00304             //                          lockStr = Lock.Substring(0, j);
00305             //                          pk = Lock.Substring(j + 4);
00306             //                  }
00307             //                  else
00308             //                  {
00309             //                          // Workaround for faulty linux hubs...
00310             //                          j = Lock.IndexOf(" ");
00311             //                          if(j > 0)
00312             //                                  lockStr = Lock.Substring(0, j);
00313             //                          else
00314             //                                  lockStr = Lock;
00315             //                  }
00316 
00317             //
00318             //                  string result = "";
00319             //                  int length = lockStr.Length;
00320             //                  if(length > 2)
00321             //                  {
00322             //                          int h = lockStr[0] ^ lockStr[length - 1] ^ lockStr[length - 2] ^ 5;
00323             //                          ConvertChar(ref h, ref result);
00324             //                          for(int i = 1; i < lockStr.Length; i++)
00325             //                          {
00326             //                                  h = lockStr[i] ^ lockStr[i - 1];
00327             //                                  ConvertChar(ref h, ref result);
00328             //                          }
00329             //                  }
00330             //                  return result;
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         }

string DCPlusPlus.Connection.MyLockToKey ( string  key  ) 

Convert a Lock to a Key (personal version) (broken somehow).

Parameters:
key the connections lock
Returns:
a hopefully valid key

Definition at line 471 of file Connection.cs.

00472         {
00473             byte[] decoded_key_buffer = new byte[key.Length]; //so we have an exact duplicate in 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         }

void DCPlusPlus.Connection.SendCommand ( string  command,
string[]  parameters 
)

Send a command with parameters (for example $SR filename filesize etc).

Parameters:
command the command to send
parameters array of parameters of the command

Definition at line 149 of file Connection.cs.

References DCPlusPlus.Connection.Disconnect(), DCPlusPlus.Connection.error_code, DCPlusPlus.Connection.Exception, DCPlusPlus.Connection.SendCommandCallback(), and DCPlusPlus.Connection.socket.

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                     //socket.Send(Encoding.UTF8.GetBytes(send_string), SocketFlags.None);
00161                     //socket.Send(System.Text.Encoding.Default.GetBytes(send_string), SocketFlags.None);
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         }

Here is the call graph for this function:

void DCPlusPlus.Connection.SendCommand ( string  command,
string  parameter 
)

Send a command with a parameter (for example $Nick test).

Parameters:
command the command to send
parameter the parameter of the command

Definition at line 136 of file Connection.cs.

References DCPlusPlus.Connection.SendCommand().

00137         {
00138             if (!string.IsNullOrEmpty(parameter))
00139                 SendCommand(command, parameter.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
00140             else
00141                 SendCommand(command, new string[0]);
00142         }

Here is the call graph for this function:

void DCPlusPlus.Connection.SendCommand ( string  command  ) 

Send a command without parameters (for example $Send).

Parameters:
command the command to send

Definition at line 126 of file Connection.cs.

Referenced by DCPlusPlus.Peer.GetFileList(), DCPlusPlus.Peer.GetTTHL(), DCPlusPlus.Connection.SendCommand(), DCPlusPlus.Peer.SendError(), DCPlusPlus.Peer.SendFailed(), DCPlusPlus.Peer.SendMaxedOut(), DCPlusPlus.Peer.StartUpload(), and DCPlusPlus.Peer.WriteDataToFile().

00127         {
00128             SendCommand(command, "");
00129         }

Here is the caller graph for this function:

void DCPlusPlus.Connection.SendCommandCallback ( IAsyncResult  ar  )  [protected]

Async Callback for SendCommand (gets called when the send is completed).

Parameters:
ar Async Result/State

Definition at line 178 of file Connection.cs.

Referenced by DCPlusPlus.Connection.SendCommand().

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         }

Here is the caller graph for this function:


Member Data Documentation

Connection.ErrorCodes DCPlusPlus.Connection.error_code = Connection.ErrorCodes.NoErrorYet [protected]

Definition at line 233 of file Connection.cs.

Referenced by DCPlusPlus.Hub.Connect(), DCPlusPlus.Hub.Disconnect(), DCPlusPlus.Hub.OnHostResolve(), DCPlusPlus.Peer.OnReceive(), DCPlusPlus.Connection.SendCommand(), DCPlusPlus.Peer.SendFileNotAvailableError(), DCPlusPlus.Peer.SendMaxedOut(), DCPlusPlus.Peer.StartReceiving(), DCPlusPlus.Peer.StartUploadTransfer(), DCPlusPlus.Peer.UploadTransferCallback(), and DCPlusPlus.Peer.WriteDataToFile().

string DCPlusPlus.Connection.ip = "" [protected]

Definition at line 78 of file Connection.cs.

Referenced by DCPlusPlus.Hub.Copy(), and DCPlusPlus.Hub.OnHostResolve().

bool DCPlusPlus.Connection.is_connected = false [protected]

Definition at line 54 of file Connection.cs.

Referenced by DCPlusPlus.Hub.Connect(), DCPlusPlus.Hub.Copy(), DCPlusPlus.Hub.Disconnect(), and DCPlusPlus.Hub.Hub().

bool DCPlusPlus.Connection.is_connecting = false [protected]

Definition at line 66 of file Connection.cs.

Referenced by DCPlusPlus.Hub.Connect(), DCPlusPlus.Hub.Copy(), DCPlusPlus.Hub.Disconnect(), and DCPlusPlus.Hub.Hub().

bool DCPlusPlus.Connection.is_extended_protocol = false [protected]

Definition at line 24 of file Connection.cs.

Referenced by DCPlusPlus.Hub.Copy(), DCPlusPlus.Hub.Disconnect(), and DCPlusPlus.Hub.Hub().

string DCPlusPlus.Connection.nick = "unknown" [protected]

Definition at line 37 of file Connection.cs.

Referenced by DCPlusPlus.Hub.Copy().

int DCPlusPlus.Connection.port = 0 [protected]

Definition at line 93 of file Connection.cs.

Referenced by DCPlusPlus.Hub.Copy(), DCPlusPlus.Hub.Hub(), and DCPlusPlus.Hub.OnHostResolve().

byte [] DCPlusPlus.Connection.receive_buffer = null [protected]

The Receive buffer used by socket.

Definition at line 115 of file Connection.cs.

Referenced by DCPlusPlus.Peer.OnReceive(), DCPlusPlus.Peer.StartReceiving(), and DCPlusPlus.Peer.WriteDataToFile().

Socket DCPlusPlus.Connection.socket = null [protected]

The Socket used for all Peer/Hub communications.

Definition at line 111 of file Connection.cs.

Referenced by DCPlusPlus.Hub.Connect(), DCPlusPlus.Hub.Disconnect(), DCPlusPlus.Hub.Hub(), DCPlusPlus.Hub.OnHostResolve(), DCPlusPlus.Connection.SendCommand(), DCPlusPlus.Peer.StartReceiving(), DCPlusPlus.Peer.StartUploadTransfer(), and DCPlusPlus.Peer.UploadTransferCallback().

string [] DCPlusPlus.Connection.supports = new string[0] [protected]

Definition at line 192 of file Connection.cs.


Property Documentation

Connection.ErrorCodes DCPlusPlus.Connection.ErrorCode [get]

Get the error code of a connection.

Definition at line 238 of file Connection.cs.

string DCPlusPlus.Connection.IP [get, set]

Get/Set ip address of the remote end of the connection.

Definition at line 83 of file Connection.cs.

Referenced by DCPlusPlus.HubList.ReadHub(), and DCPlusPlus.ListeningSockets.SearchReply().

bool DCPlusPlus.Connection.IsConnected [get]

Returns TRUE if Peer/Hub is connected.

Definition at line 59 of file Connection.cs.

bool DCPlusPlus.Connection.IsConnecting [get]

Returns TRUE if Peer/Hub is still connecting.

Definition at line 71 of file Connection.cs.

bool DCPlusPlus.Connection.IsExtendedProtocol [get]

Returns TRUE if the other side supports DC++ Extensions ($support comand).

Definition at line 30 of file Connection.cs.

string DCPlusPlus.Connection.Nick [get, set]

Get/Set your own Nickname (gets transfered via $Nick during handshake of the connection).

Definition at line 43 of file Connection.cs.

Referenced by DCPlusPlus.Client.ConnectHub(), DCPlusPlus.ListeningSockets.SearchReply(), and DCPlusPlus.Client.SetupPeerEventHandler().

int DCPlusPlus.Connection.Port [get, set]

Get/Set port of the remote end of the connection.

Definition at line 98 of file Connection.cs.

Referenced by DCPlusPlus.HubList.ReadHub(), and DCPlusPlus.ListeningSockets.SearchReply().

string [] DCPlusPlus.Connection.Supports [get]

Array of supported extensions by the remote side.

Definition at line 197 of file Connection.cs.


The documentation for this class was generated from the following file:
Generated on Wed Mar 7 19:09:27 2007 for DCPlusPlus by  doxygen 1.5.1-p1