use of dev.hawala.xns.level2.Error 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();
}
}
use of dev.hawala.xns.level2.Error in project dodo by devhawala.
the class SppServerListener method stopped.
@Override
public void stopped() {
// drop waiting connections (initiated by remote client, but still not serviced here)
synchronized (this.waitingConnections) {
for (SppServerConnection c : this.waitingConnections) {
if (c.initialPacket != null) {
Error errPacket = new Error(ErrorCode.LISTEN_REJECT, this.listenSocket, c.initialPacket.idp);
this.sender.send(errPacket.idp);
}
}
this.waitingConnections.clear();
}
}
use of dev.hawala.xns.level2.Error 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");
}
use of dev.hawala.xns.level2.Error 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());
}
use of dev.hawala.xns.level2.Error in project dodo by devhawala.
the class TestSppConnection method innerTestOpenClientConnection.
public void innerTestOpenClientConnection(final int clientWindowLength) throws InterruptedException {
// window-length: min. 2 packets for tests here
assertTrue("Requirement :: clientWindowLength >= 2", clientWindowLength >= 2);
// remarks on client window size in sendAck-response
// as: -> ackNo ::= first not received seqNo
// -> allocNo ::== last acceptable SeqNo
// so: => (ackNo == allocNo) means: 1 free slot in receive window
// => (ackNo > allocNo) means: no free slot, receive window is full
// => ( (allocNo - ackNo) == (windowsize - 1) ) means: receive window is empty, all slots are free
IdpPacketSink sink = new IdpPacketSink(true, true);
// 1. create client connection
SppConnection clientConn = new SppConnection(clientEnd, serverEnd, sink, clientWindowLength);
Assert.assertEquals("Packet count at client connection start", 1, sink.getLength());
IDP clientIdp1 = sink.get(0);
SppCounters counts1 = sink.next("client-packet1").reqSystem(true).reqAttention(false).reqDatastreamType(0).reqPayloadSize(0).counters();
assertNotEquals("1. srcIdentification of client-connect first packet", 0, counts1.srcConnectionId);
assertNotEquals("1. dstIdentification of client-connect first packet", 0, counts1.dstConnectionId);
sink.reqEmpty("1. after client-connect first packet");
// 2. let the server respond to the client-connect
SppData srvCounters = new SppData();
srvCounters.dstConnectionId = counts1.srcConnectionId;
srvCounters.srcConnectionId = 4711;
counts1 = counts1.withDstId(srvCounters.srcConnectionId);
SPP srvSpp1 = this.mkResponseSPP(clientIdp1, srvCounters).asSystemPacket();
clientConn.handleIngonePacket(srvSpp1.idp);
sink.reqEmpty("2. after first server-packet after client-connect");
// 3. send packet : server(data) => client
SPP srvSpp2 = this.mkResponseSPP(clientIdp1, srvCounters);
srvSpp2.wrBytes(0, 1024, DATA_15A, 0, DATA_15A.length);
srvSpp2.setPayloadLength(DATA_15A.length);
srvCounters.sequenceNumber++;
clientConn.handleIngonePacket(srvSpp2.idp);
sink.reqEmpty("3. after 1. data packet server => client");
// 4. send packet : server(data + sendAck) => client
SPP srvSpp3 = this.mkResponseSPP(clientIdp1, srvCounters).asSendAcknowledge();
srvSpp3.wrBytes(0, 1024, DATA_10B, 0, DATA_10B.length);
srvSpp3.setPayloadLength(DATA_10B.length);
srvCounters.sequenceNumber++;
clientConn.handleIngonePacket(srvSpp3.idp);
assertEquals("4. Packet count after [1. + 2.] server(data+sendAck) => client", 1, sink.getLength());
SppCounters counts2 = sink.next("4. client-packet2", counts1).reqSystem(true).reqAttention(false).reqDatastreamType(0).reqPayloadSize(0).reqAckNoGrown(2).reqAllocNoSame().req("4. the client window size after sending 2 data packets", (name, spp) -> (spp.getAllocationNumber() - spp.getAcknowledgeNumber()) == (clientWindowLength - 3)).reqSeqNoSame().counters();
sink.reqEmpty("after 2. data packet server => client");
// 5.a dequeue the first packet and check payload
SPP deq1 = clientConn.dequeueIngonePacket();
assertEquals("deq1.getPayloadLength()", DATA_15A.length, deq1.getPayloadLength());
for (int i = 0; i < DATA_15A.length; i++) {
assertEquals("deq1.rdByte(" + i + ")", DATA_15A[i], deq1.rdByte(i));
}
if (sink.getLength() > 0) {
sink.next("5.a intermediate window notification after dequeing first packet", counts2).reqSystem(true).reqAttention(false).reqAckNoSame().reqAllocNoGrown(1);
// ignore new counts (explicitly checked with next system+sendAck)
}
// 5.b send packet(system + sendAck) => client
SPP srvSpp4 = this.mkResponseSPP(clientIdp1, srvCounters).asSystemPacket().asSendAcknowledge();
clientConn.handleIngonePacket(srvSpp4.idp);
assertEquals("5.b Packet count after server(system+sendAck) => client", 1, sink.getLength());
SppCounters counts3 = sink.next("client-packet3", counts2).reqSystem(true).reqAttention(false).reqDatastreamType(0).reqPayloadSize(0).reqAckNoSame().reqAllocNoGrown(1).reqSeqNoSame().counters();
sink.reqEmpty("5.b after data packet server => client");
// 6.a dequeue the second packet and check payload
SPP deq2 = clientConn.dequeueIngonePacket();
assertEquals("deq2.getPayloadLength()", DATA_10B.length, deq2.getPayloadLength());
for (int i = 0; i < DATA_10B.length; i++) {
assertEquals("deq2.rdByte(" + i + ")", DATA_10B[i], deq2.rdByte(i));
}
if (sink.getLength() > 0) {
sink.next("6.a intermediate window notification after dequeing second packet", counts3).reqSystem(true).reqAttention(false).reqAckNoSame().reqAllocNoGrown(1);
// ignore new counts (explicitly checked with next system+sendAck)
}
// 6.b send packet(system + sendAck) => client
SPP srvSpp5 = this.mkResponseSPP(clientIdp1, srvCounters).asSystemPacket().asSendAcknowledge();
clientConn.handleIngonePacket(srvSpp5.idp);
assertEquals("6.b Packet count after server(system+sendAck) => client", 1, sink.getLength());
SppCounters counts4 = sink.next("client-packet4", counts3).reqSystem(true).reqAttention(false).reqDatastreamType(0).reqPayloadSize(0).reqAckNoSame().reqAllocNoGrown(1).req("6.b the client window has again the full size", (name, spp) -> (spp.getAllocationNumber() - spp.getAcknowledgeNumber()) == (clientWindowLength - 1)).reqSeqNoSame().counters();
sink.reqEmpty("6.b after data packet server => client");
// 7. send packet(system + sendAck) => client
// (should return the counters as immediately before)
SPP srvSpp6 = this.mkResponseSPP(clientIdp1, srvCounters).asSystemPacket().asSendAcknowledge();
clientConn.handleIngonePacket(srvSpp6.idp);
assertEquals("7. Packet count after server(system+sendAck) => client", 1, sink.getLength());
SppCounters counts5 = sink.next("client-packet5", counts4).reqSystem(true).reqAttention(false).reqDatastreamType(0).reqPayloadSize(0).reqAckNoSame().reqAllocNoSame().reqSeqNoSame().counters();
sink.reqEmpty("7. after data packet server => client");
// 8. fill up the client's window and expect an error packet after overflowing packet
assertEquals("8. Packet count before begin filling client window", 0, sink.getLength());
for (int i = 0; i < clientWindowLength; i++) {
SPP srvSppData = this.mkResponseSPP(clientIdp1, srvCounters);
srvSppData.wrBytes(0, 1024, DATA_10B, 0, DATA_10B.length);
srvSppData.setPayloadLength(DATA_10B.length);
srvCounters.sequenceNumber++;
clientConn.handleIngonePacket(srvSppData.idp);
assertEquals("8. Packet count while filling client window before full, packet = " + i, 0, sink.getLength());
}
SPP srvSppData = this.mkResponseSPP(clientIdp1, srvCounters);
srvSppData.wrBytes(0, 1024, DATA_10B, 0, DATA_10B.length);
srvSppData.setPayloadLength(DATA_10B.length);
srvCounters.sequenceNumber++;
clientConn.handleIngonePacket(srvSppData.idp);
assertEquals("8. Packet count after sending overflowing packet", 1, sink.getLength());
IDP errIdp = sink.consumeNext();
assertEquals("8. Packet after overflowing is error packet", PacketType.ERROR, errIdp.getPacketType());
Error err = new Error(errIdp);
assertEquals("8. Error type after sending overflow packet", ErrorCode.PROTOCOL_VIOLATION, err.getErrorCode());
// 9.a consuming one packet of out the filled-up window must send a window update
SPP deq3 = clientConn.dequeueIngonePacket();
assertEquals("deq3.getPayloadLength()", DATA_10B.length, deq3.getPayloadLength());
for (int i = 0; i < DATA_10B.length; i++) {
assertEquals("deq3.rdByte(" + i + ")", DATA_10B[i], deq3.rdByte(i));
}
assertEquals("9.a Packet count after dequeuing one packet", 1, sink.getLength());
SppCounters counts6 = sink.next("9.a after freeing one slot in receiving window", counts5).reqSystem(true).reqAttention(false).reqDatastreamType(0).reqPayloadSize(0).reqAckNoGrown(clientWindowLength).reqAllocNoGrown(1).req("9.a the client window has space for one packet after dequeuing one packet", (name, spp) -> spp.getAcknowledgeNumber() == spp.getAllocationNumber()).reqSeqNoSame().counters();
sink.reqEmpty("9.a after freeing one place in window");
// 9.b having freed up one place in the window must allow to resend the last packet...
clientConn.handleIngonePacket(srvSppData.idp);
assertEquals("9.b Packet count after having dequeued one packet and re-sending the overflowing packet", 0, sink.getLength());
// 9.c request the client's window data and check that there is no place left
SPP srvSpp9c = this.mkResponseSPP(clientIdp1, srvCounters).asSystemPacket().asSendAcknowledge();
clientConn.handleIngonePacket(srvSpp9c.idp);
assertEquals("9.c Packet count after server(system+sendAck) => client", 1, sink.getLength());
SppCounters counts9c = sink.next("9.c client-ack on full window", counts6).reqSystem(true).reqAttention(false).reqDatastreamType(0).reqPayloadSize(0).reqAckNoGrown(1).reqAllocNoSame().req("9.c the client window is full after resending overflowing packet", (name, spp) -> spp.getAcknowledgeNumber() > spp.getAllocationNumber()).reqSeqNoSame().counters();
sink.reqEmpty("9.c after system(ack) packet server => client");
}
Aggregations