Search in sources :

Example 1 with IDP

use of dev.hawala.xns.level1.IDP in project dodo by devhawala.

the class GetXNSPackets method main.

/**
 * Main startup method
 *
 * @param args
 *          ignored
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    // Will be filled with NICs
    List<PcapIf> alldevs = new ArrayList<PcapIf>();
    // For any error msgs
    StringBuilder errbuf = new StringBuilder();
    /**
     *************************************************************************
     * First get a list of devices on this system
     *************************************************************************
     */
    int r = Pcap.findAllDevs(alldevs, errbuf);
    if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
        System.err.printf("Can't read list of devices, error is %s", errbuf.toString());
        return;
    }
    System.err.printf("r -> %d\n", r);
    System.err.printf("alldevs.size() = %d\n", alldevs.size());
    System.out.println("Network devices found:");
    PcapIf matchedDev = null;
    int i = 0;
    for (PcapIf device : alldevs) {
        String description = (device.getDescription() != null) ? device.getDescription() : "No description available";
        if ("tap0".equals(description)) {
            System.err.println("** found tap0 by description");
            matchedDev = device;
        }
        if ("tap0".equals(device.getName())) {
            System.err.println("** found tap0 by name");
            matchedDev = device;
        }
        byte[] addr = device.getHardwareAddress();
        String addrLen = "X";
        String sep = "";
        String mac = "";
        if (addr != null) {
            addrLen = "" + addr.length;
            for (byte b : addr) {
                mac = String.format("%s%s%02X", mac, sep, b);
                sep = "-";
            }
        }
        System.out.printf("#%d: %s (%s)[%s] [%s]\n", i++, device.getName(), addrLen, mac, description);
    }
    // We know we have at least 1 device
    PcapIf device = (matchedDev != null) ? matchedDev : alldevs.get(alldevs.size() - 1);
    System.out.printf("\nChoosing '%s' on your behalf:\n", (device.getDescription() != null) ? device.getDescription() : device.getName());
    /**
     *************************************************************************
     * Second we open up the selected device
     *************************************************************************
     */
    // Capture all packets, no trucation
    int snaplen = 64 * 1024;
    // capture all packets
    int flags = Pcap.MODE_PROMISCUOUS;
    // 10 * 1000;           // 10 seconds in millis
    int timeout = 1;
    Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);
    if (pcap == null) {
        System.err.printf("Error while opening device for capture: " + errbuf.toString());
        return;
    }
    /**
     *************************************************************************
     * Third we create a packet handler which will receive packets from the
     * libpcap loop.
     *************************************************************************
     */
    PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {

        public void nextPacket(PcapPacket packet, String user) {
            if (packet.getByte(12) != (byte) 0x06 || packet.getByte(13) != (byte) 0x00) {
                // System.out.println("packet skipped...");
                return;
            }
            System.out.printf("\n\nReceived packet at %s caplen=%-4d len=%-4d %s\n", new Date(packet.getCaptureHeader().timestampInMillis()), // Length actually captured
            packet.getCaptureHeader().caplen(), // Original length
            packet.getCaptureHeader().wirelen(), // User supplied object
            user);
            // System.out.println(packet);
            int payloadStart = 14;
            int payloadLength = packet.getCaptureHeader().caplen() - payloadStart;
            NetPacket np = new NetPacket(packet.getByteArray(payloadStart, payloadLength));
            IDP idp = new IDP(np);
            PacketType packetType = idp.getPacketType();
            if (packetType == PacketType.SPP) {
                SPP spp = new SPP(idp);
                System.out.printf("%s\n", spp.toString());
                System.out.printf("payload: %s\n", spp.payloadToString());
            } else if (packetType == PacketType.ERROR) {
                Error err = new Error(idp);
                System.out.printf("%s\n", err.toString());
                System.out.printf("payload: %s\n", err.payloadToString());
            } else if (packetType == PacketType.PEX) {
                PEX pex = new PEX(idp);
                System.out.printf("%s\n", pex.toString());
                System.out.printf("payload: %s\n", pex.payloadToString());
            } else {
                System.out.printf("%s\n", np.toString());
                System.out.printf("payload: %s\n", np.payloadToString());
            }
        }
    };
    // JBufferHandler<String> jbufferHandler = new JBufferHandler<String>() {
    // 
    // @Override
    // public void nextPacket(PcapHeader header, JBuffer buffer, String userDate) {
    // buffer.
    // }
    // 
    // }
    /**
     *************************************************************************
     * Fourth we enter the loop and tell it to capture 10 packets. The loop
     * method does a mapping of pcap.datalink() DLT value to JProtocol ID, which
     * is needed by JScanner. The scanner scans the packet buffer and decodes
     * the headers. The mapping is done automatically, although a variation on
     * the loop method exists that allows the programmer to sepecify exactly
     * which protocol ID to use as the data link type for this pcap interface.
     *************************************************************************
     */
    pcap.loop(100000, jpacketHandler, "jNetPcap rocks!");
    /**
     *************************************************************************
     * Last thing to do is close the pcap handle
     *************************************************************************
     */
    pcap.close();
    System.out.printf("GetXNSPackets done\n");
}
Also used : PEX(dev.hawala.xns.level2.PEX) Pcap(org.jnetpcap.Pcap) ArrayList(java.util.ArrayList) NetPacket(dev.hawala.xns.level0.NetPacket) Error(dev.hawala.xns.level2.Error) Date(java.util.Date) SPP(dev.hawala.xns.level2.SPP) IDP(dev.hawala.xns.level1.IDP) PcapPacket(org.jnetpcap.packet.PcapPacket) PacketType(dev.hawala.xns.level1.IDP.PacketType) PcapPacketHandler(org.jnetpcap.packet.PcapPacketHandler) PcapIf(org.jnetpcap.PcapIf)

