org.pcap4j.packet.namednumber.IpNumber Java Examples

The following examples show how to use org.pcap4j.packet.namednumber.IpNumber. 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 check out the related API usage on the sidebar.
Example #1
Source File: MonitorController.java    From trex-stateless-gui with Apache License 2.0 6 votes vote down vote up
private <T extends IpPacket> Map<String, Object> parseIP(Stack<String> headers, T pkt) {
    headers.push(pkt instanceof IpV4Packet ? "IPv4" : "IPv6");
    Map<String, Object> result = new HashMap<>();
    result.put("info", "");
    String src = pkt.getHeader().getSrcAddr().toString();
    String dst = pkt.getHeader().getDstAddr().toString();
    result.put("src", pkt instanceof IpV4Packet ? src.substring(1) : src);
    result.put("dst", pkt instanceof IpV4Packet ? dst.substring(1) : dst);

    IpNumber protocol = pkt.getHeader().getProtocol();

    if (IpNumber.UDP.equals(protocol)) {
        result.putAll(parseUDP(headers, (UdpPacket) pkt.getPayload()));
    } else if (IpNumber.TCP.equals(protocol)) {
        result.putAll(parseTCP(headers, (TcpPacket) pkt.getPayload()));
    }

    return result;
}
 
Example #2
Source File: TrexIpV4Packet.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param builder
 * @param protocol
 */
public void buildPacket(AbstractPacket.AbstractBuilder builder, IpNumber protocol) {

    byte[] pad = new byte[1];
    String payloadPattern = payload.getPayloadPattern();
    String payloadString = payload.getPayloadType().getPadPayloadString(payloadPattern, (totalLength - 20) * 2);

    if (packetLength > DEFAULT_PACKET_LENGTH) {
        payloadString = payload.getPayloadType().getPadPayloadString(payloadPattern, payloadString.length() + ((packetLength - DEFAULT_PACKET_LENGTH) * 2));
        this.totalLength = (short) ((int) totalLength + (packetLength - DEFAULT_PACKET_LENGTH));
    }
    pad = payload.getPayloadPad(payloadString, totalLength - 20);

    ipV4Builder = new IpV4Packet.Builder();
    ipV4Builder.version(version)
            .ihl(ihl)
            .tos(tos)
            .totalLength(totalLength)
            .identification(identification)
            .reservedFlag(reservedFlag)
            .ttl(ttl)
            .fragmentOffset((short) 0)
            .protocol(protocol)
            .headerChecksum(headerChecksum)
            .srcAddr(srcAddr)
            .dstAddr(dstAddr)
            .protocol(protocol)
            .correctChecksumAtBuild(true)
            //  .correctLengthAtBuild(true)
            .payloadBuilder(builder);

    if (protocol != IpNumber.TCP && protocol != IpNumber.UDP) {
        ipV4Builder.paddingAtBuild(false)
                .padding(pad);
    } else {
        ipV4Builder.paddingAtBuild(true).correctLengthAtBuild(true);
    }

    this.packet1 = ipV4Builder.build();
}
 
Example #3
Source File: ProtocolDataView.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Prepare and return IPV4 builder
 *
 * @return
 * @throws Exception
 */
private Builder getIPV4Packet(int totalLength, int packetLength) throws Exception {
    if (selections.isIPV4Selected()) {
        TrexIpV4Packet ipV4Packet = new TrexIpV4Packet();
        ipV4Packet.setSrcAddr(ipv4View.getSourceAddress().getAddress());
        ipV4Packet.setDstAddr(ipv4View.getDestinationAddress().getAddress());
        ipV4Packet.setTotalLength((short) totalLength);
        ipV4Packet.setPacketLength(packetLength + 4);
        Payload payload = payloadView.getPayload();
        ipV4Packet.setPayload(payload);

        int ipv4PacketLength = PacketBuilderHelper.getIPV4PacketLength(selections.isTaggedVlanSelected(), packetLength, totalLength);
        int tcpUdpPacketLength = PacketBuilderHelper.getTcpUdpPacketLength(selections.isTaggedVlanSelected(), packetLength, totalLength);

        if (selections.isTCPSelected()) {
            ipV4Packet.setPacketLength(ipv4PacketLength);
            ipV4Packet.buildPacket(tcpView.getTcpPacket(payload, tcpUdpPacketLength).getBuilder(), IpNumber.TCP);
        } else if (selections.isUDPSelected()) {
            ipV4Packet.setPacketLength(ipv4PacketLength);
            ipV4Packet.buildPacket(udpView.getUDPPacket(payload, tcpUdpPacketLength).getBuilder(), IpNumber.UDP);
        } else {
            ipV4Packet.buildPacket(null, IpNumber.getInstance((byte) 0));
        }
        return ipV4Packet.getBuilder();
    }
    return null;
}
 
Example #4
Source File: TCPTest.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
@Override
protected IpV4Packet.Builder getIPV4PacketBuilder(int totalLength, int packetLength, Payload payload, PacketData packetData) throws UnknownHostException {
    LOG.info("Create IPV4 Packet");
    TrexIpV4Packet ipV4Packet = prepareIPV4Packet(totalLength, packetLength, payload, packetData);

    int ipv4PacketLength = PacketBuilderHelper.getIPV4PacketLength(packetData.isTaggedVlan(), packetLength, totalLength);
    ipV4Packet.setPacketLength(ipv4PacketLength);
    int tcpPacketLength = PacketBuilderHelper.getTcpUdpPacketLength(packetData.isTaggedVlan(), packetLength, totalLength);
    // build  packet
    ipV4Packet.buildPacket(getTcpPacket(payload, tcpPacketLength, packetData).getBuilder(), IpNumber.TCP);

    return ipV4Packet.getBuilder();
}
 
Example #5
Source File: IPV4Test.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Create IPV4 packet
 *
 * @param totalLength
 * @param packetLength
 * @param payload
 * @param packetData
 * @return IPV4 packet
 * @throws java.net.UnknownHostException
 */
@Override
protected Builder getIPV4PacketBuilder(int totalLength, int packetLength, Payload payload, PacketData packetData) throws UnknownHostException {

    LOG.info("Create IPV4 Packet");
    TrexIpV4Packet ipV4Packet = prepareIPV4Packet(totalLength, packetLength, payload, packetData);

    // build IPV4 packet
    ipV4Packet.buildPacket(null, IpNumber.getInstance((byte) 0));

    return ipV4Packet.getBuilder();
}
 
Example #6
Source File: UDPTest.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
@Override
protected IpV4Packet.Builder getIPV4PacketBuilder(int totalLength, int packetLength, Payload payload, PacketData packetData) throws UnknownHostException {
    LOG.info("Create IPV4 Packet");
    TrexIpV4Packet ipV4Packet = prepareIPV4Packet(totalLength, packetLength, payload, packetData);

    int ipv4PacketLength = PacketBuilderHelper.getIPV4PacketLength(packetData.isTaggedVlan(), packetLength, totalLength);
    ipV4Packet.setPacketLength(ipv4PacketLength);
    int udpPacketLength = PacketBuilderHelper.getTcpUdpPacketLength(packetData.isTaggedVlan(), packetLength, totalLength);
    // build  packet
    ipV4Packet.buildPacket(getUdpPacket(payload, udpPacketLength, packetData).getBuilder(), IpNumber.UDP);

    return ipV4Packet.getBuilder();
}
 
Example #7
Source File: PacketUtils.java    From finalspeed-91yun with GNU General Public License v2.0 4 votes vote down vote up
static Packet createDataPacket(
		MacAddress srcAddress_mac,
		MacAddress dstAddrress_mac,
		Inet4Address srcAddress,short srcPort,
		Inet4Address dstAddress,short dstPort,
		int sequence,int ack, byte[] data,short ident){
	Packet p=null;

	TcpPacket.Builder builder_tcp=new TcpPacket.Builder();
	builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(data));
	builder_tcp.correctChecksumAtBuild(true);
	builder_tcp.correctLengthAtBuild(true);
	builder_tcp.paddingAtBuild(true);
	builder_tcp.ack(true);
	builder_tcp.acknowledgmentNumber(ack);
	//builder_tcp.checksum(tcpHeader.getChecksum());
	//builder_tcp.dataOffset((byte)8);
	builder_tcp.dstAddr(dstAddress);
	builder_tcp.dstPort(new TcpPort( dstPort,""));
	builder_tcp.fin(false);
	//builder_tcp.options(tcpHeader.getOptions());
	//builder_tcp.padding(tcpHeader.getPadding());
	builder_tcp.psh(false);
	builder_tcp.reserved((byte) 0);
	builder_tcp.rst(false);
	builder_tcp.sequenceNumber(sequence);
	builder_tcp.srcAddr(srcAddress);
	builder_tcp.srcPort(new TcpPort( srcPort,""));
	builder_tcp.syn(false);
	builder_tcp.urg(false);
	//builder_tcp.urgentPointer(tcpHeader.getUrgentPointer());
	builder_tcp.window( window);

	IpV4Packet.Builder builder_ipv4=new IpV4Packet.Builder();
	builder_ipv4.correctChecksumAtBuild(true);
	builder_ipv4.correctLengthAtBuild(true);
	builder_ipv4.dontFragmentFlag(true);
	builder_ipv4.paddingAtBuild(true);
	builder_ipv4.dstAddr(dstAddress);
	builder_ipv4.fragmentOffset( (short)0);
	//builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum());
	//short identification= Math.abs(random.nextInt(Short.MAX_VALUE));
	//identification=ident;
	builder_ipv4.identification(ident);
	builder_ipv4.ihl((byte) 5);
	builder_ipv4.moreFragmentFlag(false);
	//builder_ipv4.options(ipV4Header.getOptions());
	//builder_ipv4.padding(ipV4Header.getPadding());

	builder_ipv4.protocol(IpNumber.TCP);
	//builder_ipv4.reservedFlag(ipV4Header.getReservedFlag());
	builder_ipv4.srcAddr(srcAddress);
	builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0));
	//builder_ipv4.totalLength( 52);
	builder_ipv4.ttl(ttl);
	builder_ipv4.version(IpVersion.IPV4);
	builder_ipv4.payloadBuilder(builder_tcp);

	p = buildIpV4(srcAddress_mac,dstAddrress_mac,builder_ipv4);

	return p;
}
 
Example #8
Source File: DhcpPacketListener.java    From dhcp4j with Apache License 2.0 4 votes vote down vote up
@Override
public void gotPacket(Packet rawPacket) {
    try {
        LOG.info("Read raw " + rawPacket);
        IpV4Packet ipPacket = rawPacket.get(IpV4Packet.class);
        UdpPacket udpPacket = rawPacket.get(UdpPacket.class);
        byte[] dhcpData = udpPacket.getPayload().getRawData();
        InetSocketAddress remoteAddress = new InetSocketAddress(ipPacket.getHeader().getSrcAddr(), udpPacket.getHeader().getSrcPort().valueAsInt());
        InetSocketAddress localAddress = new InetSocketAddress(ipPacket.getHeader().getDstAddr(), udpPacket.getHeader().getDstPort().valueAsInt());
        DhcpRequestContext context = new DhcpRequestContext(interfaceAddresses, remoteAddress, localAddress);
        DhcpMessage request = decoder.decode(ByteBuffer.wrap(dhcpData));
        LOG.info("Read DHCP " + request);
        DhcpMessage reply = service.getReplyFor(context, request);
        if (reply == null)
            return;
        byte[] replyData = new byte[1536];
        ByteBuffer buffer = ByteBuffer.wrap(replyData);
        encoder.encode(buffer, reply);
        replyData = Arrays.copyOf(replyData, buffer.position());    // Truncate array to writer position.
        UnknownPacket.Builder dhcpBuilder = new UnknownPacket.Builder()
                .rawData(replyData);
        UdpPacket.Builder udpBuilder = new UdpPacket.Builder()
                .payloadBuilder(dhcpBuilder)
                .srcPort(udpPacket.getHeader().getDstPort())
                .dstPort(udpPacket.getHeader().getSrcPort())
                .correctChecksumAtBuild(true)
                .correctLengthAtBuild(true);
        IpV4Packet.Builder ipBuilder = new IpV4Packet.Builder()
                .payloadBuilder(udpBuilder)
                .srcAddr(null) // TODO
                .dstAddr(null) // TODO
                .protocol(IpNumber.UDP)
                .correctChecksumAtBuild(true)
                .correctLengthAtBuild(true)
                .paddingAtBuild(true);
        EthernetPacket.Builder ethernetBuilder = new EthernetPacket.Builder()
                .payloadBuilder(ipBuilder)
                .type(EtherType.IPV4)
                .paddingAtBuild(true);
        Packet replyPacket = ethernetBuilder.build();
        // handle.sendPacket(replyPacket);
    } catch (Exception e) {
        LOG.error("DHCP failed", e);
    }
}
 
Example #9
Source File: DhcpPacketListener.java    From dhcp4j with Apache License 2.0 4 votes vote down vote up
@Override
public void gotPacket(Packet rawPacket) {
    try {
        LOG.info("Read raw " + rawPacket);
        IpV4Packet ipPacket = rawPacket.get(IpV4Packet.class);
        UdpPacket udpPacket = rawPacket.get(UdpPacket.class);
        byte[] dhcpData = udpPacket.getPayload().getRawData();
        InetSocketAddress remoteAddress = new InetSocketAddress(ipPacket.getHeader().getSrcAddr(), udpPacket.getHeader().getSrcPort().valueAsInt());
        InetSocketAddress localAddress = new InetSocketAddress(ipPacket.getHeader().getDstAddr(), udpPacket.getHeader().getDstPort().valueAsInt());
        DhcpRequestContext context = new DhcpRequestContext(interfaceAddresses, remoteAddress, localAddress);
        DhcpMessage request = decoder.decode(ByteBuffer.wrap(dhcpData));
        LOG.info("Read DHCP " + request);
        DhcpMessage reply = service.getReplyFor(context, request);
        if (reply == null)
            return;
        byte[] replyData = new byte[1536];
        ByteBuffer buffer = ByteBuffer.wrap(replyData);
        encoder.encode(buffer, reply);
        replyData = Arrays.copyOf(replyData, buffer.position());    // Truncate array to writer position.
        UnknownPacket.Builder dhcpBuilder = new UnknownPacket.Builder()
                .rawData(replyData);
        UdpPacket.Builder udpBuilder = new UdpPacket.Builder()
                .payloadBuilder(dhcpBuilder)
                .srcPort(udpPacket.getHeader().getDstPort())
                .dstPort(udpPacket.getHeader().getSrcPort())
                .correctChecksumAtBuild(true)
                .correctLengthAtBuild(true);
        IpV4Packet.Builder ipBuilder = new IpV4Packet.Builder()
                .payloadBuilder(udpBuilder)
                .srcAddr(null) // TODO
                .dstAddr(null) // TODO
                .protocol(IpNumber.UDP)
                .correctChecksumAtBuild(true)
                .correctLengthAtBuild(true)
                .paddingAtBuild(true);
        EthernetPacket.Builder ethernetBuilder = new EthernetPacket.Builder()
                .payloadBuilder(ipBuilder)
                .type(EtherType.IPV4)
                .paddingAtBuild(true);
        Packet replyPacket = ethernetBuilder.build();
        // handle.sendPacket(replyPacket);
    } catch (Exception e) {
        LOG.error("DHCP failed", e);
    }
}
 
Example #10
Source File: PacketUtils.java    From finalspeed with GNU General Public License v2.0 4 votes vote down vote up
static Packet createSync(
            MacAddress srcAddress_mac,
            MacAddress dstAddrress_mac,
            Inet4Address srcAddress, short srcPort,
            Inet4Address dstAddress, short dstPort,
            int sequence, short ident) {
        TcpPacket.Builder builder_tcp = new TcpPacket.Builder();
        //builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[0]));
        builder_tcp.correctChecksumAtBuild(true);
        builder_tcp.correctLengthAtBuild(true);
        builder_tcp.paddingAtBuild(true);
        //builder_tcp.ack(true);
        //builder_tcp.acknowledgmentNumber(ack_sequence);
        //builder_tcp.checksum(tcpHeader.getChecksum());
        //builder_tcp.dataOffset((byte) 8);
        builder_tcp.dstAddr(dstAddress);
        builder_tcp.dstPort(new TcpPort(dstPort, ""));
        //builder_tcp.fin(tcpHeader.getFin());

        TcpNoOperationOption nop = TcpNoOperationOption.getInstance();

        ArrayList<TcpOption> tcp_options = new ArrayList<>();

        TcpMaximumSegmentSizeOption seg_option = new TcpMaximumSegmentSizeOption.Builder().maxSegSize(mtu)
                .correctLengthAtBuild(true).build();
        tcp_options.add(seg_option);

        tcp_options.add(nop);

        TcpWindowScaleOption win_option = new TcpWindowScaleOption.Builder().shiftCount((byte) 6)
                .correctLengthAtBuild(true).build();
        tcp_options.add(win_option);

        tcp_options.add(nop);
        tcp_options.add(nop);

        TcpSackPermittedOption sack_permit_option = TcpSackPermittedOption.getInstance();
        tcp_options.add(sack_permit_option);

        builder_tcp.options(tcp_options);

        //builder_tcp.padding(tcpHeader.getPadding());
        builder_tcp.psh(false);
        builder_tcp.reserved((byte) 0);
        builder_tcp.rst(false);
        builder_tcp.sequenceNumber(sequence);
        builder_tcp.srcAddr(srcAddress);
        builder_tcp.srcPort(new TcpPort(srcPort, ""));
        builder_tcp.syn(true);
        builder_tcp.urg(false);
        //builder_tcp.urgentPointer(tcpHeader.getUrgentPointer());
        builder_tcp.window(window);

        IpV4Packet.Builder builder_ipv4 = new IpV4Packet.Builder();
        builder_ipv4.correctChecksumAtBuild(true);
        builder_ipv4.correctLengthAtBuild(true);
        builder_ipv4.paddingAtBuild(true);
        builder_ipv4.dstAddr(dstAddress);
        builder_ipv4.dontFragmentFlag(true);
        builder_ipv4.fragmentOffset((short) 0);
        //builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum());
        //short identification= Math.abs(random.nextInt(Short.MAX_VALUE));
        builder_ipv4.identification(ident);
        builder_ipv4.ihl((byte) 5);
        //builder_ipv4.moreFragmentFlag(ipV4Header.getMoreFragmentFlag());
        //builder_ipv4.options(ipV4Header.getOptions());
        //builder_ipv4.padding(ipV4Header.getPadding());

        builder_ipv4.protocol(IpNumber.TCP);
//		builder_ipv4.reservedFlag(ipV4Header.getReservedFlag());
        builder_ipv4.srcAddr(srcAddress);
        builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0));
        //builder_ipv4.totalLength( 52);
        builder_ipv4.ttl(ttl);
        builder_ipv4.version(IpVersion.IPV4);
        builder_ipv4.payloadBuilder(builder_tcp);
// 		IpV4Packet p4=builder_ipv4.build();
//		TcpPacket tcpPacket=builder_tcp.build();
        //selfAckTable.add(identification);
        //System.out.println("自定义确认 "+" identification "+identification+" ack_sequence "+ack_sequence+" # "+tcpPacket
        // .getHeader());
        return buildIpV4(srcAddress_mac, dstAddrress_mac, builder_ipv4);

    }
 
