Topic: [SOLVED] Using custom send- & receive callback

Hi,
First of all, we have an Eval board that will use an ESP8266 to communicate with the internet. The ESP8266 can only take commands via serial interface and handles all the communication with the internet, from the TCP/IP-layer.

We want to skip the "socket"-communication in wolfSSL and create our own custom send- and receive callbacks that will include serial communication with the ESP8266.

We have read the wolfSSL manual and porting guide and done the following:
- Created (or more like copying EmbedSend and EmbedRecv) our own send- and receive functions, MyEmbedSend and MyEmbedRecv.
- Registered MyEmbedRecv and MyEmbedSend as CallbackIORecv respectively CallbackIOSend in the CTX object.

We trying to create custom send- and receive callbacks but get confused by how it should be done. In the wolfSSL manual there is an explanation for the functions wolfSSL_SetIOWriteCTX and wolfSSL_SetIOReadCTX:
"... If you’ve registered your own send callback you may want to set a specific context for the session. For example, if you’re using memory buffers the context may be a pointer to a structure describing where and how to access the memory buffers."
How should the structure look like if it were to describe where and how to access the memory buffers (or something similar)? It sounds like the structure needs to be formed in a certain way but no information regarding how that shall be done is given.

Can you help us? Maybe give us an example of how it can work or should be done in order to get our project going.

Share

Re: [SOLVED] Using custom send- & receive callback

Hi,

We don't place any restrictions on the format of the I/O read and write contexts.  Your application doesn't even need to register them if you don't need to use them.  The context is simply a pointer, which can be a pointer to anything you want. 

We don't currently have an example of using the I/O callbacks with memory buffers.  If you were using memory buffers, you might have your own custom structure which contains pointers to buffers and members to hold sizes of the buffers.  When your I/O callbacks were called by wolfSSL, your callback could then access the context to get information about your own buffers.

Does this help?  Can you share any details about your project?

Best Regards,
Chris

Re: [SOLVED] Using custom send- & receive callback

Hi again,

Our project is about implementing wolfssl on a STM32F0-Nucleo and a MSP430. We want wolfSSL to use UART-communication instead of default socket communication. The basic idea is to let our hardware communicate through UART to a program on a PC that will redirect the communication to a server-program on the same PC.

We want to know in which way wolfssl sends and receives data. I think the following question will give you and idea of what we're wondering about:
Is there any way to let wolfssl know that the UART is receiving data or do wolfssl try to receive the data whenever it has time?
Which arguments in EmbedSend and EmbedReceive can be ignored and how are we suppose to use the remaining ones?
Both EmbedSend and EmbedReceive returns an integer value, how does wolfssl handle these internally? Is there anything important to know about the return value? Can wolfssl in any way use the file descriptor (or context descriptor) to do anything and if so what?
Finally, in which way do wolfssl expect to send data and recieve data (by default)?

Share

Re: [SOLVED] Using custom send- & receive callback

Hi LimeD,

Firstly, great questions.  The wolfSSL I/O callbacks can be tricky to understand at first, but once you get the hang of them, they get much easier.

I'm working on putting together a thorough response for your questions and will post them here soon.

Thanks,
Chris

Re: [SOLVED] Using custom send- & receive callback

wolfSSL's I/O callbacks are prototyped by the following:

int (*CallbackIORecv)(WOLFSSL *ssl, char *buf, int sz, void *ctx);
int (*CallbackIOSend)(WOLFSSL *ssl, char *buf, int sz, void *ctx);

Whenever wolfSSL needs additional data to advance its internal state machine, it will call the RECEIVE callback. The job of the RECEIVE callback is to place data into the provided buffer “buf”. wolfSSL asks for “sz” bytes of data, but if the callback has less data than requested, it can copy the number of bytes it has available into “buf” and return the number of bytes that have been written.

Whenever wolfSSL needs to send data in order to advance its internal state machine, it will call the SEND callback. The job of the SEND callback is to send the data in the buffer “buf” to the peer. The number of bytes in “buf” is given to the callback through the “sz” parameter. If the callback is only able to send part of the data in “buf”, it should return the number of bytes that were sent. wolfSSL will internally loop back around and call the callback again to get the rest of the data.

The third thing that comes into play with the I/O callbacks is the associated context (void *ctx). This can be a pointer to anything the user would like handed back to them during the I/O callbacks. Oftentimes this is a custom structure that would hold a pointer to data buffers, data sizes, socket handles, or in your case information necessary to send/receive data over your UART connection.

These I/O callback “contexts” can be set by the application using:

void wolfSSL_SetIOReadCtx( WOLFSSL* ssl, void *ctx);
void wolfSSL_SetIOWriteCtx(WOLFSSL* ssl, void *ctx);

From your last post, it sounds like in your case, your UART might buffer data when received, then in the wolfSSL Receive callback, you would simply copy data from the buffered area to wolfSSL’s internal buffer in the callback.

Does this make sense?  Is there anything that needs clarifying?

Best Regards,
Chris

Re: [SOLVED] Using custom send- & receive callback

Thank you so much for your reply! It makes alot more sense now.
If more questions should arise, we now know were to ask them.

Thanks again

Share

Re: [SOLVED] Using custom send- & receive callback

Hi LimeD,

Great!  Glad that helped clear things up.

Best Regards,
Chris

Re: [SOLVED] Using custom send- & receive callback

We don't currently have an example of using the I/O callbacks with memory buffers.  If you were using memory buffers, you might have your own custom structure which contains pointers to buffers and members to hold sizes of the buffers.

Hi everyone, we now have an io-callback example using memory buffers. The example has both client and server in the same file and uses memory buffers to perform TLS handshake.

Example is here: https://github.com/wolfSSL/wolfssl-exam … mory-tls.c

Regards,

wolfSSL Team