Search in sources :

Example 1 with SPP

use of dev.hawala.xns.level2.SPP in project dodo by devhawala.

the class SppServerListener method accept.

@Override
public void accept(IDP idp) {
    // ignore broadcasts to a SPP server socket
    if (idp.getDstHost() == IDP.BROADCAST_ADDR) {
        return;
    }
    // handle connection attempt
    synchronized (this.waitingConnections) {
        // check for space for pending ingone connections => produce error if not
        if (!this.listening || idp.getPacketType() != IDP.PacketType.SPP || this.waitingConnections.size() >= MAX_WAITING_CONNECTIONS) {
            Error errPacket = new Error(ErrorCode.LISTEN_REJECT, this.listenSocket, idp);
            this.sender.send(errPacket.idp);
            return;
        }
        // create new SppServerConnection
        // and enqueue it into ingone queue for a client to use it
        SPP spp = new SPP(idp);
        this.waitingConnections.add(new SppServerConnection(this.netMachine, spp));
        this.waitingConnections.notifyAll();
    }
}
Also used : SPP(dev.hawala.xns.level2.SPP) Error(dev.hawala.xns.level2.Error)

Example 2 with SPP

use of dev.hawala.xns.level2.SPP 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 3 with SPP

use of dev.hawala.xns.level2.SPP 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 SPP

use of dev.hawala.xns.level2.SPP 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)

Example 5 with SPP

use of dev.hawala.xns.level2.SPP in project dodo by devhawala.

the class TestPayloads method testSPPWriteData.

@Test
public void testSPPWriteData() {
    SPP spp = new SPP();
    spp.setConnectionControl((byte) 0xEE);
    spp.setDatastreamType((byte) 0xDD);
    spp.setSrcConnectionId(0x5544);
    spp.setDstConnectionId(0x3322);
    spp.setSequenceNumber(0x7733);
    spp.setAcknowledgeNumber(0x1155);
    spp.setAllocationNumber(0x4466);
    // length == 11
    byte[] payloadContent = { 0x7F, 0x7E, 0x7D, 0x7C, 0x7B, 0x7A, 0x79, 0x78, 0x77, 0x76, 0x75 };
    for (int i = 0; i < payloadContent.length; i++) {
        spp.wrByte(i, payloadContent[i]);
    }
    spp.setPayloadLength(payloadContent.length);
    assertEquals("payloadContent.length", 11, payloadContent.length);
    assertEquals("spp.payloadLength", payloadContent.length, spp.getPayloadLength());
    // odd
    assertEquals("idp.payloadLength", payloadContent.length + 12, spp.idp.getPayloadLength());
    // odd
    assertEquals("idp.length", payloadContent.length + 12 + 30, spp.idp.getLength());
    // even
    assertEquals("packet.payloadLength", payloadContent.length + 12 + 31, spp.idp.packet.getPayloadLength());
    byte[] refData = { // IDP automatically resets the checksum => 0xFFFF
    (byte) 0xFF, // IDP automatically resets the checksum => 0xFFFF
    (byte) 0xFF, 0x00, (byte) (payloadContent.length + 12 + 30), 0x00, IDP.PacketType.SPP.getPacketTypeCode(), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xEE, (byte) 0xDD, 0x55, 0x44, 0x33, 0x22, 0x77, 0x33, 0x11, 0x55, 0x44, 0x66, 0x7F, 0x7E, 0x7D, 0x7C, 0x7B, 0x7A, 0x79, 0x78, 0x77, 0x76, 0x75, // filler byte
    0x00 };
    // even
    assertEquals("refData.length", payloadContent.length + 12 + 31, refData.length);
    byte[] actualData = new byte[refData.length];
    int copiedLength = spp.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 : SPP(dev.hawala.xns.level2.SPP) Test(org.junit.Test)

Aggregations

SPP (dev.hawala.xns.level2.SPP)10 IDP (dev.hawala.xns.level1.IDP)6 Error (dev.hawala.xns.level2.Error)4 NetPacket (dev.hawala.xns.level0.NetPacket)3 Test (org.junit.Test)3 PacketType (dev.hawala.xns.level1.IDP.PacketType)2 PEX (dev.hawala.xns.level2.PEX)2 SppConnection (dev.hawala.xns.level2.SppConnection)2 ErrorCode (dev.hawala.xns.level2.Error.ErrorCode)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Pcap (org.jnetpcap.Pcap)1 PcapIf (org.jnetpcap.PcapIf)1 PcapPacket (org.jnetpcap.packet.PcapPacket)1 PcapPacketHandler (org.jnetpcap.packet.PcapPacketHandler)1