1 /* 2 * Copyright (C) 2016 The Android Open Source Project 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 17 package com.android.server.wifi.hotspot2.anqp; 18 19 import com.android.internal.annotations.VisibleForTesting; 20 import com.android.server.wifi.ByteBufferReader; 21 import com.android.server.wifi.hotspot2.anqp.eap.EAPMethod; 22 23 import java.net.ProtocolException; 24 import java.nio.BufferUnderflowException; 25 import java.nio.ByteBuffer; 26 import java.nio.ByteOrder; 27 import java.nio.charset.StandardCharsets; 28 import java.util.ArrayList; 29 import java.util.Arrays; 30 import java.util.Collections; 31 import java.util.List; 32 33 /** 34 * The NAI Realm Data ANQP sub-element, IEEE802.11-2012 section 8.4.4.10 figure 8-418. 35 * 36 * Format: 37 * | Length | Encoding | NAIRealm Length | NAIRealm | EAPMethod Count | EAPMethod #1 (optional) | 38 * 2 1 1 variable 1 variable 39 */ 40 public class NAIRealmData { 41 /** 42 * Mask for determining NAI Realm String encoding type. 43 */ 44 @VisibleForTesting 45 public static final int NAI_ENCODING_UTF8_MASK = 0x1; 46 47 @VisibleForTesting 48 public static final String NAI_REALM_STRING_SEPARATOR = ";"; 49 50 private final List<String> mRealms; 51 private final List<EAPMethod> mEAPMethods; 52 53 @VisibleForTesting NAIRealmData(List<String> realms, List<EAPMethod> eapMethods)54 public NAIRealmData(List<String> realms, List<EAPMethod> eapMethods) { 55 mRealms = realms; 56 mEAPMethods = eapMethods; 57 } 58 59 /** 60 * Parse a NAIRealmData from the given buffer. 61 * 62 * @param payload The byte buffer to read from 63 * @return {@link NAIRealmElement} 64 * @throws BufferUnderflowException 65 * @throws ProtocolException 66 */ parse(ByteBuffer payload)67 public static NAIRealmData parse(ByteBuffer payload) throws ProtocolException { 68 // Read and verify the length field. 69 int length = (int) ByteBufferReader.readInteger(payload, ByteOrder.LITTLE_ENDIAN, 2) 70 & 0xFFFF; 71 if (length > payload.remaining()) { 72 throw new ProtocolException("Invalid data length: " + length); 73 } 74 75 // Read the encoding field. 76 boolean utf8 = (payload.get() & NAI_ENCODING_UTF8_MASK) != 0; 77 78 // Read the realm string. 79 String realm = ByteBufferReader.readStringWithByteLength( 80 payload, utf8 ? StandardCharsets.UTF_8 : StandardCharsets.US_ASCII); 81 List<String> realmList = Arrays.asList(realm.split(NAI_REALM_STRING_SEPARATOR)); 82 83 // Read the EAP methods. 84 int methodCount = payload.get() & 0xFF; 85 List<EAPMethod> eapMethodList = new ArrayList<>(); 86 while (methodCount > 0) { 87 eapMethodList.add(EAPMethod.parse(payload)); 88 methodCount--; 89 } 90 return new NAIRealmData(realmList, eapMethodList); 91 } 92 getRealms()93 public List<String> getRealms() { 94 return Collections.unmodifiableList(mRealms); 95 } 96 getEAPMethods()97 public List<EAPMethod> getEAPMethods() { 98 return Collections.unmodifiableList(mEAPMethods); 99 } 100 101 @Override equals(Object thatObject)102 public boolean equals(Object thatObject) { 103 if (this == thatObject) { 104 return true; 105 } 106 if (!(thatObject instanceof NAIRealmData)) { 107 return false; 108 } 109 NAIRealmData that = (NAIRealmData) thatObject; 110 return mRealms.equals(that.mRealms) && mEAPMethods.equals(that.mEAPMethods); 111 } 112 113 @Override hashCode()114 public int hashCode() { 115 return mRealms.hashCode() * 31 + mEAPMethods.hashCode(); 116 } 117 118 @Override toString()119 public String toString() { 120 return "NAIRealmElement{mRealms=" + mRealms + " mEAPMethods=" + mEAPMethods + "}"; 121 } 122 } 123