Example #11
Source File: PacketUtils.java    From finalspeed with GNU General Public License v2.0 4 votes vote down vote up
static Packet createSyncAck(
        MacAddress srcAddress_mac,
        MacAddress dstAddrress_mac,
        Inet4Address srcAddress, short srcPort,
        Inet4Address dstAddress, short dstPort,
        int ack_sequence, int sequence, short ident) {

    TcpPacket.Builder builder_tcp = new TcpPacket.Builder();
    //builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[0]));
    builder_tcp.correctChecksumAtBuild(true);
    builder_tcp.correctLengthAtBuild(true);
    builder_tcp.paddingAtBuild(true);
    builder_tcp.ack(true);
    builder_tcp.acknowledgmentNumber(ack_sequence);
    //builder_tcp.checksum(tcpHeader.getChecksum());
    //builder_tcp.dataOffset((byte) 8);
    builder_tcp.dstAddr(dstAddress);
    builder_tcp.dstPort(new TcpPort(dstPort, ""));
    //builder_tcp.fin(tcpHeader.getFin());


    ArrayList<TcpOption> tcp_options = new ArrayList<>();

    TcpNoOperationOption nop = TcpNoOperationOption.getInstance();

    TcpMaximumSegmentSizeOption seg_option = new TcpMaximumSegmentSizeOption.Builder().maxSegSize(mtu)
            .correctLengthAtBuild(true).build();
    tcp_options.add(seg_option);

    tcp_options.add(nop);
    tcp_options.add(nop);

    TcpSackPermittedOption sack_permit_option = TcpSackPermittedOption.getInstance();
    tcp_options.add(sack_permit_option);

    tcp_options.add(nop);

    TcpWindowScaleOption win_option = new TcpWindowScaleOption.Builder().shiftCount(shiftCount)
            .correctLengthAtBuild(true).build();
    tcp_options.add(win_option);

    builder_tcp.options(tcp_options);

    //builder_tcp.padding(tcpHeader.getPadding());
    builder_tcp.psh(false);
    builder_tcp.reserved((byte) 0);
    builder_tcp.rst(false);
    builder_tcp.sequenceNumber(sequence);
    builder_tcp.srcAddr(srcAddress);
    builder_tcp.srcPort(new TcpPort(srcPort, ""));
    builder_tcp.syn(true);
    builder_tcp.urg(false);
    //builder_tcp.urgentPointer(tcpHeader.getUrgentPointer());
    builder_tcp.window(window);

    IpV4Packet.Builder builder_ipv4 = new IpV4Packet.Builder();
    builder_ipv4.correctChecksumAtBuild(true);
    builder_ipv4.correctLengthAtBuild(true);
    builder_ipv4.paddingAtBuild(true);
    builder_ipv4.dstAddr(dstAddress);
    builder_ipv4.dontFragmentFlag(true);
    builder_ipv4.fragmentOffset((short) 0);
    //builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum());
    //	short identification= Math.abs(random.nextInt(Short.MAX_VALUE));
    builder_ipv4.identification(ident);
    builder_ipv4.ihl((byte) 5);
    //builder_ipv4.moreFragmentFlag(ipV4Header.getMoreFragmentFlag());
    //builder_ipv4.options(ipV4Header.getOptions());
    //builder_ipv4.padding(ipV4Header.getPadding());

    builder_ipv4.protocol(IpNumber.TCP);
    //		builder_ipv4.reservedFlag(ipV4Header.getReservedFlag());
    builder_ipv4.srcAddr(srcAddress);
    builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0));
    //builder_ipv4.totalLength( 52);
    builder_ipv4.ttl(ttl);
    builder_ipv4.version(IpVersion.IPV4);
    builder_ipv4.payloadBuilder(builder_tcp);
    //
    //System.out.println("自定义确认 "+" identification "+identification+" ack_sequence "+ack_sequence+" # "+tcpPacket
    // .getHeader());
    return buildIpV4(srcAddress_mac, dstAddrress_mac, builder_ipv4);

}
 
Example #12
Source File: PacketUtils.java    From finalspeed with GNU General Public License v2.0 4 votes vote down vote up
static Packet createAck(
        MacAddress srcAddress_mac,
        MacAddress dstAddrress_mac,
        Inet4Address srcAddress, short srcPort,
        Inet4Address dstAddress, short dstPort,
        int ack_sequence, int sequence, short ident) {

    TcpPacket.Builder builder_tcp = new TcpPacket.Builder();
    //builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[0]));
    builder_tcp.correctChecksumAtBuild(true);
    builder_tcp.correctLengthAtBuild(true);
    builder_tcp.paddingAtBuild(true);
    builder_tcp.ack(true);
    builder_tcp.acknowledgmentNumber(ack_sequence);
    //builder_tcp.checksum(tcpHeader.getChecksum());
    //builder_tcp.dataOffset((byte) 8);
    builder_tcp.dstAddr(dstAddress);
    builder_tcp.dstPort(new TcpPort(dstPort, ""));
    //builder_tcp.fin(tcpHeader.getFin());

    builder_tcp.psh(false);
    builder_tcp.reserved((byte) 0);
    builder_tcp.rst(false);
    builder_tcp.sequenceNumber(sequence);
    builder_tcp.srcAddr(srcAddress);
    builder_tcp.srcPort(new TcpPort(srcPort, ""));
    builder_tcp.syn(false);
    builder_tcp.urg(false);
    //builder_tcp.urgentPointer(tcpHeader.getUrgentPointer());
    builder_tcp.window(window);

    IpV4Packet.Builder builder_ipv4 = new IpV4Packet.Builder();
    builder_ipv4.correctChecksumAtBuild(true);
    builder_ipv4.correctLengthAtBuild(true);
    builder_ipv4.paddingAtBuild(true);
    builder_ipv4.dstAddr(dstAddress);
    builder_ipv4.dontFragmentFlag(true);
    builder_ipv4.fragmentOffset((short) 0);
    //builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum());
    //short identification= Math.abs(random.nextInt(Short.MAX_VALUE));
    builder_ipv4.identification(ident);
    builder_ipv4.ihl((byte) 5);
    //builder_ipv4.moreFragmentFlag(ipV4Header.getMoreFragmentFlag());
    //builder_ipv4.options(ipV4Header.getOptions());
    //builder_ipv4.padding(ipV4Header.getPadding());

    builder_ipv4.protocol(IpNumber.TCP);
    //		builder_ipv4.reservedFlag(ipV4Header.getReservedFlag());
    builder_ipv4.srcAddr(srcAddress);
    builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0));
    //builder_ipv4.totalLength( 52);
    builder_ipv4.ttl(ttl);
    builder_ipv4.version(IpVersion.IPV4);
    builder_ipv4.payloadBuilder(builder_tcp);
    //System.out.println("自定义确认 "+" identification "+identification+" ack_sequence "+ack_sequence+" # "+tcpPacket
    // .getHeader());
    return buildIpV4(srcAddress_mac, dstAddrress_mac, builder_ipv4);

}
 