Example 2 with IDP

use of dev.hawala.xns.level1.IDP in project dodo by devhawala.

the class TestPayloads method testIDPReadContent.

@Test
public void testIDPReadContent() {
    NetPacket packet = this.mkNetPacket();
    IDP idp = new IDP(packet);
    assertEquals("idp.checksum", 0x0001, idp.getChecksum());
    assertEquals("idp.length", 0x0203, idp.getLength());
    assertEquals("idp.transportControl", 0x04, idp.getTransportControl());
    assertEquals("idp.packetTypeCode", 0x05, idp.getPacketTypeCode());
    assertEquals("idp.packetType", IDP.PacketType.SPP, idp.getPacketType());
    assertEquals("idp.dstNetwork", 0x06070809, idp.getDstNetwork());
    assertEquals("idp.dstHost", 0x0A0B0C0D0E0FL, idp.getDstHost());
    assertEquals("idp.dstSocket", 0x1011, idp.getDstSocket());
    assertEquals("idp.srcNetwork", 0x12131415, idp.getSrcNetwork());
    assertEquals("idp.srcHost", 0x161718191A1BL, idp.getSrcHost());
    assertEquals("idp.srcSocket", 0x1C1D, idp.getSrcSocket());
    assertEquals("idp.payloadLength", TESTCONTENT.length - 30, idp.getPayloadLength());
    byte[] refData = { 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F };
    byte[] actualData = new byte[TESTCONTENT.length - 30];
    int copiedLength = idp.rdBytes(0, NetPacket.MAX_PACKET_SIZE, actualData, 0, NetPacket.MAX_PACKET_SIZE);
    assertEquals("copiedLength", actualData.length, copiedLength);
    this.checkByteArrayContents(refData, actualData);
}
Also used : IDP(dev.hawala.xns.level1.IDP) NetPacket(dev.hawala.xns.level0.NetPacket) Test(org.junit.Test)

Example 3 with IDP

use of dev.hawala.xns.level1.IDP in project dodo by devhawala.

the class TestPayloads method testNominalSizes.

