"Split Packet" Mode of TCP Data Processing

Top  Previous  Next

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, 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 the sock.splittcppacket= 1- YES. After that, the on_sock_tcp_packet_arrival will be generated for each incoming TCP packet carrying any new data (i.e., not a retransmission 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_sys_init

  ...

  rx_tcp_packet_len=0

  sock.splittcppackets=YES

end sub

 

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

end sub

 

sub on_sock_data_arrival

  dim s as string

  'take exactly the contents of one packet (we assume that it can fit in the string!)

  s=sock.getdata(rx_tcp_packet_len)

  rx_tcp_packet_len=0 'very important!

end sub

 

 

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 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:

 

 

sock.splittcppackets=YES

...

...

sock.setdata("ABCDEFGH1234567890")

sock.send

while sock.txlen>0

  doevents

wend

 

 

Notice that this method has its disadvantage — your data throughput is diminished because your program is seeking an acknowledgement 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. Since in this mode the socket won't send that data unless it can send all of it, your TCP connection will get stuck!

Also, attempting to send the packet with 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 the amount of data exceeding MSS.