Python socket.TCP_MAXSEG Examples

The following are 8 code examples of socket.TCP_MAXSEG(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module socket , or try the search function .
Example #1
Source File: tcprelay.py    From shadowsocksR-b with Apache License 2.0 5 votes vote down vote up
def _update_tcp_mss(self, local_sock):
        self._tcp_mss = TCP_MSS
        try:
            tcp_mss = local_sock.getsockopt(socket.SOL_TCP, socket.TCP_MAXSEG)
            if tcp_mss > 500 and tcp_mss <= 1500:
                self._tcp_mss = tcp_mss
            logging.debug("TCP MSS = %d" % (self._tcp_mss,))
        except:
            pass 
Example #2
Source File: tcprelay.py    From shadowsocks with Apache License 2.0 5 votes vote down vote up
def _update_tcp_mss(self, local_sock):
        self._tcp_mss = TCP_MSS
        try:
            tcp_mss = local_sock.getsockopt(socket.SOL_TCP, socket.TCP_MAXSEG)
            if tcp_mss > 500 and tcp_mss <= 1500:
                self._tcp_mss = tcp_mss
            logging.debug("TCP MSS = %d" % (self._tcp_mss,))
        except:
            pass 
Example #3
Source File: tcprelay.py    From Dockerfiles with Apache License 2.0 5 votes vote down vote up
def _update_tcp_mss(self, local_sock):
        self._tcp_mss = TCP_MSS
        try:
            tcp_mss = local_sock.getsockopt(socket.SOL_TCP, socket.TCP_MAXSEG)
            if tcp_mss > 500 and tcp_mss <= 1500:
                self._tcp_mss = tcp_mss
            logging.debug("TCP MSS = %d" % (self._tcp_mss,))
        except:
            pass 
Example #4
Source File: tcprelay.py    From SSRSpeed with GNU General Public License v3.0 5 votes vote down vote up
def _update_tcp_mss(self, local_sock):
        self._tcp_mss = TCP_MSS
        try:
            tcp_mss = local_sock.getsockopt(socket.SOL_TCP, socket.TCP_MAXSEG)
            if tcp_mss > 500 and tcp_mss <= 1500:
                self._tcp_mss = tcp_mss
            logging.debug("TCP MSS = %d" % (self._tcp_mss,))
        except:
            pass 
Example #5
Source File: tcprelay.py    From ssrr with Apache License 2.0 5 votes vote down vote up
def _update_tcp_mss(self, local_sock):
        self._tcp_mss = TCP_MSS
        try:
            tcp_mss = local_sock.getsockopt(socket.SOL_TCP, socket.TCP_MAXSEG)
            if tcp_mss > 500 and tcp_mss <= 1500:
                self._tcp_mss = tcp_mss
            logging.debug("TCP MSS = %d" % (self._tcp_mss,))
        except:
            pass 
Example #6
Source File: tcprelay.py    From shadowsocksr-python with Apache License 2.0 5 votes vote down vote up
def _update_tcp_mss(self, local_sock):
        self._tcp_mss = TCP_MSS
        try:
            tcp_mss = local_sock.getsockopt(socket.SOL_TCP, socket.TCP_MAXSEG)
            if tcp_mss > 500 and tcp_mss <= 1500:
                self._tcp_mss = tcp_mss
            logging.debug("TCP MSS = %d" % (self._tcp_mss,))
        except:
            pass 
Example #7
Source File: nat64-dpkt.py    From ai-smarthome with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def handle_tcp_state_tosock(self, ip):
        tcp = ip.data
        global tun, input
        log.debug("=== NAT64 TCP sock-send: %d %s."%(tcp.flags, self.sock))
        if self.sock is None:
            log.warning("Socket already closed.")
            return
        if tcp.flags & dpkt.tcp.TH_SYN > 0:
            # Use Window size to control max segment?
            self.sock.setsockopt(socket.SOL_TCP, socket.TCP_MAXSEG, 1000)
            log.debug("Maxseg: %d" % self.sock.getsockopt(socket.SOL_TCP, socket.TCP_MAXSEG))
            self.ack = tcp.seq + 1
            # We are established...
            self.state = TCP_ESTABLISHED
            self.window = tcp.win
            # Get the MSS of the options
            opts = dpkt.tcp.parse_opts(tcp.opts)
            for k, v in opts:
                if k == dpkt.tcp.TCP_OPT_MSS:
                    print("MSS:", v)
                    self.mss, = struct.unpack("!H", v)
                    print("MSS:", self.mss)
            log.debug("TCP State: %d SYN received." % self.mss)

            self.tcp_reply(ip, dpkt.tcp.TH_SYN | dpkt.tcp.TH_ACK)

            print("IP:", repr(ip))
            send_to_tun(bytes(ip))
        #    sock.send(ip.load)
        elif tcp.flags & dpkt.tcp.TH_FIN:
            self.state = TCP_FIN_CLOSE_WAIT
            self.ack = tcp.seq + 1
            self.timeout = time.time()
            log.debug("TCP: FIN received - sending FIN. %s" % self)
            self.tcp_reply(ip, dpkt.tcp.TH_FIN | dpkt.tcp.TH_ACK)
            print("IP:", repr(ip))

            send_to_tun(bytes(ip))
            # Clean out this socket?
        elif tcp.flags & dpkt.tcp.TH_ACK:
            if self.state == TCP_ESTABLISHED:
                if len(tcp.data) == 0:
                    log.debug("ESTABLISHED or ACK from other side. seq: %d  ack: %d" % (tcp.seq, tcp.ack))
                    self.seq = tcp.ack
                else:
                    # ACK immediately - we assume that we get data from other side soon...
                    log.debug("TCP: received %d seq: %d  ack%d ." % (len(tcp.data), tcp.seq, tcp.ack))
                    self.ack = tcp.seq + len(tcp.data)
                    # We should also handle the sanity checks for the ACK
                    self.seq = tcp.ack
                    self.tcp_reply(ip, dpkt.tcp.TH_ACK)
                    print("IP:", repr(ip))

                    send_to_tun(bytes(ip))
            add_socket(self.sock)
        if len(tcp.data) > 0:
            self.sock.send(tcp.data)

# Remove the state for this specific socket 
Example #8
Source File: nat64.py    From ai-smarthome with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def handle_tcp_state_tosock(self, ip):
        global tun, input
        log.debug("=== NAT64 TCP sock-send: %d %s."%(ip.flags, self.sock))
        if self.sock is None:
            log.warning("Socket already closed.")
            return
        if ip.flags.S:
            # Use Window size to control max segment?
            self.sock.setsockopt(socket.SOL_TCP, socket.TCP_MAXSEG, 1000)
            log.debug("Maxseg: %d" % self.sock.getsockopt(socket.SOL_TCP, socket.TCP_MAXSEG))
            self.ack = ip.seq + 1
            # We are established...
            self.state = TCP_ESTABLISHED
            self.window = ip[TCP].window
            # Get the MSS of the options
            for k, v in ip[TCP].options:
                if k == 'MSS':
                    self.mss = v
            log.debug("TCP State: %s SYN received." % self)
            ipv6 = IPv6(src=ip.dst, dst=ip.src)/TCP(sport=ip.dport, dport=ip.sport, flags="SA", seq=self.seq, ack=self.ack)
            ipv6.show()
            send_to_tun(bytes(IPv6(ipv6)))
        #    sock.send(ip.load)
        elif ip.flags.FA or ip.flags.F:
            self.state = TCP_FIN_CLOSE_WAIT
            self.ack = ip.seq + 1
            self.timeout = time.time()
            log.debug("TCP: FIN received - sending FIN. %s" % self)
            ipv6 = IPv6(src=ip.dst, dst=ip.src)/TCP(sport=ip.dport, dport=ip.sport, flags="FA", seq=self.seq, ack=self.ack)
            ipv6.show()
            send_to_tun(bytes(IPv6(ipv6)))
            # Clean out this socket?
        elif ip.flags.A or ip.flags.AP:
            if self.state == TCP_ESTABLISHED:
                if not hasattr(ip,'load'):
                    log.debug("ESTABLISHED or ACK from other side. seq: %d  ack: %d" % (ip.seq, ip.ack))
                    self.seq = ip.ack
                else:
                    # ACK immediately - we assume that we get data from other side soon...
                    log.debug("TCP: received %d seq: %d  ack%d ." % (len(ip.load), ip.seq, ip.ack))
                    self.ack = ip.seq + len(ip.load)
                    # We should also handle the sanity checks for the ACK
                    self.seq = ip.ack
                    ipv6 = IPv6(src=ip.dst, dst=ip.src)/TCP(sport=ip.dport, dport=ip.sport, flags="A", seq=self.seq, ack=self.ack)
                    ipv6 = IPv6(ipv6)
                    ipv6.show()
                    send_to_tun(bytes(ipv6))
            add_socket(self.sock)
        if hasattr(ip, 'load'):
            self.sock.send(ip.load)