Example #13
Source File: PacketUtils.java    From finalspeed with GNU General Public License v2.0 4 votes vote down vote up
static Packet createDataPacket(
        MacAddress srcAddress_mac,
        MacAddress dstAddrress_mac,
        Inet4Address srcAddress, short srcPort,
        Inet4Address dstAddress, short dstPort,
        int sequence, int ack, byte[] data, short ident) {
    Packet p;

    TcpPacket.Builder builder_tcp = new TcpPacket.Builder();
    builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(data));
    builder_tcp.correctChecksumAtBuild(true);
    builder_tcp.correctLengthAtBuild(true);
    builder_tcp.paddingAtBuild(true);
    builder_tcp.ack(true);
    builder_tcp.acknowledgmentNumber(ack);
    //builder_tcp.checksum(tcpHeader.getChecksum());
    //builder_tcp.dataOffset((byte)8);
    builder_tcp.dstAddr(dstAddress);
    builder_tcp.dstPort(new TcpPort(dstPort, ""));
    builder_tcp.fin(false);
    //builder_tcp.options(tcpHeader.getOptions());
    //builder_tcp.padding(tcpHeader.getPadding());
    builder_tcp.psh(false);
    builder_tcp.reserved((byte) 0);
    builder_tcp.rst(false);
    builder_tcp.sequenceNumber(sequence);
    builder_tcp.srcAddr(srcAddress);
    builder_tcp.srcPort(new TcpPort(srcPort, ""));
    builder_tcp.syn(false);
    builder_tcp.urg(false);
    //builder_tcp.urgentPointer(tcpHeader.getUrgentPointer());
    builder_tcp.window(window);

    IpV4Packet.Builder builder_ipv4 = new IpV4Packet.Builder();
    builder_ipv4.correctChecksumAtBuild(true);
    builder_ipv4.correctLengthAtBuild(true);
    builder_ipv4.dontFragmentFlag(true);
    builder_ipv4.paddingAtBuild(true);
    builder_ipv4.dstAddr(dstAddress);
    builder_ipv4.fragmentOffset((short) 0);
    //builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum());
    //short identification= Math.abs(random.nextInt(Short.MAX_VALUE));
    //identification=ident;
    builder_ipv4.identification(ident);
    builder_ipv4.ihl((byte) 5);
    builder_ipv4.moreFragmentFlag(false);
    //builder_ipv4.options(ipV4Header.getOptions());
    //builder_ipv4.padding(ipV4Header.getPadding());

    builder_ipv4.protocol(IpNumber.TCP);
    //builder_ipv4.reservedFlag(ipV4Header.getReservedFlag());
    builder_ipv4.srcAddr(srcAddress);
    builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0));
    //builder_ipv4.totalLength( 52);
    builder_ipv4.ttl(ttl);
    builder_ipv4.version(IpVersion.IPV4);
    builder_ipv4.payloadBuilder(builder_tcp);

    p = buildIpV4(srcAddress_mac, dstAddrress_mac, builder_ipv4);

    return p;
}
 
Example #14
Source File: PacketUtils.java    From finalspeed-91yun with GNU General Public License v2.0 4 votes vote down vote up
static Packet createSync(
			MacAddress srcAddress_mac,
			MacAddress dstAddrress_mac,
			Inet4Address srcAddress,short srcPort,
			Inet4Address dstAddress,short dstPort,
			int sequence,short ident){
		TcpPacket.Builder builder_tcp=new TcpPacket.Builder();
		//builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[0]));
		builder_tcp.correctChecksumAtBuild(true);
		builder_tcp.correctLengthAtBuild(true);
		builder_tcp.paddingAtBuild(true);
		//builder_tcp.ack(true);
		//builder_tcp.acknowledgmentNumber(ack_sequence);
		//builder_tcp.checksum(tcpHeader.getChecksum());
		//builder_tcp.dataOffset((byte) 8);
		builder_tcp.dstAddr(dstAddress);
		builder_tcp.dstPort(new TcpPort( dstPort,""));
		//builder_tcp.fin(tcpHeader.getFin());
		
		TcpNoOperationOption nop=TcpNoOperationOption.getInstance();
		
		ArrayList<TcpOption> tcp_options=new ArrayList<TcpOption>();
		
		TcpMaximumSegmentSizeOption seg_option=new TcpMaximumSegmentSizeOption.Builder().maxSegSize(mtu).correctLengthAtBuild(true).build();
		tcp_options.add(seg_option);

		tcp_options.add(nop);

		TcpWindowScaleOption win_option=new TcpWindowScaleOption.Builder().shiftCount((byte)6).correctLengthAtBuild(true).build();
		tcp_options.add(win_option);

		tcp_options.add(nop);
		tcp_options.add(nop);
		
		TcpSackPermittedOption sack_permit_option=TcpSackPermittedOption.getInstance();
		tcp_options.add(sack_permit_option);
		
		builder_tcp.options(tcp_options);
		
		//builder_tcp.padding(tcpHeader.getPadding());
		builder_tcp.psh(false);
		builder_tcp.reserved((byte) 0);
		builder_tcp.rst(false);
		builder_tcp.sequenceNumber(sequence);
		builder_tcp.srcAddr(srcAddress);
		builder_tcp.srcPort(new TcpPort( srcPort,""));
		builder_tcp.syn(true);
		builder_tcp.urg(false);
		//builder_tcp.urgentPointer(tcpHeader.getUrgentPointer());
		builder_tcp.window( window);

		IpV4Packet.Builder builder_ipv4=new IpV4Packet.Builder();
		builder_ipv4.correctChecksumAtBuild(true);
		builder_ipv4.correctLengthAtBuild(true);
		builder_ipv4.paddingAtBuild(true);
		builder_ipv4.dstAddr(dstAddress);
		builder_ipv4.dontFragmentFlag(true);
		builder_ipv4.fragmentOffset((short)0);
		//builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum());
		//short identification= Math.abs(random.nextInt(Short.MAX_VALUE));
		builder_ipv4.identification(ident);
		builder_ipv4.ihl((byte) 5);
		//builder_ipv4.moreFragmentFlag(ipV4Header.getMoreFragmentFlag());
		//builder_ipv4.options(ipV4Header.getOptions());
		//builder_ipv4.padding(ipV4Header.getPadding());
		
		builder_ipv4.protocol(IpNumber.TCP);
//		builder_ipv4.reservedFlag(ipV4Header.getReservedFlag());
		builder_ipv4.srcAddr(srcAddress);
		builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0));
		//builder_ipv4.totalLength( 52);
		builder_ipv4.ttl(ttl);
		builder_ipv4.version(IpVersion.IPV4);
		builder_ipv4.payloadBuilder(builder_tcp);
//
		Packet p = buildIpV4(srcAddress_mac,dstAddrress_mac,builder_ipv4);
//		IpV4Packet p4=builder_ipv4.build();
//		TcpPacket tcpPacket=builder_tcp.build();
		//selfAckTable.add(identification);
		//System.out.println("自定义确认 "+" identification "+identification+" ack_sequence "+ack_sequence+" # "+tcpPacket.getHeader());
		return p;

	}
 
