Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -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 | */ |
| 16 | package org.onosproject.pim.impl; |
| 17 | |
| 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.IPacket; |
| 20 | import org.onlab.packet.IPv4; |
| 21 | import org.onlab.packet.Ip4Address; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.PIM; |
| 24 | |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 25 | public class PimPacket { |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 26 | |
| 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 Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 54 | public PimPacket(byte type) { |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 55 | 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 Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 108 | public void setPimPayload(IPacket payload) { |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame] | 109 | 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 | } |