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

@@ -1,4 +1,5 @@
# This is a generated file; do not edit or check into version control.
integration_test=/nix/store/8gcfk0g1lg8gccd9kv3rzj910w9pz1kj-flutter-3.3.3-unwrapped/packages/integration_test/
moxdns=/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns-0.1.4/
moxdns_android=/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_android-0.1.4/
moxdns_linux=/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_linux-0.1.4/

View File

@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[],"android":[{"name":"moxdns_android","path":"/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_android-0.1.4/","native_build":true,"dependencies":[]}],"macos":[],"linux":[{"name":"moxdns_linux","path":"/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_linux-0.1.4/","native_build":true,"dependencies":[]}],"windows":[],"web":[]},"dependencyGraph":[{"name":"moxdns","dependencies":["moxdns_android","moxdns_linux"]},{"name":"moxdns_android","dependencies":["moxdns"]},{"name":"moxdns_linux","dependencies":["moxdns"]}],"date_created":"2022-11-05 14:01:31.714716","version":"3.3.3"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"integration_test","path":"/nix/store/8gcfk0g1lg8gccd9kv3rzj910w9pz1kj-flutter-3.3.3-unwrapped/packages/integration_test/","native_build":true,"dependencies":[]}],"android":[{"name":"integration_test","path":"/nix/store/8gcfk0g1lg8gccd9kv3rzj910w9pz1kj-flutter-3.3.3-unwrapped/packages/integration_test/","native_build":true,"dependencies":[]},{"name":"moxdns_android","path":"/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_android-0.1.4/","native_build":true,"dependencies":[]}],"macos":[],"linux":[{"name":"moxdns_linux","path":"/home/alexander/.pub-cache/hosted/git.polynom.me%47api%47packages%47Moxxy%47pub%47/moxdns_linux-0.1.4/","native_build":true,"dependencies":[]}],"windows":[],"web":[]},"dependencyGraph":[{"name":"integration_test","dependencies":[]},{"name":"moxdns","dependencies":["moxdns_android","moxdns_linux"]},{"name":"moxdns_android","dependencies":["moxdns"]},{"name":"moxdns_linux","dependencies":["moxdns"]}],"date_created":"2022-11-07 22:04:34.759562","version":"3.3.3"}

44
moxxmpp_socket/.gitignore vendored Normal file
View File

@@ -0,0 +1,44 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/
# IntelliJ related
*.iml
*.ipr
*.iws
.idea/
# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/
# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/
# Symbolication related
app.*.symbols
# Obfuscation related
app.*.map.json
# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release

30
moxxmpp_socket/.metadata Normal file
View File

@@ -0,0 +1,30 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled.
version:
revision: 18a827f3933c19f51862dde3fa472197683249d6
channel: stable
project_type: app
# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: 18a827f3933c19f51862dde3fa472197683249d6
base_revision: 18a827f3933c19f51862dde3fa472197683249d6
- platform: linux
create_revision: 18a827f3933c19f51862dde3fa472197683249d6
base_revision: 18a827f3933c19f51862dde3fa472197683249d6
# User provided section
# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'

View File

@@ -0,0 +1,15 @@
include: package:very_good_analysis/analysis_options.yaml
linter:
rules:
public_member_api_docs: false
lines_longer_than_80_chars: false
use_setters_to_change_properties: false
avoid_positional_boolean_parameters: false
avoid_bool_literals_in_conditional_expressions: false
analyzer:
exclude:
- "**/*.g.dart"
- "**/*.freezed.dart"
- "lib/src/generated/*.dart"
- "test/"

View File

@@ -0,0 +1,29 @@
import 'dart:io';
import 'package:moxxmpp_socket/src/ssl.dart';
void main(List<String> argv) async {
if (argv.length != 2) {
print('Usage: test_wrong_host.dart server-addr host-name');
exit(1);
}
final server = argv[0];
final hostname = argv[1];
final ctx = MbedSockCtx('/etc/ssl/certs/');
final sock = MbedSock(ctx);
print('Connecting...');
final done = sock.connectSecure(
server,
'5223',
alpn: 'xmpp-client',
hostname: hostname,
);
print('Success? $done');
print('Secure? ${sock.isSecure()}');
sock.free();
ctx.free();
print('OKAY');
}

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);
}
}

View File

@@ -9,6 +9,7 @@ environment:
flutter: '>=2.13.0-0.1'
dependencies:
ffi:
logging: 1.0.2
moxdns:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
@@ -16,8 +17,20 @@ dependencies:
moxxmpp:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.0
path: 1.8.2
dev_dependencies:
integration_test:
sdk: flutter
ffigen: ^4.1.2
lints: ^2.0.0
test: ^1.16.0
very_good_analysis: ^3.0.1
ffigen:
output: 'lib/src/generated/ffi.dart'
llvm-path:
- '/nix/store/zq6966rjlmmwjym4jlnymjwwgjhcgryz-clang-11.1.0-lib'
headers:
entry-points:
- ../mbedsock/mbedsock.h

View File

@@ -0,0 +1,30 @@
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility in the flutter_test package. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:moxxmpp_socket/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}