Skip to content

Commit

Permalink
feat(Core): Add support for retrieving self DHT ID and port
Browse files Browse the repository at this point in the history
To allow qTox (but just tests for now) to be bootstrapped off of.
  • Loading branch information
anthonybilinski committed Mar 11, 2022
1 parent 6e7cc6d commit 4f9ca0a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,22 @@ bool parseErr(Tox_Err_Conference_Delete error, int line)
}
}

bool parseErr(Tox_Err_Get_Port error, int line)
{
switch (error) {
case TOX_ERR_GET_PORT_OK:
return true;

case TOX_ERR_GET_PORT_NOT_BOUND:
qCritical() << line << "Tox instance was not bound to any port.";
return false;

default:
qCritical() << line << "Unknown Tox_Err_Get_Port error code:" << error;
return false;
}
}

QList<DhtServer> shuffleBootstrapNodes(QList<DhtServer> bootstrapNodes)
{
std::mt19937 rng(std::chrono::high_resolution_clock::now().time_since_epoch().count());
Expand Down Expand Up @@ -1296,6 +1312,25 @@ QPair<QByteArray, QByteArray> Core::getKeypair() const
return keypair;
}

QByteArray Core::getSelfDhtId() const
{
QMutexLocker ml{&coreLoopLock};
QByteArray dhtKey(TOX_PUBLIC_KEY_SIZE, 0x00);
tox_self_get_public_key(tox.get(), reinterpret_cast<uint8_t*>(dhtKey.data()));
return dhtKey;
}

int Core::getSelfUdpPort() const
{
QMutexLocker ml{&coreLoopLock};
Tox_Err_Get_Port error;
auto port = tox_self_get_udp_port(tox.get(), &error);
if (!PARSE_ERR(error)) {
return -1;
}
return port;
}

/**
* @brief Returns our status message, or an empty string on failure
*/
Expand Down
3 changes: 3 additions & 0 deletions src/core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class Core : public QObject,
ToxPk getSelfPublicKey() const override;
QPair<QByteArray, QByteArray> getKeypair() const;

QByteArray getSelfDhtId() const;
int getSelfUdpPort() const;

public slots:
void start();

Expand Down

0 comments on commit 4f9ca0a

Please sign in to comment.