1 /* 2 * Copyright (C) 2017 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 package org.conscrypt; 17 18 /** 19 * A provider for the peer host and port information. 20 */ 21 abstract class PeerInfoProvider { 22 private static final PeerInfoProvider NULL_PEER_INFO_PROVIDER = new PeerInfoProvider() { 23 @Override 24 String getHostname() { 25 return null; 26 } 27 28 @Override 29 public String getHostnameOrIP() { 30 return null; 31 } 32 33 @Override 34 public int getPort() { 35 return -1; 36 } 37 }; 38 39 /** 40 * Returns the hostname supplied during engine/socket creation. No DNS resolution is 41 * attempted before returning the hostname. 42 */ getHostname()43 abstract String getHostname(); 44 45 /** 46 * This method attempts to create a textual representation of the peer host or IP. Does 47 * not perform a reverse DNS lookup. This is typically used during session creation. 48 */ getHostnameOrIP()49 abstract String getHostnameOrIP(); 50 51 /** 52 * Gets the port of the peer. 53 */ getPort()54 abstract int getPort(); 55 nullProvider()56 static PeerInfoProvider nullProvider() { 57 return NULL_PEER_INFO_PROVIDER; 58 } 59 forHostAndPort(final String host, final int port)60 static PeerInfoProvider forHostAndPort(final String host, final int port) { 61 return new PeerInfoProvider() { 62 @Override 63 String getHostname() { 64 return host; 65 } 66 67 @Override 68 public String getHostnameOrIP() { 69 return host; 70 } 71 72 @Override 73 public int getPort() { 74 return port; 75 } 76 }; 77 } 78 } 79