"Split Packet" Mode of TCP Data Processing
Though our customer's feedback we have learned that sometimes it may be necessary to know the size of individual TCP packets. For example, as it turns out, some data encryption methods work on a packet level. Erase the border between TCP packets — and you can't decrypt the data.
Introduced in Tibbo BASIC/C V2.0, the new sock.splittcppackets property and on_sock_tcp_packet_arrival event allow you to process incoming TCP data packet by packet. To achieve this, set sock.splittcppackets = 1 — YES. After that, on_sock_tcp_packet_arrival will be generated for each incoming TCP packet carrying any new data (i.e., not retransmitted data). Here is an example of use:
dim rx_tcp_packet_len as word 'to keep the size of the next incoming TCP packet
sub on_sock_tcp_packet_arrival(len as word)
rx_tcp_packet_len=len 'we get the size of the next packet we are going to process
dim s as string
'take exactly the contents of one packet (we assume that it can fit in the string!)
rx_tcp_packet_len=0 'very important!
Naturally, you may also need to control the size of outgoing TCP packets. This is done in a different way. With sock.splittcppackets = 1 — YES, when you put some data into the TX buffer and execute sock.send, the socket won't send this data out unless the entire contents of the TX buffer can be sent out in a single TCP packet. Here is an example of how you can use that:
Notice that this method has a disadvantage — your data throughput is diminished, because your program is seeking an acknowledgment for each TCP packet being sent. On the other hand, this is a bulletproof way of making sure that the outgoing TCP packet contains exactly the data you intended.
Be careful not to try to send more data than the RX buffer size on the other end. In this mode, the socket won't send that data unless it can send all of it, so your TCP connection will get stuck!
Also, attempting to send a packet with a size exceeding the "maximum segment size" (MSS), as specified by the other end, will lead to data fragmentation! The socket will never send any TCP packet with an amount of data exceeding the MSS.
Please note that the split packet mode is incompatible with TLS. Enabling split packet mode will corrupt the encrypted data being sent and received.