Example #15
Source File: PacketUtils.java    From finalspeed-91yun with GNU General Public License v2.0 4 votes vote down vote up
static Packet createSyncAck(
		MacAddress srcAddress_mac,
		MacAddress dstAddrress_mac,
		Inet4Address srcAddress,short srcPort,
		Inet4Address dstAddress,short dstPort,
		int ack_sequence,int sequence,short ident){

	TcpPacket.Builder builder_tcp=new TcpPacket.Builder();
	//builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[0]));
	builder_tcp.correctChecksumAtBuild(true);
	builder_tcp.correctLengthAtBuild(true);
	builder_tcp.paddingAtBuild(true);
	builder_tcp.ack(true);
	builder_tcp.acknowledgmentNumber(ack_sequence);
	//builder_tcp.checksum(tcpHeader.getChecksum());
	//builder_tcp.dataOffset((byte) 8);
	builder_tcp.dstAddr(dstAddress);
	builder_tcp.dstPort(new TcpPort(dstPort,""));
	//builder_tcp.fin(tcpHeader.getFin());


	ArrayList<TcpOption> tcp_options=new ArrayList<TcpOption>();
	
	TcpNoOperationOption nop=TcpNoOperationOption.getInstance();

	TcpMaximumSegmentSizeOption seg_option=new TcpMaximumSegmentSizeOption.Builder().maxSegSize(mtu).correctLengthAtBuild(true).build();
	tcp_options.add(seg_option);
	
	tcp_options.add(nop);
	tcp_options.add(nop);

	TcpSackPermittedOption sack_permit_option=TcpSackPermittedOption.getInstance();
	tcp_options.add(sack_permit_option);
	
	tcp_options.add(nop);

	TcpWindowScaleOption win_option=new TcpWindowScaleOption.Builder().shiftCount(shiftCount).correctLengthAtBuild(true).build();
	tcp_options.add(win_option);

	builder_tcp.options(tcp_options);

	//builder_tcp.padding(tcpHeader.getPadding());
	builder_tcp.psh(false);
	builder_tcp.reserved((byte) 0);
	builder_tcp.rst(false);
	builder_tcp.sequenceNumber(sequence);
	builder_tcp.srcAddr(srcAddress);
	builder_tcp.srcPort(new TcpPort(srcPort,""));
	builder_tcp.syn(true);
	builder_tcp.urg(false);
	//builder_tcp.urgentPointer(tcpHeader.getUrgentPointer());
	builder_tcp.window( window);

	IpV4Packet.Builder builder_ipv4=new IpV4Packet.Builder();
	builder_ipv4.correctChecksumAtBuild(true);
	builder_ipv4.correctLengthAtBuild(true);
	builder_ipv4.paddingAtBuild(true);
	builder_ipv4.dstAddr(dstAddress);
	builder_ipv4.dontFragmentFlag(true);
	builder_ipv4.fragmentOffset((short)0);
	//builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum());
//	short identification= Math.abs(random.nextInt(Short.MAX_VALUE));
	builder_ipv4.identification(ident);
	builder_ipv4.ihl((byte) 5);
	//builder_ipv4.moreFragmentFlag(ipV4Header.getMoreFragmentFlag());
	//builder_ipv4.options(ipV4Header.getOptions());
	//builder_ipv4.padding(ipV4Header.getPadding());

	builder_ipv4.protocol(IpNumber.TCP);
	//		builder_ipv4.reservedFlag(ipV4Header.getReservedFlag());
	builder_ipv4.srcAddr(srcAddress);
	builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0));
	//builder_ipv4.totalLength( 52);
	builder_ipv4.ttl(ttl);
	builder_ipv4.version(IpVersion.IPV4);
	builder_ipv4.payloadBuilder(builder_tcp);
	//
	Packet p = buildIpV4(srcAddress_mac,dstAddrress_mac,builder_ipv4);
	//System.out.println("自定义确认 "+" identification "+identification+" ack_sequence "+ack_sequence+" # "+tcpPacket.getHeader());
	return p;

}
 
Example #16
Source File: PacketUtils.java    From finalspeed-91yun with GNU General Public License v2.0 4 votes vote down vote up
static Packet createAck(
		MacAddress srcAddress_mac,
		MacAddress dstAddrress_mac,
		Inet4Address srcAddress,short srcPort,
		Inet4Address dstAddress,short dstPort,
		int ack_sequence,int sequence,short ident){

	TcpPacket.Builder builder_tcp=new TcpPacket.Builder();
	//builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[0]));
	builder_tcp.correctChecksumAtBuild(true);
	builder_tcp.correctLengthAtBuild(true);
	builder_tcp.paddingAtBuild(true);
	builder_tcp.ack(true);
	builder_tcp.acknowledgmentNumber(ack_sequence);
	//builder_tcp.checksum(tcpHeader.getChecksum());
	//builder_tcp.dataOffset((byte) 8);
	builder_tcp.dstAddr(dstAddress);
	builder_tcp.dstPort(new TcpPort( dstPort,""));
	//builder_tcp.fin(tcpHeader.getFin());

	builder_tcp.psh(false);
	builder_tcp.reserved((byte) 0);
	builder_tcp.rst(false);
	builder_tcp.sequenceNumber(sequence);
	builder_tcp.srcAddr(srcAddress);
	builder_tcp.srcPort(new TcpPort( srcPort,""));
	builder_tcp.syn(false);
	builder_tcp.urg(false);
	//builder_tcp.urgentPointer(tcpHeader.getUrgentPointer());
	builder_tcp.window( window);

	IpV4Packet.Builder builder_ipv4=new IpV4Packet.Builder();
	builder_ipv4.correctChecksumAtBuild(true);
	builder_ipv4.correctLengthAtBuild(true);
	builder_ipv4.paddingAtBuild(true);
	builder_ipv4.dstAddr(dstAddress);
	builder_ipv4.dontFragmentFlag(true);
	builder_ipv4.fragmentOffset( (short) 0);
	//builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum());
	//short identification= Math.abs(random.nextInt(Short.MAX_VALUE));
	builder_ipv4.identification(ident);
	builder_ipv4.ihl((byte) 5);
	//builder_ipv4.moreFragmentFlag(ipV4Header.getMoreFragmentFlag());
	//builder_ipv4.options(ipV4Header.getOptions());
	//builder_ipv4.padding(ipV4Header.getPadding());

	builder_ipv4.protocol(IpNumber.TCP);
	//		builder_ipv4.reservedFlag(ipV4Header.getReservedFlag());
	builder_ipv4.srcAddr(srcAddress);
	builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0));
	//builder_ipv4.totalLength( 52);
	builder_ipv4.ttl(ttl);
	builder_ipv4.version(IpVersion.IPV4);
	builder_ipv4.payloadBuilder(builder_tcp);
	//
	
	Packet p = buildIpV4(srcAddress_mac,dstAddrress_mac,builder_ipv4);
	//System.out.println("自定义确认 "+" identification "+identification+" ack_sequence "+ack_sequence+" # "+tcpPacket.getHeader());
	return p;

}
 
