Hello I try to use microhttpd (like <https://githu...
# kotlin-native
a
Hello I try to use microhttpd (like https://github.com/Kotlin/kotlinconf-spinner/blob/master/httpserver/src/hostMain/kotlin/server/HttpServer.kt) for localhost only (https://stackoverflow.com/questions/46023399/make-local-http-server-inaccessable-from-outside). On macOS
Copy code
val portShort = 8888.toUShort()
        val loopbackAddr = alloc<sockaddr_in>()
        loopbackAddr.sin_addr.s_addr = INADDR_LOOPBACK
        loopbackAddr.sin_family = AF_INET.convert()
        loopbackAddr.sin_port = portShort
...
        val daemon = MHD_start_daemon(
            MHD_USE_POLL_INTERNALLY,
            portShort,
            null,
            null,
            staticCFunction(::processRequest), cPointer,
            MHD_OPTION_SOCK_ADDR, loopbackAddr.ptr,
            MHD_OPTION_END
        )
If I remove
MHD_OPTION_SOCK_ADDR, loopbackAddr.ptr
- it works. But with this option daemon is null. What do I wrong?
a
I would guess there is a misunderstanding between K/N sending
CPointer<sockaddr_in>
and lib API asking for
struct sockaddr *
. I found a couple of similar situations in the kotlin-native/samples, the pointer was casted there as
serverAddr.ptr.reinterpret()
(e.g. here) . I think you can try something like that.
a
As I know reinterpret() do nothing - it’s a shugar. In any case
_MHD_OPTION_SOCK_ADDR_, loopbackAddr._ptr_._reinterpret_<sockaddr>()
leads the same problem
a
IIRC it makes sense when the compiler can resolve type correctly on its own. Have you zero’ed the structure? Sorry for such stupid questions, but for now it seems like we’re sending something incorrect here. I’m just trying to guess what.
a
Yes, I’ve added
_memset_(loopbackAddr._ptr_, 0, sockaddr_in.size._convert_())
after
alloc
. Full code (with https)
Copy code
val portShort = 8888.toUShort()
...
        val loopbackAddr = alloc<sockaddr_in>()
        memset(loopbackAddr.ptr, 0, sockaddr_in.size.convert())
        loopbackAddr.sin_addr.s_addr = INADDR_LOOPBACK
        loopbackAddr.sin_family = AF_INET.convert()
        loopbackAddr.sin_port = portShort
        MHD_start_daemon(
            MHD_USE_POLL_INTERNALLY or MHD_USE_SSL,
            portShort,
            null,
            null,
            staticCFunction(::processRequest), cPointer,
            MHD_OPTION_HTTPS_MEM_KEY, KEY.cstr,
            MHD_OPTION_HTTPS_MEM_CERT, CERT.cstr,
            MHD_OPTION_SOCK_ADDR, loopbackAddr.ptr.reinterpret<sockaddr>(),
            MHD_OPTION_CONNECTION_TIMEOUT, 120,
            MHD_OPTION_END
        )
It works if I remove
MHD_OPTION_SOCK_ADDR, loopbackAddr.ptr.reinterpret<sockaddr>(),