Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 16 | package org.onlab.packet.ndp; |
| 17 | |
| 18 | import org.onlab.packet.BasePacket; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 19 | import org.onlab.packet.Deserializer; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 20 | |
| 21 | import java.nio.ByteBuffer; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 22 | import java.util.List; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 23 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 25 | import static org.onlab.packet.PacketUtils.checkInput; |
| 26 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 27 | /** |
| 28 | * Implements ICMPv6 Router Solicitation packet format. (RFC 4861) |
| 29 | */ |
| 30 | public class RouterSolicitation extends BasePacket { |
| 31 | public static final byte HEADER_LENGTH = 4; // bytes |
| 32 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 33 | private final NeighborDiscoveryOptions options = new NeighborDiscoveryOptions(); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Gets the Neighbor Discovery Protocol packet options. |
| 37 | * |
| 38 | * @return the Neighbor Discovery Protocol packet options |
| 39 | */ |
| 40 | public List<NeighborDiscoveryOptions.Option> getOptions() { |
| 41 | return this.options.options(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Adds a Neighbor Discovery Protocol packet option. |
| 46 | * |
| 47 | * @param type the option type |
| 48 | * @param data the option data |
| 49 | * @return this |
| 50 | */ |
| 51 | public RouterSolicitation addOption(final byte type, final byte[] data) { |
| 52 | this.options.addOption(type, data); |
| 53 | return this; |
| 54 | } |
| 55 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 56 | @Override |
| 57 | public byte[] serialize() { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 58 | byte[] optionsData = null; |
| 59 | if (this.options.hasOptions()) { |
| 60 | optionsData = this.options.serialize(); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 61 | } |
| 62 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 63 | int optionsLength = 0; |
| 64 | if (optionsData != null) { |
| 65 | optionsLength = optionsData.length; |
Charles M.C. Chan | 5c0b476 | 2015-01-10 18:38:37 +0800 | [diff] [blame] | 66 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 67 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 68 | final byte[] data = new byte[HEADER_LENGTH + optionsLength]; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 69 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 70 | |
| 71 | bb.putInt(0); |
| 72 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 73 | if (optionsData != null) { |
| 74 | bb.put(optionsData); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | return data; |
| 78 | } |
| 79 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * (non-Javadoc) |
| 83 | * |
| 84 | * @see java.lang.Object#hashCode() |
| 85 | */ |
| 86 | @Override |
| 87 | public int hashCode() { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 88 | final int prime = 5807; |
| 89 | int result = super.hashCode(); |
| 90 | result = prime * result + this.options.hashCode(); |
| 91 | return result; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
| 95 | * (non-Javadoc) |
| 96 | * |
| 97 | * @see java.lang.Object#equals(java.lang.Object) |
| 98 | */ |
| 99 | @Override |
| 100 | public boolean equals(final Object obj) { |
| 101 | if (this == obj) { |
| 102 | return true; |
| 103 | } |
| 104 | if (!super.equals(obj)) { |
| 105 | return false; |
| 106 | } |
| 107 | if (!(obj instanceof RouterSolicitation)) { |
| 108 | return false; |
| 109 | } |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 110 | final RouterSolicitation other = (RouterSolicitation) obj; |
| 111 | if (!this.options.equals(other.options)) { |
| 112 | return false; |
| 113 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 114 | return true; |
| 115 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Deserializer function for router solicitation packets. |
| 119 | * |
| 120 | * @return deserializer function |
| 121 | */ |
| 122 | public static Deserializer<RouterSolicitation> deserializer() { |
| 123 | return (data, offset, length) -> { |
| 124 | checkInput(data, offset, length, HEADER_LENGTH); |
| 125 | |
| 126 | RouterSolicitation routerSolicitation = new RouterSolicitation(); |
| 127 | |
| 128 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 129 | |
| 130 | bb.getInt(); |
| 131 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 132 | if (bb.limit() - bb.position() > 0) { |
| 133 | NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer() |
| 134 | .deserialize(data, bb.position(), bb.limit() - bb.position()); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 135 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 136 | for (NeighborDiscoveryOptions.Option option : options.options()) { |
| 137 | routerSolicitation.addOption(option.type(), option.data()); |
| 138 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | return routerSolicitation; |
| 142 | }; |
| 143 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 144 | |
| 145 | @Override |
| 146 | public String toString() { |
| 147 | return toStringHelper(getClass()) |
| 148 | .toString(); |
| 149 | // TODO: need to handle options |
| 150 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 151 | } |