Example #17
Source File: PacketUtils.java    From NSS with Apache License 2.0 4 votes vote down vote up
static Packet createDataPacket(
		MacAddress srcAddress_mac,
		MacAddress dstAddrress_mac,
		Inet4Address srcAddress,short srcPort,
		Inet4Address dstAddress,short dstPort,
		int sequence,int ack, byte[] data,short ident){
	Packet p=null;

	TcpPacket.Builder builder_tcp=new TcpPacket.Builder();
	builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(data));
	builder_tcp.correctChecksumAtBuild(true);
	builder_tcp.correctLengthAtBuild(true);
	builder_tcp.paddingAtBuild(true);
	builder_tcp.ack(true);
	builder_tcp.acknowledgmentNumber(ack);
	//builder_tcp.checksum(tcpHeader.getChecksum());
	//builder_tcp.dataOffset((byte)8);
	builder_tcp.dstAddr(dstAddress);
	builder_tcp.dstPort(new TcpPort( dstPort,""));
	builder_tcp.fin(false);
	//builder_tcp.options(tcpHeader.getOptions());
	//builder_tcp.padding(tcpHeader.getPadding());
	builder_tcp.psh(false);
	builder_tcp.reserved((byte) 0);
	builder_tcp.rst(false);
	builder_tcp.sequenceNumber(sequence);
	builder_tcp.srcAddr(srcAddress);
	builder_tcp.srcPort(new TcpPort( srcPort,""));
	builder_tcp.syn(false);
	builder_tcp.urg(false);
	//builder_tcp.urgentPointer(tcpHeader.getUrgentPointer());
	builder_tcp.window( window);

	IpV4Packet.Builder builder_ipv4=new IpV4Packet.Builder();
	builder_ipv4.correctChecksumAtBuild(true);
	builder_ipv4.correctLengthAtBuild(true);
	builder_ipv4.dontFragmentFlag(true);
	builder_ipv4.paddingAtBuild(true);
	builder_ipv4.dstAddr(dstAddress);
	builder_ipv4.fragmentOffset( (short)0);
	//builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum());
	//short identification= Math.abs(random.nextInt(Short.MAX_VALUE));
	//identification=ident;
	builder_ipv4.identification(ident);
	builder_ipv4.ihl((byte) 5);
	builder_ipv4.moreFragmentFlag(false);
	//builder_ipv4.options(ipV4Header.getOptions());
	//builder_ipv4.padding(ipV4Header.getPadding());

	builder_ipv4.protocol(IpNumber.TCP);
	//builder_ipv4.reservedFlag(ipV4Header.getReservedFlag());
	builder_ipv4.srcAddr(srcAddress);
	builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0));
	//builder_ipv4.totalLength( 52);
	builder_ipv4.ttl(ttl);
	builder_ipv4.version(IpVersion.IPV4);
	builder_ipv4.payloadBuilder(builder_tcp);

	p = buildIpV4(srcAddress_mac,dstAddrress_mac,builder_ipv4);

	return p;
}
 
Example #18
Source File: PacketUtils.java    From NSS with Apache License 2.0 4 votes vote down vote up
static Packet createSync(
			MacAddress srcAddress_mac,
			MacAddress dstAddrress_mac,
			Inet4Address srcAddress,short srcPort,
			Inet4Address dstAddress,short dstPort,
			int sequence,short ident){
		TcpPacket.Builder builder_tcp=new TcpPacket.Builder();
		//builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[0]));
		builder_tcp.correctChecksumAtBuild(true);
		builder_tcp.correctLengthAtBuild(true);
		builder_tcp.paddingAtBuild(true);
		//builder_tcp.ack(true);
		//builder_tcp.acknowledgmentNumber(ack_sequence);
		//builder_tcp.checksum(tcpHeader.getChecksum());
		//builder_tcp.dataOffset((byte) 8);
		builder_tcp.dstAddr(dstAddress);
		builder_tcp.dstPort(new TcpPort( dstPort,""));
		//builder_tcp.fin(tcpHeader.getFin());
		
		TcpNoOperationOption nop= TcpNoOperationOption.getInstance();
		
		ArrayList<TcpOption> tcp_options=new ArrayList<TcpOption>();
		
		TcpMaximumSegmentSizeOption seg_option=new TcpMaximumSegmentSizeOption.Builder().maxSegSize(mtu).correctLengthAtBuild(true).build();
		tcp_options.add(seg_option);

		tcp_options.add(nop);

		TcpWindowScaleOption win_option=new TcpWindowScaleOption.Builder().shiftCount((byte)6).correctLengthAtBuild(true).build();
		tcp_options.add(win_option);

		tcp_options.add(nop);
		tcp_options.add(nop);
		
		TcpSackPermittedOption sack_permit_option= TcpSackPermittedOption.getInstance();
		tcp_options.add(sack_permit_option);
		
		builder_tcp.options(tcp_options);
		
		//builder_tcp.padding(tcpHeader.getPadding());
		builder_tcp.psh(false);
		builder_tcp.reserved((byte) 0);
		builder_tcp.rst(false);
		builder_tcp.sequenceNumber(sequence);
		builder_tcp.srcAddr(srcAddress);
		builder_tcp.srcPort(new TcpPort( srcPort,""));
		builder_tcp.syn(true);
		builder_tcp.urg(false);
		//builder_tcp.urgentPointer(tcpHeader.getUrgentPointer());
		builder_tcp.window( window);

		IpV4Packet.Builder builder_ipv4=new IpV4Packet.Builder();
		builder_ipv4.correctChecksumAtBuild(true);
		builder_ipv4.correctLengthAtBuild(true);
		builder_ipv4.paddingAtBuild(true);
		builder_ipv4.dstAddr(dstAddress);
		builder_ipv4.dontFragmentFlag(true);
		builder_ipv4.fragmentOffset((short)0);
		//builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum());
		//short identification= Math.abs(random.nextInt(Short.MAX_VALUE));
		builder_ipv4.identification(ident);
		builder_ipv4.ihl((byte) 5);
		//builder_ipv4.moreFragmentFlag(ipV4Header.getMoreFragmentFlag());
		//builder_ipv4.options(ipV4Header.getOptions());
		//builder_ipv4.padding(ipV4Header.getPadding());
		
		builder_ipv4.protocol(IpNumber.TCP);
//		builder_ipv4.reservedFlag(ipV4Header.getReservedFlag());
		builder_ipv4.srcAddr(srcAddress);
		builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0));
		//builder_ipv4.totalLength( 52);
		builder_ipv4.ttl(ttl);
		builder_ipv4.version(IpVersion.IPV4);
		builder_ipv4.payloadBuilder(builder_tcp);
//
		Packet p = buildIpV4(srcAddress_mac,dstAddrress_mac,builder_ipv4);
//		IpV4Packet p4=builder_ipv4.build();
//		TcpPacket tcpPacket=builder_tcp.build();
		//selfAckTable.add(identification);
		//System.out.println("自定义确认 "+" identification "+identification+" ack_sequence "+ack_sequence+" # "+tcpPacket.getHeader());
		return p;

	}
 
