PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/utilities/roswtf/src/roswtf/network.py

https://gitlab.com/F34140r/ros_comm
Python | 110 lines | 49 code | 20 blank | 41 comment | 14 complexity | 9a6aebba96c3b147408b97e6bf12a01c MD5 | raw file
Possible License(s): LGPL-2.1
  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. import os
  35. import socket
  36. import stat
  37. import string
  38. import sys
  39. import rosgraph
  40. import rosgraph.network
  41. from roswtf.rules import warning_rule, error_rule
  42. # #1220
  43. def ip_check(ctx):
  44. # best we can do is compare roslib's routine against socket resolution and make sure they agree
  45. local_addrs = rosgraph.network.get_local_addresses()
  46. resolved_ips = [host[4][0] for host in socket.getaddrinfo(socket.gethostname(), 0, 0, 0, socket.SOL_TCP)]
  47. global_ips = [ ip for ip in resolved_ips if not ip.startswith('127.') and not ip == '::1']
  48. remote_ips = list(set(global_ips) - set(local_addrs))
  49. if remote_ips:
  50. return "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))
  51. # suggestion by mquigley based on laptop dhcp issues
  52. def ros_hostname_check(ctx):
  53. """Make sure that ROS_HOSTNAME resolves to a local IP address"""
  54. if not rosgraph.ROS_HOSTNAME in ctx.env:
  55. return
  56. hostname = ctx.env[rosgraph.ROS_HOSTNAME]
  57. try:
  58. resolved_ips = [host[4][0] for host in socket.getaddrinfo(hostname, 0, 0, 0, socket.SOL_TCP)]
  59. except socket.gaierror:
  60. return "ROS_HOSTNAME [%s] cannot be resolved to an IP address"%(hostname)
  61. # best we can do is compare roslib's routine against socket resolution and make sure they agree
  62. local_addrs = rosgraph.network.get_local_addresses()
  63. remote_ips = list(set(resolved_ips) - set(local_addrs))
  64. if remote_ips:
  65. 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))
  66. def ros_ip_check(ctx):
  67. """Make sure that ROS_IP is a local IP address"""
  68. if not rosgraph.ROS_IP in ctx.env:
  69. return
  70. ip = ctx.env[rosgraph.ROS_IP]
  71. # best we can do is compare roslib's routine against socket resolution and make sure they agree
  72. addrs = rosgraph.network.get_local_addresses()
  73. if ip not in addrs:
  74. return "ROS_IP [%s] does not appear to be a local IP address %s."%(ip, str(addrs))
  75. # Error/Warning Rules
  76. warnings = [
  77. (ros_hostname_check,
  78. "ROS_HOSTNAME may be incorrect: "),
  79. (ros_ip_check,
  80. "ROS_IP may be incorrect: "),
  81. ]
  82. errors = [
  83. (ip_check,
  84. "Local network configuration is invalid: "),
  85. ]
  86. def wtf_check(ctx):
  87. for r in warnings:
  88. warning_rule(r, r[0](ctx), ctx)
  89. for r in errors:
  90. error_rule(r, r[0](ctx), ctx)