@Test
public void testNominalSizes() {
    final int maxNetPayloadSize = NetPacket.MAX_PACKET_SIZE;
    final int maxIdpPayloadSize = NetPacket.MAX_PACKET_SIZE - IDP.IDP_DATA_START;
    final int maxSppPayloadSize = NetPacket.MAX_PACKET_SIZE - IDP.IDP_DATA_START - SPP.SPP_DATA_START;
    final int maxPexPayloadSize = NetPacket.MAX_PACKET_SIZE - IDP.IDP_DATA_START - PEX.PEX_DATA_START;
    final int maxErrPayloadSize = NetPacket.MAX_PACKET_SIZE - IDP.IDP_DATA_START - Error.ERROR_DATA_START;
    NetPacket netPacket = new NetPacket();
    assertEquals("netPacket.getMaxPayloadLength()", maxNetPayloadSize, netPacket.getMaxPayloadLength());
    IDP idp1 = new IDP();
    assertEquals("idp1.getMaxPayloadLength()", maxIdpPayloadSize, idp1.getMaxPayloadLength());
    // IDP idp2 = new IDP(new NetPacket());
    // assertEquals("idp2.getMaxPayloadLength()", maxIdpPayloadSize, idp2.getMaxPayloadLength());
    SPP spp1 = new SPP();
    assertEquals("spp1.getMaxPayloadLength()", maxSppPayloadSize, spp1.getMaxPayloadLength());
    // SPP spp2 = new SPP(new IDP());
    // assertEquals("spp2.getMaxPayloadLength()", maxSppPayloadSize, spp2.getMaxPayloadLength());
    // SPP spp3 = new SPP(new IDP(new NetPacket()));
    // assertEquals("spp3.getMaxPayloadLength()", maxSppPayloadSize, spp3.getMaxPayloadLength());
    PEX pex1 = new PEX();
    assertEquals("pex1.getMaxPayloadLength()", maxPexPayloadSize, pex1.getMaxPayloadLength());
// PEX pex2 = new PEX(new IDP());
// assertEquals("pex2.getMaxPayloadLength()", maxPexPayloadSize, pex2.getMaxPayloadLength());
// PEX pex3 = new PEX(new IDP(new NetPacket()));
// assertEquals("pex3.getMaxPayloadLength()", maxPexPayloadSize, pex3.getMaxPayloadLength());
// Error err1 = new Error();
// assertEquals("err1.getMaxPayloadLength()", maxErrPayloadSize, err1.getMaxPayloadLength());
// Error err2 = new Error(new IDP());
// assertEquals("err2.getMaxPayloadLength()", maxErrPayloadSize, err2.getMaxPayloadLength());
// Error err3 = new Error(new IDP(new NetPacket()));
// assertEquals("err3.getMaxPayloadLength()", maxErrPayloadSize, err3.getMaxPayloadLength());
}
Also used : SPP(dev.hawala.xns.level2.SPP) PEX(dev.hawala.xns.level2.PEX) IDP(dev.hawala.xns.level1.IDP) NetPacket(dev.hawala.xns.level0.NetPacket) Test(org.junit.Test)

Example 4 with IDP

use of dev.hawala.xns.level1.IDP in project dodo by devhawala.

the class TestPayloads method testIDPWriteContent.

