org.pcap4j.packet.UdpPacket Java Examples

The following examples show how to use org.pcap4j.packet.UdpPacket. 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: PacketUpdater.java    From trex-stateless-gui with Apache License 2.0 7 votes vote down vote up
private Packet fixL3L4Checksums(Packet packet) {
    Packet.Builder builder = packet.getBuilder();
    builder.get(IpV4Packet.Builder.class).correctChecksumAtBuild(true);

    TcpPacket.Builder tcpBuilder = builder.get(TcpPacket.Builder.class);
    if (tcpBuilder != null) {
        tcpBuilder.srcAddr(packet.get(IpV4Packet.class).getHeader().getSrcAddr());
        tcpBuilder.dstAddr(packet.get(IpV4Packet.class).getHeader().getDstAddr());
        tcpBuilder.correctChecksumAtBuild(true);
    }

    UdpPacket.Builder udpBuilder = builder.get(UdpPacket.Builder.class);
    if (udpBuilder != null) {
        udpBuilder.srcAddr(packet.get(IpV4Packet.class).getHeader().getSrcAddr());
        udpBuilder.dstAddr(packet.get(IpV4Packet.class).getHeader().getDstAddr());
        udpBuilder.correctChecksumAtBuild(true);
    }
    return builder.build();
}
 
Example #2
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 #3
Source File: PacketUpdater.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Return source port
 *
 * @param packet
 * @return
 */
private String getSrcPort(Packet packet) {
    if (protocolType.equals("TCP")) {
        return packet.get(TcpPacket.class).getHeader().getSrcPort().toString();

    } else if (protocolType.equals("UDP")) {
        return packet.get(UdpPacket.class).getHeader().getSrcPort().toString();
    }

    return "80";
}
 
Example #4
Source File: PacketUpdater.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Return destination port
 *
 * @param packet
 * @return
 */
private String getDstPort(Packet packet) {
    if (protocolType.equals("TCP")) {
        return packet.get(TcpPacket.class).getHeader().getDstPort().toString();

    } else if (protocolType.equals("UDP")) {
        return packet.get(UdpPacket.class).getHeader().getDstPort().toString();
    }

    return "80";
}
 
Example #5
Source File: Boot.java    From MakeLobbiesGreatAgain with MIT License 4 votes vote down vote up
public static void main(String[] args) throws UnsupportedLookAndFeelException, AWTException, ClassNotFoundException, InterruptedException,
		FontFormatException, InstantiationException, IllegalAccessException, IOException, PcapNativeException, NotOpenException {
	System.setProperty("jna.nosys", "true");
	if (!Sanity.check()) {
		System.exit(1);
	}
	Settings.init();
	Settings.set("autoload", Settings.get("autoload", "0")); //"autoload" is an ini-only toggle for advanced users.
	setupTray();

	getLocalAddr();
	nif = Pcaps.getDevByAddress(addr);
	if (nif == null) {
		JOptionPane.showMessageDialog(null, "The device you selected doesn't seem to exist. Double-check the IP you entered.", "Error", JOptionPane.ERROR_MESSAGE);
		System.exit(1);
	}

	final int addrHash = addr.hashCode();
	final int snapLen = 65536;
	final PromiscuousMode mode = PromiscuousMode.NONPROMISCUOUS;
	final int timeout = 0;
	handle = nif.openLive(snapLen, mode, timeout);
	handle.setFilter("udp && less 150", BpfProgram.BpfCompileMode.OPTIMIZE);

	ui = new Overlay();

	while (running) {
		final Packet packet = handle.getNextPacket();

		if (packet != null) {
			final IpV4Packet ippacket = packet.get(IpV4Packet.class);

			if (ippacket != null) {
				final UdpPacket udppack = ippacket.get(UdpPacket.class);

				if (udppack != null && udppack.getPayload() != null) {
					final int srcAddrHash = ippacket.getHeader().getSrcAddr().hashCode();
					final int dstAddrHash = ippacket.getHeader().getDstAddr().hashCode();
					final int payloadLen = udppack.getPayload().getRawData().length;

					if (active.containsKey(srcAddrHash) && srcAddrHash != addrHash) {
						if (active.get(srcAddrHash) != null && payloadLen == 68  //Packets are STUN related: 56 is request, 68 is response
								&& dstAddrHash == addrHash) {
							ui.setPing(ippacket.getHeader().getSrcAddr(), handle.getTimestamp().getTime() - active.get(srcAddrHash).getTime());
							active.put(srcAddrHash, null); //No longer expect ping
						}
					} else {
						if (payloadLen == 56 && srcAddrHash == addrHash) {
							active.put(ippacket.getHeader().getDstAddr().hashCode(), handle.getTimestamp());
						}
					}
				}
			}
		}
	}
}
 
