blob: e279a40405acdf30868d681e25dd1bdb3bc48a38 [file] [log] [blame]
Charles M.C. Chanea5aa472015-01-03 13:40:39 +08001/*
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -08002 * Copyright 2014-2015 Open Networking Laboratory
Charles M.C. Chanea5aa472015-01-03 13:40:39 +08003 *
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. Chanea5aa472015-01-03 13:40:39 +080016package org.onlab.packet.ndp;
17
18import org.onlab.packet.BasePacket;
Jonathan Hart2a655752015-04-07 16:46:33 -070019import org.onlab.packet.Deserializer;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080020import org.onlab.packet.IPacket;
21
22import java.nio.ByteBuffer;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080023import java.util.List;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080024
Jonathan Hart2a655752015-04-07 16:46:33 -070025import static org.onlab.packet.PacketUtils.checkInput;
26
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080027/**
28 * Implements ICMPv6 Router Solicitation packet format. (RFC 4861)
29 */
30public class RouterSolicitation extends BasePacket {
31 public static final byte HEADER_LENGTH = 4; // bytes
32
Jonathan Hart2a655752015-04-07 16:46:33 -070033 private final NeighborDiscoveryOptions options = new NeighborDiscoveryOptions();
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080034
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. Chanea5aa472015-01-03 13:40:39 +080056 @Override
57 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080058 byte[] optionsData = null;
59 if (this.options.hasOptions()) {
60 optionsData = this.options.serialize();
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080061 }
62
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080063 int optionsLength = 0;
64 if (optionsData != null) {
65 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +080066 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080067
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080068 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080069 final ByteBuffer bb = ByteBuffer.wrap(data);
70
71 bb.putInt(0);
72
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080073 if (optionsData != null) {
74 bb.put(optionsData);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080075 }
76
77 return data;
78 }
79
80 @Override
81 public IPacket deserialize(byte[] data, int offset, int length) {
82 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
83
84 bb.getInt();
85
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080086 this.options.deserialize(data, bb.position(),
87 bb.limit() - bb.position());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080088
89 return this;
90 }
91
92 /*
93 * (non-Javadoc)
94 *
95 * @see java.lang.Object#hashCode()
96 */
97 @Override
98 public int hashCode() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080099 final int prime = 5807;
100 int result = super.hashCode();
101 result = prime * result + this.options.hashCode();
102 return result;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800103 }
104
105 /*
106 * (non-Javadoc)
107 *
108 * @see java.lang.Object#equals(java.lang.Object)
109 */
110 @Override
111 public boolean equals(final Object obj) {
112 if (this == obj) {
113 return true;
114 }
115 if (!super.equals(obj)) {
116 return false;
117 }
118 if (!(obj instanceof RouterSolicitation)) {
119 return false;
120 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800121 final RouterSolicitation other = (RouterSolicitation) obj;
122 if (!this.options.equals(other.options)) {
123 return false;
124 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800125 return true;
126 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700127
128 /**
129 * Deserializer function for router solicitation packets.
130 *
131 * @return deserializer function
132 */
133 public static Deserializer<RouterSolicitation> deserializer() {
134 return (data, offset, length) -> {
135 checkInput(data, offset, length, HEADER_LENGTH);
136
137 RouterSolicitation routerSolicitation = new RouterSolicitation();
138
139 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
140
141 bb.getInt();
142
Charles Chan3599d632015-09-05 14:47:51 +0800143 if (bb.limit() - bb.position() > 0) {
144 NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
145 .deserialize(data, bb.position(), bb.limit() - bb.position());
Jonathan Hart2a655752015-04-07 16:46:33 -0700146
Charles Chan3599d632015-09-05 14:47:51 +0800147 for (NeighborDiscoveryOptions.Option option : options.options()) {
148 routerSolicitation.addOption(option.type(), option.data());
149 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700150 }
151
152 return routerSolicitation;
153 };
154 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800155}