Package DebTorrent :: Package BT1 :: Module Encrypter :: Class Connection
[hide private]
[frames] | no frames]

Class Connection

source code

A single, possibly encrypted, connection to a peer.

The BitTorrent handshake is in the order: header, options, download id (info hash), peer id, [length, message], ....

Instance Methods [hide private]
 
__init__(self, Encoder, connection, dns, id, ext_handshake=True, encrypted=None, options=None)
Initialize the instance and start handling the connection.
source code
    Logging
 
_log_start(self)
Start logging detailed connection data.
source code
 
_log_write(self, s)
Write to the log file, then the connection.
source code
    Information
string
get_ip(self, real=True)
Get the IP address of the connection.
source code
string
get_id(self)
Get the peer ID.
source code
string
get_readable_id(self)
Get the human-readable peer ID.
source code
boolean
is_locally_initiated(self)
Determine whether the connection was locally initiated.
source code
boolean
is_encrypted(self)
Determine whether the connection is encrypted.
source code
boolean
is_flushed(self)
Determine whether the connection is flushed.
source code
    Encryption
int, method
read_header(self, s)
Read the possibly encrypted protocol header.
source code
 
_start_crypto(self)
Setup the connection for encrypted communication.
source code
 
_end_crypto(self)
Return the connection back to unencrypted communication.
source code
int, method
read_crypto_header(self, s)
Read the encryption key.
source code
boolean
_search_for_pattern(self, s, pat)
Search for a pattern in the encrypted protocol header.
source code
int, method
read_encrypted_header(self, s)
Read the regular protocol name header from the encrypted stream.
source code
    Incoming Encryption
int, method
read_crypto_block3a(self, s)
Find the block3a crypto information in the connection.
source code
boolean
read_crypto_block3b(self, s)
Process the block3b crypto information in the connection.
source code
boolean
read_crypto_block3c(self, s)
Read the encrypted protocol mode.
source code
int, method
read_crypto_pad3(self, s)
Read the encrypted protocol padding.
source code
int, method
read_crypto_ia(self, s)
Read the initial payload data from the connection.
source code
int, method
read_crypto_block3done(self, ia='')
Finish with the encrypted header.
source code
    Outgoing Encryption
int, method
read_crypto_block4a(self, s)
Read the encrypted protocol header.
source code
int, method
read_crypto_block4b(self, s)
Read the encrypted protocol mode and padding.
source code
int, method
read_crypto_pad4(self, s)
Read the encrypted protocol padding.
source code
int, method
read_crypto_block4done(self)
Finish with the encrypted header.
source code
    BitTorrent Handshake
int, method
_read_header(self, s)
Read the protocol header.
source code
int, method
read_options(self, s)
Read the options from the header.
source code
int, method
read_download_id(self, s)
Verify the torrent infohash from the header.
source code
int, method
read_peer_id(self, s)
Read/verify the peer's ID.
source code
    BitTorrent Messages
int, method
read_len(self, s)
Read the length of the message.
source code
int, method
read_message(self, s)
Read the message.
source code
None
read_dead(self, s)
Return None to close the connection.
source code
    Data
 
send_message_raw(self, message)
Write a message out on the connection.
source code
 
_write(self, message)
Write a raw message out on the connection.
source code
 
data_came_in(self, connection, s)
Process the incoming data on the connection.
source code
 
_write_buffer(self, s)
Write data back onto the buffer.
source code
 
_read(self, s)
Process the data that comes in.
source code
 
_switch_to_read2(self)
Switch from _read to the more efficient _read2 method.
source code
 
_read2(self, s)
Efficiently process the data that comes in.
source code
    Connection
 
_auto_close(self)
Close the connection if the handshake is not yet complete.
source code
 
close(self)
Close the connection.
source code
 
sever(self)
Clean up the connection for closing.
source code
 
connection_flushed(self, connection)
Flush the connection.
source code
 
connection_lost(self, connection)
Sever the connection.
source code
Instance Variables [hide private]
Encoder Encoder
the collection of all connections
method _logwritefunc
the saved write method for intercepting connection writes for logging purposes
int _max_search
the number of remaining bytes to search for the pattern
string buffer
the buffer of received data from the connection
int bufferlen
the length of the buffer
boolean closed
whether the connection has been closed
boolean complete
whether the handshake is complete
Connecter.Connecter connecter
the Connecter instance to use
DebTorrent.SocketHandler.SingleSocket connection
the low-level connection to the peer
int cryptmode
the type of encryption being used
(string, int) dns
the IP address and port to connect to
boolean encrypted
whether the connection is encrypted (will be None if that is not yet known)
DebTorrent.BTcrypto.Crypto encrypter
the already created Crypto instance, if the connection was externally handshaked (optional, defaults to creating a new one)
string id
the peer ID of the peer
method keepalive
the method to call to send a keepalive message on the connection
boolean locally_initiated
whether the connectio was initiated locally
file log
the log file to write to
method next_func
the next method to use to process incoming data on the connection
int next_len
the next amount of data to read from the connection
string options
the options read from the externally handshaked connection (optional, defaults to None)
method read
the method to use to read from the connection
string readable_id
the human-readable ID of the peer
method write
the method to use to write to the connection
Method Details [hide private]

