Skip to content

Commit

Permalink
To support VSOCK, stop limiting port to 16 bits. (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
graebm authored Jan 2, 2024
1 parent d155069 commit 5cc916e
Show file tree
Hide file tree
Showing 22 changed files with 49 additions and 53 deletions.
2 changes: 1 addition & 1 deletion crt/aws-c-common
2 changes: 1 addition & 1 deletion crt/aws-c-s3
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public void onConnectionResumed(boolean sessionPresent) {
// Set all the settings
tlsOptions.overrideDefaultTrustStoreFromPath(null, caFilePath);
config.setEndpoint(endpoint);
config.setPort((short)port);
config.setPort(port);
config.setClientId(clientId);
config.setCleanSession(true);
config.setProtocolOperationTimeoutMs(60000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,14 @@ public ClientConnectionContinuation newStream(final ClientConnectionContinuation
* @param hostName hostname to connect to, this can be an IPv4 address, IPv6 address, a local socket address, or a
* dns name.
* @param port port to connect to hostName with. For local socket address, this value is ignored.
* For 32bit values exceeding Integer.MAX_VALUE use two's complement (i.e. -1 == 0xFFFFFFFF).
* @param socketOptions socketOptions to use.
* @param tlsContext (optional) tls context to use for using SSL/TLS in the connection.
* @param bootstrap clientBootstrap object to run the connection on.
* @param connectionHandler handler to process connection messages and state changes.
* @return The future will be completed once the connection either succeeds or fails.
*/
public static CompletableFuture<Void> connect(final String hostName, short port, final SocketOptions socketOptions,
public static CompletableFuture<Void> connect(final String hostName, int port, final SocketOptions socketOptions,
final ClientTlsContext tlsContext, final ClientBootstrap bootstrap,
final ClientConnectionHandler connectionHandler) {
long tlsContextHandle = tlsContext != null ? tlsContext.getNativeHandle() : 0;
Expand Down Expand Up @@ -196,7 +197,7 @@ protected boolean canReleaseReferencesImmediately() {
return true;
}

private static native int clientConnect(byte[] hostName, short port, long socketOptions, long tlsContext, long bootstrap, ClientConnectionHandler connectionHandler);
private static native int clientConnect(byte[] hostName, int port, long socketOptions, long tlsContext, long bootstrap, ClientConnectionHandler connectionHandler);
private static native boolean isClientConnectionOpen(long connection);
private static native void closeClientConnection(long connection, int errorCode);
private static native void acquireClientConnection(long connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ public class ServerListener extends CrtResource {
* @param hostName name of the host to listen on. Can be a dns name, ip address, or unix
* domain socket (or named pipe on windows) name.
* @param port port to listen on. Ignored for local domain sockets.
* For 32bit values exceeding Integer.MAX_VALUE use two's complement (i.e. -1 == 0xFFFFFFFF).
* @param socketOptions socket options to apply to the listening socket.
* @param tlsContext optional tls context to apply to the connection if you want to use TLS.
* @param serverBootstrap bootstrap object for handling connections.
* @param handler functor interface for handling incoming connections and connection closures.
*/
public ServerListener(final String hostName, short port, final SocketOptions socketOptions,
public ServerListener(final String hostName, int port, final SocketOptions socketOptions,
final ServerTlsContext tlsContext, final ServerBootstrap serverBootstrap,
final ServerListenerHandler handler) {

Expand Down Expand Up @@ -67,6 +68,8 @@ protected boolean canReleaseReferencesImmediately() {

/**
* @return the port which the listener socket is bound to.
* Note that two's complement is used for 32bit values exceeding
* Integer.MAX_VALUE (i.e. -1 == 0xFFFFFFFF).
*/
public int getBoundPort() {
return boundPort;
Expand All @@ -87,7 +90,7 @@ private void onShutdownComplete() {
public CompletableFuture<Void> getShutdownCompleteFuture() { return shutdownComplete; }

private static native long serverListenerNew(ServerListener serverListener, byte[] hostName,
short port, long socketOptionsHandle,
int port, long socketOptionsHandle,
long tlsContextHandle, long bootstrapHandle,
ServerListenerHandler handler);
private static native int getBoundPort(long serverListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ public HttpClientConnectionManagerOptions withUri(URI uri) {
public URI getUri() { return uri; }

/**
* Sets the port to connect to for connections in the connection pool
* Sets the port to connect to for connections in the connection pool.
* For 32bit values exceeding Integer.MAX_VALUE use two's complement
* (i.e. -1 == 0xFFFFFFFF).
* @param port The port to connect to
* @return this
*/
Expand All @@ -168,6 +170,8 @@ public HttpClientConnectionManagerOptions withPort(int port) {
/**
* @return the port to connect to for connections in the connection pool.
* Returns -1 if none has been explicitly set.
* Note that two's complement is used for 32bit values exceeding
* Integer.MAX_VALUE (i.e. -1 == 0xFFFFFFFF).
*/
public int getPort() { return port; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,18 @@ public void setHost(String host) {
public String getHost() { return host; }

/**
* Sets the proxy port to connect through
* Sets the proxy port to connect through.
* For 32bit values exceeding Integer.MAX_VALUE use two's complement (i.e. -1 == 0xFFFFFFFF).
* @param port proxy port to connect through
*/
public void setPort(int port) {
this.port = port;
}

/**
* @return the proxy port to connect through
* @return the proxy port to connect through.
* Note that two's complement is used for 32bit values exceeding
* Integer.MAX_VALUE (i.e. -1 == 0xFFFFFFFF).
*/
public int getPort() { return port; }

Expand Down
4 changes: 2 additions & 2 deletions src/native/credentials_provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ JNIEXPORT jlong JNICALL
proxy_connection_type,
&proxy_tls_connection_options,
jni_proxy_host,
(uint16_t)jni_proxy_port,
jni_proxy_port,
jni_proxy_authorization_username,
jni_proxy_authorization_password,
jni_proxy_authorization_type,
Expand Down Expand Up @@ -739,7 +739,7 @@ jlong JNICALL Java_software_amazon_awssdk_crt_auth_credentials_CognitoCredential
proxy_connection_type,
&proxy_tls_connection_options,
proxy_host,
(uint16_t)proxy_port,
proxy_port,
proxy_authorization_username,
proxy_authorization_password,
proxy_authorization_type,
Expand Down
4 changes: 2 additions & 2 deletions src/native/event_stream_rpc_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ jint JNICALL Java_software_amazon_awssdk_crt_eventstream_ClientConnection_client
JNIEnv *env,
jclass jni_class,
jbyteArray jni_host_name,
jshort port,
jint port,
jlong jni_socket_options,
jlong jni_tls_ctx,
jlong jni_client_bootstrap,
Expand Down Expand Up @@ -221,7 +221,7 @@ jint JNICALL Java_software_amazon_awssdk_crt_eventstream_ClientConnection_client
.socket_options = socket_options,
.tls_options = conn_options_ptr,
.user_data = callback_data,
.port = (uint16_t)port,
.port = (uint32_t)port,
.bootstrap = client_bootstrap,
.host_name = c_str_host_name,
.on_connection_setup = s_on_connection_setup,
Expand Down
6 changes: 3 additions & 3 deletions src/native/event_stream_rpc_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ jlong JNICALL Java_software_amazon_awssdk_crt_eventstream_ServerListener_serverL
jclass jni_class,
jobject jni_server_listener,
jbyteArray jni_host_name,
jshort port,
jint port,
jlong jni_socket_options,
jlong jni_tls_ctx,
jlong jni_server_bootstrap,
Expand Down Expand Up @@ -552,7 +552,7 @@ jlong JNICALL Java_software_amazon_awssdk_crt_eventstream_ServerListener_serverL
.on_destroy_callback = s_server_listener_shutdown_complete,
.bootstrap = server_bootstrap,
.tls_options = conn_options_ptr,
.port = port,
.port = (uint32_t)port,
.host_name = c_str_host_name,
.user_data = callback_data,
.on_new_connection = s_on_new_connection_fn,
Expand Down Expand Up @@ -598,7 +598,7 @@ jint JNICALL Java_software_amazon_awssdk_crt_eventstream_ServerListener_getBound
(struct aws_event_stream_rpc_server_listener *)jni_server_listener;
if (!listener) {
aws_jni_throw_runtime_exception(env, "ServerListener.getBoundPort: Invalid serverListener");
return (jshort)-1;
return -1;
}

return (jint)aws_event_stream_rpc_server_listener_get_bound_port(listener);
Expand Down
8 changes: 4 additions & 4 deletions src/native/http2_stream_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_http_Http2StreamManager_

struct aws_byte_cursor endpoint = aws_jni_byte_cursor_from_jbyteArray_acquire(env, jni_endpoint);

if (jni_port <= 0 || 65535 < jni_port) {
aws_jni_throw_illegal_argument_exception(env, "Port must be between 1 and 65535");
if (jni_port == 0) {
aws_jni_throw_illegal_argument_exception(env, "Port must not be 0");
goto cleanup;
}

Expand All @@ -165,7 +165,7 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_http_Http2StreamManager_
goto cleanup;
}

uint16_t port = (uint16_t)jni_port;
uint32_t port = (uint32_t)jni_port;

bool new_tls_conn_opts = (jni_tls_ctx != 0 && !tls_connection_options);

Expand Down Expand Up @@ -229,7 +229,7 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_http_Http2StreamManager_
jni_proxy_connection_type,
&proxy_tls_conn_options,
jni_proxy_host,
(uint16_t)jni_proxy_port,
jni_proxy_port,
jni_proxy_authorization_username,
jni_proxy_authorization_password,
jni_proxy_authorization_type,
Expand Down
9 changes: 2 additions & 7 deletions src/native/http_connection_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,6 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_http_HttpClientConnectio
struct aws_allocator *allocator = aws_jni_get_allocator();
struct aws_byte_cursor endpoint = aws_jni_byte_cursor_from_jbyteArray_acquire(env, jni_endpoint);

if (jni_port <= 0 || 65535 < jni_port) {
aws_jni_throw_runtime_exception(env, "Port must be between 1 and 65535");
goto cleanup;
}

size_t window_size;
if (aws_size_t_from_java(env, &window_size, jni_window_size, "Initial window size")) {
goto cleanup;
Expand All @@ -156,7 +151,7 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_http_HttpClientConnectio
goto cleanup;
}

uint16_t port = (uint16_t)jni_port;
uint32_t port = (uint32_t)jni_port;

bool new_tls_conn_opts = (jni_tls_ctx != 0 && !tls_connection_options);

Expand Down Expand Up @@ -216,7 +211,7 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_http_HttpClientConnectio
jni_proxy_connection_type,
&proxy_tls_conn_options,
jni_proxy_host,
(uint16_t)jni_proxy_port,
jni_proxy_port,
jni_proxy_authorization_username,
jni_proxy_authorization_password,
jni_proxy_authorization_type,
Expand Down
4 changes: 2 additions & 2 deletions src/native/http_proxy_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void aws_http_proxy_options_jni_init(
jint proxy_connection_type,
struct aws_tls_connection_options *tls_options,
jbyteArray proxy_host,
uint16_t proxy_port,
jint proxy_port,
jbyteArray proxy_authorization_username,
jbyteArray proxy_authorization_password,
int proxy_authorization_type,
Expand All @@ -39,7 +39,7 @@ void aws_http_proxy_options_jni_init(
struct aws_allocator *allocator = aws_jni_get_allocator();

options->connection_type = proxy_connection_type;
options->port = proxy_port;
options->port = (uint32_t)proxy_port;
options->auth_type = proxy_authorization_type;

if (proxy_host != NULL) {
Expand Down
2 changes: 1 addition & 1 deletion src/native/http_proxy_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void aws_http_proxy_options_jni_init(
jint proxy_connection_type,
struct aws_tls_connection_options *tls_options,
jbyteArray proxy_host,
uint16_t proxy_port,
jint proxy_port,
jbyteArray proxy_authorization_username,
jbyteArray proxy_authorization_password,
int proxy_authorization_type,
Expand Down
15 changes: 3 additions & 12 deletions src/native/mqtt5_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,7 @@ static struct aws_http_proxy_options_java_jni *s_aws_mqtt5_http_proxy_options_cr
goto on_error;
}
if (jni_proxy_port) {
int32_t jni_proxy_port_check = (int32_t)jni_proxy_port;
if (jni_proxy_port_check < 0) {
AWS_LOGF_ERROR(AWS_LS_MQTT_CLIENT, "HTTP Proxy Options port is less than 0");
goto on_error;
} else if (jni_proxy_port_check > UINT16_MAX) {
AWS_LOGF_ERROR(AWS_LS_MQTT_CLIENT, "HTTP Proxy Options port is more than UINT16_MAX");
goto on_error;
} else {
http_options->options.port = (uint16_t)jni_proxy_port;
}
http_options->options.port = (uint32_t)jni_proxy_port;
}

jobject jni_proxy_tls_context = (*env)->CallObjectMethod(
Expand Down Expand Up @@ -1746,8 +1737,8 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_mqtt5_Mqtt5Client_mqtt5C
goto clean_up;
}

uint16_t port = 0;
if (aws_get_uint16_from_jobject(
uint32_t port = 0;
if (aws_get_uint32_from_jobject(
env,
jni_options,
mqtt5_client_options_properties.options_port_field_id,
Expand Down
7 changes: 3 additions & 4 deletions src/native/mqtt_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,12 +594,11 @@ void JNICALL Java_software_amazon_awssdk_crt_mqtt_MqttClientConnection_mqttClien
struct aws_byte_cursor client_id;
AWS_ZERO_STRUCT(client_id);
struct aws_byte_cursor endpoint = aws_jni_byte_cursor_from_jstring_acquire(env, jni_endpoint);
uint16_t port = (uint16_t)jni_port;
uint32_t port = (uint32_t)jni_port;
if (!port) {
aws_jni_throw_runtime_exception(
env,
"MqttClientConnection.mqtt_new: Endpoint should be in the format hostname:port and port must be between 1 "
"and 65535");
"MqttClientConnection.mqtt_new: Endpoint should be in the format hostname:port and port must not be 0");
goto cleanup;
}

Expand Down Expand Up @@ -1327,7 +1326,7 @@ void JNICALL Java_software_amazon_awssdk_crt_mqtt_MqttClientConnection_mqttClien
proxy_options.connection_type = (enum aws_http_proxy_connection_type)jni_proxy_connection_type;

proxy_options.host = aws_jni_byte_cursor_from_jstring_acquire(env, jni_proxy_host);
proxy_options.port = (uint16_t)jni_proxy_port;
proxy_options.port = (uint32_t)jni_proxy_port;

proxy_options.auth_type = (enum aws_http_proxy_authentication_type)jni_proxy_authorization_type;

Expand Down
2 changes: 1 addition & 1 deletion src/native/s3_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_s3_S3Client_s3ClientNew(
jni_proxy_connection_type,
&proxy_tls_conn_options,
jni_proxy_host,
(uint16_t)jni_proxy_port,
jni_proxy_port,
jni_proxy_authorization_username,
jni_proxy_authorization_password,
jni_proxy_authorization_type,
Expand Down

0 comments on commit 5cc916e

Please sign in to comment.