def decompose_options(ofield): tcpoptions = [] olen = len(ofield) if olen == 0: return tcpoptions # Sequentially parse each option idx = 0 while olen > 0: if len(ofield[idx:idx+1]) == 0: break kind = struct.unpack('b', ofield[idx:idx+1])[0] if kind == 0: tcpoptions.append(['EOL','']) elif kind == 1: tcpoptions.append(['NOP','']) if kind == 0 or kind == 1: idx += 1 olen -= 1 continue optlen = struct.unpack('b', ofield[idx+1: idx+2])[0] if kind == tcplib.TO_MSS and olen >= 4: mss = tcplib.unpack_mss(ofield[idx+2: idx+4]) tcpoptions.append(['MSS', mss]) elif kind == tcplib.TO_WSCALE and olen >= 3: wscale = tcplib.unpack_winscale(ofield[idx+2: idx+3]) tcpoptions.append(['WSCALE', wscale]) elif kind == tcplib.TO_SACKOK and olen >= 2: tcpoptions.append(['SACKOK', '']) elif kind == tcplib.TO_TIMESTAMP and olen >= 10: tsval, tsecr = tcplib.unpack_timestamp(ofield[idx+2: idx+10]) tcpoptions.append(['TIMESTAMP', tsval, tsecr]) elif kind == tcplib.TO_MP_CAPABLE and olen >= 12: token, idsn = tcplib.unpack_mpcapable(ofield[idx+2: idx+12]) tcpoptions.append(['MP_CAPABLE', token, idsn]) elif kind == tcplib.TO_MP_DATA and olen >= 16: dseq, dlen, sseq = tcplib.unpack_mpdata(ofield[idx+2: idx+16]) tcpoptions.append(['MP_DATA', dseq, dlen, sseq]) elif kind == tcplib.TO_MP_ACK and olen >= 10: dack = tcplib.unpack_mpack(ofield[idx+2: idx+10]) tcpoptions.append(['MP_ACK', dack]) idx += optlen olen -= optlen return tcpoptions
olen = len(tcpoption) idx = 0 while olen > 0: kind = struct.unpack('b', tcpoption[idx:idx+1])[0] if kind == 0: break # EOF if kind == 1: # NOP idx += 1 olen -= 1 continue optlen = struct.unpack('b', tcpoption[idx+1:idx+2])[0] if platform == P_PLAB and kind == tcplib.TO_TIMESTAMP and optlen == 10 \ and tcph.flag & tcplib.TH_SYN == 1: pass elif kind == tcplib.TO_TIMESTAMP and optlen == 10: rcv_syntsecr = tcplib.unpack_timestamp(tcpoption[idx+2:idx+10])[1] if tcph.flag & tcplib.TH_SYN and rcv_syntsecr != 0: if rcv_syntsecr == SynTsEcr: sigval = 2 else: sigval = 4 mssoption = tcplib.create_mss(MSSval-sigval) wsoption = tcplib.create_winscale(WSval-sigval) tcpwindow -= sigval TSval = rcv_syntsecr tcpsoption = mssoption + wsoption + sackokoption tsecr = tcplib.unpack_timestamp(tcpoption[idx+2:idx+10])[0] tcpsoption += tcplib.create_timestamp(TSval, tsecr) elif kind == tcplib.TO_MP_CAPABLE and optlen == 12: if datalen is 0: mssoption = tcplib.create_mss(MSSval-2)