The question is as follows: what is the algorithm to resegment an incoming TCP/UDP packet?

So, for example, I have a TCP packet, which size is more than some number, and I want to split this packet into two, three, etc., so that each segment's size would be less or equal to that predefined number. The basics are simple: divide the data in the packet, recalculate hashes and update sequence numbers, but I am sure there is more to it, and was wondering if there is an algorithm, for instance, in pseudocode, which would describe the process - I could not find it.

Also, I should note that the question is only about L4 segmentation, L3 is not covered. Thanks in advance!

  • Sadly, programming questions are off topic here. You could try asking on Stack Overflow.
    – Ron Trunk
    Mar 5 '21 at 13:17
  • Did any answer help you? if so, you should accept the answer so that the question does not keep popping up forever, looking for an answer. Alternatively, you could post and accept your own answer.
    – Ron Maupin
    Dec 23 '21 at 17:13
  • @RonMaupin none of the answers have helped me. I solved the problem, however, and can write an asnwer with an algorithm of how to segment a packet manually, but as it was mentioned before, it could be too programmatical for this stack exchange branch. Should I? Dec 25 '21 at 15:41

TCP (also other transport protocols) choose the PDU size when the datagram is sent. After that, it is up to the network protocol, and the transport protocol is not involved until the datagram is received by the transport protocol at the destination, where it is presented to the application.

IPv4 can fragment network packets in the path, but that is now mostly deprecated. IPv6 has eliminated path fragmentation.


what is the algorithm to resegment an incoming TCP/UDP packet?

That is not a thing.

You can fragment the encapsulating IP packet if an MTU decrease within the path doesn't allow its current size, that's it.

To avoid this problem you can use path MTU discovery (pMTUd) - mandatory for IPv6 and recommended for IPv4 as well - or MSS clamping for TCP. For UDP, the only option is to use smaller datagrams (application-specific, off-topic here) or IP fragmentation.

Not the answer you're looking for? Browse other questions tagged or ask your own question.