__init__(self, Encoder, connection, dns, id, ext_handshake=True, encrypted=None, options=None)
(Constructor)

source code 
Initialize the instance and start handling the connection.
Parameters:
  • Encoder (Encoder) - the collection of all connections
  • connection (DebTorrent.SocketHandler.SingleSocket) - the low-level connection to the peer
  • dns ((string, int)) - the IP address and port to connect to
  • id (string) - the peer ID of the peer to connect to (will be None if the connection is being initiated locally)
  • ext_handshake (boolean) - whether the connection has already been handshaked by another module (optional, defaults to False)
  • encrypted (int or DebTorrent.BT1Crypto.Crypto) - the type of encryption the connection supports (0 for none), or the already created Crypto instance, if the connection was externally handshaked (optional, defaults to creating a new one)
  • options (string) - the options read from the externally handshaked connection (optional, defaults to None)

_log_start(self)

source code 

Start logging detailed connection data.

Only called with DEBUG = True (i.e. never). Adds an intercept function so that all connection writes are first written to the log file, then out on the connection.

_log_write(self, s)

source code 
Write to the log file, then the connection.
Parameters:
  • s (string) - the data to write

get_ip(self, real=True)

source code 
Get the IP address of the connection.
Parameters:
  • real (boolean) - whether to double check that it's the reall IP address
Returns: string
the IP address

get_id(self)

source code 
Get the peer ID.
Returns: string
the peer ID

get_readable_id(self)

source code 
Get the human-readable peer ID.
Returns: string
the peer ID

is_locally_initiated(self)

source code 
Determine whether the connection was locally initiated.
Returns: boolean
whether the connection was locally initiated

is_encrypted(self)

source code 
Determine whether the connection is encrypted.
Returns: boolean
whether the connection is encrypted

is_flushed(self)

source code 
Determine whether the connection is flushed.
Returns: boolean
whether the connection is flushed

_read_header(self, s)

source code 
Read the protocol header.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_header(self, s)

source code 
Read the possibly encrypted protocol header.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_crypto_header(self, s)

source code 
Read the encryption key.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

_search_for_pattern(self, s, pat)

source code 
Search for a pattern in the encrypted protocol header.
Parameters:
  • s (string) - the incoming data from the connection
  • pat (string) - the pattern to find
Returns: boolean
whether the pattern was found

read_crypto_block3a(self, s)

source code 
Find the block3a crypto information in the connection.
Parameters:
  • s (string) - the data read from the conection
Returns: int, method
the next length to read and method to call with the data

read_crypto_block3b(self, s)

source code 
Process the block3b crypto information in the connection.
Parameters:
  • s (string) - the data read from the conection
Returns: boolean
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_crypto_block3c(self, s)

source code 
Read the encrypted protocol mode.
Parameters:
  • s (string) - the data read from the conection
Returns: boolean
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_crypto_pad3(self, s)

source code 
Read the encrypted protocol padding.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_crypto_ia(self, s)

source code 
Read the initial payload data from the connection.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_crypto_block3done(self, ia='')

source code 
Finish with the encrypted header.
Parameters:
  • ia (string) - the initial payload data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_crypto_block4a(self, s)

source code 
Read the encrypted protocol header.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_crypto_block4b(self, s)

source code 
Read the encrypted protocol mode and padding.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_crypto_pad4(self, s)

source code 
Read the encrypted protocol padding.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_crypto_block4done(self)

source code 
Finish with the encrypted header.
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_encrypted_header(self, s)

source code 
Read the regular protocol name header from the encrypted stream.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_options(self, s)

source code 
Read the options from the header.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_download_id(self, s)

source code 
Verify the torrent infohash from the header.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_peer_id(self, s)

source code 
Read/verify the peer's ID.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_len(self, s)

source code 
Read the length of the message.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_message(self, s)

source code 
Read the message.
Parameters:
  • s (string) - the incoming data from the connection
Returns: int, method
the next amount of data to read and the method to call with it, or None if there is no next method to call

read_dead(self, s)

source code 
Return None to close the connection.
Parameters:
  • s (string) - the incoming data from the connection (not used)
Returns: None
None

send_message_raw(self, message)

source code 
Write a message out on the connection.
Parameters:
  • message (string) - the data to write to the connection

_write(self, message)

source code 
Write a raw message out on the connection.
Parameters:
  • message (string) - the raw data to write to the connection

data_came_in(self, connection, s)

source code 
Process the incoming data on the connection.
Parameters:

_write_buffer(self, s)

source code 
Write data back onto the buffer.
Parameters:
  • s (string) - the data to rebuffer

_read(self, s)

source code 
Process the data that comes in.
Parameters:
  • s (string) - the (unencrypted) incoming data from the connection

_read2(self, s)

source code 

Efficiently process the data that comes in.

More efficient, buffers the incoming data by appending it to a list rather than creating a new string by adding it to the end. Requires buffer to be a list of strings, and bufferlen to be it's total length.
Parameters:
  • s (string) - the (unencrypted) incoming data from the connection

connection_flushed(self, connection)

source code 
Flush the connection.
Parameters:

connection_lost(self, connection)

source code 
Sever the connection.
Parameters: