home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / aolcha1a / pingip.bas < prev   
Encoding:
BASIC Source File  |  1999-09-24  |  9.4 KB  |  307 lines

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