@Override public String toString() { return "Type=" + getClass().getSimpleName() + ", code=<" + getPacketTypeCode() + ">, sizeBytes=" + getSize(); } }
/** * Returns <code>true</code> if the packet uses a protocol version that is compatible * with this I2P-Bote version, <code>false</code> otherwise. */ public boolean isProtocolVersionOk() { // everything above 4 is backwards compatible, everything below 4 is incompatible return getProtocolVersion() >= 4; }
/** * Returns the size of the packet in bytes. */ // TODO rename to getPacketSize // TODO override in subclasses to avoid calling toByteArray by adding field lengths public int getSize() { return toByteArray().length; }
public static Class<? extends DhtStorablePacket> decodePacketTypeCode(char packetTypeCode) { Class<? extends I2PBotePacket> packetType = I2PBotePacket.decodePacketTypeCode(packetTypeCode); if (packetType!=null && DhtStorablePacket.class.isAssignableFrom(packetType)) return packetType.asSubclass(DhtStorablePacket.class); else { log.debug("Invalid type code for DhtStorablePacket: <" + packetTypeCode + ">"); return null; } }
/** * @param peer * @param packet A packet received from a peer */ public void update(Destination peer, I2PBotePacket packet) { if (packet.isProtocolVersionOk()) unban(peer); else ban(peer, new BanReason(WRONG_PROTO_VER, "" + packet.getProtocolVersion())); } }
public char getPacketTypeCode() { return getPacketTypeCode(getClass()); }
@Override @SuppressWarnings("unchecked") protected PacketType createFolderElement(File file) throws IOException { try { return (PacketType)I2PBotePacket.createPacket(file); } catch (MalformedPacketException e) { log.error("Found malformed packet, deleting file: " + file.getAbsolutePath() + " (file size=" + file.length() + ")", e); if (!file.delete()) log.error("Can't delete malformed packet"); return null; } }
/** * Returns <code>false</code> if this packet can't fit into an I2P datagram. */ public boolean isTooBig() { return getSize() > MAX_DATAGRAM_SIZE; }
/** * Saves a packet to a file in the folder. If the file already exists, it is overwritten. * @param packetToStore * @param filename The filename to store the packet under, relative to this folder's storage directory. */ protected void add(I2PBotePacket packetToStore, String filename) { FileOutputStream outputStream = null; File file = new File(storageDir, filename); try { outputStream = new SecureFileOutputStream(file); packetToStore.writeTo(outputStream); } catch (Exception e) { log.error("Can't save packet to file: <" + filename + ">", e); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { log.error("Can't close file: <" + filename + ">", e); } if (file.length() == 0) { log.error("Nothing was written, deleting empty file: <" + file.getAbsolutePath() + ">"); file.delete(); } } } }
@Test public void testDecodePacketTypeCode() throws Exception { Field allPacketTypesField = I2PBotePacket.class.getDeclaredField("ALL_PACKET_TYPES"); allPacketTypesField.setAccessible(true); @SuppressWarnings("unchecked") Class<? extends I2PBotePacket>[] allPacketTypes = (Class<? extends I2PBotePacket>[])allPacketTypesField.get(null); for (Class<? extends I2PBotePacket> packetType: allPacketTypes) { TypeCode typeCode = packetType.getAnnotation(TypeCode.class); assertTrue(I2PBotePacket.decodePacketTypeCode(typeCode.value()).equals(packetType)); } } }
/** * Logs an error if the packet type of the packet instance is not correct * @param packetTypeCode */ protected void checkPacketType(byte packetTypeCode) { if (getPacketTypeCode() != (char)packetTypeCode) log.error("Packet type code of class " + getClass().getSimpleName() + " should be " + getPacketTypeCode() + ", is <" + packetTypeCode + ">"); }
try { inputStream = new FileInputStream(file); I2PBotePacket packet = createPacket(Util.readBytes(inputStream)); return packet;
/** * Writes the packet to an <code>OutputStream</code> in binary representation. */ public void writeTo(OutputStream outputStream) throws IOException { outputStream.write(toByteArray()); }