home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD1196611232000.psc / modPing.bas < prev    next >
Encoding:
BASIC Source File  |  2000-11-13  |  9.4 KB  |  305 lines

  1. Attribute VB_Name = "modPing"
  2. Option Explicit
  3.  
  4. Public Const IP_STATUS_BASE = 11000
  5. Public Const IP_SUCCESS = 0
  6. Public Const IP_BUF_TOO_SMALL = (11000 + 1)
  7. Public Const IP_DEST_NET_UNREACHABLE = (11000 + 2)
  8. Public Const IP_DEST_HOST_UNREACHABLE = (11000 + 3)
  9. Public Const IP_DEST_PROT_UNREACHABLE = (11000 + 4)
  10. Public Const IP_DEST_PORT_UNREACHABLE = (11000 + 5)
  11. Public Const IP_NO_RESOURCES = (11000 + 6)
  12. Public Const IP_BAD_OPTION = (11000 + 7)
  13. Public Const IP_HW_ERROR = (11000 + 8)
  14. Public Const IP_PACKET_TOO_BIG = (11000 + 9)
  15. Public Const IP_REQ_TIMED_OUT = (11000 + 10)
  16. Public Const IP_BAD_REQ = (11000 + 11)
  17. Public Const IP_BAD_ROUTE = (11000 + 12)
  18. Public Const IP_TTL_EXPIRED_TRANSIT = (11000 + 13)
  19. Public Const IP_TTL_EXPIRED_REASSEM = (11000 + 14)
  20. Public Const IP_PARAM_PROBLEM = (11000 + 15)
  21. Public Const IP_SOURCE_QUENCH = (11000 + 16)
  22. Public Const IP_OPTION_TOO_BIG = (11000 + 17)
  23. Public Const IP_BAD_DESTINATION = (11000 + 18)
  24. Public Const IP_ADDR_DELETED = (11000 + 19)
  25. Public Const IP_SPEC_MTU_CHANGE = (11000 + 20)
  26. Public Const IP_MTU_CHANGE = (11000 + 21)
  27. Public Const IP_UNLOAD = (11000 + 22)
  28. Public Const IP_ADDR_ADDED = (11000 + 23)
  29. Public Const IP_GENERAL_FAILURE = (11000 + 50)
  30. Public Const MAX_IP_STATUS = 11000 + 50
  31. Public Const IP_PENDING = (11000 + 255)
  32. Public Const PING_TIMEOUT = 200
  33. Public Const WS_VERSION_REQD = &H101
  34. Public Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
  35. Public Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
  36. Public Const MIN_SOCKETS_REQD = 1
  37. Public Const SOCKET_ERROR = -1
  38.  
  39. Public Const MAX_WSADescription = 256
  40. Public Const MAX_WSASYSStatus = 128
  41.  
  42. Public Type ICMP_OPTIONS
  43.     Ttl             As Byte
  44.     Tos             As Byte
  45.     Flags           As Byte
  46.     OptionsSize     As Byte
  47.     OptionsData     As Long
  48. End Type
  49.  
  50. Dim ICMPOPT As ICMP_OPTIONS
  51.  
  52. Public Type ICMP_ECHO_REPLY
  53.     Address         As Long
  54.     status          As Long
  55.     RoundTripTime   As Long
  56.     DataSize        As Integer
  57.     Reserved        As Integer
  58.     DataPointer     As Long
  59.     Options         As ICMP_OPTIONS
  60.     Data            As String * 250
  61. End Type
  62.  
  63. Public Type HOSTENT
  64.     hName As Long
  65.     hAliases As Long
  66.     hAddrType As Integer
  67.     hLen As Integer
  68.     hAddrList As Long
  69. End Type
  70.  
  71. Public Type WSADATA
  72.     wVersion As Integer
  73.     wHighVersion As Integer
  74.     szDescription(0 To MAX_WSADescription) As Byte
  75.     szSystemStatus(0 To MAX_WSASYSStatus) As Byte
  76.     wMaxSockets As Integer
  77.     wMaxUDPDG As Integer
  78.     dwVendorInfo As Long
  79. End Type
  80.  
  81.  
  82. Public Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
  83.  
  84. Public Declare Function IcmpCloseHandle Lib "icmp.dll" _
  85.    (ByVal IcmpHandle As Long) As Long
  86.    
  87. Public Declare Function IcmpSendEcho Lib "icmp.dll" _
  88.    (ByVal IcmpHandle As Long, _
  89.     ByVal DestinationAddress As Long, _
  90.     ByVal RequestData As String, _
  91.     ByVal RequestSize As Integer, _
  92.     ByVal RequestOptions As Long, _
  93.     ReplyBuffer As ICMP_ECHO_REPLY, _
  94.     ByVal ReplySize As Long, _
  95.     ByVal Timeout As Long) As Long
  96.     
  97. Public Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
  98.  
  99. Public Declare Function WSAStartup Lib "WSOCK32.DLL" _
  100.    (ByVal wVersionRequired As Long, _
  101.     lpWSADATA As WSADATA) As Long
  102.     
  103. Public Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
  104.  
  105. Public Declare Function gethostname Lib "WSOCK32.DLL" _
  106.    (ByVal szHost As String, _
  107.     ByVal dwHostLen As Long) As Long
  108.     
  109. Public Declare Function gethostbyname Lib "WSOCK32.DLL" _
  110.    (ByVal szHost As String) As Long
  111.    
  112. Public Declare Sub RtlMoveMemory Lib "kernel32" _
  113.    (hpvDest As Any, _
  114.     ByVal hpvSource As Long, _
  115.     ByVal cbCopy As Long)
  116.  
  117.  
  118. Public Function GetStatusCode(status As Long) As String
  119.  
  120.    Dim msg As String
  121.  
  122.    Select Case status
  123.       Case IP_SUCCESS:               msg = "ip success"
  124.       Case IP_BUF_TOO_SMALL:         msg = "ip buf too_small"
  125.       Case IP_DEST_NET_UNREACHABLE:  msg = "ip dest net unreachable"
  126.       Case IP_DEST_HOST_UNREACHABLE: msg = "ip dest host unreachable"
  127.       Case IP_DEST_PROT_UNREACHABLE: msg = "ip dest prot unreachable"
  128.       Case IP_DEST_PORT_UNREACHABLE: msg = "ip dest port unreachable"
  129.       Case IP_NO_RESOURCES:          msg = "ip no resources"
  130.       Case IP_BAD_OPTION:            msg = "ip bad option"
  131.       Case IP_HW_ERROR:              msg = "ip hw_error"
  132.       Case IP_PACKET_TOO_BIG:        msg = "ip packet too_big"
  133.       Case IP_REQ_TIMED_OUT:         msg = "ip req timed out"
  134.       Case IP_BAD_REQ:               msg = "ip bad req"
  135.       Case IP_BAD_ROUTE:             msg = "ip bad route"
  136.       Case IP_TTL_EXPIRED_TRANSIT:   msg = "ip ttl expired transit"
  137.       Case IP_TTL_EXPIRED_REASSEM:   msg = "ip ttl expired reassem"
  138.       Case IP_PARAM_PROBLEM:         msg = "ip param_problem"
  139.       Case IP_SOURCE_QUENCH:         msg = "ip source quench"
  140.       Case IP_OPTION_TOO_BIG:        msg = "ip option too_big"
  141.       Case IP_BAD_DESTINATION:       msg = "ip bad destination"
  142.       Case IP_ADDR_DELETED:          msg = "ip addr deleted"
  143.       Case IP_SPEC_MTU_CHANGE:       msg = "ip spec mtu change"
  144.       Case IP_MTU_CHANGE:            msg = "ip mtu_change"
  145.       Case IP_UNLOAD:                msg = "ip unload"
  146.       Case IP_ADDR_ADDED:            msg = "ip addr added"
  147.       Case IP_GENERAL_FAILURE:       msg = "ip general failure"
  148.       Case IP_PENDING:               msg = "ip pending"
  149.       Case PING_TIMEOUT:             msg = "ping timeout"
  150.       Case Else:                     msg = "unknown  msg returned"
  151.    End Select
  152.    
  153.    GetStatusCode = CStr(status) & "   [ " & msg & " ]"
  154.    
  155. End Function
  156.  
  157.  
  158. Public Function HiByte(ByVal wParam As Integer)
  159.  
  160.     HiByte = wParam \ &H100 And &HFF&
  161.  
  162. End Function
  163.  
  164.  
  165. Public Function LoByte(ByVal wParam As Integer)
  166.  
  167.     LoByte = wParam And &HFF&
  168.  
  169. End Function
  170.  
  171.  
  172. Public Function Ping(szAddress As String, ECHO As ICMP_ECHO_REPLY) As Long
  173.  
  174.    Dim hPort As Long
  175.    Dim dwAddress As Long
  176.    Dim sDataToSend As String
  177.    Dim iOpt As Long
  178.    
  179.    sDataToSend = "Echo This"
  180.    dwAddress = AddressStringToLong(szAddress)
  181.    
  182.    Call SocketsInitialize
  183.    hPort = IcmpCreateFile()
  184.    
  185.    If IcmpSendEcho(hPort, _
  186.                    dwAddress, _
  187.                    sDataToSend, _
  188.                    Len(sDataToSend), _
  189.                    0, _
  190.                    ECHO, _
  191.                    Len(ECHO), _
  192.                    PING_TIMEOUT) Then
  193.    
  194.         'the ping succeeded,
  195.         '.Status will be 0
  196.         '.RoundTripTime is the time in ms for
  197.         '               the ping to complete,
  198.         '.Data is the data returned (NULL terminated)
  199.         '.Address is the Ip address that actually replied
  200.         '.DataSize is the size of the string in .Data
  201.          Ping = ECHO.RoundTripTime
  202.    Else: Ping = ECHO.status * -1
  203.    End If
  204.                        
  205.    Call IcmpCloseHandle(hPort)
  206.    Call SocketsCleanup
  207.    
  208. End Function
  209.    
  210.  
  211. Function AddressStringToLong(ByVal tmp As String) As Long
  212.  
  213.    Dim i As Integer
  214.    Dim parts(1 To 4) As String
  215.    
  216.    i = 0
  217.    
  218.   'we have to extract each part of the
  219.   '123.456.789.123 string, delimited by
  220.   'a period
  221.    While InStr(tmp, ".") > 0
  222.       i = i + 1
  223.       parts(i) = Mid(tmp, 1, InStr(tmp, ".") - 1)
  224.       tmp = Mid(tmp, InStr(tmp, ".") + 1)
  225.    Wend
  226.    
  227.    i = i + 1
  228.    parts(i) = tmp
  229.    
  230.    If i <> 4 Then
  231.       AddressStringToLong = 0
  232.       Exit Function
  233.    End If
  234.    
  235.   'build the long value out of the
  236.   'hex of the extracted strings
  237.    AddressStringToLong = Val("&H" & Right("00" & Hex(parts(4)), 2) & _
  238.                          Right("00" & Hex(parts(3)), 2) & _
  239.                          Right("00" & Hex(parts(2)), 2) & _
  240.                          Right("00" & Hex(parts(1)), 2))
  241.    
  242. End Function
  243.  
  244.  
  245. Public Function SocketsCleanup() As Boolean
  246.  
  247.     Dim X As Long
  248.     
  249.     X = WSACleanup()
  250.     
  251.     If X <> 0 Then
  252.         MsgBox "Windows Sockets error " & Trim$(Str$(X)) & _
  253.                " occurred in Cleanup.", vbExclamation
  254.         SocketsCleanup = False
  255.     Else
  256.         SocketsCleanup = True
  257.     End If
  258.     
  259. End Function
  260.  
  261.  
  262. Public Function SocketsInitialize() As Boolean
  263.  
  264.     Dim WSAD As WSADATA
  265.     Dim X As Integer
  266.     Dim szLoByte As String, szHiByte As String, szBuf As String
  267.     
  268.     X = WSAStartup(WS_VERSION_REQD, WSAD)
  269.     
  270.     If X <> 0 Then
  271.         MsgBox "Windows Sockets for 32 bit Windows " & _
  272.                "environments is not successfully responding."
  273.         SocketsInitialize = False
  274.         Exit Function
  275.     End If
  276.     
  277.     If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or _
  278.        (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And _
  279.         HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
  280.         
  281.         szHiByte = Trim$(Str$(HiByte(WSAD.wVersion)))
  282.         szLoByte = Trim$(Str$(LoByte(WSAD.wVersion)))
  283.         szBuf = "Windows Sockets Version " & szLoByte & "." & szHiByte
  284.         szBuf = szBuf & " is not supported by Windows " & _
  285.                           "Sockets for 32 bit Windows environments."
  286.         MsgBox szBuf, vbExclamation
  287.         SocketsInitialize = False
  288.         Exit Function
  289.         
  290.     End If
  291.     
  292.     If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
  293.         szBuf = "This application requires a minimum of " & _
  294.                  Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets."
  295.         MsgBox szBuf, vbExclamation
  296.         SocketsInitialize = False
  297.         Exit Function
  298.     End If
  299.     
  300.     SocketsInitialize = True
  301.         
  302. End Function
  303.  
  304.  
  305.