blob: 185b2d8c2def6b28bbe5276455df92557c30b184 [file] [log] [blame]
Rusty Eddy4d5a92f2016-01-25 17:12:14 -08001/*
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.onosproject.pim.impl;
17
18import org.onlab.packet.Ethernet;
19import org.onlab.packet.IPacket;
20import org.onlab.packet.IPv4;
21import org.onlab.packet.Ip4Address;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.PIM;
24
Jonathan Hartfbfe2a82016-03-29 11:36:33 -070025public class PimPacket {
Rusty Eddy4d5a92f2016-01-25 17:12:14 -080026
27 // Ethernet header
28 private Ethernet ethHeader = new Ethernet();
29
30 // IP header
31 private IPv4 ipHeader = new IPv4();
32
33 // PIM Header
34 private PIM pimHeader = new PIM();
35
36 // The pim type
37 private byte pimType;
38
39 // PIM MAC address
40 private MacAddress pimDestinationMac = MacAddress.valueOf("01:00:5E:00:00:0d");
41
42 /**
43 * Create a PIM packet for a given PIM type.
44 *
45 * The resulting packet will have Ethernet and IPv4 headers with all defaults filled in.
46 * The final packet will require a PIM header that corresponds to the PIM type set as
47 * a payload.
48 *
49 * Additionally the source MAC and IPv4 address will need to be filled in for the
50 * packet to be ready to serialize in most cases.
51 *
52 * @param type PIM.TYPE_XXXX where XXX is the PIM message type
53 */
Jonathan Hartfbfe2a82016-03-29 11:36:33 -070054 public PimPacket(byte type) {
Rusty Eddy4d5a92f2016-01-25 17:12:14 -080055 pimType = type;
56 initDefaults();
57 }
58
59 /**
60 * Fill in defaults for the Ethernet, IPv4 and PIM headers, then associate each
61 * of these headers as payload and parent accordingly.
62 */
63 public void initDefaults() {
64 // Prepopulate dst MACAddress and Ethernet Types. The Source MAC needs to be filled in.
65 ethHeader.setDestinationMACAddress(pimDestinationMac);
66 ethHeader.setEtherType(Ethernet.TYPE_IPV4);
67
68 // Prepopulate the IP Type and Dest address. The Source IP address needs to be filled in.
69 ipHeader.setDestinationAddress(PIM.PIM_ADDRESS.getIp4Address().toInt());
70 ipHeader.setTtl((byte) 1);
71 ipHeader.setProtocol(IPv4.PROTOCOL_PIM);
72
73 // Establish the order between Ethernet and IP headers
74 ethHeader.setPayload(ipHeader);
75 ipHeader.setParent(ethHeader);
76
77 // Prepopulate the PIM packet
78 pimHeader.setPIMType(pimType);
79
80 // Establish the order between IP and PIM headers
81 ipHeader.setPayload(pimHeader);
82 pimHeader.setParent(ipHeader);
83 }
84
85 /**
86 * Set the source MAC address.
87 *
88 * @param src source MAC address
89 */
90 public void setSrcMacAddr(MacAddress src) {
91 ethHeader.setSourceMACAddress(src);
92 }
93
94 /**
95 * Set the source IPv4 address.
96 *
97 * @param ipSrcAddress the source IPv4 address
98 */
99 public void setSrcIpAddress(Ip4Address ipSrcAddress) {
100 ipHeader.setSourceAddress(ipSrcAddress.toInt());
101 }
102
103 /**
104 * Set the PIM payload.
105 *
106 * @param payload the PIM payload
107 */
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700108 public void setPimPayload(IPacket payload) {
Rusty Eddy4d5a92f2016-01-25 17:12:14 -0800109 pimHeader.setPayload(payload);
110 payload.setParent(pimHeader);
111 }
112
113 /**
114 * Get the ethernet header.
115 *
116 * @return the Ethernet header
117 */
118 public Ethernet getEthernet() {
119 return ethHeader;
120 }
121
122 /**
123 * Get the IPv4 header.
124 *
125 * @return the IPv4 header
126 */
127 public IPv4 getIpv4() {
128 return ipHeader;
129 }
130}