cleanup jna mappings

Signed-off-by: Ludovic Orban <lorban@bitronix.be>
This commit is contained in:
Ludovic Orban 2021-09-29 09:05:15 +02:00 committed by Simone Bordet
parent b93436d0e8
commit 49dc5617ac
4 changed files with 24 additions and 29 deletions

View File

@ -13,6 +13,7 @@
package org.eclipse.jetty.quic.quiche; package org.eclipse.jetty.quic.quiche;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
@ -101,11 +102,15 @@ public class QuicheConnection
String[] applicationProtos = config.getApplicationProtos(); String[] applicationProtos = config.getApplicationProtos();
if (applicationProtos != null) if (applicationProtos != null)
{ {
StringBuilder sb = new StringBuilder(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (String proto : applicationProtos) for (String proto : applicationProtos)
sb.append((char)proto.getBytes(LibQuiche.CHARSET).length).append(proto); {
String theProtos = sb.toString(); byte[] bytes = proto.getBytes(LibQuiche.CHARSET);
LibQuiche.INSTANCE.quiche_config_set_application_protos(quicheConfig, theProtos, new size_t(theProtos.getBytes(LibQuiche.CHARSET).length)); baos.write(bytes.length);
baos.write(bytes);
}
byte[] bytes = baos.toByteArray();
LibQuiche.INSTANCE.quiche_config_set_application_protos(quicheConfig, bytes, new size_t(bytes.length));
} }
QuicheConfig.CongestionControl cc = config.getCongestionControl(); QuicheConfig.CongestionControl cc = config.getCongestionControl();
@ -222,7 +227,7 @@ public class QuicheConnection
ssize_t generated = LibQuiche.INSTANCE.quiche_negotiate_version(scid, scid_len.getPointee(), dcid, dcid_len.getPointee(), packetToSend, new size_t(packetToSend.remaining())); ssize_t generated = LibQuiche.INSTANCE.quiche_negotiate_version(scid, scid_len.getPointee(), dcid, dcid_len.getPointee(), packetToSend, new size_t(packetToSend.remaining()));
packetToSend.position(packetToSend.position() + generated.intValue()); packetToSend.position(packetToSend.position() + generated.intValue());
if (generated.intValue() < 0) if (generated.intValue() < 0)
throw new IOException("failed to create vneg packet : " + generated); throw new IOException("failed to create vneg packet : " + LibQuiche.quiche_error.errToString(generated.intValue()));
return true; return true;
} }

View File

@ -47,27 +47,15 @@ public class QuicheConnectionId
private static String bytesToHex(byte[] bytes) private static String bytesToHex(byte[] bytes)
{ {
byte[] hexChars = new byte[bytes.length * 2]; byte[] hexChars = new byte[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) for (int i = 0; i < bytes.length; i++)
{ {
int v = bytes[j] & 0xFF; int c = bytes[i] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4]; hexChars[i * 2] = HEX_ARRAY[c >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; hexChars[i * 2 + 1] = HEX_ARRAY[c & 0x0F];
} }
return new String(hexChars, StandardCharsets.US_ASCII); return new String(hexChars, StandardCharsets.US_ASCII);
} }
public static QuicheConnectionId fromCid(byte[] cid)
{
byte[] sizedDcid = resizeIfNeeded(cid, cid.length);
return new QuicheConnectionId(sizedDcid);
}
static QuicheConnectionId fromCid(byte[] dcid, size_t_pointer dcidLen)
{
byte[] sizedDcid = resizeIfNeeded(dcid, (int)dcidLen.getValue());
return new QuicheConnectionId(sizedDcid);
}
/** /**
* Does not consume the packet byte buffer. * Does not consume the packet byte buffer.
*/ */
@ -94,7 +82,8 @@ public class QuicheConnectionId
token, tokenLen); token, tokenLen);
if (rc < 0) if (rc < 0)
return null; return null;
return fromCid(dcid, dcidLen); byte[] sizedDcid = resizeIfNeeded(dcid, (int)dcidLen.getValue());
return new QuicheConnectionId(sizedDcid);
} }
private static byte[] resizeIfNeeded(byte[] buffer, int length) private static byte[] resizeIfNeeded(byte[] buffer, int length)

