3

Is there any way to associate callback functions with sockets in Linux?

For example,when the function

connect(sock, (struct sockaddr *)peeraddr, sizeof(*peeraddr))

has connected the client with the server, is there a way to associate a function "afterRecv" with the socket of name sock such that after function recv has read some data from sock, afterRecv gets called?

That sock is of blocking type. This is due to the many internal send/recv calls on sock during the handshake period of OpenSSL.

If I modify OpenSSL, it would be cumbersome to modify every recv. So I wondered, if I can add a callback instead.

The flow should be:

  1. connect to socket

  2. recv function

  3. When recv has read data, afterRecv gets called as a callback on recv

3
  • As you seem to be planing to recompile OpenSSL's sources, replacing recv() by your own version (wrapping around the original recv()) would do the job, wouldn't it?
    – alk
    Commented Jul 29, 2013 at 17:32
  • @alk Yes, that seems a good option.
    – jshag
    Commented Jul 29, 2013 at 17:36
  • if you're open to using C++ the boost ASIO library has fantastic support for event-driven I/O and SSL.
    – Sam Miller
    Commented Jul 29, 2013 at 20:44

1 Answer 1

4

Some event libraries implement such callback hooks:

and all graphical or HTTP server libraries (e.g. Gtk/Glib, Qt, LibOnion, ...) provide (or use) such event libraries (around a multiplexing syscall like poll(2) etc).

Both Glib (from GTK) and QtCore (from Qt) are event libraries usable without any GUI

Read also about the C10K problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.