Example #6
Source File: PacketParser.java    From trex-stateless-gui with Apache License 2.0 4 votes vote down vote up
/**
 * Extract packet info
 *
 * @param packet
 * @return
 */
private PacketInfo extractPacketInfo(Packet packet) {

    if (packet != null) {
        packetInfo.setPacketHex(formatHex(DatatypeConverter.printHexBinary(packet.getRawData())));
        packetInfo.setPacketRawData(formatPayLoad(new String(packet.getRawData())));
    }

    // defien whether packet has vlan or not
    if(packet.get(Dot1qVlanTagPacket.class) != null){
        packetInfo.setVlanPacket(true);
    }
    // If the packet has Ethernet
    if (packet.get(EthernetPacket.class) != null) {
        packetInfo.setEthernetHex(getHeaderOffset(packet.get(EthernetPacket.class).getHeader().toHexString().toUpperCase()));
        packetInfo.setEthernetRawData(new String());
        packetInfo.setDestMac(packet.get(EthernetPacket.class).getHeader().getDstAddr().toString());
        packetInfo.setSrcMac(packet.get(EthernetPacket.class).getHeader().getSrcAddr().toString());
    }

    // if the packet has IPV4
    if (packet.get(IpV4Packet.class) != null) {
        packetInfo.setIpv4Hex(getHeaderOffset(packet.get(IpV4Packet.class).getHeader().toHexString().toUpperCase()));
        packetInfo.setIpv4RawData(new String());
        packetInfo.setDestIpv4(packet.get(IpV4Packet.class).getHeader().getDstAddr().getHostAddress());
        packetInfo.setSrcIpv4(packet.get(IpV4Packet.class).getHeader().getSrcAddr().getHostAddress());
    }

    // if the packet has TCP
    if (packet.get(TcpPacket.class) != null) {
        packetInfo.setL4Name("TCP");
        packetInfo.setL4Hex(getHeaderOffset(packet.get(TcpPacket.class).getHeader().toHexString().toUpperCase()));
        packetInfo.setL4RawData(new String());
        if (packet.get(TcpPacket.class).getPayload() != null) {
            packetInfo.setPacketPayLoad(getHeaderOffset(spaceHex(Hex.encodeHexString(packet.get(TcpPacket.class).getPayload().getRawData()))));
        } else {
            packetInfo.setPacketPayLoad(null);
        }
    }

    // if the packet has UDP
    if (packet.get(UdpPacket.class) != null) {
        packetInfo.setL4Name("UDP");
        packetInfo.setL4Hex(getHeaderOffset(packet.get(UdpPacket.class).getHeader().toHexString().toUpperCase()));
        packetInfo.setL4RawData(new String());

        if (packet.get(UdpPacket.class).getPayload() != null) {
            packetInfo.setPacketPayLoad(getHeaderOffset(spaceHex(Hex.encodeHexString(packet.get(UdpPacket.class).getPayload().getRawData()))));
        } else {
            packetInfo.setPacketPayLoad(null);
        }
    }
    return packetInfo;
}
 
Example #7
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 #8
Source File: IpmiPacketReader.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
protected void processUdp(UdpPacket packet) {
}
 
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: TrexUdpPacket.java    From trex-stateless-gui with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @return
 */
public UdpPacket.Builder getBuilder() {
    return udpBuilder;
}