/** * Returns the ip (given in BIG-endian) format of buf[offset]...buf[offset+3] as standard * dotted-decimal, e.g., 192.168.0.1 * * <p> * * @param ip the IP address to convert * @param offset the offset into the IP array to convert * @return the IP address as a dotted-quad string */ public static final String ip2string(byte[] ip, int offset) { // xxx.xxx.xxx.xxx => 15 chars StringBuffer sbuf = new StringBuffer(16); sbuf.append(ByteOrder.ubyte2int(ip[offset])); sbuf.append('.'); sbuf.append(ByteOrder.ubyte2int(ip[offset + 1])); sbuf.append('.'); sbuf.append(ByteOrder.ubyte2int(ip[offset + 2])); sbuf.append('.'); sbuf.append(ByteOrder.ubyte2int(ip[offset + 3])); return sbuf.toString(); }
/** * Make a new PatchTableMessage object by parsing data a remote computer sent us. * * <p>Creates a new PATCH variant with data read from the network. The first byte is guaranteed to * be PATCH_VARIANT. * * <p>Throws BadPacketException the remaining values in payload are not well-formed, e.g., because * it's the wrong length, the sequence size is less than the sequence number, etc. * * @param guid The message GUID we read from the header * @param ttl The TTL we read from the message header * @param hops The hops count we read from the message header * @param payload The payload of the message after that, like fNSCBpatchdatapatchdatapatchdata */ protected PatchTableMessage(byte[] guid, byte ttl, byte hops, byte[] payload) throws BadPacketException { // Save the GUID, TTL, hops and length in the Message and RouteTableMessage cores of this new // PatchTableMessage super( guid, ttl, hops, payload.length, RouteTableMessage.PATCH_VARIANT); // 0x01 identifies this as a PatchTableMessage /* * TODO: maybe we shouldn't enforce this * if (payload.length < 5) throw new BadPacketException("Extra arguments in reset message."); */ /* * The payload of a patch message looks like this: * * fNSCBpatchdatapatchdata... */ // Make sure the first payload byte, f, is 0x01, identifying this as a patch message Assert.that(payload[0] == PATCH_VARIANT); // Read the sequence number and sequence size, N and S, a distance of 1 and 2 bytes into the // payload data this.sequenceNumber = (short) ByteOrder.ubyte2int(payload[1]); this.sequenceSize = (short) ByteOrder.ubyte2int(payload[2]); if (sequenceNumber < 1 || sequenceSize < 1 || sequenceNumber > sequenceSize) throw new BadPacketException("Bad sequence/size: " + sequenceNumber + "/" + sequenceSize); // Find out if the patch data is compressed by reading C 3 bytes into the payload data this.compressor = payload[3]; if (!(compressor == COMPRESSOR_NONE || compressor == COMPRESSOR_DEFLATE)) throw new BadPacketException("Bad compressor: " + compressor); // Find out if the patch data was halved or not by reading B 4 bytes into the payload data this.entryBits = payload[4]; if (entryBits < 0) throw new BadPacketException("Negative entryBits: " + entryBits); // Clip out the chunk of probably compressed and halved patch data this patch message carries // after that this.data = new byte [payload.length - 5]; // It's the whole paylaod not including the first 4 fNSCB bytes we read System.arraycopy(payload, 5, data, 0, data.length); // Copy it from 5 bytes in to the end }