Hi, I am having some issues with staticCFunction. ...
# kotlin-native
t
Hi, I am having some issues with staticCFunction. See code below:
Copy code
val identityBytes: ByteArray = someValue()
val pskBytes: ByteArray = someValue()

// <https://www.openssl.org/docs/man1.0.2/man3/SSL_CTX_set_psk_client_callback.html>

SSL_CTX_set_psk_client_callback(ctx, staticCFunction { ssl, hint, identity, maxIdentityLen, psk, maxPskLen ->
    require(identityBytes.size <= maxIdentityLen.toInt())
    require(pskBytes.size <= maxPskLen.toInt())

    identityBytes.forEachIndexed { index, value ->
        identity!![index] = value
    }
    pskBytes.forEachIndexed { index, value ->
        psk!![index] = value.toUByte()
    }

    // ...
    TODO()
})
When compiling this gives an error:
Copy code
kotlinx.cinterop.staticCFunction must take an unbound, non-capturing function or lambda
I need to access identityBytes and pskBytes from this lambda. Is that possible?
o
C function != complete Kotlin lambda, all state must come explicitly as an argument or be obtained from the global.
t
@olonho Hmm, in this example I pass the staticCFunction to another function (SSL_CTX_set_psk_client_callback) which is not mine. How am I supposed to write the bytes in the C function if I can not access identityBytes and pskBytes?
I don’t see how I could pass the two byte arrays as an argument in this example
Global variables are not an option either as the two byte arrays are not the same for every user and are returned from another network request
o
there’s no difference from C, I guess. How would you pass those parameters into C callback? From https://github.com/Chronic-Dev/openssl/blob/master/ssl/ssltest.c seems everything needed is passed as an argument callback.