Results 1 to 6 of 6

Thread: Finding The ISP [RESOLVED]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Location
    Scotland
    Posts
    123

    Resolved Finding The ISP [RESOLVED]

    i am writing a program and on the form there will be a txt box and a cmd but.

    When the command button is pressed it will go and find what the isp is for the ip address typed into the txt box is and then return it on a label.

    The only problem i have is i have absolutely no code and dont have a clue where to start so much help would be appreciated.

    Happy posting
    Last edited by Aaron Smith; Jul 18th, 2005 at 02:25 PM.
    Regards
    Aaron Smith

    Did my post help you, rate my post

    When Quoting VB Code use [vbcode][/vbcode]

    When your problem has been resolved change to *subject* [RESOLVED]

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Re: Finding The ISP

    just use the GetHostFromIP function with this module..But take note this only returns the registered host for the IP. It doesn't necessarily mean its the users ISP. There is no way to get that otherwise.
    Attached Files Attached Files
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Location
    Scotland
    Posts
    123

    Re: Finding The ISP

    thanks for replying but as usual i forgot to mension i cannot accept files as i am using a publicly shared computer so could you please copy and paste the module into a reply plz

    Thanks
    Regards
    Aaron Smith

    Did my post help you, rate my post

    When Quoting VB Code use [vbcode][/vbcode]

    When your problem has been resolved change to *subject* [RESOLVED]

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Re: Finding The ISP

    VB Code:
    1. Option Explicit
    2. Public Enum IP_STATUS
    3.     IP_STATUS_BASE = 11000
    4.     IP_SUCCESS = 0
    5.     IP_BUF_TOO_SMALL = (11000 + 1)
    6.     IP_DEST_NET_UNREACHABLE = (11000 + 2)
    7.     IP_DEST_HOST_UNREACHABLE = (11000 + 3)
    8.     IP_DEST_PROT_UNREACHABLE = (11000 + 4)
    9.     IP_DEST_PORT_UNREACHABLE = (11000 + 5)
    10.     IP_NO_RESOURCES = (11000 + 6)
    11.     IP_BAD_OPTION = (11000 + 7)
    12.     IP_HW_ERROR = (11000 + 8)
    13.     IP_PACKET_TOO_BIG = (11000 + 9)
    14.     IP_REQ_TIMED_OUT = (11000 + 10)
    15.     IP_BAD_REQ = (11000 + 11)
    16.     IP_BAD_ROUTE = (11000 + 12)
    17.     IP_TTL_EXPIRED_TRANSIT = (11000 + 13)
    18.     IP_TTL_EXPIRED_REASSEM = (11000 + 14)
    19.     IP_PARAM_PROBLEM = (11000 + 15)
    20.     IP_SOURCE_QUENCH = (11000 + 16)
    21.     IP_OPTION_TOO_BIG = (11000 + 17)
    22.     IP_BAD_DESTINATION = (11000 + 18)
    23.     IP_ADDR_DELETED = (11000 + 19)
    24.     IP_SPEC_MTU_CHANGE = (11000 + 20)
    25.     IP_MTU_CHANGE = (11000 + 21)
    26.     IP_UNLOAD = (11000 + 22)
    27.     IP_ADDR_ADDED = (11000 + 23)
    28.     IP_GENERAL_FAILURE = (11000 + 50)
    29.     MAX_IP_STATUS = 11000 + 50
    30.     IP_PENDING = (11000 + 255)
    31.     PING_TIMEOUT = 200
    32. End Enum
    33. Private Const MAX_WSADescription = 256
    34. Private Const MAX_WSASYSStatus = 128
    35. Private Const ERROR_SUCCESS       As Long = 0
    36. Private Const WS_VERSION_REQD     As Long = &H101
    37. Private Const WS_VERSION_MAJOR    As Long = _
    38.     WS_VERSION_REQD \ &H100 And &HFF&
    39. Private Const WS_VERSION_MINOR    As Long = _
    40.    WS_VERSION_REQD And &HFF&
    41. Private Const MIN_SOCKETS_REQD    As Long = 1
    42. Private Const SOCKET_ERROR        As Long = -1
    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 Long  'formerly integer
    59.   '  Reserved        As Integer
    60.     DataPointer     As Long
    61.     Options         As ICMP_OPTIONS
    62.     Data            As String * 250
    63. End Type
    64.  
    65. Private 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. Private 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.  
    85. 'OK, this one is a bit more complicated. First, change the declaration of
    86. 'gethostbyaddr to:
    87. Private Declare Function gethostbyaddr Lib "wsock32.dll" (ByRef dwHost As Long, ByVal hLen As Integer, ByVal aType As Integer) As Long
    88. Private Declare Function inet_addr Lib "wsock32.dll" (ByVal szHost As String) As Long
    89. Private Declare Function lstrlen Lib "kernel32" (ByVal lpString As Long) As Long
    90.  
    91. Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
    92. Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal IcmpHandle As Long) As Long
    93. Private Declare Function IcmpSendEcho Lib "icmp.dll" _
    94.    (ByVal IcmpHandle As Long, _
    95.     ByVal DestinationAddress As Long, _
    96.     ByVal RequestData As String, _
    97.     ByVal RequestSize As Long, _
    98.     ByVal RequestOptions As Long, _
    99.     ReplyBuffer As ICMP_ECHO_REPLY, _
    100.     ByVal ReplySize As Long, _
    101.     ByVal TimeOut As Long) As Long
    102.  
    103. Private Declare Function WSAGetLastError Lib "wsock32.dll" () As Long
    104. Private Declare Function WSAStartup Lib "wsock32.dll" _
    105.         (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long
    106. Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
    107. Private Declare Function gethostname Lib "wsock32.dll" _
    108.         (ByVal szHost As String, ByVal dwHostLen As Long) As Long
    109. Private Declare Function gethostbyname Lib "wsock32.dll" (ByVal szHost As String) As Long
    110.  
    111. 'Private Declare Function gethostbyaddr Lib "wsock32.dll" _
    112. '        (ByVal szHost As String, ByVal hLen As Integer, _
    113. '         ByVal aType As Integer) As Long
    114.  
    115. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
    116.  
    117.  
    118. Private Function HiByte(ByVal wParam As Integer)
    119.    
    120. HiByte = wParam \ &H1 And &HFF&
    121.  
    122. End Function
    123.  
    124. Private Function LoByte(ByVal wParam As Integer)
    125.    
    126. LoByte = wParam And &HFF&
    127.  
    128. End Function
    129.  
    130. Private Sub SocketsCleanup()
    131.    
    132. If WSACleanup() <> ERROR_SUCCESS Then
    133.     App.LogEvent "Socket error occurred in Cleanup.", _
    134.     vbLogEventTypeError
    135. End If
    136.  
    137. End Sub
    138.  
    139. Private Function SocketsInitialize(Optional sErr As String) As Boolean
    140.  
    141. Dim WSAD As WSADATA, sLoByte As String, sHiByte As String
    142.    
    143. If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
    144.     sErr = "The 32-bit Windows Socket is not responding."
    145.     SocketsInitialize = False
    146.     Exit Function
    147. End If
    148.  
    149. If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
    150.     sErr = "This application requires a minimum of " & _
    151.             CStr(MIN_SOCKETS_REQD) & " supported sockets."
    152.  
    153.     SocketsInitialize = False
    154.     Exit Function
    155. End If
    156.  
    157.  
    158. If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or _
    159.         (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And _
    160.         HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
    161.  
    162.     sHiByte = CStr(HiByte(WSAD.wVersion))
    163.     sLoByte = CStr(LoByte(WSAD.wVersion))
    164.  
    165.     sErr = "Sockets version " & sLoByte & "." & sHiByte & _
    166.             " is not supported by 32-bit Windows Sockets."
    167.  
    168.     SocketsInitialize = False
    169.     Exit Function
    170. End If
    171. SocketsInitialize = True
    172.  
    173. End Function
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Re: Finding The ISP

    part 2

    VB Code:
    1. Private Function DoPing(szAddress As String, sDataToSend As String, ECHO As ICMP_ECHO_REPLY, Optional TimeOut As Long = PING_TIMEOUT) As Long
    2.  
    3. Dim hPort As Long, dwAddress As Long, iOpt As Long
    4.     dwAddress = AddressStringToLong(szAddress)
    5.    
    6. hPort = IcmpCreateFile()
    7.  
    8. If IcmpSendEcho(hPort, dwAddress, sDataToSend, Len(sDataToSend), 0, _
    9.     ECHO, Len(ECHO), TimeOut) Then
    10.     'the ping succeeded,
    11.     '.Status will be 0
    12.     '.RoundTripTime is the time in ms for
    13.     '               the ping to complete,
    14.     '.Data is the data returned (NULL terminated)
    15.     '.Address is the Ip address that actually replied
    16.     '.DataSize is the size of the string in .Data
    17.     DoPing = IP_SUCCESS
    18. Else
    19.     If ECHO.Status = 0 Then
    20.         DoPing = -1
    21.     Else
    22.         DoPing = ECHO.Status * -1
    23.     End If
    24. End If
    25.                        
    26. Call IcmpCloseHandle(hPort)
    27.  
    28. End Function
    29.    
    30. Private Function AddressStringToLong(ByVal tmp As String) As Long
    31.  
    32. Dim i As Integer, parts(1 To 4) As String
    33. i = 0
    34. 'we have to extract each part of the
    35. '123.456.789.123 string, delimited by
    36. 'a period
    37. While InStr(tmp, ".") > 0
    38.     i = i + 1
    39.     parts(i) = Mid(tmp, 1, InStr(tmp, ".") - 1)
    40.     tmp = Mid(tmp, InStr(tmp, ".") + 1)
    41. Wend
    42.    
    43. i = i + 1
    44. parts(i) = tmp
    45.    
    46. If i <> 4 Then
    47.     AddressStringToLong = 0
    48.     Exit Function
    49. End If
    50.    
    51. 'build the long value out of the
    52. 'hex of the extracted strings
    53. AddressStringToLong = Val("&H" & Right("00" & Hex(parts(4)), 2) & _
    54.                      Right("00" & Hex(parts(3)), 2) & _
    55.                      Right("00" & Hex(parts(2)), 2) & _
    56.                      Right("00" & Hex(parts(1)), 2))
    57.    
    58. End Function
    59.  
    60. Public Function GetStatusCode(Status As IP_STATUS) As String
    61.  
    62. Dim msg As String
    63.    Select Case Status
    64.       Case IP_SUCCESS:               msg = "IP Success"
    65.       Case IP_BUF_TOO_SMALL:         msg = "IP Buffer too small"
    66.       Case IP_DEST_NET_UNREACHABLE:  msg = "IP Destination Net Unreachable"
    67.       Case IP_DEST_HOST_UNREACHABLE: msg = "IP Destination Host Unreachable"
    68.       Case IP_DEST_PROT_UNREACHABLE: msg = "IP Destination Protocol Unreachable"
    69.       Case IP_DEST_PORT_UNREACHABLE: msg = "IP Destination Port Unreachable"
    70.       Case IP_NO_RESOURCES:          msg = "IP No Resources"
    71.       Case IP_BAD_OPTION:            msg = "IP Bad Option"
    72.       Case IP_HW_ERROR:              msg = "IP Hardware Error"
    73.       Case IP_PACKET_TOO_BIG:        msg = "IP Packet too big"
    74.       Case IP_REQ_TIMED_OUT:         msg = "IP Reqquest timed out"
    75.       Case IP_BAD_REQ:               msg = "IP Bad Request"
    76.       Case IP_BAD_ROUTE:             msg = "IP Bad Route"
    77.       Case IP_TTL_EXPIRED_TRANSIT:   msg = "IP TTL Expired Transit"
    78.       Case IP_TTL_EXPIRED_REASSEM:   msg = "IP TTL Expired Reassem"
    79.       Case IP_PARAM_PROBLEM:         msg = "IP Parameter Problem"
    80.       Case IP_SOURCE_QUENCH:         msg = "IP Source Quench"
    81.       Case IP_OPTION_TOO_BIG:        msg = "IP Option too big"
    82.       Case IP_BAD_DESTINATION:       msg = "IP Bad Destination"
    83.       Case IP_ADDR_DELETED:          msg = "IP Address Deleted"
    84.       Case IP_SPEC_MTU_CHANGE:       msg = "IP Spec MTU Change"
    85.       Case IP_MTU_CHANGE:            msg = "IP MTU Change"
    86.       Case IP_UNLOAD:                msg = "IP Unload"
    87.       Case IP_ADDR_ADDED:            msg = "IP Address Added"
    88.       Case IP_GENERAL_FAILURE:       msg = "IP General Failure"
    89.       Case IP_PENDING:               msg = "IP Pending"
    90.       Case PING_TIMEOUT:             msg = "Ping timeout"
    91.       Case -1:                       msg = "Destination host unreachable."
    92.       Case Else:                     msg = "Unknown message returned"
    93.    End Select
    94. Debug.Print Status
    95.  
    96.    GetStatusCode = msg
    97.    
    98. End Function
    99.  
    100. Public Function GetIPAddress(Optional sHost As String, _
    101. Optional serrmsg As String) As String
    102.  
    103. 'Resolves the host-name (or current machine if balnk) to an IP address
    104. Dim sHostName   As String * 256
    105. Dim lpHost      As Long
    106. Dim HOST        As HOSTENT
    107. Dim dwIPAddr    As Long
    108. Dim tmpIPAddr() As Byte
    109. Dim i           As Integer
    110. Dim sIPAddr     As String
    111. Dim werr        As Long
    112.  
    113.     If Not SocketsInitialize() Then
    114.         GetIPAddress = ""
    115.         Exit Function
    116.     End If
    117.    
    118.     If sHost = "" Then
    119.         If gethostname(sHostName, 256) = SOCKET_ERROR Then
    120.             werr = WSAGetLastError()
    121.             GetIPAddress = ""
    122.             serrmsg = "Windows Sockets error " & Str$(werr) & _
    123.                 " has occurred. Unable to successfully get Host Name." & vbCrLf
    124.             GetIPAddress = ""
    125.            
    126.             SocketsCleanup
    127.             Exit Function
    128.         End If
    129.  
    130.         sHostName = Trim$(sHostName)
    131.     Else
    132.         sHostName = Trim$(sHost) & Chr$(0)
    133.     End If
    134.    
    135.     lpHost = gethostbyname(sHostName)
    136.  
    137.     If lpHost = 0 Then
    138.         werr = WSAGetLastError()
    139.         GetIPAddress = ""
    140.         serrmsg = "Windows Sockets error " & Str$(werr) & _
    141.                 " has occurred. Unable to successfully get Host Name." & vbCrLf
    142.         GetIPAddress = ""
    143.        
    144.         SocketsCleanup
    145.         Exit Function
    146.     End If
    147.  
    148.     CopyMemory HOST, lpHost, Len(HOST)
    149.     CopyMemory dwIPAddr, HOST.hAddrList, 4
    150.  
    151.     ReDim tmpIPAddr(1 To HOST.hLen)
    152.     CopyMemory tmpIPAddr(1), dwIPAddr, HOST.hLen
    153.  
    154.     For i = 1 To HOST.hLen
    155.         sIPAddr = sIPAddr & tmpIPAddr(i) & "."
    156.     Next
    157.  
    158.     GetIPAddress = Mid$(sIPAddr, 1, Len(sIPAddr) - 1)
    159.  
    160.     SocketsCleanup
    161. End Function
    162.  
    163. Public Function GetIPHostName() As String
    164. 'Returns the current machine's name
    165. Dim sHostName As String * 256
    166.  
    167.     If Not SocketsInitialize() Then
    168.         GetIPHostName = ""
    169.         Exit Function
    170.     End If
    171.  
    172.     If gethostname(sHostName, 256) = SOCKET_ERROR Then
    173.         GetIPHostName = ""
    174.         MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & _
    175.                 " has occurred.  Unable to successfully get Host Name."
    176.         SocketsCleanup
    177.         Exit Function
    178.     End If
    179.  
    180.     GetIPHostName = Left$(sHostName, InStr(sHostName, Chr(0)) - 1)
    181.     SocketsCleanup
    182. End Function
    183.  
    184. Public Function Ping(Address As String, RoundTripTime As String, DataMatch As Boolean, Optional DataSize As Long = 32, Optional TimeOut As Long = PING_TIMEOUT) As Long
    185. Dim ECHO As ICMP_ECHO_REPLY, pos As Integer, Dt As String, sAddress As String
    186. On Error GoTo DPErr
    187.     If AddressStringToLong(Address) = 0 Then
    188.         sAddress = GetIPAddress(Address)
    189.     Else
    190.         sAddress = Address
    191.     End If
    192.    
    193.     If SocketsInitialize() Then
    194.         If DataSize <= 0 Then DataSize = 10
    195.         For pos = 1 To DataSize
    196.             Dt = Dt & Chr$(Rnd() * 254 + 1)
    197.         Next pos
    198.        
    199.         'ping an ip address, passing the
    200.         'address and the ECHO structure
    201.         Ping = DoPing(sAddress, Dt, ECHO, TimeOut)
    202.        
    203.         'display the results from the ECHO structure
    204.         RoundTripTime = ECHO.RoundTripTime & " ms"
    205.        
    206.         'DataSize = ECHO.DataSize & " bytes"
    207.      
    208.         If Left$(ECHO.Data, 1) <> Chr$(0) Then
    209.             pos = InStr(ECHO.Data, Chr$(0))
    210.             DataMatch = (Left$(ECHO.Data, pos - 1) = Dt)
    211.         End If
    212.    
    213.         SocketsCleanup
    214.     Else
    215.         Ping = IP_GENERAL_FAILURE
    216.     End If
    217.     Exit Function
    218. DPErr:
    219.     Ping = IP_GENERAL_FAILURE
    220. End Function
    221.  
    222.  
    223.  
    224. Private Function PointerToString(lpString As Long) As String
    225.            
    226. ' The PointerToString function is used to convert a
    227. ' pointer to a string into a string variable:
    228.            
    229. Dim Buffer() As Byte
    230. Dim nLen As Long
    231.  
    232.            If lpString Then
    233.               nLen = lstrlen(lpString)
    234.               If nLen Then
    235.                  ReDim Buffer(0 To (nLen - 1)) As Byte
    236.                  CopyMemory Buffer(0), ByVal lpString, nLen
    237.                  PointerToString = StrConv(Buffer, vbUnicode)
    238.               End If
    239.            End If
    240.  
    241. End Function
    242.  
    243.  
    244.        
    245. Public Function GetHostFromIP(sIPAddr As String, Optional serrmsg As String) As String
    246.        
    247. ' Finally, the GetHostFromIP function returns the host name
    248. ' from an IP address string:
    249.        
    250.         'Resolves the IP address to a host name
    251.         Dim dwIPAddr    As Long
    252.         Dim lpHost      As Long
    253.         Dim HOST        As HOSTENT
    254.         Dim werr        As Long
    255.  
    256.             If Not SocketsInitialize() Then
    257.                 GetHostFromIP = ""
    258.                 Exit Function
    259.             End If
    260.        
    261.             dwIPAddr = inet_addr(sIPAddr)
    262.             lpHost = gethostbyaddr(dwIPAddr, Len(dwIPAddr), 2)
    263.  
    264.             If lpHost = 0 Then
    265.                 werr = WSAGetLastError()
    266.                 serrmsg = "Windows Sockets error " & Str$(werr) & _
    267.                   " has occurred. Unable to successfully get Host Name." & vbCrLf
    268.                 GetHostFromIP = ""
    269.        
    270.                 SocketsCleanup
    271.                 Exit Function
    272.             End If
    273.  
    274.             CopyMemory HOST, lpHost, Len(HOST)
    275.             GetHostFromIP = PointerToString(HOST.hName)
    276.        
    277.             SocketsCleanup
    278. End Function
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Location
    Scotland
    Posts
    123

    Re: Finding The ISP

    thank you after i do a bit of tweaking to the module then it will work the way i wish

    Thanks again

    Aaron
    Regards
    Aaron Smith

    Did my post help you, rate my post

    When Quoting VB Code use [vbcode][/vbcode]

    When your problem has been resolved change to *subject* [RESOLVED]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width