I am writing an application that makes use of libwireshark, though the specifics are not entirely relevant here.
I prepare a packet for the library to dissect and hence call epan_dissect_run(packet), which in turn dissects the given packet and hence calls a callback function that I have specified — using register_tap_listener().
My question is: how can I wrap this process up in a single function, dissected_information* MyDissectPacket(packet)? This applies not just to libwireshark as I have outlined, but libraries that make judicious use of callbacks in general.
Does the answer depend on whether the callback is executed asynchronously, on another thread or both? If the callback is executed synchronously on the same thread, does this make the wrapping code simpler?
Perhaps it is my Google-fu that is weak — I do not know which terms I should be using, as this question has surely been asked many times before.
If you have no idea about if it is asynchronous or not, you should write your function like if it was.
It is a producer/consumer problem: is there anything to comsume – is the packet dissected? So semaphores can resolve it to wait until there is something to consume. Your callback function has to signal that the packet dissection is finished – there is something to consume.
And what I would do is (pseudo code):
I hope I correctly understood your problem…
EDIT:
In case of you get your result through the callback:
I can’t see any other reasonable solution.