View File

@ -33,8 +33,10 @@ public interface LibQuiche extends Library
// It needs to be reviewed each time the native lib version changes. // It needs to be reviewed each time the native lib version changes.
String EXPECTED_QUICHE_VERSION = "0.10.0"; String EXPECTED_QUICHE_VERSION = "0.10.0";
// load the native lib // The charset used to convert java.lang.String to char * and vice versa.
Charset CHARSET = StandardCharsets.UTF_8; Charset CHARSET = StandardCharsets.UTF_8;
// Load the native lib.
LibQuiche INSTANCE = Native.load("quiche", LibQuiche.class, Map.of(Library.OPTION_STRING_ENCODING, CHARSET.name())); LibQuiche INSTANCE = Native.load("quiche", LibQuiche.class, Map.of(Library.OPTION_STRING_ENCODING, CHARSET.name()));
class Logging class Logging
@ -189,7 +191,7 @@ public interface LibQuiche extends Library
void quiche_config_verify_peer(quiche_config config, boolean v); void quiche_config_verify_peer(quiche_config config, boolean v);
// Configures the list of supported application protocols. // Configures the list of supported application protocols.
int quiche_config_set_application_protos(quiche_config config, String protos, size_t protos_len); int quiche_config_set_application_protos(quiche_config config, byte[] protos, size_t protos_len);
// Sets the `max_idle_timeout` transport parameter. // Sets the `max_idle_timeout` transport parameter.
void quiche_config_set_max_idle_timeout(quiche_config config, uint64_t v); void quiche_config_set_max_idle_timeout(quiche_config config, uint64_t v);
@ -437,7 +439,7 @@ public interface LibQuiche extends Library
boolean quiche_conn_peer_error(quiche_conn conn, boolean quiche_conn_peer_error(quiche_conn conn,
bool_pointer is_app, bool_pointer is_app,
uint64_t_pointer error_code, uint64_t_pointer error_code,
char_pointer/*const uint8_t ***/ reason, char_pointer reason,
size_t_pointer reason_len); size_t_pointer reason_len);
// Returns true if a connection error was queued or sent, and updates the provided // Returns true if a connection error was queued or sent, and updates the provided
@ -445,7 +447,7 @@ public interface LibQuiche extends Library
boolean quiche_conn_local_error(quiche_conn conn, boolean quiche_conn_local_error(quiche_conn conn,
bool_pointer is_app, bool_pointer is_app,
uint64_t_pointer error_code, uint64_t_pointer error_code,
Pointer/*const uint8_t ***/ reason, char_pointer reason,
size_t_pointer reason_len); size_t_pointer reason_len);
// Closes the connection with the given error and reason. // Closes the connection with the given error and reason.

View File

@ -249,10 +249,9 @@ public class LowLevelQuicheTest
AbstractMap.SimpleImmutableEntry<QuicheConnection, QuicheConnection> entry = new AbstractMap.SimpleImmutableEntry<>(clientQuicheConnection, serverQuicheConnection); AbstractMap.SimpleImmutableEntry<QuicheConnection, QuicheConnection> entry = new AbstractMap.SimpleImmutableEntry<>(clientQuicheConnection, serverQuicheConnection);
StringBuilder sb = new StringBuilder(); int protosLen = 0;
for (String proto : clientQuicheConfig.getApplicationProtos()) for (String proto : clientQuicheConfig.getApplicationProtos())
sb.append((char)proto.getBytes(LibQuiche.CHARSET).length).append(proto); protosLen += 1 + proto.getBytes(LibQuiche.CHARSET).length;
int protosLen = sb.toString().getBytes(LibQuiche.CHARSET).length;
drainServerToFeedClient(entry, 300 + protosLen); drainServerToFeedClient(entry, 300 + protosLen);
assertThat(serverQuicheConnection.isConnectionEstablished(), is(false)); assertThat(serverQuicheConnection.isConnectionEstablished(), is(false));