Skip to content

Commit

Permalink
Load libssl.so dynamically (to support both OpenSSL 1.0.x and 1.1.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Nov 22, 2017
1 parent 5889911 commit eb2470a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.xml
Expand Up @@ -54,7 +54,7 @@
<arg line="-g -O3 -fPIC -D_GNU_SOURCE ${gcc.extra.args}
-o ${build.dir}/native/libonenio.so -shared -Wl,-soname,libonenio.so
-I ${java.home}/../include -I ${java.home}/../include/linux
${src.native} -lssl -lrt"/>
${src.native} -ldl -lrt"/>
</exec>
</target>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -114,7 +114,7 @@
<arg line="-g -O3 -fPIC -D_GNU_SOURCE ${gcc.extra.args}
-o ${build.dir}/native/libonenio.so -shared -Wl,-soname,libonenio.so
-I ${java.home}/../include -I ${java.home}/../include/linux
${src.native} -lssl -lrt"/>
${src.native} -ldl -lrt"/>
</exec>

<move verbose="true" failonerror="false" file="${build.dir}/native/libonenio.so" todir="${build.dir}/classes"/>
Expand Down
7 changes: 6 additions & 1 deletion src/one/nio/net/native/ssl.c
Expand Up @@ -397,11 +397,16 @@ static void ssl_info_callback(const SSL* ssl, int cb, int ret) {

JNIEXPORT void JNICALL
Java_one_nio_net_NativeSslContext_init(JNIEnv* env, jclass cls) {
if (dlopen("libssl.so", RTLD_LAZY | RTLD_GLOBAL) == NULL || !OPENSSL_init_ssl(0, NULL)) {
if (dlopen("libssl.so", RTLD_LAZY | RTLD_GLOBAL) == NULL && dlopen("libssl.so.1.0.0", RTLD_LAZY | RTLD_GLOBAL) == NULL) {
throw_by_name(env, "java/lang/UnsupportedOperationException", "Failed to load libssl.so");
return;
}

if (!OPENSSL_init_ssl(0, NULL)) {
throw_by_name(env, "java/lang/UnsupportedOperationException", "Failed to initialize OpenSSL library");
return;
}

f_ctx = cache_field(env, "one/nio/net/NativeSslContext", "ctx", "J");
f_ssl = cache_field(env, "one/nio/net/NativeSslSocket", "ssl", "J");

Expand Down

0 comments on commit eb2470a

Please sign in to comment.