Setting the flags field in the IP header

I have a simple Python script that uses a socket module to send a UDP packet. The script works fine on my Windows box, but on my Ubuntu Linux machine the package it sends is a bit different. On Windows, the flags field in the IP header is zero, but using the same code on Linux, a package was created with the flag field set to 4. I would like to change my script to have consistent behavior on Windows and Linux.

Is there a way to control the flag field in a socket module? Or is this the parameter I need to change in Linux?

+5
source share
3 answers

I assume that the flags field is actually set to 2 = b010 instead of 4 - flags equal to 4 is an invalid IP packet. Remember that flags are a 3-bit value in the IP Header . I would expect to see UDP datagrams with a flag value of 2, which means "Do not fragment."

As for your question, I do not believe that there is a way to set IP flags directly without using all the raw sockets features . I would not worry about this, since most applications really have no good reason for extinguishing IP or even UDP / TCP headers directly.

+2
source

, . , SashaN D.Shwley, , " " UDP- Linux. , - PMTU. , UDP- Python, setsockopts .

import socket
IP_MTU_DISCOVER   = 10
IP_PMTUDISC_DONT  =  0  # Never send DF frames.
IP_PMTUDISC_WANT  =  1  # Use per route hints.
IP_PMTUDISC_DO    =  2  # Always DF.
IP_PMTUDISC_PROBE =  3  # Ignore dst pmtu.
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("10.0.0.1", 8000))
s.send("Hello World!") # DF bit is set in this packet
s.setsockopt(socket.SOL_IP, IP_MTU_DISCOVER, IP_PMTUDISC_DONT)
s.send("Hello World!") # DF bit is cleared in this packet
+6

can construct do the job?

+1
source

All Articles