Skip to content
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

use cbindgen for jwt and buildconfig #48016

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions cbindgen.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
include_guard = "RUST_BINDINGS_H"
namespace = "ffi"

[parse]
parse_deps = true
include = ["jsonwebtoken", "zmq"]

[export.rename]
"TimerWheel" = "CTimerWheel"
6 changes: 3 additions & 3 deletions src/cpp/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "config.h"

#include "rust/build_config.h"
#include "rust/bindings.h"

namespace Config {

Expand All @@ -34,11 +34,11 @@ Config & get()
{
Config *c = new Config;

BuildConfig *bc = build_config_new();
ffi::BuildConfig *bc = ffi::build_config_new();
c->version = QString(bc->version);
c->configDir = QString(bc->config_dir);
c->libDir = QString(bc->lib_dir);
build_config_destroy(bc);
ffi::build_config_destroy(bc);

g_config = c;
}
Expand Down
24 changes: 12 additions & 12 deletions src/cpp/jwt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,28 @@ EncodingKey::Private::Private() :
{
}

EncodingKey::Private::Private(JwtEncodingKey key) :
EncodingKey::Private::Private(ffi::JwtEncodingKey key) :
type((KeyType)key.type),
raw(key.key)
{
}

EncodingKey::Private::~Private()
{
jwt_encoding_key_destroy(raw);
ffi::jwt_encoding_key_destroy(raw);
}

EncodingKey EncodingKey::fromSecret(const QByteArray &key)
{
EncodingKey k;
k.d = new Private(jwt_encoding_key_from_secret((const quint8 *)key.data(), key.size()));
k.d = new Private(ffi::jwt_encoding_key_from_secret((const quint8 *)key.data(), key.size()));
return k;
}

EncodingKey EncodingKey::fromPem(const QByteArray &key)
{
EncodingKey k;
k.d = new Private(jwt_encoding_key_from_pem((const quint8 *)key.data(), key.size()));
k.d = new Private(ffi::jwt_encoding_key_from_pem((const quint8 *)key.data(), key.size()));
return k;
}

Expand Down Expand Up @@ -108,28 +108,28 @@ DecodingKey::Private::Private() :
{
}

DecodingKey::Private::Private(JwtDecodingKey key) :
DecodingKey::Private::Private(ffi::JwtDecodingKey key) :
type((KeyType)key.type),
raw(key.key)
{
}

DecodingKey::Private::~Private()
{
jwt_decoding_key_destroy(raw);
ffi::jwt_decoding_key_destroy(raw);
}

DecodingKey DecodingKey::fromSecret(const QByteArray &key)
{
DecodingKey k;
k.d = new Private(jwt_decoding_key_from_secret((const quint8 *)key.data(), key.size()));
k.d = new Private(ffi::jwt_decoding_key_from_secret((const quint8 *)key.data(), key.size()));
return k;
}

DecodingKey DecodingKey::fromPem(const QByteArray &key)
{
DecodingKey k;
k.d = new Private(jwt_decoding_key_from_pem((const quint8 *)key.data(), key.size()));
k.d = new Private(ffi::jwt_decoding_key_from_pem((const quint8 *)key.data(), key.size()));
return k;
}

Expand Down Expand Up @@ -177,14 +177,14 @@ QByteArray encodeWithAlgorithm(Algorithm alg, const QByteArray &claim, const Enc
{
char *token;

if(jwt_encode((int)alg, (const char *)claim.data(), key.raw(), &token) != 0)
if(ffi::jwt_encode((int)alg, (const char *)claim.data(), key.raw(), &token) != 0)
{
// error
return QByteArray();
}

QByteArray out = QByteArray(token);
jwt_str_destroy(token);
ffi::jwt_str_destroy(token);

return out;
}
Expand All @@ -193,14 +193,14 @@ QByteArray decodeWithAlgorithm(Algorithm alg, const QByteArray &token, const Dec
{
char *claim;

if(jwt_decode((int)alg, (const char *)token.data(), key.raw(), &claim) != 0)
if(ffi::jwt_decode((int)alg, (const char *)token.data(), key.raw(), &claim) != 0)
{
// error
return QByteArray();
}

QByteArray out = QByteArray(claim);
jwt_str_destroy(claim);
ffi::jwt_str_destroy(claim);

return out;
}
Expand Down
26 changes: 13 additions & 13 deletions src/cpp/jwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@
#include <QVariant>
#include <QSharedData>
#include <QDir>
#include "rust/jwt.h"
#include "rust/bindings.h"

class QString;

namespace Jwt {

enum KeyType {
Secret = JWT_KEYTYPE_SECRET,
Ec = JWT_KEYTYPE_EC,
Rsa = JWT_KEYTYPE_RSA,
Secret = ffi::JWT_KEYTYPE_SECRET,
Ec = ffi::JWT_KEYTYPE_EC,
Rsa = ffi::JWT_KEYTYPE_RSA,
};

enum Algorithm
{
HS256 = JWT_ALGORITHM_HS256,
ES256 = JWT_ALGORITHM_ES256,
RS256 = JWT_ALGORITHM_RS256,
HS256 = ffi::JWT_ALGORITHM_HS256,
ES256 = ffi::JWT_ALGORITHM_ES256,
RS256 = ffi::JWT_ALGORITHM_RS256,
};

class EncodingKey
Expand All @@ -53,7 +53,7 @@ class EncodingKey
bool isNull() const { return !d; }
KeyType type() const { if(d) { return d->type; } else { return (KeyType)-1; } }

const void *raw() const { if(d) { return d->raw; } else { return 0; } }
const ffi::EncodingKey *raw() const { if(d) { return d->raw; } else { return 0; } }

static EncodingKey fromSecret(const QByteArray &key);
static EncodingKey fromPem(const QByteArray &key);
Expand All @@ -65,10 +65,10 @@ class EncodingKey
{
public:
KeyType type;
void *raw;
ffi::EncodingKey *raw;

Private();
Private(JwtEncodingKey key);
Private(ffi::JwtEncodingKey key);
~Private();
};

Expand All @@ -81,7 +81,7 @@ class DecodingKey
bool isNull() const { return !d; }
KeyType type() const { if(d) { return d->type; } else { return (KeyType)-1; } }

const void *raw() const { if(d) { return d->raw; } else { return 0; } }
const ffi::DecodingKey *raw() const { if(d) { return d->raw; } else { return 0; } }

static DecodingKey fromSecret(const QByteArray &key);
static DecodingKey fromPem(const QByteArray &key);
Expand All @@ -93,10 +93,10 @@ class DecodingKey
{
public:
KeyType type;
void *raw;
ffi::DecodingKey *raw;

Private();
Private(JwtDecodingKey key);
Private(ffi::JwtDecodingKey key);
~Private();
};

Expand Down
2 changes: 2 additions & 0 deletions src/cpp/tests/tests.pro
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ INCLUDEPATH += $$SRC_DIR
INCLUDEPATH += $$SRC_DIR/proxy
INCLUDEPATH += $$SRC_DIR/handler
INCLUDEPATH += $$SRC_DIR/runner

INCLUDEPATH += $$PWD/../../../target/include
INCLUDEPATH += $$QZMQ_DIR/src

DEFINES += NO_IRISNET
Expand Down
14 changes: 7 additions & 7 deletions src/cpp/timerwheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,37 @@

TimerWheel::TimerWheel(int capacity)
{
raw_ = timer_wheel_create(capacity);
raw_ = ffi::timer_wheel_create(capacity);
}

TimerWheel::~TimerWheel()
{
timer_wheel_destroy(raw_);
ffi::timer_wheel_destroy(raw_);
}

int TimerWheel::add(quint64 expires, size_t userData)
{
return timer_add(raw_, expires, userData);
return ffi::timer_add(raw_, expires, userData);
}

void TimerWheel::remove(int key)
{
timer_remove(raw_, key);
ffi::timer_remove(raw_, key);
}

qint64 TimerWheel::timeout() const
{
return timer_wheel_timeout(raw_);
return ffi::timer_wheel_timeout(raw_);
}

void TimerWheel::update(quint64 curtime)
{
timer_wheel_update(raw_, curtime);
ffi::timer_wheel_update(raw_, curtime);
}

TimerWheel::Expired TimerWheel::takeExpired()
{
ExpiredTimer ret = timer_wheel_take_expired(raw_);
ffi::ExpiredTimer ret = ffi::timer_wheel_take_expired(raw_);

Expired expired;
expired.key = ret.key;
Expand Down
6 changes: 4 additions & 2 deletions src/cpp/timerwheel.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

#include <QPair>

struct CTimerWheel;
namespace ffi {
struct TimerWheel;
}

class TimerWheel
{
Expand Down Expand Up @@ -53,7 +55,7 @@ class TimerWheel
Expired takeExpired();

private:
CTimerWheel *raw_;
ffi::TimerWheel *raw_;
};

#endif
20 changes: 10 additions & 10 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ pub unsafe extern "C" fn timer_wheel_take_expired(wheel: *mut TimerWheel) -> Exp
}
}

const JWT_KEYTYPE_SECRET: libc::c_int = 0;
const JWT_KEYTYPE_EC: libc::c_int = 1;
const JWT_KEYTYPE_RSA: libc::c_int = 2;
const JWT_ALGORITHM_HS256: libc::c_int = 0;
const JWT_ALGORITHM_ES256: libc::c_int = 1;
const JWT_ALGORITHM_RS256: libc::c_int = 2;
pub const JWT_KEYTYPE_SECRET: libc::c_int = 0;
pub const JWT_KEYTYPE_EC: libc::c_int = 1;
pub const JWT_KEYTYPE_RSA: libc::c_int = 2;
pub const JWT_ALGORITHM_HS256: libc::c_int = 0;
pub const JWT_ALGORITHM_ES256: libc::c_int = 1;
pub const JWT_ALGORITHM_RS256: libc::c_int = 2;

#[repr(C)]
pub struct JwtEncodingKey {
Expand Down Expand Up @@ -269,7 +269,7 @@ pub unsafe extern "C" fn jwt_encode(
alg: libc::c_int,
claim: *const c_char,
key: *const jsonwebtoken::EncodingKey,
out_token: *mut *const c_char,
out_token: *mut *mut c_char,
) -> libc::c_int {
if claim.is_null() || out_token.is_null() {
return 1; // null pointers
Expand Down Expand Up @@ -313,7 +313,7 @@ pub unsafe extern "C" fn jwt_decode(
alg: libc::c_int,
token: *const c_char,
key: *const jsonwebtoken::DecodingKey,
out_claim: *mut *const c_char,
out_claim: *mut *mut c_char,
) -> libc::c_int {
if token.is_null() || out_claim.is_null() {
return 1; // null pointers
Expand Down Expand Up @@ -1084,7 +1084,7 @@ pub struct BuildConfig {
}

#[no_mangle]
pub fn build_config_new() -> *mut BuildConfig {
pub extern "C" fn build_config_new() -> *mut BuildConfig {
let lib_dir = env!("LIB_DIR");
let config_dir = env!("CONFIG_DIR");

Expand All @@ -1099,7 +1099,7 @@ pub fn build_config_new() -> *mut BuildConfig {

#[allow(clippy::missing_safety_doc)]
#[no_mangle]
pub unsafe fn build_config_destroy(c: *mut BuildConfig) {
pub unsafe extern "C" fn build_config_destroy(c: *mut BuildConfig) {
let c = match c.as_mut() {
Some(c) => Box::from_raw(c),
None => return,
Expand Down
39 changes: 0 additions & 39 deletions src/rust/build_config.h

This file was deleted.

Loading
Loading