SkypeMeSM
SkypeMeSM

Reputation: 3287

udp packet fragmentation for raw sockets

Follow-up of question packet fragmentation for raw sockets

If I have a raw socket implemented as such:

  if ((sip_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
      {
    cout << "Unable to create the SIP sockets."<< sip_socket<<" \n";
    return -3;
      }

   if ( setsockopt(sip_socket, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) == -1)
      {
   cerr << "Unable to set option to Raw Socket.\n";
   return -4;
      };  

how can I set the ipHdr->fragment_offset (16 bits including 3 bit flags) if I have a packet of size 1756 (not including the IP header)?
Do I need to prepare two packets-one of size 1480 and another of size 276, and then slap IP headers on both packets?

Can anyone point to an example code for this?

Upvotes: 2

Views: 4530

Answers (2)

user23089815
user23089815

Reputation: 11

NOTE: the fragment offset value is expressed as 8byte blocks. So for the above the first packet, fragment offset value will be zero but more fragments bit will be set ipHdr->fragment_offset = 0x01 << 13

For the second packet, fragment offset value is 1480/8 = 135 (same as right shift by 3bits) and flags is zero ipHdr->fragment_offset = 1480 >> 3

Upvotes: 1

caf
caf

Reputation: 239321

Yes, you need to prepare two packets, each with their own IP header.

If you put 1480 bytes of data in the first packet and 276 in the second, then the IP headers should be identical, except for these fields:

  • Fragment Offset: set to 0 in the first packet, and 1480 in the second;
  • Total Length: set to 1480 plus the header length in the first packet, and 276 plus the header length in the second packet;
  • MF flag: set to 1 in the first packet and 0 in the second;
  • Header Checksum: Recalculated over the different headers as appropriate.

Upvotes: 2

Related Questions