Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.opencord.dhcpl2relay.impl.packet; |
| 17 | |
| 18 | import java.io.ByteArrayOutputStream; |
| 19 | import java.nio.BufferUnderflowException; |
| 20 | import java.nio.ByteBuffer; |
| 21 | import java.nio.charset.StandardCharsets; |
| 22 | |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | /** |
| 27 | * Represents the DHCP Option 82 sub-options. Currently only supports sub option |
| 28 | * 1 (agent-circuit-id) and 2 (agent-remote-id). |
| 29 | */ |
| 30 | public class DhcpOption82Data { |
| 31 | |
| 32 | private String agentCircuitId = null; |
| 33 | private String agentRemoteId = null; |
| 34 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 35 | public static final byte CIRCUIT_ID_CODE = 1; |
| 36 | public static final byte REMOTE_ID_CODE = 2; |
| 37 | |
| 38 | public DhcpOption82Data() { |
| 39 | |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Constructs a DhcpOption82Data object from the given byte array. The |
| 44 | * expectation is that the byte array starts with the first suboption (i.e |
| 45 | * it does not include the option-code and overall length of the option 82) |
| 46 | * |
| 47 | * @param b byte array representing the data portion of the dhcp option 82 |
| 48 | */ |
| 49 | public DhcpOption82Data(byte[] b) { |
| 50 | ByteBuffer bb = ByteBuffer.wrap(b, 0, b.length); |
| 51 | if (b.length < 3) { |
| 52 | log.warn("Malformed option82 sub-options {}", b); |
| 53 | return; |
| 54 | } |
| 55 | while (bb.hasRemaining() && bb.limit() - bb.position() > 2) { |
| 56 | byte subOptionCode = bb.get(); |
| 57 | byte subOptionLen = bb.get(); |
| 58 | byte[] subOptionData = new byte[subOptionLen]; |
| 59 | try { |
| 60 | bb.get(subOptionData); |
| 61 | } catch (BufferUnderflowException e) { |
| 62 | log.warn("Malformed option82 sub-option {}", e.getMessage()); |
| 63 | return; |
| 64 | } |
| 65 | if (subOptionCode == CIRCUIT_ID_CODE) { |
| 66 | agentCircuitId = new String(subOptionData); |
| 67 | } else if (subOptionCode == REMOTE_ID_CODE) { |
| 68 | agentRemoteId = new String(subOptionData); |
| 69 | } else { |
| 70 | log.debug("Unsupported subOption {} in DHCP option82 - {}", |
| 71 | subOptionCode, new String(subOptionData)); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public void setAgentCircuitId(String value) { |
| 77 | this.agentCircuitId = value; |
| 78 | } |
| 79 | |
| 80 | public void setAgentCircuitId(byte[] subOptionData) { |
| 81 | agentCircuitId = new String(subOptionData); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * |
| 86 | * @return agentCircuitId |
| 87 | */ |
| 88 | public String getAgentCircuitId() { |
| 89 | return this.agentCircuitId; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * sets AgentRemoteId. |
| 94 | * @param value Value to be set |
| 95 | */ |
| 96 | public void setAgentRemoteId(String value) { |
| 97 | this.agentRemoteId = value; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * |
| 102 | * @return agentRemoteId |
| 103 | */ |
| 104 | public String getAgentRemoteId() { |
| 105 | return this.agentRemoteId; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * |
| 110 | * @return length of option 82. |
| 111 | */ |
| 112 | public byte length() { |
| 113 | int length = 0; |
| 114 | |
| 115 | // +2 below for sub option ID and length of sub option |
| 116 | if (agentCircuitId != null) { |
| 117 | length += agentCircuitId.length() + 2; |
| 118 | } |
| 119 | if (agentRemoteId != null) { |
| 120 | length += agentRemoteId.length() + 2; |
| 121 | } |
| 122 | return (byte) length; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns the representation of the option 82 specification as a byte |
| 127 | * array. |
| 128 | * @return returns byte array |
| 129 | */ |
| 130 | public byte[] toByteArray() { |
| 131 | ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
| 132 | |
| 133 | // Add sub option if set |
| 134 | if (agentCircuitId != null) { |
| 135 | buf.write(CIRCUIT_ID_CODE); |
| 136 | buf.write((byte) agentCircuitId.length()); |
| 137 | byte[] bytes = agentCircuitId.getBytes(StandardCharsets.UTF_8); |
| 138 | buf.write(bytes, 0, bytes.length); |
| 139 | } |
| 140 | |
| 141 | // Add sub option if set |
| 142 | if (agentRemoteId != null) { |
| 143 | buf.write(REMOTE_ID_CODE); |
| 144 | buf.write((byte) agentRemoteId.length()); |
| 145 | byte[] bytes = agentRemoteId.getBytes(StandardCharsets.UTF_8); |
| 146 | buf.write(bytes, 0, bytes.length); |
| 147 | } |
| 148 | |
| 149 | return buf.toByteArray(); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public String toString() { |
| 154 | return "circuitId: " + agentCircuitId + " remoteId: " + agentRemoteId; |
| 155 | } |
| 156 | |
| 157 | } |