win32icmp.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # ctypes version of win32icmp
  2. # so I don't have to recompile for future versions of python.
  3. # you're welcome.
  4. #
  5. # The contents of this file are subject to the Python Software Foundation
  6. # License Version 2.3 (the License). You may not copy or use this file, in
  7. # either source code or executable form, except in compliance with the License.
  8. # You may obtain a copy of the License at http://www.python.org/license.
  9. #
  10. # Software distributed under the License is distributed on an AS IS basis,
  11. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. # for the specific language governing rights and limitations under the
  13. # License.
  14. #
  15. # by Greg Hazel
  16. import ctypes
  17. from BTL.iptypes import IPAddr, inet_addr
  18. icmp = ctypes.windll.icmp
  19. IcmpCreateFile = icmp.IcmpCreateFile
  20. IcmpCloseHandle = icmp.IcmpCloseHandle
  21. class IP_OPTION_INFORMATION(ctypes.Structure):
  22. _fields_ = [ ("Ttl", ctypes.c_ubyte),
  23. ("Tos", ctypes.c_ubyte),
  24. ("Flags", ctypes.c_ubyte),
  25. ("OptionsSize", ctypes.c_ubyte),
  26. ("OptionsData", ctypes.POINTER(ctypes.c_ubyte)),
  27. ]
  28. Options = IP_OPTION_INFORMATION
  29. class ICMP_ECHO_REPLY(ctypes.Structure):
  30. _fields_ = [ ("Address", IPAddr),
  31. ("Status", ctypes.c_ulong),
  32. ("RoundTripTime", ctypes.c_ulong),
  33. ("DataSize", ctypes.c_ushort),
  34. ("Reserved", ctypes.c_ushort),
  35. ("Data", ctypes.c_void_p),
  36. ("Options", IP_OPTION_INFORMATION),
  37. ]
  38. def IcmpSendEcho(handle, addr, data, options, timeout):
  39. reply = ICMP_ECHO_REPLY()
  40. data = data or ''
  41. if options:
  42. options = ctypes.byref(options)
  43. r = icmp.IcmpSendEcho(handle, inet_addr(addr),
  44. data, len(data),
  45. options,
  46. ctypes.byref(reply),
  47. ctypes.sizeof(ICMP_ECHO_REPLY) + len(data),
  48. timeout)
  49. return str(reply.Address), reply.Status, reply.RoundTripTime
  50. IP_STATUS_BASE = 11000
  51. IP_SUCCESS = 0
  52. IP_BUF_TOO_SMALL = (IP_STATUS_BASE + 1)
  53. IP_DEST_NET_UNREACHABLE = (IP_STATUS_BASE + 2)
  54. IP_DEST_HOST_UNREACHABLE = (IP_STATUS_BASE + 3)
  55. IP_DEST_PROT_UNREACHABLE = (IP_STATUS_BASE + 4)
  56. IP_DEST_PORT_UNREACHABLE = (IP_STATUS_BASE + 5)
  57. IP_NO_RESOURCES = (IP_STATUS_BASE + 6)
  58. IP_BAD_OPTION = (IP_STATUS_BASE + 7)
  59. IP_HW_ERROR = (IP_STATUS_BASE + 8)
  60. IP_PACKET_TOO_BIG = (IP_STATUS_BASE + 9)
  61. IP_REQ_TIMED_OUT = (IP_STATUS_BASE + 10)
  62. IP_BAD_REQ = (IP_STATUS_BASE + 11)
  63. IP_BAD_ROUTE = (IP_STATUS_BASE + 12)
  64. IP_TTL_EXPIRED_TRANSIT = (IP_STATUS_BASE + 13)
  65. IP_TTL_EXPIRED_REASSEM = (IP_STATUS_BASE + 14)
  66. IP_PARAM_PROBLEM = (IP_STATUS_BASE + 15)
  67. IP_SOURCE_QUENCH = (IP_STATUS_BASE + 16)
  68. IP_OPTION_TOO_BIG = (IP_STATUS_BASE + 17)
  69. IP_BAD_DESTINATION = (IP_STATUS_BASE + 18)
  70. status = {}
  71. for k, v in dict(globals()).iteritems():
  72. if k.startswith("IP_"):
  73. status[v] = k