org.pcap4j.packet.UnknownPacket Java Examples

The following examples show how to use org.pcap4j.packet.UnknownPacket. 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: TrexUdpPacket.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Build packet
 *
 * @throws java.net.UnknownHostException
 */
public void buildPacket() throws UnknownHostException {

    int calculatedLength = getPacketLength();
    String payloadString = payload.getPayloadType().getPadPayloadString(payload.getPayloadPattern(), calculatedLength * 2);

    udpBuilder = new UdpPacket.Builder();
    UnknownPacket.Builder unknownb = new UnknownPacket.Builder();
    unknownb.rawData(payload.getPayloadPad(payloadString, calculatedLength));
    Inet4Address srcAddr = (Inet4Address) InetAddress.getByAddress(
            new byte[]{(byte) 0, (byte) 0, (byte) 0, (byte) 0}
    );
    Inet4Address dstAddr = (Inet4Address) InetAddress.getByAddress(
            new byte[]{(byte) 0, (byte) 0, (byte) 0, (byte) 0}
    );

    udpBuilder.srcPort(UdpPort.getInstance((short) srcPort.value()))
            .dstPort(UdpPort.getInstance((short) dstPort.value()))
            .srcAddr(srcAddr)
            .dstAddr(dstAddr)
            .length((short) calculatedLength)
            .payloadBuilder(unknownb)
            .checksum((short) 0)
            .correctLengthAtBuild(false);

    this.packet = udpBuilder.build();
}
 
Example #2
Source File: TrexTcpPacket.java    From trex-stateless-gui with Apache License 2.0 4 votes vote down vote up
/**
 * Build packet
 */
public void buildPacket() {

    int calculatedLength = getPacketLength() - 14;

    String payloadString = payload.getPayloadType().getPadPayloadString(payload.getPayloadPattern(), calculatedLength * 2);

    UnknownPacket.Builder unknownb = null;
    if (calculatedLength != 0) {
        unknownb = new UnknownPacket.Builder();
        unknownb.rawData(payload.getPayloadPad(payloadString, calculatedLength));
    }
    ArrayList<TcpOption> options = new ArrayList<TcpOption>();

    options.add(TcpEndOfOptionList.getInstance());

    tcpBuilder = new TcpPacket.Builder();
    tcpBuilder.dstPort(dstPort)
            .srcPort(srcPort)
            .sequenceNumber(sequenceNumber)
            .acknowledgmentNumber(acknowledgmentNumber)
            .dataOffset(dataOffset)
            .reserved(reserved)
            .urg(urg)
            .ack(ack)
            .psh(psh)
            .rst(rst)
            .syn(syn)
            .fin(fin)
            .window(window)
            .checksum(checksum)
            .urgentPointer(urgentPointer)
            .options(options)
            .padding(new byte[]{(byte) 0xaa})
            .correctChecksumAtBuild(false)
            .correctLengthAtBuild(false)
            .paddingAtBuild(false);

    if (calculatedLength != 0) {
        tcpBuilder.payloadBuilder(unknownb);
    }
    this.packet = tcpBuilder.build();
}
 
Example #3
Source File: PacketUtils.java    From finalspeed-91yun with GNU General Public License v2.0 4 votes vote down vote up
public static Packet buildIpV4(
		MacAddress srcAddress_mac,
		MacAddress dstAddrress_mac,
		IpV4Packet.Builder builder_ipv4){

	org.pcap4j.packet.Packet.Builder builder=null;
	EtherType etherType=null;
	Packet p=null;
	if(ppp){
		etherType=EtherType.PPPOE_SESSION_STAGE;
		
		UnknownPacket.Builder pppBuilder=new UnknownPacket.Builder();
		byte[] ipData=builder_ipv4.build().getRawData();
		
		byte[] lenb=new byte[2];
		ByteShortConvert.toByteArray((short) (ipData.length+2), lenb, 0);
		
		byte[] pppHead=new byte[8];
		System.arraycopy(pppHead_static, 0, pppHead, 0, pppHead.length);
		System.arraycopy(lenb, 0, pppHead, 4, 2);
		
		byte[] newData=new byte[pppHead.length+ipData.length];
		System.arraycopy(pppHead, 0, newData, 0, pppHead.length);
		System.arraycopy(ipData, 0, newData, 8, ipData.length);
		pppBuilder.rawData(newData);
		
		builder=pppBuilder;
	}else {
		etherType=EtherType.IPV4;
		builder=builder_ipv4;
	}
	
	EthernetPacket.Builder etherBuilder = new EthernetPacket.Builder();
	etherBuilder.dstAddr(dstAddrress_mac)
	.srcAddr(srcAddress_mac)
	.type(etherType)
	.payloadBuilder(builder)
	.paddingAtBuild(true);
	
	p = etherBuilder.build();
	
	return p;
}
 
Example #4
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 #5
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 #6
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);
    }
}