#ifdef ENABLE_LTEP
- #define HANDSHAKE_HAS_LTEP(bits)(((bits)[5] & 0x10) ? 1 : 0)
+ #define HANDSHAKE_HAS_LTEP(bits)(((bits)[5] & 0x10) != 0)
#define HANDSHAKE_SET_LTEP(bits)((bits)[5] |= 0x10)
#else
- #define HANDSHAKE_HAS_LTEP(bits)(0)
+ #define HANDSHAKE_HAS_LTEP(bits)(false)
#define HANDSHAKE_SET_LTEP(bits)((void)0)
#endif
#ifdef ENABLE_FAST
- #define HANDSHAKE_HAS_FASTEXT(bits)(((bits)[7] & 0x04) ? 1 : 0)
+ #define HANDSHAKE_HAS_FASTEXT(bits)(((bits)[7] & 0x04) != 0)
#define HANDSHAKE_SET_FASTEXT(bits)((bits)[7] |= 0x04)
#else
- #define HANDSHAKE_HAS_FASTEXT(bits)(0)
+ #define HANDSHAKE_HAS_FASTEXT(bits)(false)
#define HANDSHAKE_SET_FASTEXT(bits)((void)0)
#endif
#ifdef ENABLE_DHT
- #define HANDSHAKE_HAS_DHT(bits)(((bits)[7] & 0x01) ? 1 : 0)
+ #define HANDSHAKE_HAS_DHT(bits)(((bits)[7] & 0x01) != 0)
#define HANDSHAKE_SET_DHT(bits)((bits)[7] |= 0x01)
#else
- #define HANDSHAKE_HAS_DHT(bits)(0)
+ #define HANDSHAKE_HAS_DHT(bits)(false)
#define HANDSHAKE_SET_DHT(bits)((void)0)
#endif
#define HANDSHAKE_GET_EXTPREF(reserved) ((reserved)[5] & 0x03)
#define HANDSHAKE_SET_EXTPREF(reserved, val)((reserved)[5] |= 0x03 & (val))
-typedef uint8_t handshake_state_t;
-
-struct tr_handshake
-{
- bool haveReadAnythingFromPeer;
- bool havePeerID;
- bool haveSentBitTorrentHandshake;
- tr_peerIo * io;
- tr_crypto * crypto;
- tr_session * session;
- handshake_state_t state;
- tr_encryption_mode encryptionMode;
- uint16_t pad_c_len;
- uint16_t pad_d_len;
- uint16_t ia_len;
- uint32_t crypto_select;
- uint32_t crypto_provide;
- uint8_t myReq1[SHA_DIGEST_LENGTH];
- handshakeDoneCB doneCB;
- void * doneUserData;
- struct event * timeout_timer;
-};
-
/**
***
**/
-enum
+typedef enum
{
/* incoming */
AWAITING_HANDSHAKE,
AWAITING_PAD_D,
N_STATES
+}
+handshake_state_t;
+
+struct tr_handshake
+{
+ bool haveReadAnythingFromPeer;
+ bool havePeerID;
+ bool haveSentBitTorrentHandshake;
+ tr_peerIo * io;
+ tr_crypto * crypto;
+ tr_session * session;
+ handshake_state_t state;
+ tr_encryption_mode encryptionMode;
+ uint16_t pad_c_len;
+ uint16_t pad_d_len;
+ uint16_t ia_len;
+ uint32_t crypto_select;
+ uint32_t crypto_provide;
+ uint8_t myReq1[SHA_DIGEST_LENGTH];
+ handshakeDoneCB doneCB;
+ void * doneUserData;
+ struct event * timeout_timer;
};
/**
return success;
}
-static int tr_handshakeDone (tr_handshake * handshake,
- bool isConnected);
+static ReadState tr_handshakeDone (tr_handshake * handshake,
+ bool isConnected);
-enum
+typedef enum
{
HANDSHAKE_OK,
HANDSHAKE_ENCRYPTION_WRONG,
HANDSHAKE_BAD_TORRENT,
HANDSHAKE_PEER_IS_SELF,
-};
+}
+handshake_parse_err_t;
-static int
+static handshake_parse_err_t
parseHandshake (tr_handshake * handshake,
struct evbuffer * inbuf)
{
HANDSHAKE_SIZE, evbuffer_get_length (inbuf));
if (evbuffer_get_length (inbuf) < HANDSHAKE_SIZE)
- return READ_LATER;
+ return HANDSHAKE_ENCRYPTION_WRONG;
/* confirm the protocol */
tr_peerIoReadBytes (handshake->io, inbuf, name, HANDSHAKE_NAME_LEN);
tr_cryptoSecretKeySha1 (handshake->crypto, name, 4, NULL, 0, hash);
}
-static int
+static ReadState
readYb (tr_handshake * handshake, struct evbuffer * inbuf)
{
- int isEncrypted;
+ bool isEncrypted;
uint8_t yb[KEY_LEN];
struct evbuffer * outbuf;
size_t needlen = HANDSHAKE_NAME_LEN;
if (evbuffer_get_length (inbuf) < needlen)
return READ_LATER;
- isEncrypted = memcmp (evbuffer_pullup (inbuf, HANDSHAKE_NAME_LEN), HANDSHAKE_NAME, HANDSHAKE_NAME_LEN);
+ isEncrypted = memcmp (evbuffer_pullup (inbuf, HANDSHAKE_NAME_LEN), HANDSHAKE_NAME, HANDSHAKE_NAME_LEN) != 0;
if (isEncrypted)
{
needlen = KEY_LEN;
return READ_LATER;
}
-static int
+static ReadState
readVC (tr_handshake * handshake,
struct evbuffer * inbuf)
{
return READ_NOW;
}
-static int
+static ReadState
readCryptoSelect (tr_handshake * handshake,
struct evbuffer * inbuf)
{
return READ_NOW;
}
-static int
+static ReadState
readPadD (tr_handshake * handshake,
struct evbuffer * inbuf)
{
****
***/
-static int
+static ReadState
readHandshake (tr_handshake * handshake,
struct evbuffer * inbuf)
{
return READ_NOW;
}
-static int
+static ReadState
readPeerId (tr_handshake * handshake,
struct evbuffer * inbuf)
{
return tr_handshakeDone (handshake, !connected_to_self);
}
-static int
+static ReadState
readYa (tr_handshake * handshake,
struct evbuffer * inbuf)
{
return READ_NOW;
}
-static int
+static ReadState
readPadA (tr_handshake * handshake, struct evbuffer * inbuf)
{
/* resynchronizing on HASH ('req1',S) */
}
}
-static int
+static ReadState
readCryptoProvide (tr_handshake * handshake,
struct evbuffer * inbuf)
{
return READ_NOW;
}
-static int
+static ReadState
readPadC (tr_handshake * handshake,
struct evbuffer * inbuf)
{
return READ_NOW;
}
-static int
+static ReadState
readIA (tr_handshake * handshake,
struct evbuffer * inbuf)
{
return READ_NOW;
}
-static int
+static ReadState
readPayloadStream (tr_handshake * handshake,
struct evbuffer * inbuf)
{
- int i;
+ handshake_parse_err_t i;
const size_t needlen = HANDSHAKE_SIZE;
dbgmsg (handshake, "reading payload stream... have %"TR_PRIuSIZE", need %"TR_PRIuSIZE,
tr_free (handshake);
}
-static int
+static ReadState
tr_handshakeDone (tr_handshake * handshake, bool isOK)
{
bool success;