pythonsocketstcppacketmtu

How to change TCP Header and options using Python's socket library


I will like to get some help on how to modify the TCP header as well as change the options on a TCP header. I am especially interested in the MSS section of the options.

I have tried using the setsockopt() with different options to no success.

Here is some code attempting to change the MSS:

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #! Settings mss
    s.setsockopt(socket.IPPROTO_TCP, socket.IP_OPTIONS , b"MSS:400")

I expect the MSS to change to 400. The code runs but it doesn't change the MSS (inspected with Wireshark).


Solution

  • Use the TCP_MAXSEG option.

    s.setsockopt(socket.IPPROTO_TCP, socket.TCP_MAXSEG, 400)