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 resolved_ips = [host[4][0] for host in socket.getaddrinfo(socket.gethostname(), 0, 0, 0, socket.SOL_TCP)] 52 global_ips = [ ip for ip in resolved_ips if not ip.startswith('127.') and not ip == '::1'] 53 54 remote_ips = list(set(global_ips) - set(local_addrs)) 55 if remote_ips: 56 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)) 57 # IPv6 support % to denote zone/scope ids. The value is expanded 58 # in other functions, this is why we are using replace command in 59 # the return. For more info https://github.com/ros/ros_comm/pull/598 60 return retval.replace('%', '%%')
61 62 # suggestion by mquigley based on laptop dhcp issues
63 -def ros_hostname_check(ctx):
64 """Make sure that ROS_HOSTNAME resolves to a local IP address""" 65 if not rosgraph.ROS_HOSTNAME in ctx.env: 66 return 67 68 hostname = ctx.env[rosgraph.ROS_HOSTNAME] 69 try: 70 resolved_ips = [host[4][0] for host in socket.getaddrinfo(hostname, 0, 0, 0, socket.SOL_TCP)] 71 except socket.gaierror: 72 return "ROS_HOSTNAME [%s] cannot be resolved to an IP address"%(hostname) 73 74 # best we can do is compare roslib's routine against socket resolution and make sure they agree 75 local_addrs = rosgraph.network.get_local_addresses() 76 77 remote_ips = list(set(resolved_ips) - set(local_addrs)) 78 if remote_ips: 79 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))
80
81 -def ros_ip_check(ctx):
82 """Make sure that ROS_IP is a local IP address""" 83 if not rosgraph.ROS_IP in ctx.env: 84 return 85 86 ip = ctx.env[rosgraph.ROS_IP] 87 88 # best we can do is compare roslib's routine against socket resolution and make sure they agree 89 addrs = rosgraph.network.get_local_addresses() 90 91 if ip not in addrs: 92 return "ROS_IP [%s] does not appear to be a local IP address %s."%(ip, str(addrs))
93 94 # Error/Warning Rules 95 96 warnings = [ 97 (ros_hostname_check, 98 "ROS_HOSTNAME may be incorrect: "), 99 (ros_ip_check, 100 "ROS_IP may be incorrect: "), 101 102 ] 103 104 errors = [ 105 (ip_check, 106 "Local network configuration is invalid: "), 107 ] 108
109 -def wtf_check(ctx):
110 for r in warnings: 111 warning_rule(r, r[0](ctx), ctx) 112 for r in errors: 113 error_rule(r, r[0](ctx), ctx)
114