mbedsock: Init

This commit is contained in:
2022-11-07 23:20:45 +01:00
parent 276ad4f22b
commit 4ef73546aa
17 changed files with 911 additions and 1 deletions

View File

@@ -0,0 +1,293 @@
// AUTO GENERATED FILE, DO NOT EDIT.
//
// Generated by `package:ffigen`.
import 'dart:ffi' as ffi;
class NativeLibrary {
/// Holds the symbol lookup function.
final ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
_lookup;
/// The symbols are looked up in [dynamicLibrary].
NativeLibrary(ffi.DynamicLibrary dynamicLibrary)
: _lookup = dynamicLibrary.lookup;
/// The symbols are looked up with [lookup].
NativeLibrary.fromLookup(
ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
lookup)
: _lookup = lookup;
int mbedsock_ctx_new(
ffi.Pointer<mbedsock_ctx> ctx,
ffi.Pointer<ffi.Int8> capath,
) {
return _mbedsock_ctx_new(
ctx,
capath,
);
}
late final _mbedsock_ctx_newPtr = _lookup<
ffi.NativeFunction<
ffi.Int32 Function(ffi.Pointer<mbedsock_ctx>,
ffi.Pointer<ffi.Int8>)>>('mbedsock_ctx_new');
late final _mbedsock_ctx_new = _mbedsock_ctx_newPtr.asFunction<
int Function(ffi.Pointer<mbedsock_ctx>, ffi.Pointer<ffi.Int8>)>();
ffi.Pointer<mbedsock_ctx> mbedsock_ctx_new_ex(
ffi.Pointer<ffi.Int8> capath,
) {
return _mbedsock_ctx_new_ex(
capath,
);
}
late final _mbedsock_ctx_new_exPtr = _lookup<
ffi.NativeFunction<
ffi.Pointer<mbedsock_ctx> Function(
ffi.Pointer<ffi.Int8>)>>('mbedsock_ctx_new_ex');
late final _mbedsock_ctx_new_ex = _mbedsock_ctx_new_exPtr
.asFunction<ffi.Pointer<mbedsock_ctx> Function(ffi.Pointer<ffi.Int8>)>();
int mbedsock_new(
ffi.Pointer<mbedsock_ctx> ctx,
ffi.Pointer<mbedsock> sock,
) {
return _mbedsock_new(
ctx,
sock,
);
}
late final _mbedsock_newPtr = _lookup<
ffi.NativeFunction<
ffi.Int32 Function(ffi.Pointer<mbedsock_ctx>,
ffi.Pointer<mbedsock>)>>('mbedsock_new');
late final _mbedsock_new = _mbedsock_newPtr.asFunction<
int Function(ffi.Pointer<mbedsock_ctx>, ffi.Pointer<mbedsock>)>();
ffi.Pointer<mbedsock> mbedsock_new_ex(
ffi.Pointer<mbedsock_ctx> ctx,
) {
return _mbedsock_new_ex(
ctx,
);
}
late final _mbedsock_new_exPtr = _lookup<
ffi.NativeFunction<
ffi.Pointer<mbedsock> Function(
ffi.Pointer<mbedsock_ctx>)>>('mbedsock_new_ex');
late final _mbedsock_new_ex = _mbedsock_new_exPtr
.asFunction<ffi.Pointer<mbedsock> Function(ffi.Pointer<mbedsock_ctx>)>();
void mbedsock_free(
ffi.Pointer<mbedsock> sock,
) {
return _mbedsock_free(
sock,
);
}
late final _mbedsock_freePtr =
_lookup<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<mbedsock>)>>(
'mbedsock_free');
late final _mbedsock_free =
_mbedsock_freePtr.asFunction<void Function(ffi.Pointer<mbedsock>)>();
void mbedsock_free_ex(
ffi.Pointer<mbedsock> sock,
) {
return _mbedsock_free_ex(
sock,
);
}
late final _mbedsock_free_exPtr =
_lookup<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<mbedsock>)>>(
'mbedsock_free_ex');
late final _mbedsock_free_ex =
_mbedsock_free_exPtr.asFunction<void Function(ffi.Pointer<mbedsock>)>();
void mbedsock_ctx_free(
ffi.Pointer<mbedsock_ctx> ctx,
) {
return _mbedsock_ctx_free(
ctx,
);
}
late final _mbedsock_ctx_freePtr =
_lookup<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<mbedsock_ctx>)>>(
'mbedsock_ctx_free');
late final _mbedsock_ctx_free = _mbedsock_ctx_freePtr
.asFunction<void Function(ffi.Pointer<mbedsock_ctx>)>();
void mbedsock_ctx_free_ex(
ffi.Pointer<mbedsock_ctx> ctx,
) {
return _mbedsock_ctx_free_ex(
ctx,
);
}
late final _mbedsock_ctx_free_exPtr =
_lookup<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<mbedsock_ctx>)>>(
'mbedsock_ctx_free_ex');
late final _mbedsock_ctx_free_ex = _mbedsock_ctx_free_exPtr
.asFunction<void Function(ffi.Pointer<mbedsock_ctx>)>();
int mbedsock_do_handshake(
ffi.Pointer<mbedsock> sock,
ffi.Pointer<ffi.Int8> alpn,
ffi.Pointer<ffi.Int8> sni,
) {
return _mbedsock_do_handshake(
sock,
alpn,
sni,
);
}
late final _mbedsock_do_handshakePtr = _lookup<
ffi.NativeFunction<
ffi.Int32 Function(ffi.Pointer<mbedsock>, ffi.Pointer<ffi.Int8>,
ffi.Pointer<ffi.Int8>)>>('mbedsock_do_handshake');
late final _mbedsock_do_handshake = _mbedsock_do_handshakePtr.asFunction<
int Function(ffi.Pointer<mbedsock>, ffi.Pointer<ffi.Int8>,
ffi.Pointer<ffi.Int8>)>();
int mbedsock_connect_secure(
ffi.Pointer<mbedsock> sock,
ffi.Pointer<ffi.Int8> host,
ffi.Pointer<ffi.Int8> port,
ffi.Pointer<ffi.Int8> alpn,
ffi.Pointer<ffi.Int8> sni,
) {
return _mbedsock_connect_secure(
sock,
host,
port,
alpn,
sni,
);
}
late final _mbedsock_connect_securePtr = _lookup<
ffi.NativeFunction<
ffi.Int32 Function(
ffi.Pointer<mbedsock>,
ffi.Pointer<ffi.Int8>,
ffi.Pointer<ffi.Int8>,
ffi.Pointer<ffi.Int8>,
ffi.Pointer<ffi.Int8>)>>('mbedsock_connect_secure');
late final _mbedsock_connect_secure = _mbedsock_connect_securePtr.asFunction<
int Function(
ffi.Pointer<mbedsock>,
ffi.Pointer<ffi.Int8>,
ffi.Pointer<ffi.Int8>,
ffi.Pointer<ffi.Int8>,
ffi.Pointer<ffi.Int8>)>();
int mbedsock_connect(
ffi.Pointer<mbedsock> sock,
ffi.Pointer<ffi.Int8> host,
ffi.Pointer<ffi.Int8> port,
) {
return _mbedsock_connect(
sock,
host,
port,
);
}
late final _mbedsock_connectPtr = _lookup<
ffi.NativeFunction<
ffi.Int32 Function(ffi.Pointer<mbedsock>, ffi.Pointer<ffi.Int8>,
ffi.Pointer<ffi.Int8>)>>('mbedsock_connect');
late final _mbedsock_connect = _mbedsock_connectPtr.asFunction<
int Function(ffi.Pointer<mbedsock>, ffi.Pointer<ffi.Int8>,
ffi.Pointer<ffi.Int8>)>();
int mbedsock_write(
ffi.Pointer<mbedsock> sock,
ffi.Pointer<ffi.Uint8> data,
int len,
) {
return _mbedsock_write(
sock,
data,
len,
);
}
late final _mbedsock_writePtr = _lookup<
ffi.NativeFunction<
ffi.Int32 Function(ffi.Pointer<mbedsock>, ffi.Pointer<ffi.Uint8>,
ffi.Int32)>>('mbedsock_write');
late final _mbedsock_write = _mbedsock_writePtr.asFunction<
int Function(ffi.Pointer<mbedsock>, ffi.Pointer<ffi.Uint8>, int)>();
int mbedsock_read(
ffi.Pointer<mbedsock> sock,
ffi.Pointer<ffi.Uint8> buf,
int len,
) {
return _mbedsock_read(
sock,
buf,
len,
);
}
late final _mbedsock_readPtr = _lookup<
ffi.NativeFunction<
ffi.Int32 Function(ffi.Pointer<mbedsock>, ffi.Pointer<ffi.Uint8>,
ffi.Int32)>>('mbedsock_read');
late final _mbedsock_read = _mbedsock_readPtr.asFunction<
int Function(ffi.Pointer<mbedsock>, ffi.Pointer<ffi.Uint8>, int)>();
int mbedsock_is_secure(
ffi.Pointer<mbedsock> sock,
) {
return _mbedsock_is_secure(
sock,
);
}
late final _mbedsock_is_securePtr =
_lookup<ffi.NativeFunction<ffi.Int32 Function(ffi.Pointer<mbedsock>)>>(
'mbedsock_is_secure');
late final _mbedsock_is_secure =
_mbedsock_is_securePtr.asFunction<int Function(ffi.Pointer<mbedsock>)>();
}
class mbedsock_ctx extends ffi.Struct {
@ffi.Int32()
external int entropy;
@ffi.Int32()
external int ctr_drbg;
@ffi.Int32()
external int chain;
}
class mbedsock extends ffi.Struct {
@ffi.Int32()
external int ssl;
@ffi.Int32()
external int conf;
@ffi.Int32()
external int server_fd;
@ffi.Int32()
external int secure;
}
const String SSL_PERS = 'moxxmpp_socket';
const int SSL_PERS_LEN = 15;

