| 1 | package cz.vutbr.feec.packets.rsi; |
| 2 | |
| 3 | import cz.vutbr.feec.packets.PacketGenerateException; |
| 4 | import cz.vutbr.feec.packets.PacketParseException; |
| 5 | import cz.vutbr.feec.packets.PacketUtils; |
| 6 | import org.apache.commons.lang.builder.ToStringBuilder; |
| 7 | |
| 8 | public class AddressBlock extends SubReportBlock { |
| 9 | private int port = -1; |
| 10 | private byte[] address; |
| 11 | |
| 12 | public enum Type { |
| 13 | IPv4, IPv6, DNS |
| 14 | } |
| 15 | |
| 16 | public AddressBlock(Type type) { |
| 17 | switch (type) { |
| 18 | case IPv4: |
| 19 | this.setType(0); |
| 20 | this.length = 4 + 4; |
| 21 | this.address = new byte[4]; |
| 22 | break; |
| 23 | case IPv6: |
| 24 | this.setType(1); |
| 25 | this.length = 4 + 16; |
| 26 | this.address = new byte[16]; |
| 27 | break; |
| 28 | case DNS: |
| 29 | this.setType(2); |
| 30 | this.length = 4 + 4; |
| 31 | break; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public int generate(byte[] array, int offset) |
| 37 | throws PacketGenerateException { |
| 38 | assert this.getType() == ipv4Address || this.getType() == ipv6Address |
| 39 | || this.getType() == dnsUnicastFeedback; |
| 40 | int len = super.generate(array, offset); |
| 41 | PacketUtils.setOctet2(array, offset+2, this.port); |
| 42 | if (this.getType() == ipv6Address) { |
| 43 | len += 2 + 4 * 4; |
| 44 | System.arraycopy(this.address, 0, array, 4 + offset, 4 * 4); |
| 45 | } else if (this.getType() == ipv4Address) { |
| 46 | len += 2 + 4; |
| 47 | System.arraycopy(array, offset + 4, this.address, 0, 4); |
| 48 | } else { |
| 49 | System.arraycopy(this.address, 0, array, 4 + offset, |
| 50 | this.address.length); |
| 51 | len = this.length; |
| 52 | } |
| 53 | return len; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public int parse(byte[] array, int offset, int packetlength) |
| 58 | throws PacketParseException { |
| 59 | int len = super.parse(array, offset, packetlength); |
| 60 | assert this.getType() == ipv4Address || this.getType() == ipv6Address |
| 61 | || this.getType() == dnsUnicastFeedback; |
| 62 | assert this.length == 8 || this.length == 4 + 16; |
| 63 | |
| 64 | this.port = PacketUtils.getOctet2(array, offset + 2); |
| 65 | len += 2; |
| 66 | if (this.getType() == ipv4Address) { |
| 67 | assert this.getType() == ipv4Address; |
| 68 | if (array.length < offset + 4 + 4) { |
| 69 | throw new PacketParseException("Received packet is too short."); |
| 70 | } |
| 71 | // IPv4 |
| 72 | System.arraycopy(array, 4 + offset, this.address, 0, 4); |
| 73 | return len + 4; |
| 74 | } else if (this.getType() == ipv6Address) { |
| 75 | if (array.length < offset + 4 + 16) { |
| 76 | throw new PacketParseException("Received packet is too short."); |
| 77 | } |
| 78 | assert this.getType() == ipv6Address; |
| 79 | // IPv6 |
| 80 | System.arraycopy(array, 4 + offset, this.address, 0, 4 * 4); |
| 81 | return len + 4 * 4; |
| 82 | } else { |
| 83 | assert this.getType() == dnsUnicastFeedback; |
| 84 | this.address = new byte[packetlength - 4]; |
| 85 | System.arraycopy(array, 4 + offset, this.address, 0, |
| 86 | packetlength - 4); |
| 87 | return packetlength - 4; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public void setAddress(byte[] addr) { |
| 92 | if (getType() == 0) { |
| 93 | assert addr.length == 4; |
| 94 | System.arraycopy(addr, 0, this.address, 0, 4); |
| 95 | } else if (getType() == 1) { |
| 96 | assert addr.length == 16; |
| 97 | System.arraycopy(addr, 0, this.address, 0, 16); |
| 98 | } else if (getType() == 2) { |
| 99 | this.length = 4 + 4 * ((addr.length + 1 + 3) / 4); |
| 100 | this.address = new byte[addr.length + 1]; |
| 101 | System.arraycopy(addr, 0, this.address, 0, addr.length); |
| 102 | this.address[addr.length] = 0; |
| 103 | } else { |
| 104 | throw new RuntimeException("Zatim neimlementovano pro typ: " |
| 105 | + getType()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public int getPort() { |
| 110 | return port; |
| 111 | } |
| 112 | |
| 113 | public void setPort(int port) { |
| 114 | this.port = port; |
| 115 | } |
| 116 | |
| 117 | public byte[] getAddress() { |
| 118 | return address; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @see java.lang.Object#toString() |
| 123 | */ |
| 124 | public String toString() { |
| 125 | return new ToStringBuilder(this).append("port", this.port).append( |
| 126 | "type", this.getType()).append("length", this.length).append( |
| 127 | "address", this.address).toString(); |
| 128 | } |
| 129 | |
| 130 | } |