Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * * Copyright 2015 AT&T Foundry |
| 4 | * * |
| 5 | * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * * you may not use this file except in compliance with the License. |
| 7 | * * You may obtain a copy of the License at |
| 8 | * * |
| 9 | * * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * * |
| 11 | * * Unless required by applicable law or agreed to in writing, software |
| 12 | * * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * * See the License for the specific language governing permissions and |
| 15 | * * limitations under the License. |
| 16 | * |
| 17 | */ |
| 18 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 19 | package org.onlab.packet; |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 20 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 21 | import java.util.Arrays; |
| 22 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 23 | /** |
| 24 | * An attribute in a RADIUS packet. |
| 25 | */ |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 26 | public class RADIUSAttribute { |
| 27 | protected byte type; |
| 28 | protected byte length; |
| 29 | protected byte[] value; |
| 30 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 31 | // RADIUS attribute types |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 32 | public static final byte RADIUS_ATTR_USERNAME = 1; |
| 33 | public static final byte RADIUS_ATTR_NAS_IP = 4; |
| 34 | public static final byte RADIUS_ATTR_NAS_PORT = 5; |
| 35 | public static final byte RADIUS_ATTR_FRAMED_MTU = 12; |
| 36 | public static final byte RADIUS_ATTR_STATE = 24; |
| 37 | public static final byte RADIUS_ATTR_VENDOR_SPECIFIC = 26; |
| 38 | public static final byte RADIUS_ATTR_CALLING_STATION_ID = 31; |
| 39 | public static final byte RADIUS_ATTR_NAS_ID = 32; |
| 40 | public static final byte RADIUS_ATTR_ACCT_SESSION_ID = 44; |
| 41 | public static final byte RADIUS_ATTR_NAS_PORT_TYPE = 61; |
| 42 | public static final byte RADIUS_ATTR_EAP_MESSAGE = 79; |
| 43 | public static final byte RADIUS_ATTR_MESSAGE_AUTH = 80; |
| 44 | public static final byte RADIUS_ATTR_NAS_PORT_ID = 87; |
| 45 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Default constructor. |
| 48 | */ |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 49 | public RADIUSAttribute() { |
| 50 | } |
| 51 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Constructs a RADIUS attribute with the give type, length and value. |
| 54 | * |
| 55 | * @param type type |
| 56 | * @param length length |
| 57 | * @param value value |
| 58 | */ |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 59 | public RADIUSAttribute(final byte type, final byte length, final byte[] value) { |
| 60 | this.type = type; |
| 61 | this.length = length; |
| 62 | this.value = value; |
| 63 | } |
| 64 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Checks if the attribute type is valid. |
| 67 | * |
| 68 | * @return whether the type is valid or not |
| 69 | */ |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 70 | public boolean isValidType() { |
| 71 | return this.type == RADIUS_ATTR_USERNAME || |
| 72 | this.type == RADIUS_ATTR_NAS_IP || |
| 73 | this.type == RADIUS_ATTR_NAS_PORT || |
| 74 | this.type == RADIUS_ATTR_VENDOR_SPECIFIC || |
| 75 | this.type == RADIUS_ATTR_CALLING_STATION_ID || |
| 76 | this.type == RADIUS_ATTR_NAS_ID || |
| 77 | this.type == RADIUS_ATTR_ACCT_SESSION_ID || |
| 78 | this.type == RADIUS_ATTR_NAS_PORT_TYPE || |
| 79 | this.type == RADIUS_ATTR_EAP_MESSAGE || |
| 80 | this.type == RADIUS_ATTR_MESSAGE_AUTH || |
| 81 | this.type == RADIUS_ATTR_NAS_PORT_ID; |
| 82 | } |
| 83 | |
| 84 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 85 | * Gets the attribute type. |
| 86 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 87 | * @return the type |
| 88 | */ |
| 89 | public byte getType() { |
| 90 | return this.type; |
| 91 | } |
| 92 | |
| 93 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 94 | * Sets the attribute type. |
| 95 | * |
| 96 | * @param type the code to set |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 97 | * @return this |
| 98 | */ |
| 99 | public RADIUSAttribute setType(final byte type) { |
| 100 | this.type = type; |
| 101 | return this; |
| 102 | } |
| 103 | |
| 104 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 105 | * Gets the attribute length. |
| 106 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 107 | * @return the length |
| 108 | */ |
| 109 | public byte getLength() { |
| 110 | return this.length; |
| 111 | } |
| 112 | |
| 113 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 114 | * Sets the attribute length. |
| 115 | * |
| 116 | * @param length the length to set |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 117 | * @return this |
| 118 | */ |
| 119 | public RADIUSAttribute setLength(final byte length) { |
| 120 | this.length = length; |
| 121 | return this; |
| 122 | } |
| 123 | |
| 124 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 125 | * Gets the attribute value. |
| 126 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 127 | * @return the value |
| 128 | */ |
| 129 | public byte[] getValue() { |
| 130 | return this.value; |
| 131 | } |
| 132 | |
| 133 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 134 | * Sets the attribute value. |
| 135 | * |
| 136 | * @param value the data to set |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 137 | * @return this |
| 138 | */ |
| 139 | public RADIUSAttribute setValue(final byte[] value) { |
| 140 | this.value = value; |
| 141 | return this; |
| 142 | } |
| 143 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 144 | @Override |
| 145 | public String toString() { |
| 146 | StringBuilder sb = new StringBuilder(); |
| 147 | sb.append("["); |
| 148 | sb.append("type= "); |
| 149 | sb.append(type); |
| 150 | sb.append("length= "); |
| 151 | sb.append(length); |
| 152 | sb.append("value= "); |
| 153 | sb.append(Arrays.toString(value)); |
| 154 | sb.append("]"); |
| 155 | return sb.toString(); |
| 156 | } |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 157 | } |