@Test
public void testIDPWriteContent() {
    IDP idp = new IDP();
    assertEquals("(new)idp.maxPayloadLength", NetPacket.MAX_PACKET_SIZE - 30, idp.getMaxPayloadLength());
    assertEquals("(new)idp.payloadLength", 0, idp.getPayloadLength());
    assertEquals("(new)idp.packet.payloadLength", 30, idp.packet.getPayloadLength());
    idp.resetChecksum();
    idp.setTransportControl((byte) 0xEE);
    // 0x01
    idp.setPacketType(IDP.PacketType.RIP);
    idp.setDstNetwork(0x22334455);
    idp.setDstHost(0x112233445566L);
    idp.setDstSocket(0x6677);
    idp.setSrcNetwork(0x55443322);
    idp.setSrcHost(0x665544332211L);
    idp.setSrcSocket(0x7766);
    // length == 11
    byte[] payloadContent = { 0x7F, 0x7E, 0x7D, 0x7C, 0x7B, 0x7A, 0x79, 0x78, 0x77, 0x76, 0x75 };
    assertEquals("payloadContent.length", 11, payloadContent.length);
    for (int i = 0; i < payloadContent.length; i++) {
        idp.wrByte(i, payloadContent[i]);
    }
    idp.setPayloadLength(payloadContent.length);
    assertEquals("idp.maxPayloadLength", NetPacket.MAX_PACKET_SIZE - 30, idp.getMaxPayloadLength());
    // odd(11)
    assertEquals("idp.payloadLength", payloadContent.length, idp.getPayloadLength());
    // even(42)
    assertEquals("idp.packet.payloadLength", payloadContent.length + 31, idp.packet.getPayloadLength());
    byte[] refData = { (byte) 0xFF, (byte) 0xFF, // this is the "real" packet length
    0x00, // this is the "real" packet length
    (byte) (payloadContent.length + 30), (byte) 0xEE, 0x01, 0x22, 0x33, 0x44, 0x55, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x66, 0x77, 0x55, 0x44, 0x33, 0x22, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x77, 0x66, 0x7F, 0x7E, 0x7D, 0x7C, 0x7B, 0x7A, 0x79, 0x78, 0x77, 0x76, 0x75, // filler byte
    0x00 };
    assertEquals("refData.length", 42, refData.length);
    byte[] actualData = new byte[42];
    int copiedLength = idp.packet.rdBytes(0, NetPacket.MAX_PACKET_SIZE, actualData, 0, NetPacket.MAX_PACKET_SIZE);
    assertEquals("copiedLength", actualData.length, copiedLength);
    this.checkByteArrayContents(refData, actualData);
}
Also used : IDP(dev.hawala.xns.level1.IDP) Test(org.junit.Test)

Example 5 with IDP

use of dev.hawala.xns.level1.IDP in project dodo by devhawala.

the class TestPayloads method testSPPReadData.

@Test
public void testSPPReadData() {
    NetPacket packet = this.mkNetPacket();
    IDP idp = new IDP(packet);
    SPP spp = new SPP(idp);
    assertEquals("spp.connectionControl", 0x1E, spp.getConnectionControl());
    assertEquals("spp.datastreamType", 0x1F, spp.getDatastreamType());
    assertEquals("spp.scrConnectionId", 0x2021, spp.getSrcConnectionId());
    assertEquals("spp.dstConnectionId", 0x2223, spp.getDstConnectionId());
    assertEquals("spp.sequenceNumber", 0x2425, spp.getSequenceNumber());
    assertEquals("spp.acknowloedgeNumber", 0x2627, spp.getAcknowledgeNumber());
    assertEquals("spp.allocationNumber", 0x2829, spp.getAllocationNumber());
    assertEquals("spp.payloadLen", TESTCONTENT.length - 42, spp.getPayloadLength());
    byte[] refData = { 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F };
    assertEquals("refData.length", TESTCONTENT.length - 42, refData.length);
    byte[] actualData = new byte[TESTCONTENT.length - 42];
    int copiedLength = spp.rdBytes(0, NetPacket.MAX_PACKET_SIZE, actualData, 0, NetPacket.MAX_PACKET_SIZE);
    assertEquals("copiedLength", actualData.length, copiedLength);
    this.checkByteArrayContents(refData, actualData);
}
Also used : SPP(dev.hawala.xns.level2.SPP) IDP(dev.hawala.xns.level1.IDP) NetPacket(dev.hawala.xns.level0.NetPacket) Test(org.junit.Test)

Aggregations

IDP (dev.hawala.xns.level1.IDP)16 NetPacket (dev.hawala.xns.level0.NetPacket)9 Test (org.junit.Test)7 SPP (dev.hawala.xns.level2.SPP)6 Error (dev.hawala.xns.level2.Error)5 PEX (dev.hawala.xns.level2.PEX)4 PacketType (dev.hawala.xns.level1.IDP.PacketType)2 Payload (dev.hawala.xns.level0.Payload)1 SppConnection (dev.hawala.xns.level2.SppConnection)1 NetMachine (dev.hawala.xns.network.NetMachine)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Pcap (org.jnetpcap.Pcap)1 PcapIf (org.jnetpcap.PcapIf)1