Skip to content

Commit

Permalink
fixup! refactor: adjust to new tinydtls API
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Aug 1, 2022
1 parent 5aaf6df commit d4401e6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 52 deletions.
17 changes: 9 additions & 8 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import 'types.dart';
import 'util.dart';

int _handleWrite(Pointer<dtls_context_t> context, Pointer<session_t> session,
Pointer<ffi.UnsignedChar> dataAddress, int dataLength) {
Pointer<ffi.Uint8> dataAddress, int dataLength) {
final data =
dataAddress.cast<Uint8>().asTypedList(dataLength).buffer.asUint8List();
final address = addressFromSession(session);
Expand All @@ -34,7 +34,7 @@ int _handleWrite(Pointer<dtls_context_t> context, Pointer<session_t> session,
}

int _handleRead(Pointer<dtls_context_t> context, Pointer<session_t> session,
Pointer<ffi.UnsignedChar> dataAddress, int dataLength) {
Pointer<ffi.Uint8> dataAddress, int dataLength) {
final connection = DtlsClientConnection._connections[context.address];

if (connection == null) {
Expand Down Expand Up @@ -68,9 +68,9 @@ int _retrievePskInfo(
Pointer<dtls_context_t> context,
Pointer<session_t> session,
int type,
Pointer<ffi.UnsignedChar> id,
Pointer<ffi.Uint8> id,
int idLen,
Pointer<ffi.UnsignedChar> result,
Pointer<ffi.Uint8> result,
int resultLength,
) {
final connection = DtlsClientConnection._connections[context.address];
Expand Down Expand Up @@ -141,8 +141,8 @@ int _retrieveEcdsaInfo(
int _verifyEcdsaKey(
ffi.Pointer<dtls_context_t> context,
ffi.Pointer<session_t> session,
ffi.Pointer<ffi.UnsignedChar> publicKeyX,
ffi.Pointer<ffi.UnsignedChar> publicKeyY,
ffi.Pointer<ffi.Uint8> publicKeyX,
ffi.Pointer<ffi.Uint8> publicKeyY,
int keySize) {
return success;
}
Expand Down Expand Up @@ -371,7 +371,8 @@ class DtlsClient {

int _send(List<int> data, Pointer<dtls_context_t> context,
Pointer<session_t> session) {
final result = dtlsWrite(_tinyDtls, data, context, session);
buffer.asTypedList(data.length).setAll(0, data);
final result = _tinyDtls.dtls_write(context, session, buffer, data.length);

if (result < 0) {
throw DtlsException("Error sending DTLS message");
Expand All @@ -383,7 +384,7 @@ class DtlsClient {
void _receive(Uint8List data, Pointer<dtls_context_t> context,
Pointer<session_t> session) {
buffer.asTypedList(data.length).setAll(0, data);
_tinyDtls.dtls_handle_message(context, session, buffer.cast(), data.length);
_tinyDtls.dtls_handle_message(context, session, buffer, data.length);
}

/// Closes this [DtlsClient].
Expand Down
2 changes: 1 addition & 1 deletion lib/src/ecdsa_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class KeyComponent {
}
}

Pointer<UnsignedChar> _createKeyPointer() {
Pointer<Uint8> _createKeyPointer() {
final keySize = _ecdsaCurve.byteLength;
final pointer = malloc<Uint8>(keySize)
..asTypedList(keySize).setAll(0, byteArray);
Expand Down
19 changes: 8 additions & 11 deletions lib/src/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ DtlsServerConnection? _serverConnectionFromSession(Pointer<session_t> session) {
}

int _handleWrite(Pointer<dtls_context_t> context, Pointer<session_t> session,
Pointer<UnsignedChar> dataAddress, int dataLength) {
Pointer<Uint8> dataAddress, int dataLength) {
final data =
dataAddress.cast<Uint8>().asTypedList(dataLength).buffer.asUint8List();
final address = addressFromSession(session);
Expand All @@ -42,7 +42,7 @@ int _handleWrite(Pointer<dtls_context_t> context, Pointer<session_t> session,
}

int _handleRead(Pointer<dtls_context_t> context, Pointer<session_t> session,
Pointer<UnsignedChar> dataAddress, int dataLength) {
Pointer<Uint8> dataAddress, int dataLength) {
final address = addressFromSession(session);
final port = portFromSession(session);
final connection = _serverConnectionFromSession(session);
Expand Down Expand Up @@ -76,9 +76,9 @@ int _retrievePskInfo(
Pointer<dtls_context_t> context,
Pointer<session_t> session,
int type,
Pointer<UnsignedChar> id,
Pointer<Uint8> id,
int idLen,
Pointer<UnsignedChar> result,
Pointer<Uint8> result,
int resultLength,
) {
if (type == dtls_credentials_type_t.DTLS_PSK_IDENTITY) {
Expand Down Expand Up @@ -135,12 +135,8 @@ int _retrieveEcdsaInfo(Pointer<dtls_context_t> context,
return success;
}

int _verifyEcdsaKey(
Pointer<dtls_context_t> context,
Pointer<session_t> session,
Pointer<UnsignedChar> publicKeyX,
Pointer<UnsignedChar> publicKeyY,
int keySize) {
int _verifyEcdsaKey(Pointer<dtls_context_t> context, Pointer<session_t> session,
Pointer<Uint8> publicKeyX, Pointer<Uint8> publicKeyY, int keySize) {
return success;
}

Expand Down Expand Up @@ -301,7 +297,8 @@ class DtlsServer extends Stream<DtlsServerConnection> {

int _send(List<int> data, Pointer<dtls_context_t> context,
Pointer<session_t> session) {
final result = dtlsWrite(_tinyDtls, data, context, session);
buffer.asTypedList(data.length).setAll(0, data);
final result = _tinyDtls.dtls_write(context, session, buffer, data.length);

if (result < 0) {
throw DtlsException("Error sending DTLS message");
Expand Down
28 changes: 14 additions & 14 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,39 @@ const AF_INET = 2;
const AF_INET6 = 10;

/// Native function signature of a Write handler for tinyDTLS.
typedef NativeWriteHandler = ffi.Int Function(Pointer<dtls_context_t>,
Pointer<session_t>, Pointer<ffi.UnsignedChar>, ffi.Int);
typedef NativeWriteHandler = ffi.Int32 Function(
Pointer<dtls_context_t>, Pointer<session_t>, Pointer<ffi.Uint8>, ffi.Int32);

/// Native function signature of a Read handler for tinyDTLS.
///
/// Has the same signature as the Write handler.
typedef NativeReadHandler = NativeWriteHandler;

/// Native function signature of an Event handler for tinyDTLS.
typedef NativeEventHandler = ffi.Int Function(
Pointer<dtls_context_t>, Pointer<session_t>, ffi.Int32, ffi.UnsignedShort);
typedef NativeEventHandler = ffi.Int32 Function(
Pointer<dtls_context_t>, Pointer<session_t>, ffi.Int32, ffi.Uint16);

/// Native function signature of a PSK store handler for tinyDTLS.
typedef NativePskHandler = ffi.Int Function(
typedef NativePskHandler = ffi.Int32 Function(
ffi.Pointer<dtls_context_t>,
ffi.Pointer<session_t>,
ffi.Int32,
ffi.Pointer<ffi.UnsignedChar>,
ffi.Int,
ffi.Pointer<ffi.UnsignedChar>,
ffi.Int);
ffi.Pointer<ffi.Uint8>,
ffi.Int32,
ffi.Pointer<ffi.Uint8>,
ffi.Int32);

/// Native function signature of an ECDSA key store handler for tinyDTLS.
typedef NativeEcdsaHandler = ffi.Int Function(ffi.Pointer<dtls_context_t>,
typedef NativeEcdsaHandler = ffi.Int32 Function(ffi.Pointer<dtls_context_t>,
ffi.Pointer<session_t>, ffi.Pointer<ffi.Pointer<dtls_ecdsa_key_t>>);

/// Native function signature of an ECDSA verification handler for tinyDTLS.
typedef NativeEcdsaVerifyHandler = ffi.Int Function(
typedef NativeEcdsaVerifyHandler = ffi.Int32 Function(
ffi.Pointer<dtls_context_t>,
ffi.Pointer<session_t>,
ffi.Pointer<ffi.UnsignedChar>,
ffi.Pointer<ffi.UnsignedChar>,
ffi.Int);
ffi.Pointer<ffi.Uint8>,
ffi.Pointer<ffi.Uint8>,
ffi.Int32);

/// Function signature for a callback function for retrieving/generating
/// [PskCredentials].
Expand Down
18 changes: 0 additions & 18 deletions lib/src/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,3 @@ int createFatalError(int description) {
String getConnectionKey(InternetAddress address, int port) {
return "${address.address}:$port";
}

/// Wrapper around [TinyDTLS.dtls_writev] for a single buffer.
///
/// Necessary due to the fact that `dtls_write` has become an inline function.
int dtlsWrite(TinyDTLS tinyDtls, List<int> data,
Pointer<dtls_context_t> context, Pointer<session_t> session) {
buffer.asTypedList(data.length).setAll(0, data);

final bufferPointer = malloc.call<Size>(1)..value = buffer.address;
final lengthPointer = malloc.call<Size>(1)..value = data.length;

final result = tinyDtls.dtls_writev(
context, session, bufferPointer.cast(), lengthPointer.cast(), 1);
malloc
..free(bufferPointer)
..free(lengthPointer);
return result;
}

0 comments on commit d4401e6

Please sign in to comment.