KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Integrating Socket Handles from C Library into kdb+ for WebSocket Management Reply To: Integrating Socket Handles from C Library into kdb+ for WebSocket Management

  • jlucid

    Member
    June 9, 2024 at 11:45 am

    I’ve since found a solution to this, specifically, a way to specify which network interface Q should use when establishing a socket connection.

    1. Download and rename the attached file to interpose.c.
    This file contains code that intercepts the creation of socket connections to bind them to a specific network interface

    2. Compile it into a shared library gcc -shared -fPIC -o libinterpose.so interpose.c -ldl

    3. Then start q using LD_PRELOAD=/path/to/libinterpose.so q
    You need to set this LD_PRELOAD env variable to ensure that the socket creation function in the library is used instead of the default one
    4. Within Q, specify the socket interface you want to use by setting the

    environmental variable setenv[SOCKET_INTERFACE;”eth1″]

    5. Open your socket connection as normal. Before opening additional socket connections on other interfaces, just change the SOCKET_INTERFACE value again

    6. Verify its working. Download verify.txt and rename to verify.c. Compile: gcc -DKXVER=3 -shared -fPIC -o verify.so verify.c , You need k.h in the same directory. Then Load the shared library into Q and pass the handle value of the connection to the function get_socket_interface

    libPath:/path/to/verify;

    get_socket_interface:libPath 2: (get_socket_interface; 1);

    q)get_socket_interface[5i] // 5i is just your handle value
    “eth1”

    User beware, while this works for me currently, we would need a core Kx person to say if its ok to do this or not, because I don’t know whats going on under the hood. There might be some negative side effect I am currently not seeing. It would be better if this could be done natively in Q, possibly an idea for a future release.