blob: 45f22855a37055bdc68d8b35f6816db0285da4ce [file] [log] [blame]
Ari Saha79d7c252015-06-26 10:31:48 -07001/*
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 Hart4a60bb32015-06-30 15:31:20 -070019package org.onlab.packet;
Ari Saha79d7c252015-06-26 10:31:48 -070020
Jian Li5fc14292015-12-04 11:30:46 -080021import java.util.Arrays;
22
Jonathan Hart4a60bb32015-06-30 15:31:20 -070023/**
24 * An attribute in a RADIUS packet.
25 */
Ari Saha79d7c252015-06-26 10:31:48 -070026public class RADIUSAttribute {
27 protected byte type;
28 protected byte length;
29 protected byte[] value;
30
Jonathan Hart4a60bb32015-06-30 15:31:20 -070031 // RADIUS attribute types
Ari Saha79d7c252015-06-26 10:31:48 -070032 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 Hart4a60bb32015-06-30 15:31:20 -070046 /**
47 * Default constructor.
48 */
Ari Saha79d7c252015-06-26 10:31:48 -070049 public RADIUSAttribute() {
50 }
51
Jonathan Hart4a60bb32015-06-30 15:31:20 -070052 /**
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 Saha79d7c252015-06-26 10:31:48 -070059 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 Hart4a60bb32015-06-30 15:31:20 -070065 /**
66 * Checks if the attribute type is valid.
67 *
68 * @return whether the type is valid or not
69 */
Ari Saha79d7c252015-06-26 10:31:48 -070070 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 Hart4a60bb32015-06-30 15:31:20 -070085 * Gets the attribute type.
86 *
Ari Saha79d7c252015-06-26 10:31:48 -070087 * @return the type
88 */
89 public byte getType() {
90 return this.type;
91 }
92
93 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070094 * Sets the attribute type.
95 *
96 * @param type the code to set
Ari Saha79d7c252015-06-26 10:31:48 -070097 * @return this
98 */
99 public RADIUSAttribute setType(final byte type) {
100 this.type = type;
101 return this;
102 }
103
104 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700105 * Gets the attribute length.
106 *
Ari Saha79d7c252015-06-26 10:31:48 -0700107 * @return the length
108 */
109 public byte getLength() {
110 return this.length;
111 }
112
113 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700114 * Sets the attribute length.
115 *
116 * @param length the length to set
Ari Saha79d7c252015-06-26 10:31:48 -0700117 * @return this
118 */
119 public RADIUSAttribute setLength(final byte length) {
120 this.length = length;
121 return this;
122 }
123
124 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700125 * Gets the attribute value.
126 *
Ari Saha79d7c252015-06-26 10:31:48 -0700127 * @return the value
128 */
129 public byte[] getValue() {
130 return this.value;
131 }
132
133 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700134 * Sets the attribute value.
135 *
136 * @param value the data to set
Ari Saha79d7c252015-06-26 10:31:48 -0700137 * @return this
138 */
139 public RADIUSAttribute setValue(final byte[] value) {
140 this.value = value;
141 return this;
142 }
143
Jian Li5fc14292015-12-04 11:30:46 -0800144 @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 Saha79d7c252015-06-26 10:31:48 -0700157}