Results 1 to 3 of 3

Thread: check ip address

  1. #1

    Thread Starter
    Addicted Member Xdream's Avatar
    Join Date
    Mar 2001
    Location
    Switzerland
    Posts
    194

    check ip address

    Hi

    I'm creating a kind of messaging program where I have a predefined subnet mask and a standard gateway. How can I now check if a certain IP adress is within the the range defined by subnet mask and gateway?

    e.g. Subnet mask: 255.255.255.0
    Gateway: 192.168.1.254

    Check if IP adress 192.168.1.168 is within the range by code?

    thanks and regards
    xdream

  2. #2
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    I've the code for the entire form posted below. With this form you can perform a manual check. It assumes that both IP addresses and the subnet mask are provided in text strings.

    VB Code:
    1. VERSION 5.00
    2. Begin VB.Form frmMain
    3.    BorderStyle     =   1  'Fixed Single
    4.    Caption         =   "IP Checker"
    5.    ClientHeight    =   2070
    6.    ClientLeft      =   45
    7.    ClientTop       =   330
    8.    ClientWidth     =   3240
    9.    LinkTopic       =   "Form1"
    10.    MaxButton       =   0   'False
    11.    MinButton       =   0   'False
    12.    ScaleHeight     =   2070
    13.    ScaleWidth      =   3240
    14.    StartUpPosition =   3  'Windows Default
    15.    Begin VB.CommandButton cmdCheck
    16.       Caption         =   "Check IP"
    17.       Height          =   375
    18.       Left            =   1080
    19.       TabIndex        =   6
    20.       Top             =   1560
    21.       Width           =   1095
    22.    End
    23.    Begin VB.TextBox txtIP2
    24.       Height          =   285
    25.       Left            =   1800
    26.       TabIndex        =   2
    27.       Text            =   "192.168.1.1"
    28.       Top             =   1080
    29.       Width           =   1335
    30.    End
    31.    Begin VB.TextBox txtSubnetMask
    32.       Height          =   285
    33.       Left            =   1800
    34.       TabIndex        =   1
    35.       Text            =   "255.255.255.0"
    36.       Top             =   600
    37.       Width           =   1335
    38.    End
    39.    Begin VB.TextBox txtIP1
    40.       Height          =   285
    41.       Left            =   1800
    42.       TabIndex        =   0
    43.       Text            =   "192.168.1.168"
    44.       Top             =   240
    45.       Width           =   1335
    46.    End
    47.    Begin VB.Label lblIP2
    48.       Caption         =   "Second IP Number"
    49.       Height          =   255
    50.       Left            =   120
    51.       TabIndex        =   5
    52.       Top             =   1080
    53.       Width           =   1575
    54.    End
    55.    Begin VB.Label lblSubnetMask
    56.       Caption         =   "Subnet Mask"
    57.       Height          =   255
    58.       Left            =   120
    59.       TabIndex        =   4
    60.       Top             =   600
    61.       Width           =   1575
    62.    End
    63.    Begin VB.Label lblIP1
    64.       Caption         =   "First IP Number"
    65.       Height          =   255
    66.       Left            =   120
    67.       TabIndex        =   3
    68.       Top             =   240
    69.       Width           =   1575
    70.    End
    71. End
    72. Attribute VB_Name = "frmMain"
    73. Attribute VB_GlobalNameSpace = False
    74. Attribute VB_Creatable = False
    75. Attribute VB_PredeclaredId = True
    76. Attribute VB_Exposed = False
    77. Option Explicit
    78.  
    79. Private Sub cmdCheck_Click()
    80.   Dim lIP1, lIP2, lMask
    81.  
    82.   lIP1 = IPText2Value(txtIP1.Text)
    83.   lIP2 = IPText2Value(txtIP2.Text)
    84.  
    85.   'Beware, there is no check of the correctness of the mask!
    86.   'In binary form a correct subnet mask starts with a number of one's and is
    87.   'then follwed by a number of zero's.
    88.   lMask = IPText2Value(txtSubnetMask.Text)
    89.  
    90.   If lIP1 = 0 Or lIP2 = 0 Or lMask = 0 Then
    91.     'Something has gone wrong
    92.     MsgBox "One of the text boxes contains an invalid IP Address", vbCritical
    93.     Exit Sub
    94.   End If
    95.  
    96.   If (lIP1 And lMask) = (lIP2 And lMask) Then
    97.     MsgBox "Both addresses are within the same subnet!", vbInformation
    98.   Else
    99.     MsgBox "Both addresses are within different subnets!", vbExclamation
    100.   End If
    101.  
    102. End Sub
    103.  
    104. Private Function IPText2Value(p_sIPText As String) As Long
    105.   Dim asFields() As String
    106.   Dim i As Integer
    107.   Dim lIP As Long
    108.  
    109.   'Split the IP address in its respective values
    110.   asFields = Split(p_sIPText, ".")
    111.  
    112.   'Check if there are really three values
    113.   If UBound(asFields) <> 3 Then
    114.     Exit Function
    115.   End If
    116.  
    117.   'Check for numerical values, lower (0) and upper (255) bounds
    118.   For i = 0 To 3
    119.     If Not IsNumeric(asFields(i)) Then
    120.       Exit Function
    121.     End If
    122.    
    123.     'CLng will fail on non-numeric values, so there check is divided in two parts
    124.     If CLng(asFields(i)) < 0 Or CLng(asFields(i)) > 255 Then
    125.       Exit Function
    126.     End If
    127.    
    128.     'Overflow check
    129.     If i = 0 And CLng(asFields(i)) > 127 Then
    130.       lIP = lIP + ((CLng(asFields(i)) - 128) * 2 ^ ((3 - i) * 8) - 2 ^ 31)
    131.     Else
    132.       lIP = lIP + CLng(asFields(i)) * 2 ^ ((3 - i) * 8)
    133.     End If
    134.    
    135.   Next i
    136.  
    137.   IPText2Value = lIP
    138.  
    139.   Debug.Print Hex$(lIP)
    140.  
    141. End Function

  3. #3

    Thread Starter
    Addicted Member Xdream's Avatar
    Join Date
    Mar 2001
    Location
    Switzerland
    Posts
    194

    wow :)

    Hi riis

    I've just tried your form and it's really great! Thanks a lot, that was exactly what I was searching for, a simple check without a lot of code! Great!

    Best regards, Xdream

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