blob: 9653115ba9e4c898d46fdbf462351f1479fe94ad [file] [log] [blame]
Rusty Eddy80f12522015-09-03 22:42:02 +00001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onlab.packet.pim;
17
18import org.onlab.packet.BasePacket;
19import org.onlab.packet.Deserializer;
20import org.onlab.packet.IPacket;
21import org.onlab.packet.IpPrefix;
22
23import java.nio.ByteBuffer;
24import java.util.HashMap;
25
26import static org.onlab.packet.PacketUtils.checkInput;
27
28public class PIMJoinPrune extends BasePacket {
29
30 private PIMAddrUnicast upstreamAddr = new PIMAddrUnicast();
31 private short holdTime = (short) 0xffff;
32
33 private class JoinPruneGroup {
34 protected IpPrefix group;
35 protected HashMap<IpPrefix, IpPrefix> joins = new HashMap<>();
36 protected HashMap<IpPrefix, IpPrefix> prunes = new HashMap<>();
37
38 public JoinPruneGroup(IpPrefix grp) {
39 group = grp;
40 }
41 }
42 private HashMap<IpPrefix, JoinPruneGroup> joinPrunes = new HashMap<>();
43
44 /**
45 * Get the J/P hold time.
46 *
47 * @return specified in seconds.
48 */
49 public short getHoldTime() {
50 return holdTime;
51 }
52
53 /**
54 * Set the J/P holdtime in seconds.
55 *
56 * @param holdTime return the holdtime.
57 */
58 public void setHoldTime(short holdTime) {
59 this.holdTime = holdTime;
60 }
61
62 /**
63 * Get the upstreamAddr for this J/P request.
64 *
65 * @return the upstream address.
66 */
67 public PIMAddrUnicast getUpstreamAddr() {
68 return upstreamAddr;
69 }
70
71 /**
72 * Set the upstream address of this PIM J/P request.
73 *
74 * @param upstr the PIM Upstream unicast address
75 */
76 public void setUpstreamAddr(PIMAddrUnicast upstr) {
77 this.upstreamAddr = upstr;
78 }
79
80 /**
81 * Add the specified s,g to join field.
82 *
83 * @param saddr the source address of the route
84 * @param gaddr the group address of the route
85 * @param join true for a join, false for a prune.
86 */
87 public void addJoinPrune(String saddr, String gaddr, boolean join) {
88 IpPrefix gpfx = IpPrefix.valueOf(gaddr);
89 IpPrefix spfx = IpPrefix.valueOf(saddr);
90 addJoinPrune(spfx, gpfx, join);
91 }
92
93 /**
94 * Add the specified S, G to the join field.
95 *
96 * @param spfx the source prefix of the route
97 * @param gpfx the group prefix of the route
98 * @param join true for join, false for prune
99 */
100 public void addJoinPrune(IpPrefix spfx, IpPrefix gpfx, boolean join) {
101 JoinPruneGroup jpg = joinPrunes.get(gpfx);
102 if (jpg == null) {
103 jpg = new JoinPruneGroup(gpfx);
104 joinPrunes.put(gpfx, jpg);
105 }
106
107 HashMap<IpPrefix, IpPrefix> members = (join) ? jpg.joins : jpg.prunes;
108 if (members.get(spfx) == null) {
109 members.put(spfx, spfx);
110 }
111 }
112
113 /**
114 * Add a join given strings represending the source and group addresses.
115 *
116 * @param saddr source address
117 * @param gaddr group address
118 */
119 public void addJoin(String saddr, String gaddr) {
120 this.addJoinPrune(saddr, gaddr, true);
121 }
122
123 /**
124 * Add a prune given strings represending the source and group addresses.
125 *
126 * @param saddr source address
127 * @param gaddr group address
128 */
129 public void addPrune(String saddr, String gaddr) {
130 this.addJoinPrune(saddr, gaddr, false);
131 }
132
133 /**
134 * Sets all payloads parent packet if applicable, then serializes this
135 * packet and all payloads.
136 *
137 * @return a byte[] containing this packet and payloads
138 */
139 @Override
140 public byte[] serialize() {
141
142 byte[] data = new byte[8096]; // Come up with something better
143 ByteBuffer bb = ByteBuffer.wrap(data);
144
145 bb.put(upstreamAddr.serialize());
146 bb.put((byte) 0); // reserved
147
148 int ngrps = joinPrunes.size();
149 bb.put((byte) ngrps);
150 bb.putShort(this.holdTime);
151
152 // Walk the group list and input all groups
153 for (JoinPruneGroup jpg : joinPrunes.values()) {
154 PIMAddrGroup grp = new PIMAddrGroup(jpg.group);
155 bb.put(grp.serialize());
156
157 // put the number of joins and prunes
158 bb.putShort((short) jpg.joins.size());
159 bb.putShort((short) jpg.prunes.size());
160
161 // Set all of the joins
162 for (IpPrefix spfx : jpg.joins.values()) {
163 PIMAddrSource src = new PIMAddrSource(spfx);
164 bb.put(src.serialize());
165 }
166
167 // Set all of the prunes
168 for (IpPrefix spfx : jpg.prunes.values()) {
169 PIMAddrSource src = new PIMAddrSource(spfx);
170 bb.put(src.serialize());
171 }
172 }
173
174 int len = bb.position();
175 byte[] data2 = new byte[len];
176 bb = ByteBuffer.wrap(data2, 0, len);
177 bb.put(data, 0, len);
178 return data2;
179 }
180
181 // TODO: I suppose I really need to implement this?
182 @Override
183 public IPacket deserialize(final byte[] data, final int offset,
184 final int length) {
185 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
186 return this;
187 }
188
189 /**
190 * Return the J/P deserializer function.
191 *
192 * @return a function that will deserialize a J/P message.
193 */
194 public static Deserializer<PIMJoinPrune> deserializer() {
195 return (data, offset, length) -> {
196
197 /*
198 * Delay buffer checks until we read enough of the packet to know how
199 * much data we will require. Each encoded address deserializer function
200 * will ensure there is enough data for that address.
201 */
202 PIMJoinPrune jp = new PIMJoinPrune();
203 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
204
205 // We must get a PIM encoded unicast address
206 PIMAddrUnicast upstream = new PIMAddrUnicast();
207 upstream.deserialize(bb);
208 jp.setUpstreamAddr(upstream);
209
210 // Use this boolean to determine the buffer space we need according to address sizes
211 boolean ipv4 = upstream.getAddr().isIp4();
212
213 // We need at minimum 4 bytes for reserved(1), ngroups(1) & holdtime(2)
214 checkInput(bb.array(), bb.position(), bb.limit() - bb.position(), 4);
215
216 // get and skip the reserved byte
217 bb.get();
218
219 // Get the number of groups.
220 int ngroups = bb.get();
221
222 // Save the holdtime.
223 jp.setHoldTime(bb.getShort());
224
225
226 for (int i = 0; i < ngroups; i++) {
227 PIMAddrGroup grp = new PIMAddrGroup();
228
229 /*
230 * grp.deserialize will ensure the buffer has enough data to read the group address.
231 */
232 grp.deserialize(bb);
233
234 checkInput(bb.array(), bb.position(), bb.limit() - bb.position(), 4);
235 int njoins = bb.getShort();
236 int nprunes = bb.getShort();
237
238 /*
239 * Now we'll verify we have enough buffer to read the next
240 * group of join and prune addresses for this group.
241 */
242 int required = (njoins + nprunes) *
243 (ipv4 ? PIMAddrSource.ENC_SOURCE_IPV4_BYTE_LENGTH : PIMAddrSource.ENC_SOURCE_IPV6_BYTE_LENGTH);
244 checkInput(bb.array(), bb.position(), bb.limit() - bb.position(), required);
245
246 // Now iterate through the joins for this group
247 for (; njoins > 0; njoins--) {
248
249 PIMAddrSource src = new PIMAddrSource();
250 src.deserialize(bb);
251
252 jp.addJoinPrune(
253 src.getAddr().toIpPrefix(),
254 grp.getAddr().toIpPrefix(), true);
255 }
256
257 // Now iterate through the prunes for this group
258 for (; nprunes > 0; nprunes--) {
259
260 PIMAddrSource src = new PIMAddrSource();
261 src.deserialize(bb);
262 jp.addJoinPrune(
263 src.getAddr().toIpPrefix(),
264 grp.getAddr().toIpPrefix(), false);
265 }
266 }
267
268 return jp;
269 };
270 }
271}