Apzo Posted November 10, 2006 Share Posted November 10, 2006 Hello_IPInRange("192.168.10.12", "192.168.10.8/29") will tell you if 192.168.10.12 is in 192.168.10.8/29 for example.It uses CIDR notation (more details about CIDR notation here).Can be usefull when you want to add net control into your scripts Apzo.expandcollapse popup;~ function _IPRange() : checks if a given IP address is a given network (CIDR notation) ;~ $IP : 192.168.10.12 for example. Must be valid. ;~ $CIDR : 192.168.10.8/29 for example. Google CIDR or see http://www.pantz.org/networking/tcpip/subnetchart.shtml ;~ Return : true if $IP in $CIDR, false if not, sets @error if something wrong. Func _IPInRange($IP, $CIDR) Local $T = StringSplit($CIDR, "/") If @error Then SetError(1) Return false EndIf If $T[0] <> 2 Then SetError(2) Return false EndIf If Not _IsIP($IP) Then SetError(7) Return false EndIf If Not _IsIP($T[1]) Then SetError(7) Return false EndIf If $T[2] = 0 Then Return True Local $NETSTART = _IPToInt($T[1]) If @error Then SetError(3) Return false EndIf Local $BITMASK = 32 - $T[2] $NETSTART = BitShift($NETSTART, $BITMASK) $IP = _IPToInt($IP) $IP = BitShift($IP, $BITMASK) If @error Then SetError(4) Return false EndIf If $IP = $NETSTART Then Return true Return false EndFunc ;~ function _IPToInt() : converts a quad dotted IP into an integer for further comparison. ;~ $IP : quad dotted notation. Ex : 192.168.10.12 ;~ Return : according integer if OK or sets @error Func _IPToInt($IP) If Not _IsIP($IP) Then SetError(7) Return false EndIf Local $I = StringSplit($IP, ".") If @error Then SetError(5) Return 0 EndIf Return BitShift($I[1], -12) + BitShift($I[2], -8) + BitShift($I[3], -4) + $I[4] EndFunc ;~ Checks if $IP is [0-255].[0-255].[0-255].[0-255] style Func _IsIP($IP) Local $T = StringSPlit($IP, ".") If $T[0] <> 4 Then Return false If Not StringIsInt($T[1]) Then Return false If Not StringIsInt($T[2]) Then Return false If Not StringIsInt($T[3]) Then Return false If Not StringIsInt($T[4]) Then Return false If $T[1] < 0 Or $T[1] > 255 Then Return false If $T[2] < 0 Or $T[2] > 255 Then Return false If $T[3] < 0 Or $T[3] > 255 Then Return false If $T[4] < 0 Or $T[4] > 255 Then Return false Return true EndFunc All the pop3 functions.Rsync your files on your USB key (or anywhere else) Link to comment Share on other sites More sharing options...
Schlumpf Posted November 10, 2006 Share Posted November 10, 2006 it don't works for me... please make a samplescript [CENTER]Sorry for my bad English... ;)[/CENTER] Link to comment Share on other sites More sharing options...
nitekram Posted November 10, 2006 Share Posted November 10, 2006 it don't works for me... please make a samplescript The example that was given should work - copy everything in the code box and then add this to the top.$test = _IPInRange("192.168.10.12", "192.168.10.8/29")msgbox('','0',$test) 2¢ All by me:"Sometimes you have to go back to where you started, to get to where you want to go." "Everybody catches up with everyone, eventually" "As you teach others, you are really teaching yourself." From my dad "Do not worry about yesterday, as the only thing that you can control is tomorrow."  WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2 AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit Docs SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language Programming Tips Excel Changes ControlHover.UDF GDI_Plus Draw_On_Screen GDI Basics GDI_More_Basics GDI Rotate GDI Graph GDI  CheckExistingItems GDI Trajectory Replace $ghGDIPDll with $__g_hGDIPDll DLL 101? Array via Object GDI Swimlane GDI Plus French 101 Site GDI Examples UEZ GDI Basic Clock GDI Detection Ternary operator Link to comment Share on other sites More sharing options...
ConsultingJoe Posted November 11, 2006 Share Posted November 11, 2006 It worked for me but I dont quite understand, what is the range of 8/29? Check out ConsultingJoe.com Link to comment Share on other sites More sharing options...
/dev/null Posted November 11, 2006 Share Posted November 11, 2006 It worked for me but I dont quite understand, what is the range of 8/29?http://jodies.de/ipcalc?host=192.168.10.8&...1=29&mask2=CheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
ConsultingJoe Posted November 11, 2006 Share Posted November 11, 2006 http://jodies.de/ipcalc?host=192.168.10.8&...1=29&mask2=CheersKurtyeah but what is this used for? Check out ConsultingJoe.com Link to comment Share on other sites More sharing options...
/dev/null Posted November 11, 2006 Share Posted November 11, 2006 (edited) yeah but what is this used for?erm, networking... you know the tcp/ip stuff and the like... ??? EDIT: O.K. I guess you don't know the concept of subnetting, right? Then read this: http://en.wikipedia.org/wiki/SubnettingCheersKurt Edited November 11, 2006 by /dev/null __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
Apzo Posted November 13, 2006 Author Share Posted November 13, 2006 My apologies, I didn't even gave a sample script The one from nitekram is good, thanks ! So this is a more complex (and useful) example, allowing connexion only from local addresses. Global $sock While 1 $sock = TCPAccept("0.0.0.0") If $sock >= 0 Then If _IPInRange(SocketToIP($sock), "129.168.0.0/16") Then Local $recv= TCPRecv($sock, 2048) ;~ do some stuff with $recv EndIf EndIf TCPCloseSocket($sock) Wend Func SocketToIP($SHOCKET) Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]") Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _ "ptr",DLLStructGetPtr($sockaddr),"int_ptr",DLLStructGetSize($sockaddr)) If Not @error And $aRet[0] = 0 Then $aRet = DLLCall("Ws2_32.dll","str","inet_ntoa","int",DLLStructGetData($sockaddr,3)) If Not @error Then $aRet = $aRet[0] Else $aRet = 0 EndIf $sockaddr = 0 Return $aRet EndFunc This way, the socket is closed if not in the range 192.168.0.0/16, aka 192.168.0.0 netmask 255.255.0.0 If in range, TcpRecv will be performed. Apzo. All the pop3 functions.Rsync your files on your USB key (or anywhere else) Link to comment Share on other sites More sharing options...
newcoder Posted December 12, 2008 Share Posted December 12, 2008 This script was submitted a couple of years ago and looks great, but I'm finding its accuracy to be off a bit and its been way too long since doing any bitshift's to figure out why.The test will show True for addresses in the scope, but also returns true for items outside that should be False. The following should be False as the IP is outside of the scope 192.168.2.0-192.168.2.255$test = _IPInRange("192.168.1.105", "192.168.2.0/24")Tests with some smaller subnets such as /29 seemed to work, but /25 and larger subnets failed. Does anyone know where the error is in this script or know of another way to determine if an IP is within a certain range? Link to comment Share on other sites More sharing options...
SkinnyWhiteGuy Posted December 12, 2008 Share Posted December 12, 2008 Kinda an old topic, but I took a quick look at it (BitWise operators get me every time). I think the problem was in this _IPToInt() function. He was only shifting the bytes (which are 8 bits each) 4 bits, so he was mixing stuff up that shouldn't be mixed. He was also just adding them, which normally is fine, but BitOR is a better way to do it, and will make sure the other bit shifts play nice as well. Here's the corrected version, which makes your above test show False, as it should. Func _IPToInt($IP) If Not _IsIP($IP) Then SetError(7) Return false EndIf Local $I = StringSplit($IP, ".") If @error Then SetError(5) Return 0 EndIf Return BitOR(BitShift($I[1], -24), BitShift($I[2], -16), BitShift($I[3], -8), $I[4]) EndFunc shaqan 1 Link to comment Share on other sites More sharing options...
newcoder Posted December 12, 2008 Share Posted December 12, 2008 Here's the corrected version, which makes your above test show False, as it should.Outstanding - works great. Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now