Jump to content
Sign in to follow this  
slaughter

TCP/ IP Filter

Recommended Posts

slaughter

Hello, since i finished my project VNC Proxy I run in another problem. I nead to make function witch would filter ip address like a firewall if address is listed in array should be accepted if not rejected Function can return 1 or 0 it doesn't mater. It should understand the wild card like * (that all addresses in this field is right)

alowed_ip.txt

Allowed ip:
192.168.1.*
192.168.2.*
88.16.22.33
88.16.22.34
88.16.22.35

Funtion

_FileReadToArray("alowed_ip.txt",$vnc_alowed)

Func Match_ip($a_ip)
$a_arr = StringSplit($a_ip,".")
$Ret = 1

For $r = 1 to UBound($vnc_alowed)-1
$b_arr = StringSplit($vnc_alowed[$r],".")
    if $a_arr[1] <> $b_arr[1] OR $a_arr[1] <> "*" Then
        $Ret = -1
    EndIf
    if $a_arr[2] <> $b_arr[2] OR $a_arr[2] <> "*" Then
        $Ret = -1
    EndIf
    if $a_arr[3] <> $b_arr[3] OR $a_arr[3] <> "*" Then
        $Ret = -1
    EndIf
    if $a_arr[4] <> $b_arr[4] OR $a_arr[4] <> "*" Then
        $Ret = -1
    EndIf

Next

Return $Ret
EndFunc

Edited by slaughter

Share this post


Link to post
Share on other sites
slaughter

;mattches ip A_ip is with wild card or simple
;B_ip is given ip
;if ip matches returns 1 if not returns -1 and sets @error
Func Match_ip($a_ip, $b_ip)
$a_arr = StringSplit($a_ip,".")
$b_arr = StringSplit($b_ip,".")
$Ret = 0

        if $a_arr[1] <> "*" Then
        if $a_arr[1] <> $b_arr[1] Then
            $Ret = -1
        EndIf
    EndIf

        if $a_arr[2] <> "*" Then
        if $a_arr[2] <> $b_arr[2] Then
            $Ret = -1
        EndIf
    EndIf


        if $a_arr[3] <> "*" Then
        if $a_arr[3] <> $b_arr[3] Then
            $Ret = -1
        EndIf
    EndIf

        if $a_arr[4] <> "*" Then
        if $a_arr[4] <> $b_arr[4] Then
            $Ret = -1
        EndIf
    EndIf

If $Ret = -1 Then
    SetError(1)
EndIf

Return $Ret
EndFunc

$ip1 = "192.168.1.*"
$ip2 = "192.168.1.110"
Match_ip($ip1, $ip2)
if @error Then MsgBox(0, "ERROR", "IP dosent match")

Edited by slaughter

Share this post


Link to post
Share on other sites
slaughter

alowed_ip.txt

192.168.1.*
127.0.0.1

Code

#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <array.au3>
#include <inet.au3>
#include <String.au3>
#Include <File.au3>
#include <GuiListView.au3>
#include <Date.au3>
#include <Timers.au3>
Opt('MustDeclareVars', 0)




;mattches ip A_ip is with wild card or simple
;B_ip is given ip
;if ip matches returns 1 if not returns -1
Func Match_ip($a_ip, $b_ip)
$a_arr = StringSplit($a_ip,".")
$b_arr = StringSplit($b_ip,".")
$Ret = 0

        if $a_arr[1] <> "*" Then
        if $a_arr[1] <> $b_arr[1] Then
            $Ret = -1
        EndIf
    EndIf

        if $a_arr[2] <> "*" Then
        if $a_arr[2] <> $b_arr[2] Then
            $Ret = -1
        EndIf
    EndIf


        if $a_arr[3] <> "*" Then
        if $a_arr[3] <> $b_arr[3] Then
            $Ret = -1
        EndIf
    EndIf

        if $a_arr[4] <> "*" Then
        if $a_arr[4] <> $b_arr[4] Then
            $Ret = -1
        EndIf
    EndIf

If $Ret = -1 Then
    SetError(1)
EndIf

Return $Ret
EndFunc


Func match_ip_array($ip_array, $ip)
For $x=1 to UBound($ip_array)-1
$check = Match_ip($ip_array[$x], $ip)
if Not @error Then
    Return 1
EndIf
Next
SetError(1)
$err = -1
Return $err

EndFunc


$ip2 = "192.168.2.110"


Dim $vnc_alowed
_FileReadToArray("alowed_ip.txt",$vnc_alowed)
match_ip_array($vnc_alowed, $ip2)
if @error Then MsgBox(0, "ERROR", "IP is not in list to accept")

Now working. There is 2 functions

Match_ip($a_ip, $b_ip)

Matches single ip for wildcard.

$a_ip can be static or with wildcard (192.168.1.1 or wit subnet 192.168.1.* this includ all 255 adreses)

$b_ip is ip witch you give to check

If it is false function set @error an retunrs -1

match_ip_array($ip_array, $ip2)

Does almost as match_ip just goes over ip array if even one ip in list matches given retuns 1 if not retunrs -1 and sets @error

Edited by slaughter

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.