libssh2_session_callback_set2(3)
π libssh2_session_callback_set2 β the callback-gluing page, from the libssh2 manual β¨
okay bestie, buckle up, because this one is literally about plugging your own functions into libssh2 and letting it call them for you. it's giving "build your own I/O layer" energy π¦
πΈ NAME πΈ
libssh2_session_callback_set2 β set a callback function π
β¨ SYNOPSIS β¨
#include <libssh2.h>
libssh2_cb_generic *
libssh2_session_callback_set2(LIBSSH2_SESSION *session, int cbtype,
libssh2_cb_generic *callback);
π DESCRIPTION π
So basically, this sets a custom callback handler on a session object that you already initialized (yes, libssh2_session_init_ex(3) first, no skipping steps bestie π«Ά). These callbacks get triggered when special packets show up at the Transport layer. And if you want to disable one? Just set it to NULL. bye boo π
sessionβ the Session instance, as returned bylibssh2_session_init_ex(3)πcbtypeβ the callback type. One of the types listed down in Callback Types. πcallbackβ pointer to your custom callback function. the prototype for it must match the associated callback declaration macro, no improvising fr fr π
π CALLBACK TYPES π
LIBSSH2_CALLBACK_IGNOREβ gets called when aSSH_MSG_IGNOREmessage is received β¨LIBSSH2_CALLBACK_DEBUGβ gets called when aSSH_MSG_DEBUGmessage is received πLIBSSH2_CALLBACK_DISCONNECTβ gets called when aSSH_MSG_DISCONNECTmessage is received πLIBSSH2_CALLBACK_MACERRORβ gets called when a mismatched MAC has been detected in the transport layer. and listen, if the function returns0, the packet gets accepted anyway. like, "eh, close enough" πLIBSSH2_CALLBACK_X11β gets called when an X11 connection has been accepted π₯οΈLIBSSH2_CALLBACK_SENDβ gets called when libssh2 wants to send data on the connection. you can set this to a custom function to handle I/O your own way, main character behavior π
the prototype of the callback:
ssize_t sendcb(libssh2_socket_t sockfd, const void *buffer,
size_t length, int flags, void **abstract);
so: sockfd is the socket to write to, buffer points to the data to send, length is the size of the data, flags is the flags that would have been used to a send() call, and abstract is a pointer to the abstract pointer you set in the libssh2_session_init_ex(3) call. iykyk π«Ά
the callback returns the number of bytes sent, or -1 for error. and there's a special return code, -EAGAIN, that you can return to signal that the send was aborted to prevent getting blocked and it needs to be called again. iconic escape hatch ngl π₯
LIBSSH2_CALLBACK_RECVβ gets called when libssh2 wants to read data from the connection. same deal, set it to a custom function to handle I/O your own way π«
the prototype of the callback:
ssize_t recvcb(libssh2_socket_t sockfd, void *buffer,
size_t length, int flags, void **abstract);
so: sockfd is the socket to read from, buffer is where to store received data into, length is the size of the buffer, flags is the flags that would have been used to a recv() call, and abstract is a pointer to the abstract pointer set in the libssh2_session_init_ex(3) call πΈ
the callback returns the number of bytes read, or -1 for error. and again, the special return code -EAGAIN can be returned to signal that the read was aborted to prevent getting blocked and it needs to be called again β
LIBSSH2_CALLBACK_AUTHAGENTβ gets called during the authentication process, so the client can connect to the ssh-agent and do any setup, like configuring the agent or adding keys π
the prototype of the callback:
void authagent(LIBSSH2_SESSION* session, LIBSSH2_CHANNEL *channel,
void **abstract);
LIBSSH2_CALLBACK_AUTHAGENT_IDENTITIESβ not called by libssh2. the client is responsible for calling this method when aSSH2_AGENTC_REQUEST_IDENTITIESmessage has been received π
the prototype of the callback:
void identities(LIBSSH2_SESSION* session, void *buffer,
const char *agent_path,
void **abstract)
buffer must be filled in by the callback. different clients may implement this differently β for example, one client may pass in an unsigned char ** for this parameter, while another may pass in a pointer to a struct. so it's flexible, bestie π§
regardless of the type of buffer used, the client will need to send back a list of identities in the following format:
uint32 buffer length
uint32 number of entries
entries
where each entry in the entries list is of the format:
string data
cstring comment
agent_path β the path to a running ssh-agent on the client machine, from which identities can be listed π
LIBSSH2_CALLBACK_AUTHAGENT_SIGNβ not called by libssh2. the client is responsible for calling this method when aSSH2_AGENTC_SIGN_REQUESTmessage has been received βοΈ
the prototype of the callback:
void sign(LIBSSH2_SESSION* session,
unsigned char *blob, unsigned int blen,
const unsigned char *data, unsigned int dlen,
unsigned char **sig, unsigned int *sig_len,
const char *agent_path,
void **abstract);
when you're interfacing with an ssh-agent installed on the client system, this method can call libssh2_agent_sign(3) to perform the signing. slay π
π RETURN VALUE π
a pointer to the previous callback handler. and it returns NULL if no prior callback handler was set, or if the callback type was unknown. so yeah, unknown type = NULL, no drama π
π SEE ALSO π
and that's the whole page, bestie!! this one's from libssh2 1.11.1, dated 13 Dec 2023 β go forth and set some callbacks, you absolute legend π«Άπβ¨π
π¦ where this page comes from
- alpine 3.22: libssh2-doc 1.11.1-r3 Β· BSD-3-Clause
- alpine 3.23: libssh2-doc 1.11.1-r4 Β· BSD-3-Clause
- alpine 3.24: libssh2-doc 1.11.1-r4 Β· BSD-3-Clause
- arch rolling: libssh2 1.11.1-7 Β· BSD-3-Clause
- debian trixie: libssh2-1-dev 1.11.1-1+deb13u1 Β· BSD3
- debian trixie: libssh2-1-dev 1.11.1-1+deb13u2 Β· BSD3
- epel 10: libssh2-docs 1.11.1-9.el10_3 Β· BSD-3-Clause
- epel 9: libssh2-docs 1.11.1-9.el9 Β· BSD-3-Clause
- fedora 43: libssh2-docs 1.11.1-4.fc43 Β· BSD-3-Clause
- fedora 43: libssh2-docs 1.11.1-9.fc43 Β· BSD-3-Clause
- fedora 44: libssh2-docs 1.11.1-5.fc44 Β· BSD-3-Clause
- fedora 44: libssh2-docs 1.11.1-9.fc44 Β· BSD-3-Clause
- opensuse-leap 16.0: libssh2-devel 1.11.1-160000.5.1 Β· BSD-3-Clause
- opensuse-leap 16.0: libssh2-devel 1.11.1-160000.5.1 Β· BSD-3-Clause
- opensuse-leap 16.0: libssh2-devel 1.11.1-160000.5.1 Β· BSD-3-Clause
- opensuse-leap 16.0: libssh2-devel 1.11.1-160000.5.1 Β· BSD-3-Clause
- ubuntu questing: libssh2-1-dev 1.11.1-1build1
- ubuntu questing: libssh2-1-dev 1.11.1-1ubuntu0.25.10.2
- ubuntu resolute: libssh2-1-dev 1.11.1-1build2 Β· BSD3
- ubuntu resolute: libssh2-1-dev 1.11.1-1ubuntu0.26.04.4 Β· BSD3
- ubuntu stonking: libssh2-1-dev 1.11.1-6 Β· BSD3
- π the page's license: BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms