Skip to content

Commit

Permalink
zrtp: Refactored ZRTP for slightly cleaner code
Browse files Browse the repository at this point in the history
  • Loading branch information
jrsnen committed Aug 26, 2024
1 parent 4d02e41 commit 8019b4d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 48 deletions.
58 changes: 24 additions & 34 deletions src/zrtp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ using namespace uvgrtp::zrtp_msg;
#define ZRTP_VERSION 110

uvgrtp::zrtp::zrtp():
ssrc_(0),
initialized_(false),
dh_finished_(false),
remote_addr_(),
remote_ip6_addr_(),
initialized_(false),
hello_(nullptr),
hello_ack_(nullptr),
commit_(nullptr),
Expand All @@ -45,8 +45,7 @@ uvgrtp::zrtp::zrtp():
confack_(nullptr),
hello_len_(0),
commit_len_(0),
dh_len_(0),
zrtp_busy_(false)
dh_len_(0)
{
cctx_.sha256 = new uvgrtp::crypto::sha256;
cctx_.dh = new uvgrtp::crypto::dh;
Expand Down Expand Up @@ -789,14 +788,7 @@ rtp_error_t uvgrtp::zrtp::init_dhm(uint32_t ssrc, std::shared_ptr<uvgrtp::socket
UVG_LOG_DEBUG("Starting ZRTP Diffie-Hellman negotiation with %s", uvgrtp::socket::sockaddr_to_string(addr).c_str());
}

hello_ = nullptr;
hello_ack_ = nullptr;
commit_ = nullptr;
dh1_ = nullptr;
dh2_ = nullptr;
conf1_ = nullptr;
conf2_ = nullptr;
confack_ = nullptr;
resetSession(ssrc, socket, addr, addr6);

/* TODO: set all fields initially to zero */
memset(session_.hash_ctx.o_hvi, 0, sizeof(session_.hash_ctx.o_hvi));
Expand All @@ -808,13 +800,6 @@ rtp_error_t uvgrtp::zrtp::init_dhm(uint32_t ssrc, std::shared_ptr<uvgrtp::socket
/* Initialize the session hashes H0 - H3 defined in Section 9 of RFC 6189 */
init_session_hashes();

local_socket_ = socket;
remote_addr_ = addr;
remote_ip6_addr_ = addr6;

session_.seq = 0;
session_.ssrc = ssrc;

/* Begin session by exchanging Hello and HelloACK messages.
*
* After begin_session() we know what remote is capable of
Expand Down Expand Up @@ -888,21 +873,7 @@ rtp_error_t uvgrtp::zrtp::init_msm(uint32_t ssrc, std::shared_ptr<uvgrtp::socket
{
rtp_error_t ret;

local_socket_ = socket;
remote_addr_ = addr;
remote_ip6_addr_ = addr6;

session_.ssrc = ssrc;
session_.seq = 0;

hello_ = nullptr;
hello_ack_ = nullptr;
commit_ = nullptr;
dh1_ = nullptr;
dh2_ = nullptr;
conf1_ = nullptr;
conf2_ = nullptr;
confack_ = nullptr;
resetSession(ssrc, socket, addr, addr6);

UVG_LOG_DEBUG("Generating ZRTP keys in multistream mode");

Expand Down Expand Up @@ -1243,4 +1214,23 @@ rtp_error_t uvgrtp::zrtp::packet_handler(void* args, int rce_flags, uint8_t* rea
return RTP_OK;
}
}
}

void uvgrtp::zrtp::resetSession(uint32_t ssrc, std::shared_ptr<uvgrtp::socket> socket,
sockaddr_in& addr, sockaddr_in6& addr6){
local_socket_ = socket;
remote_addr_ = addr;
remote_ip6_addr_ = addr6;

session_.ssrc = ssrc;
session_.seq = 0;

hello_ = nullptr;
hello_ack_ = nullptr;
commit_ = nullptr;
dh1_ = nullptr;
dh2_ = nullptr;
conf1_ = nullptr;
conf2_ = nullptr;
confack_ = nullptr;
}
33 changes: 19 additions & 14 deletions src/zrtp.hh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ namespace uvgrtp {
*
* Return RTP_OK on success
* Return RTP_TIMEOUT if remote did not send messages in timely manner */
rtp_error_t init(uint32_t ssrc, std::shared_ptr<uvgrtp::socket> socket, sockaddr_in& addr, sockaddr_in6& addr6, bool perform_dh, bool ipv6);
rtp_error_t init(uint32_t ssrc, std::shared_ptr<uvgrtp::socket> socket, sockaddr_in& addr, sockaddr_in6& addr6,
bool perform_dh, bool ipv6);

/* Get SRTP keys for the session that was just initialized
*
Expand Down Expand Up @@ -195,42 +196,46 @@ namespace uvgrtp {
* Before this step validate_session() is called to make sure we have a valid session */
rtp_error_t initiator_finalize_session();

uint32_t ssrc_;
std::shared_ptr<uvgrtp::socket> local_socket_;
sockaddr_in remote_addr_;
sockaddr_in6 remote_ip6_addr_;
void resetSession(uint32_t ssrc, std::shared_ptr<uvgrtp::socket> socket,
sockaddr_in& addr, sockaddr_in6& addr6);

/* Has the ZRTP connection been initialized using DH */
bool initialized_;
bool dh_finished_ = false;

std::shared_ptr<uvgrtp::socket> local_socket_;
sockaddr_in remote_addr_;
sockaddr_in6 remote_ip6_addr_;

/* Our own and remote capability structs */
zrtp_capab_t capab_;
zrtp_capab_t rcapab_;
//zrtp_capab_t capab_;
//zrtp_capab_t rcapab_;

zrtp_crypto_ctx_t cctx_;
zrtp_session_t session_;

std::mutex zrtp_mtx_;

// received ZRTP messages
uvgrtp::zrtp_msg::zrtp_hello* hello_;
size_t hello_len_;

uvgrtp::zrtp_msg::zrtp_hello_ack* hello_ack_;

uvgrtp::zrtp_msg::zrtp_commit* commit_;
size_t commit_len_;

uvgrtp::zrtp_msg::zrtp_dh* dh1_;
uvgrtp::zrtp_msg::zrtp_dh* dh2_;
size_t dh_len_;

uvgrtp::zrtp_msg::zrtp_confirm* conf1_;
uvgrtp::zrtp_msg::zrtp_confirm* conf2_;
uvgrtp::zrtp_msg::zrtp_confack* confack_;

size_t hello_len_;
size_t commit_len_;
size_t dh_len_;

std::mutex state_mutex_;
bool dh_finished_ = false;

std::mutex busy_mutex_;
bool zrtp_busy_;

};
}

Expand Down

0 comments on commit 8019b4d

Please sign in to comment.