1# Copyright 2014 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import logging 6import socket 7 8from autotest_lib.client.common_lib import error 9 10# See server/cros/network/wifi_test_context_manager.py for commandline 11# flags to control IP addresses in WiFi tests. 12DEFAULT_FAILURE_MESSAGE = ( 13 'Cannot infer DNS name of companion device from an IP address.') 14ATTENUATOR_FAILURE_MESSAGE = ( 15 'Cannot infer DNS name of WiFi variable attenuator from a client IP ' 16 'address. Use --atten_addr=<ip or dns name>') 17BTATTENUATOR_FAILURE_MESSAGE = ( 18 'Cannot infer DNS name of Bluetooth variable attenuator from a client IP ' 19 'address. Use --btatten_addr=<ip or dns name>') 20ROUTER_FAILURE_MESSAGE = ( 21 'Cannot infer DNS name of WiFi router from a client IP address.') 22PCAP_FAILURE_MESSAGE = ( 23 'Cannot infer DNS name of Packet Capturer from a client IP address.') 24 25 26def is_ip_address(hostname): 27 """Infers whether |hostname| could be an IP address. 28 29 @param hostname: string DNS name or IP address. 30 @return True iff hostname is a valid IP address. 31 32 """ 33 try: 34 socket.inet_aton(hostname) 35 return True 36 except socket.error: 37 return False 38 39 40def get_companion_device_addr(client_hostname, 41 suffix, 42 cmdline_override=None, 43 not_dnsname_msg=DEFAULT_FAILURE_MESSAGE, 44 allow_failure=False): 45 """Build a usable hostname for a test companion device from the client name. 46 47 Optionally, override the generated name with a commandline provided version. 48 49 @param client_hostname: string DNS name of device under test (the client). 50 @param suffix: string suffix to append to the client hostname. 51 @param cmdline_override: optional DNS name of companion device. If this is 52 given, it overrides the generated client based hostname. 53 @param not_dnsname_msg: string message to include in the exception raised 54 if the client hostname is found to be an IP address rather than a 55 DNS name. 56 @param allow_failure: boolean True iff we should return None on failure to 57 infer a DNS name. 58 @return string DNS name of companion device or None if |allow_failure| 59 is True and no DNS name can be inferred. 60 61 """ 62 if cmdline_override is not None: 63 return cmdline_override 64 if is_ip_address(client_hostname): 65 logging.error('%r looks like an IP address?', client_hostname) 66 if allow_failure: 67 return None 68 raise error.TestError(not_dnsname_msg) 69 parts = client_hostname.split('.', 1) 70 parts[0] = parts[0] + suffix 71 return '.'.join(parts) 72 73 74def get_router_addr(client_hostname, cmdline_override=None): 75 """Build a hostname for a WiFi router from the client hostname. 76 77 Optionally override that hostname with the provided command line hostname. 78 79 @param client_hostname: string DNS name of the client. 80 @param cmdline_override: string DNS name of the router provided 81 via commandline arguments. 82 @return usable DNS name for router host. 83 84 """ 85 return get_companion_device_addr( 86 client_hostname, 87 '-router', 88 cmdline_override=cmdline_override, 89 not_dnsname_msg=ROUTER_FAILURE_MESSAGE) 90 91 92def get_pcap_addr(client_hostname, 93 cmdline_override=None, 94 allow_failure=False): 95 """Build a hostname for a packet capturer from the client hostname. 96 97 @param client_hostname: string DNS name of the client. 98 @param cmdline_override: string DNS name of the packet capturer provided 99 via commandline arguments. 100 @return usable DNS name for capturer host or None. 101 102 """ 103 return get_companion_device_addr( 104 client_hostname, 105 '-pcap', 106 cmdline_override=cmdline_override, 107 not_dnsname_msg=PCAP_FAILURE_MESSAGE, 108 allow_failure=allow_failure) 109 110 111def get_attenuator_addr(client_hostname, 112 cmdline_override=None, 113 allow_failure=False): 114 """Build a hostname for a WiFi variable attenuator from the client hostname. 115 116 Optionally override that hostname with the provided command line hostname. 117 118 @param client_hostname: string DNS name of the client. 119 @param cmdline_override: string DNS name of the variable attenuator 120 controller provided via commandline arguments. 121 @param allow_failure: boolean True iff we should return None on failure to 122 infer a DNS name. 123 @return usable DNS name for attenuator controller. 124 125 """ 126 return get_companion_device_addr( 127 client_hostname, 128 '-attenuator', 129 cmdline_override=cmdline_override, 130 not_dnsname_msg=ATTENUATOR_FAILURE_MESSAGE, 131 allow_failure=allow_failure) 132 133 134def get_btattenuator_addr(client_hostname, 135 cmdline_override=None, 136 allow_failure=False): 137 """Build a hostname for a Bluetooth variable attenuator from the client hostname. 138 139 Optionally override that hostname with the provided command line hostname. 140 141 @param client_hostname: string DNS name of the client. 142 @param cmdline_override: string DNS name of the variable attenuator 143 controller provided via commandline arguments. 144 @param allow_failure: boolean True iff we should return None on failure to 145 infer a DNS name. 146 @return usable DNS name for attenuator controller. 147 148 """ 149 return get_companion_device_addr( 150 client_hostname, 151 '-btattenuator', 152 cmdline_override=cmdline_override, 153 not_dnsname_msg=BTATTENUATOR_FAILURE_MESSAGE, 154 allow_failure=allow_failure) 155