Package roswtf :: Module network
[frames] | no frames]

Source Code for Module roswtf.network

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2009, Willow Garage, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32  # 
 33  # Revision $Id: environment.py 4428 2009-05-05 05:48:36Z jfaustwg $ 
 34   
 35  import os 
 36  import socket 
 37  import stat 
 38  import string 
 39  import sys 
 40   
 41  import rosgraph 
 42  import rosgraph.network 
 43   
 44  from roswtf.rules import warning_rule, error_rule 
 45   
 46  # #1220 
47 -def ip_check(ctx):
48 # best we can do is compare roslib's routine against socket resolution and make sure they agree 49 local_addrs = rosgraph.network.get_local_addresses() 50 51 if rosgraph.network.use_ipv6(): 52 resolved_ips = [host[4][0] for host in socket.getaddrinfo(socket.gethostname(), 0, 0, 0, socket.SOL_TCP)] 53 else: 54 resolved_ips = [host[4][0] for host in socket.getaddrinfo(socket.gethostname(), 0, socket.AF_INET, 0, socket.SOL_TCP)] 55 56 global_ips = [ ip for ip in resolved_ips if not ip.startswith('127.') and not ip == '::1'] 57 58 remote_ips = list(set(global_ips) - set(local_addrs)) 59 if remote_ips: 60 retval = "Local hostname [%s] resolves to [%s], which does not appear to be a local IP address %s." % (socket.gethostname(), ','.join(remote_ips), str(local_addrs)) 61 # IPv6 support % to denote zone/scope ids. The value is expanded 62 # in other functions, this is why we are using replace command in 63 # the return. For more info https://github.com/ros/ros_comm/pull/598 64 return retval.replace('%', '%%')
65 66 # suggestion by mquigley based on laptop dhcp issues
67 -def ros_hostname_check(ctx):
68 """Make sure that ROS_HOSTNAME resolves to a local IP address""" 69 if not rosgraph.ROS_HOSTNAME in ctx.env: 70 return 71 72 hostname = ctx.env[rosgraph.ROS_HOSTNAME] 73 try: 74 resolved_ips = [host[4][0] for host in socket.getaddrinfo(hostname, 0, 0, 0, socket.SOL_TCP)] 75 except socket.gaierror: 76 return "ROS_HOSTNAME [%s] cannot be resolved to an IP address"%(hostname) 77 78 # best we can do is compare roslib's routine against socket resolution and make sure they agree 79 local_addrs = rosgraph.network.get_local_addresses() 80 81 remote_ips = list(set(resolved_ips) - set(local_addrs)) 82 if remote_ips: 83 return "ROS_HOSTNAME [%s] resolves to [%s], which does not appear to be a local IP address %s."%(hostname, ','.join(remote_ips), str(local_addrs))
84
85 -def ros_ip_check(ctx):
86 """Make sure that ROS_IP is a local IP address""" 87 if not rosgraph.ROS_IP in ctx.env: 88 return 89 90 ip = ctx.env[rosgraph.ROS_IP] 91 92 # best we can do is compare roslib's routine against socket resolution and make sure they agree 93 addrs = rosgraph.network.get_local_addresses() 94 95 if " " in ip: 96 return "ROS_IP [%s] contains whitespace. This is not a valid IP."%ip 97 98 if ip not in addrs: 99 return "ROS_IP [%s] does not appear to be an IP address of a local network interface (one of %s)."%(ip, str(addrs))
100 101 # Error/Warning Rules 102 103 warnings = [ 104 (ros_hostname_check, 105 "ROS_HOSTNAME may be incorrect: "), 106 (ros_ip_check, 107 "ROS_IP may be incorrect: "), 108 109 ] 110 111 errors = [ 112 (ip_check, 113 "Local network configuration is invalid: "), 114 ] 115
116 -def wtf_check(ctx):
117 for r in warnings: 118 warning_rule(r, r[0](ctx), ctx) 119 for r in errors: 120 error_rule(r, r[0](ctx), ctx)
121