8000 Restyle cleanup: Split large switch statement into functions. by restyled-io[bot] · Pull Request #1907 · TokTok/c-toxcore · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Restyle cleanup: Split large switch statement into functions. #1907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
8000
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 85 additions & 60 deletions toxcore/net_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -1628,95 +1628,120 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const
return 0;
}

/* Handle a packet that was received for the connection.
*
* return -1 on failure.
* return 0 on success.
*/
static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length,
bool udp, void *userdata)
static int handle_packet_cookie_response(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length)
{
if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) {
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);

if (conn == nullptr) {
return -1;
}

if (conn->status != CRYPTO_CONN_COOKIE_REQUESTING) {
return -1;
}

uint8_t cookie[COOKIE_LENGTH];
uint64_t number;

if (handle_cookie_response(c->log, cookie, &number, packet, length, conn->shared_key 10000 ) != sizeof(cookie)) {
return -1;
}

if (number != conn->cookie_request_number) {
return -1;
}

if (create_send_handshake(c, crypt_connection_id, cookie, conn->dht_public_key) != 0) {
return -1;
}

conn->status = CRYPTO_CONN_HANDSHAKE_SENT;
return 0;
}

static int handle_packet_crypto_hs(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length,
void *userdata)
{
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);

if (conn == nullptr) {
return -1;
}

switch (packet[0]) {
case NET_PACKET_COOKIE_RESPONSE: {
if (conn->status != CRYPTO_CONN_COOKIE_REQUESTING) {
return -1;
}
if (conn->status != CRYPTO_CONN_COOKIE_REQUESTING
&& conn->status != CRYPTO_CONN_HANDSHAKE_SENT
&& conn->status != CRYPTO_CONN_NOT_CONFIRMED) {
return -1;
}

uint8_t cookie[COOKIE_LENGTH];
uint64_t number;
uint8_t peer_real_pk[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t dht_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t cookie[COOKIE_LENGTH];

if (handle_cookie_response(c->log, cookie, &number, packet, length, conn->shared_key) != sizeof(cookie)) {
return -1;
}
if (handle_crypto_handshake(c, conn->recv_nonce, conn->peersessionpublic_key, peer_real_pk, dht_public_key, cookie,
packet, length, conn->public_key) != 0) {
return -1;
}

if (number != conn->cookie_request_number) {
return -1;
}
if (public_key_cmp(dht_public_key, conn->dht_public_key) == 0) {
encrypt_precompute(conn->peersessionpublic_key, conn->sessionsecret_key, conn->shared_key);

if (create_send_handshake(c, crypt_connection_id, cookie, conn->dht_public_key) != 0) {
if (conn->status == CRYPTO_CONN_COOKIE_REQUESTING) {
if (create_send_handshake(c, crypt_connection_id, cookie, dht_public_key) != 0) {
return -1;
}
}

conn->status = CRYPTO_CONN_HANDSHAKE_SENT;
return 0;
conn->status = CRYPTO_CONN_NOT_CONFIRMED;
} else {
if (conn->dht_pk_callback) {
conn->dht_pk_callback(conn->dht_pk_callback_object, conn->dht_pk_callback_number, dht_public_key, userdata);
}
}

case NET_PACKET_CRYPTO_HS: {
if (conn->status != CRYPTO_CONN_COOKIE_REQUESTING
&& conn->status != CRYPTO_CONN_HANDSHAKE_SENT
&& conn->status != CRYPTO_CONN_NOT_CONFIRMED) {
return -1;
}
return 0;
}

uint8_t peer_real_pk[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t dht_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t cookie[COOKIE_LENGTH];
static int handle_packet_crypto_data(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length,
bool udp, void *userdata)
{
const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);

if (handle_crypto_handshake(c, conn->recv_nonce, conn->peersessionpublic_key, peer_real_pk, dht_public_key, cookie,
packet, length, conn->public_key) != 0) {
return -1;
}
if (conn == nullptr) {
return -1;
}

if (public_key_cmp(dht_public_key, conn->dht_public_key) == 0) {
encrypt_precompute(conn->peersessionpublic_key, conn->sessionsecret_key, conn->shared_key);
if (conn->status != CRYPTO_CONN_NOT_CONFIRMED && conn->status != CRYPTO_CONN_ESTABLISHED) {
return -1;
}

if (conn->status == CRYPTO_CONN_COOKIE_REQUESTING) {
if (create_send_handshake(c, crypt_connection_id, cookie, dht_public_key) != 0) {
return -1;
}
}
return handle_data_packet_core(c, crypt_connection_id, packet, length, udp, userdata);
}

conn->status = CRYPTO_CONN_NOT_CONFIRMED;
} else {
if (conn->dht_pk_callback) {
conn->dht_pk_callback(conn->dht_pk_callback_object, conn->dht_pk_callback_number, dht_public_key, userdata);
}
}
/* Handle a packet that was received for the connection.
*
* return -1 on failure.
* return 0 on success.
*/
static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length,
bool udp, void *userdata)
{
if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) {
return -1;
}

return 0;
}
switch (packet[0]) {
case NET_PACKET_COOKIE_RESPONSE:
return handle_packet_cookie_response(c, crypt_connection_id, packet, length);

case NET_PACKET_CRYPTO_DATA: {
if (conn->status != CRYPTO_CONN_NOT_CONFIRMED && conn->status != CRYPTO_CONN_ESTABLISHED) {
return -1;
}
case NET_PACKET_CRYPTO_HS:
return handle_packet_crypto_hs(c, crypt_connection_id, packet, length, userdata);

return handle_data_packet_core(c, crypt_connection_id, packet, length, udp, userdata);
}
case NET_PACKET_CRYPTO_DATA:
return handle_packet_crypto_data(c, crypt_connection_id, packet, length, udp, userdata);

default: {
default:
return -1;
}
}
}

Expand Down
0