def compose_options(options): tcpsoption = "" avail_opt_len = 40 for opt in options: if opt[0] == 'NOP': if avail_opt_len < 1: break nop = tcplib.create_nop() tcpsoption += nop avail_opt_len -= 1 continue if opt[0] == 'MSS': if avail_opt_len < 4: break mssoption = tcplib.create_mss(opt[1]) tcpsoption += mssoption avail_opt_len -= 4 continue if opt[0] == 'WSCALE': if avail_opt_len < 3: break wsoption = tcplib.create_winscale(opt[1]) tcpsoption += wsoption avail_opt_len -= 3 continue if opt[0] == 'SACKOK': if avail_opt_len < 2: break sackok_option = tcplib.create_sackok() tcpsoption += sackok_option avail_opt_len -= 2 continue if opt[0] == 'TIMESTAMP': if avail_opt_len < 10: break tsoption = tcplib.create_timestamp(opt[1], opt[2]) tcpsoption += tsoption avail_opt_len -= 10 continue if opt[0] == 'MP_CAPABLE': length = 20 if opt[3] is not None else 12 if avail_opt_len < length: break mpcap_option = tcplib.create_mpcapable(opt[1], opt[2], opt[3]) tcpsoption += mpcap_option avail_opt_len -= length continue if opt[0] == 'MP_DSS': length = (14 if opt[6] is not None else 0) + (4 if opt[4] is not None else 0) + \ (4 if opt[4] is not None and opt[3] else 0) + (4 if opt[5] else 0) if avail_opt_len < length: break dss_option = tcplib.create_mpdss(opt[1], opt[2], opt[3], opt[4], \ opt[5], opt[6], opt[7]) tcpsoption += dss_option avail_opt_len -= length continue if opt[0] == 'MP_ACK': if avail_opt_len < 10: break dataack_option = tcplib.create_mpack(opt[1]) tcpsoption += dataack_option avail_opt_len -= 10 continue tcpsoption += tcplib.check_padding(tcpsoption) return tcpsoption
def compose_options(options): tcpsoption = "" avail_opt_len = 40 for opt in options: if opt[0] == 'NOP': if avail_opt_len < 1: break nop = tcplib.create_nop() tcpsoption += nop avail_opt_len -= 1 continue if opt[0] == 'MSS': if avail_opt_len < 4: break mssoption = tcplib.create_mss(opt[1]) tcpsoption += mssoption avail_opt_len -= 4 continue if opt[0] == 'WSCALE': if avail_opt_len < 3: break wsoption = tcplib.create_winscale(opt[1]) tcpsoption += wsoption avail_opt_len -= 3 continue if opt[0] == 'SACKOK': if avail_opt_len < 2: break sackok_option = tcplib.create_sackok() tcpsoption += sackok_option avail_opt_len -= 2 continue if opt[0] == 'TIMESTAMP': if avail_opt_len < 10: break tsoption = tcplib.create_timestamp(opt[1], opt[2]) tcpsoption += tsoption avail_opt_len -= 10 continue if opt[0] == 'MP_CAPABLE': if avail_opt_len < 12: break mpcap_option = tcplib.create_mpcapable(opt[1], opt[2]) tcpsoption += mpcap_option avail_opt_len -= 12 continue if opt[0] == 'MP_DATA': if avail_opt_len < 16: break # (Dsn (8 Byte), Dlen (2 Byte), Ssn(4 Byte)) dsn_option = tcplib.create_mpdata(opt[1], opt[2], opt[3]) tcpsoption += dsn_option avail_opt_len -= 16 continue if opt[0] == 'MP_ACK': if avail_opt_len < 10: break dataack_option = tcplib.create_mpack(opt[1]) tcpsoption += dataack_option avail_opt_len -= 10 continue tcpsoption += tcplib.check_padding(tcpsoption) return tcpsoption
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 # special hack parse