0siris
0siris

Reputation: 127

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).

Upvotes: 1

Views: 4626

Answers (2)

Sam Mason
Sam Mason

Reputation: 16184

in addition to what @Barmar said, you can find more info in the "man pages" on any unix system by typing one of:

man 7 ip
man 7 tcp
man 7 udp

man is short for manual, 7 is overview/misc section

the tcp page says this about TCP_MAXSEG:

The maximum segment size for outgoing TCP packets. In Linux 2.2 and earlier, and in Linux 2.6.28 and later, if this option is set before connection establishment, it also changes the MSS value announced to the other end in the initial packet. Values greater than the (eventual) interface MTU have no effect. TCP will also impose its minimum and maximum bounds over the value provided.

Upvotes: 0

Barmar
Barmar

Reputation: 781004

Use the TCP_MAXSEG option.

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

Upvotes: 5

Related Questions