Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 18 | package net.onrc.onos.core.packet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | |
| 20 | import java.nio.ByteBuffer; |
| 21 | import java.util.HashMap; |
| 22 | import java.util.Map; |
| 23 | |
Yuta HIGUCHI | aa132f5 | 2014-06-26 10:18:39 -0700 | [diff] [blame] | 24 | // CHECKSTYLE IGNORE WriteTag FOR NEXT 2 LINES |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 25 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 26 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 27 | */ |
| 28 | public class UDP extends BasePacket { |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 29 | public static final short DHCP_SERVER_PORT = (short) 67; |
| 30 | public static final short DHCP_CLIENT_PORT = (short) 68; |
| 31 | public static final Map<Short, Class<? extends IPacket>> DECODE_MAP; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 32 | |
| 33 | static { |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 34 | DECODE_MAP = new HashMap<Short, Class<? extends IPacket>>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 35 | /* |
| 36 | * Disable DHCP until the deserialize code is hardened to deal with garbage input |
| 37 | */ |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 38 | UDP.DECODE_MAP.put(DHCP_SERVER_PORT, DHCP.class); |
| 39 | UDP.DECODE_MAP.put(DHCP_CLIENT_PORT, DHCP.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 40 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | protected short sourcePort; |
| 44 | protected short destinationPort; |
| 45 | protected short length; |
| 46 | protected short checksum; |
| 47 | |
| 48 | /** |
| 49 | * @return the sourcePort |
| 50 | */ |
| 51 | public short getSourcePort() { |
| 52 | return sourcePort; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param sourcePort the sourcePort to set |
| 57 | */ |
| 58 | public UDP setSourcePort(short sourcePort) { |
| 59 | this.sourcePort = sourcePort; |
| 60 | return this; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return the destinationPort |
| 65 | */ |
| 66 | public short getDestinationPort() { |
| 67 | return destinationPort; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param destinationPort the destinationPort to set |
| 72 | */ |
| 73 | public UDP setDestinationPort(short destinationPort) { |
| 74 | this.destinationPort = destinationPort; |
| 75 | return this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @return the length |
| 80 | */ |
| 81 | public short getLength() { |
| 82 | return length; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @return the checksum |
| 87 | */ |
| 88 | public short getChecksum() { |
| 89 | return checksum; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @param checksum the checksum to set |
| 94 | */ |
| 95 | public UDP setChecksum(short checksum) { |
| 96 | this.checksum = checksum; |
| 97 | return this; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void resetChecksum() { |
| 102 | this.checksum = 0; |
| 103 | super.resetChecksum(); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Serializes the packet. Will compute and set the following fields if they |
| 108 | * are set to specific values at the time serialize is called: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 109 | * -checksum : 0 |
| 110 | * -length : 0 |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 111 | */ |
Yuta HIGUCHI | aa132f5 | 2014-06-26 10:18:39 -0700 | [diff] [blame] | 112 | @Override |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 113 | public byte[] serialize() { |
| 114 | byte[] payloadData = null; |
| 115 | if (payload != null) { |
| 116 | payload.setParent(this); |
| 117 | payloadData = payload.serialize(); |
| 118 | } |
| 119 | |
| 120 | this.length = (short) (8 + ((payloadData == null) ? 0 |
| 121 | : payloadData.length)); |
| 122 | |
| 123 | byte[] data = new byte[this.length]; |
| 124 | ByteBuffer bb = ByteBuffer.wrap(data); |
| 125 | |
| 126 | bb.putShort(this.sourcePort); |
| 127 | bb.putShort(this.destinationPort); |
| 128 | bb.putShort(this.length); |
| 129 | bb.putShort(this.checksum); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 130 | if (payloadData != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 131 | bb.put(payloadData); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 132 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 133 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 134 | if (this.parent != null && this.parent instanceof IPv4) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 135 | ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_UDP); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 136 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 137 | |
| 138 | // compute checksum if needed |
| 139 | if (this.checksum == 0) { |
| 140 | bb.rewind(); |
| 141 | int accumulation = 0; |
| 142 | |
| 143 | // compute pseudo header mac |
| 144 | if (this.parent != null && this.parent instanceof IPv4) { |
| 145 | IPv4 ipv4 = (IPv4) this.parent; |
| 146 | accumulation += ((ipv4.getSourceAddress() >> 16) & 0xffff) |
| 147 | + (ipv4.getSourceAddress() & 0xffff); |
| 148 | accumulation += ((ipv4.getDestinationAddress() >> 16) & 0xffff) |
| 149 | + (ipv4.getDestinationAddress() & 0xffff); |
| 150 | accumulation += ipv4.getProtocol() & 0xff; |
| 151 | accumulation += this.length & 0xffff; |
| 152 | } |
| 153 | |
| 154 | for (int i = 0; i < this.length / 2; ++i) { |
| 155 | accumulation += 0xffff & bb.getShort(); |
| 156 | } |
| 157 | // pad to an even number of shorts |
| 158 | if (this.length % 2 > 0) { |
| 159 | accumulation += (bb.get() & 0xff) << 8; |
| 160 | } |
| 161 | |
| 162 | accumulation = ((accumulation >> 16) & 0xffff) |
| 163 | + (accumulation & 0xffff); |
| 164 | this.checksum = (short) (~accumulation & 0xffff); |
| 165 | bb.putShort(6, this.checksum); |
| 166 | } |
| 167 | return data; |
| 168 | } |
| 169 | |
| 170 | /* (non-Javadoc) |
| 171 | * @see java.lang.Object#hashCode() |
| 172 | */ |
| 173 | @Override |
| 174 | public int hashCode() { |
| 175 | final int prime = 5807; |
| 176 | int result = super.hashCode(); |
| 177 | result = prime * result + checksum; |
| 178 | result = prime * result + destinationPort; |
| 179 | result = prime * result + length; |
| 180 | result = prime * result + sourcePort; |
| 181 | return result; |
| 182 | } |
| 183 | |
| 184 | /* (non-Javadoc) |
| 185 | * @see java.lang.Object#equals(java.lang.Object) |
| 186 | */ |
| 187 | @Override |
| 188 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 189 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 190 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 191 | } |
| 192 | if (!super.equals(obj)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 193 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 194 | } |
| 195 | if (!(obj instanceof UDP)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 196 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 197 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 198 | UDP other = (UDP) obj; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 199 | if (checksum != other.checksum) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 200 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 201 | } |
| 202 | if (destinationPort != other.destinationPort) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 203 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 204 | } |
| 205 | if (length != other.length) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 206 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 207 | } |
| 208 | if (sourcePort != other.sourcePort) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 209 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 210 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 211 | return true; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 216 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 217 | this.sourcePort = bb.getShort(); |
| 218 | this.destinationPort = bb.getShort(); |
| 219 | this.length = bb.getShort(); |
| 220 | this.checksum = bb.getShort(); |
| 221 | |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 222 | if (UDP.DECODE_MAP.containsKey(this.destinationPort)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 223 | try { |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 224 | this.payload = UDP.DECODE_MAP.get(this.destinationPort).getConstructor().newInstance(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 225 | } catch (Exception e) { |
| 226 | throw new RuntimeException("Failure instantiating class", e); |
| 227 | } |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 228 | } else if (UDP.DECODE_MAP.containsKey(this.sourcePort)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 229 | try { |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 230 | this.payload = UDP.DECODE_MAP.get(this.sourcePort).getConstructor().newInstance(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 231 | } catch (Exception e) { |
| 232 | throw new RuntimeException("Failure instantiating class", e); |
| 233 | } |
| 234 | } else { |
| 235 | this.payload = new Data(); |
| 236 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 237 | this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 238 | this.payload.setParent(this); |
| 239 | return this; |
| 240 | } |
| 241 | } |