View File

@@ -0,0 +1,86 @@
import 'dart:io';
import 'dart:ffi';
import 'dart:typed_data';
import 'package:ffi/ffi.dart';
import 'package:path/path.dart' as path;
import 'package:moxxmpp_socket/src/generated/ffi.dart' as libmbedsock;
//final libPath = path.join(Directory.current.path, 'libmbedsock.so');
final lib = libmbedsock.NativeLibrary(DynamicLibrary.open('libmbedsock.so'));
class MbedSockCtx {
late Pointer<libmbedsock.mbedsock_ctx> _ctxPtr;
MbedSockCtx(String caPath) {
final caPathNative = caPath.toNativeUtf8();
_ctxPtr = lib.mbedsock_ctx_new_ex(caPathNative.cast());
malloc.free(caPathNative);
}
void free() {
lib.mbedsock_ctx_free_ex(_ctxPtr);
}
Pointer<libmbedsock.mbedsock_ctx> get ctx => _ctxPtr;
}
class MbedSock {
late Pointer<libmbedsock.mbedsock> sock;
MbedSock(MbedSockCtx ctx) {
sock = lib.mbedsock_new_ex(ctx.ctx);
}
bool connect(String host, int port) {
final nativeHost = host.toNativeUtf8();
final nativePort = port.toString().toNativeUtf8();
final ret = lib.mbedsock_connect(sock, nativeHost.cast(), nativePort.cast());
malloc
..free(nativeHost)
..free(nativePort);
return ret == 0;
}
bool connectSecure(String host, String port, {String? alpn, String? hostname}) {
final nativeHost = host.toNativeUtf8();
final nativePort = port.toNativeUtf8();
final nativeAlpn = alpn != null ? alpn.toNativeUtf8() : nullptr;
final nativeHostname = hostname != null ? hostname.toNativeUtf8() : nullptr;
final ret = lib.mbedsock_connect_secure(
sock,
nativeHost.cast(),
nativePort.cast(),
nativeAlpn.cast(),
nativeHostname.cast(),
);
malloc
..free(nativeHost)
..free(nativePort);
if (alpn != null) {
malloc.free(nativeAlpn);
}
if (hostname != null) {
malloc.free(nativeHostname);
}
print(ret);
return ret == 0;
}
bool isSecure() {
return lib.mbedsock_is_secure(sock) == 1;
}
void write(String data) {
//lib.mbedsock_write(sock, data, data.length);
}
void free() {
lib.mbedsock_free_ex(sock);
}
}