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

  • Integrating Socket Handles from C Library into kdb+ for WebSocket Management

    Posted by jlucid on May 25, 2024 at 6:51 pm

    I have a shared C library that contains a function capable of opening WebSocket connections and performing socket-level operations, such as binding the socket to a different local IP address or change other socket related options.

    While the C function successfully creates the WebSocket connection and allows for various socket manipulations, I cannot integrate this handle value within kdb+.

    The Problem

    • C Functionality: The C function creates a WebSocket connection and can perform operations like binding the socket to different IP addresses, which I cannot do in Q directly (as far as I am aware).
    • Integration Challenge: Despite being able to pass the handle value from the C function back to the Q process, I am unable to use this handle within kdb+. (Which is not surprising of course)
    • Alternative Approach Attempted: I have also tried creating the socket connection in Q, as usual, and then passing the handle value to the shared library to perform socket-level changes. However, this approach does not allow me to bind the socket to a different IP address.

    Question

    Is there a way to “register” or otherwise integrate a socket handle created and returned by a shared C library function so that it can be recognised and managed by kdb+? Or is there another way to modify socket options.

    Any guidance or examples on how to achieve would be great, thanks.

    jlucid replied 6 months, 2 weeks ago 1 Member · 1 Reply
  • 1 Reply
  • 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.

Log in to reply.