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
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) wsoption = tcplib.create_winscale(WSval-2) tcpwindow -= 2 tsoption = tcplib.create_timestamp(TSval-2, tsecr) tcpsoption = mssoption + wsoption + sackokoption + tsoption tcpsoption += tcpoption[idx:idx+12] elif kind == tcplib.TO_MP_DATA and optlen == 16: dsn = tcplib.unpack_mpdata(tcpoption[idx+2:idx+16]) tcpsoption += tcplib.create_mpack(dsn[0]+dsn[1]) idx += optlen olen -= optlen tcpflag = tcplib.TH_ACK if tcph.flag & tcplib.TH_SYN: tcpflag |= tcplib.TH_SYN datalen = 1 if tcph.flag & tcplib.TH_FIN: datalen = 1 if tcpflag & tcplib.TH_SYN or tcpflag & tcplib.TH_FIN or datalen > 0: if not tcpflag & tcplib.TH_SYN: tcpseqno = tcph.ackno tcpackno = tcph.seqno + datalen