Casting function pointers

I have a case where a callback has the following signature:

@convention(c) (_ ssl: OpaquePointer?, _ num: UnsafeMutablePointer<Int32>?, _ arg: UnsafeMutableRawPointer?) -> Int32

But to install this callback I have to use a c-function with this signature:

SSL_CTX_callback_ctrl (OpaquePointer!, Int32, (() -> Void)! )

Is it possible to cast the first function to the second? If so, how?

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

I should clarify: is it possible to cast:

@convention(c) (_ ssl: OpaquePointer?, _ num: UnsafeMutablePointer<Int32>?, _ arg: UnsafeMutableRawPointer?) -> Int32

to:

(() -> Void)!

???

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

···

On 25 Jan 2017, at 10:23, Rien via swift-users <swift-users@swift.org> wrote:

I have a case where a callback has the following signature:

@convention(c) (_ ssl: OpaquePointer?, _ num: UnsafeMutablePointer<Int32>?, _ arg: UnsafeMutableRawPointer?) -> Int32

But to install this callback I have to use a c-function with this signature:

SSL_CTX_callback_ctrl (OpaquePointer!, Int32, (() -> Void)! )

Is it possible to cast the first function to the second? If so, how?

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

I think your best bet would be to write the shim closure yourself. Is `num` a pointer to the value you're supposed to receive from SSL_CTX_callback_ctrl? Is the `arg` really a function pointer? In that case, something like this might work:

func callback(_ ssl: OpaquePointer?, _ num: UnsafeMutablePointer<Int32>?, _ arg: UnsafeMutableRawPointer?) -> Int32

let shimCallback: SSL_CTX_callback_ctrl = { ctx, cmd, fp in
  var cmdNum = cmd
  callback(ctx, &cmdNum, unsafeBitCast(fp, to: UnsafeMutableRawPointer?.self))
}

-Joe

···

On Jan 25, 2017, at 1:23 AM, Rien via swift-users <swift-users@swift.org> wrote:

I have a case where a callback has the following signature:

@convention(c) (_ ssl: OpaquePointer?, _ num: UnsafeMutablePointer<Int32>?, _ arg: UnsafeMutableRawPointer?) -> Int32

But to install this callback I have to use a c-function with this signature:

SSL_CTX_callback_ctrl (OpaquePointer!, Int32, (() -> Void)! )

Is it possible to cast the first function to the second? If so, how?

Thank Joe,

A first attempt was not successful, I will try again in a few days.

For now I have put in a work around where I put glue-code into the openSSL library itself. I don’t like that solution, but it works.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

···

On 25 Jan 2017, at 19:37, Joe Groff <jgroff@apple.com> wrote:

On Jan 25, 2017, at 1:23 AM, Rien via swift-users <swift-users@swift.org> wrote:

I have a case where a callback has the following signature:

@convention(c) (_ ssl: OpaquePointer?, _ num: UnsafeMutablePointer<Int32>?, _ arg: UnsafeMutableRawPointer?) -> Int32

But to install this callback I have to use a c-function with this signature:

SSL_CTX_callback_ctrl (OpaquePointer!, Int32, (() -> Void)! )

Is it possible to cast the first function to the second? If so, how?

I think your best bet would be to write the shim closure yourself. Is `num` a pointer to the value you're supposed to receive from SSL_CTX_callback_ctrl? Is the `arg` really a function pointer? In that case, something like this might work:

func callback(_ ssl: OpaquePointer?, _ num: UnsafeMutablePointer<Int32>?, _ arg: UnsafeMutableRawPointer?) -> Int32

let shimCallback: SSL_CTX_callback_ctrl = { ctx, cmd, fp in
var cmdNum = cmd
callback(ctx, &cmdNum, unsafeBitCast(fp, to: UnsafeMutableRawPointer?.self))
}

-Joe