Example #19
Source File: PacketUtils.java    From NSS with Apache License 2.0 4 votes vote down vote up
static Packet createSyncAck(
		MacAddress srcAddress_mac,
		MacAddress dstAddrress_mac,
		Inet4Address srcAddress,short srcPort,
		Inet4Address dstAddress,short dstPort,
		int ack_sequence,int sequence,short ident){

	TcpPacket.Builder builder_tcp=new TcpPacket.Builder();
	//builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[0]));
	builder_tcp.correctChecksumAtBuild(true);
	builder_tcp.correctLengthAtBuild(true);
	builder_tcp.paddingAtBuild(true);
	builder_tcp.ack(true);
	builder_tcp.acknowledgmentNumber(ack_sequence);
	//builder_tcp.checksum(tcpHeader.getChecksum());
	//builder_tcp.dataOffset((byte) 8);
	builder_tcp.dstAddr(dstAddress);
	builder_tcp.dstPort(new TcpPort(dstPort,""));
	//builder_tcp.fin(tcpHeader.getFin());


	ArrayList<TcpOption> tcp_options=new ArrayList<TcpOption>();
	
	TcpNoOperationOption nop= TcpNoOperationOption.getInstance();

	TcpMaximumSegmentSizeOption seg_option=new TcpMaximumSegmentSizeOption.Builder().maxSegSize(mtu).correctLengthAtBuild(true).build();
	tcp_options.add(seg_option);
	
	tcp_options.add(nop);
	tcp_options.add(nop);

	TcpSackPermittedOption sack_permit_option= TcpSackPermittedOption.getInstance();
	tcp_options.add(sack_permit_option);
	
	tcp_options.add(nop);

	TcpWindowScaleOption win_option=new TcpWindowScaleOption.Builder().shiftCount(shiftCount).correctLengthAtBuild(true).build();
	tcp_options.add(win_option);

	builder_tcp.options(tcp_options);

	//builder_tcp.padding(tcpHeader.getPadding());
	builder_tcp.psh(false);
	builder_tcp.reserved((byte) 0);
	builder_tcp.rst(false);
	builder_tcp.sequenceNumber(sequence);
	builder_tcp.srcAddr(srcAddress);
	builder_tcp.srcPort(new TcpPort(srcPort,""));
	builder_tcp.syn(true);
	builder_tcp.urg(false);
	//builder_tcp.urgentPointer(tcpHeader.getUrgentPointer());
	builder_tcp.window( window);

	IpV4Packet.Builder builder_ipv4=new IpV4Packet.Builder();
	builder_ipv4.correctChecksumAtBuild(true);
	builder_ipv4.correctLengthAtBuild(true);
	builder_ipv4.paddingAtBuild(true);
	builder_ipv4.dstAddr(dstAddress);
	builder_ipv4.dontFragmentFlag(true);
	builder_ipv4.fragmentOffset((short)0);
	//builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum());
//	short identification= Math.abs(random.nextInt(Short.MAX_VALUE));
	builder_ipv4.identification(ident);
	builder_ipv4.ihl((byte) 5);
	//builder_ipv4.moreFragmentFlag(ipV4Header.getMoreFragmentFlag());
	//builder_ipv4.options(ipV4Header.getOptions());
	//builder_ipv4.padding(ipV4Header.getPadding());

	builder_ipv4.protocol(IpNumber.TCP);
	//		builder_ipv4.reservedFlag(ipV4Header.getReservedFlag());
	builder_ipv4.srcAddr(srcAddress);
	builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0));
	//builder_ipv4.totalLength( 52);
	builder_ipv4.ttl(ttl);
	builder_ipv4.version(IpVersion.IPV4);
	builder_ipv4.payloadBuilder(builder_tcp);
	//
	Packet p = buildIpV4(srcAddress_mac,dstAddrress_mac,builder_ipv4);
	//System.out.println("自定义确认 "+" identification "+identification+" ack_sequence "+ack_sequence+" # "+tcpPacket.getHeader());
	return p;

}
 
Example #20
Source File: PacketUtils.java    From NSS with Apache License 2.0 4 votes vote down vote up
static Packet createAck(
		MacAddress srcAddress_mac,
		MacAddress dstAddrress_mac,
		Inet4Address srcAddress,short srcPort,
		Inet4Address dstAddress,short dstPort,
		int ack_sequence,int sequence,short ident){

	TcpPacket.Builder builder_tcp=new TcpPacket.Builder();
	//builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[0]));
	builder_tcp.correctChecksumAtBuild(true);
	builder_tcp.correctLengthAtBuild(true);
	builder_tcp.paddingAtBuild(true);
	builder_tcp.ack(true);
	builder_tcp.acknowledgmentNumber(ack_sequence);
	//builder_tcp.checksum(tcpHeader.getChecksum());
	//builder_tcp.dataOffset((byte) 8);
	builder_tcp.dstAddr(dstAddress);
	builder_tcp.dstPort(new TcpPort( dstPort,""));
	//builder_tcp.fin(tcpHeader.getFin());

	builder_tcp.psh(false);
	builder_tcp.reserved((byte) 0);
	builder_tcp.rst(false);
	builder_tcp.sequenceNumber(sequence);
	builder_tcp.srcAddr(srcAddress);
	builder_tcp.srcPort(new TcpPort( srcPort,""));
	builder_tcp.syn(false);
	builder_tcp.urg(false);
	//builder_tcp.urgentPointer(tcpHeader.getUrgentPointer());
	builder_tcp.window( window);

	IpV4Packet.Builder builder_ipv4=new IpV4Packet.Builder();
	builder_ipv4.correctChecksumAtBuild(true);
	builder_ipv4.correctLengthAtBuild(true);
	builder_ipv4.paddingAtBuild(true);
	builder_ipv4.dstAddr(dstAddress);
	builder_ipv4.dontFragmentFlag(true);
	builder_ipv4.fragmentOffset( (short) 0);
	//builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum());
	//short identification= Math.abs(random.nextInt(Short.MAX_VALUE));
	builder_ipv4.identification(ident);
	builder_ipv4.ihl((byte) 5);
	//builder_ipv4.moreFragmentFlag(ipV4Header.getMoreFragmentFlag());
	//builder_ipv4.options(ipV4Header.getOptions());
	//builder_ipv4.padding(ipV4Header.getPadding());

	builder_ipv4.protocol(IpNumber.TCP);
	//		builder_ipv4.reservedFlag(ipV4Header.getReservedFlag());
	builder_ipv4.srcAddr(srcAddress);
	builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0));
	//builder_ipv4.totalLength( 52);
	builder_ipv4.ttl(ttl);
	builder_ipv4.version(IpVersion.IPV4);
	builder_ipv4.payloadBuilder(builder_tcp);
	//
	
	Packet p = buildIpV4(srcAddress_mac,dstAddrress_mac,builder_ipv4);
	//System.out.println("自定义确认 "+" identification "+identification+" ack_sequence "+ack_sequence+" # "+tcpPacket.getHeader());
	return p;

}