r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Core] Discuss core issues

Andreas
16-Apr-2010
[16341]
From a quick glance your masking logic is wrong. You need to mask 
both the address and the network and then compare if the results 
match.
Steeve
16-Apr-2010
[16342]
I guess it's in the last line::::
ip-as-integer = (ip-as-integer and mask-integer)
Andreas
16-Apr-2010
[16343x3]
I have a correct function for R3 lying around somewhere ... if only 
I could find it :)
Ah, got it.
cidr-match?: funct [address [tuple!] network [tuple!] bits [integer!]] 
[
    mask: skip to-binary (shift -1 32 - bits) 4
    (mask and to-binary address) = (mask and to-binary network)
]
Steeve
16-Apr-2010
[16346]
not R2 though ;-)
Andreas
16-Apr-2010
[16347]
For R3
, as I mentioned.
Steeve
16-Apr-2010
[16348]
I on
Andreas
16-Apr-2010
[16349x3]
Usage:
>> cidr-match? 10.10.10.10 10.10.0.0 16 ; == true
>> cidr-match? 10.10.10.10 10.10.0.0 24 ; == false
It's much nicer in R3, as to-binary on numbers works properly :)
In any case, here's an R2 version (assuming 2.7.7+ for funct):


cidr-match?: funct [address [tuple!] network [tuple!] bits [integer!]] 
[
    mask: shift/left -1 32 - bits

    (mask and to-integer to-binary address) = (mask and to-integer to-binary 
    network)
]
Steeve
16-Apr-2010
[16352]
See that weird one :)


cidr-match?: funct [address [tuple!] network [tuple!] bits [integer!]] 
[
    mask: to-tuple skip to-binary (shift -1 32 - bits) 4
    mask and address xor network = .0.
]
BrianH
16-Apr-2010
[16353]
Andreas, I'm sure my function is flawed - I just came up with it 
right then. Thanks for the fix :)
Steeve
16-Apr-2010
[16354]
rather optimized...


cidr-match?: funct [address [tuple!] network [tuple!] bits [integer!]] 
[
    mask: to-tuple to-binary shift -1 64 - bits
    mask and address xor network = .0.
]
Andreas
16-Apr-2010
[16355]
Heh, that's great Steeve. Esp the ".0."
Steeve
16-Apr-2010
[16356]
yeah,
.0. = 0.0.0 = 0.0.0.0.0 ...
Andreas
16-Apr-2010
[16357]
Yeah :)
BrianH
16-Apr-2010
[16358x2]
Yeah, silly me, forgot about SHIFT :)
That should work on R2 with 32 instead of 64.
Steeve
16-Apr-2010
[16360]
Yup
Andreas
16-Apr-2010
[16361]
Nope. You'd need shift/left on R2
Steeve
16-Apr-2010
[16362]
He knew... :)
Andreas
16-Apr-2010
[16363]
Just a reminder :)
BrianH
16-Apr-2010
[16364x2]
The last time I did IP calculations in REBOL was before 2.7.6 came 
out, so the reminder is appreciated :)
Though your functions do indicate the value of using REBOL's datatypes 
properly. Take note, Pekr, no strings :)
Pekr
17-Apr-2010
[16366x2]
wow, what a bunch of reactions :-) BrianH - I used strings, because 
of original Mikrotik format. They use 10.10.10.10/24 format for IP, 
so it was easier for me to carry around in a string form, then parse 
it later when needed ....


IP arithmetics, and ranges would be 2 nice new datatypes for REBOL 
imo :-)
Steeve - your optimised version for R2 does not work correctly:

>> cidr-match? 10.10.10.10 10.10.10.0 24
== false
Steeve
17-Apr-2010
[16368]
Because it was for R3 only, try this for R2:


cidr-match?: func [address [tuple!] network [tuple!] bits [integer!]] 
[

    address xor network and (to-tuple debase/base to-hex shift/left -1 
    32 - bits 16) = .0.
]

Don't know if it's faster than Andreas's, though
ChristianE
17-Apr-2010
[16369]
(-:-:- = -:0:-) = (0:-:0 = 0:0:0)
Steeve
17-Apr-2010
[16370x2]
-:-
and so:
+:+
Henrik
18-Apr-2010
[16372]
I'm trying to figure out which index comes earlier in two different 
references to the same block and was trying with MIN and MAX, because 
I didn't want to resort to LESSER? INDEX? things.


The result with MIN/MAX doesn't seem to be particularly useful. What 
is the basis of comparison of two blocks when using MIN and MAX?
BrianH
18-Apr-2010
[16373]
Their contents, not their indexes.
Henrik
18-Apr-2010
[16374]
yes, I figure that, but what's the basis for comparison?
Steeve
18-Apr-2010
[16375]
...
ChristianE
18-Apr-2010
[16376]
Their first elements, as in
>> max [0 9 9] [2]
== [2]
Henrik
18-Apr-2010
[16377x2]
are you sure?

>> min [b 1] [b b 2]
== [b b 2]
also because MIN and MAX don't compare on words, so there must be 
a different base of comparison.
ChristianE
18-Apr-2010
[16379]
>> min reduce [word! integer!] reduce [word! word! integer!]
== [word! word! integer!]
BrianH
18-Apr-2010
[16380x2]
MIN and MAX compare the spelling of words in blocks in R3.
And it's not just the first element:
>> min [b b] [b a]
== [b a]
ChristianE
18-Apr-2010
[16382]
Henrik just showed that.
BrianH
18-Apr-2010
[16383]
If the types aren't the same the datatypes are compared, by the arbitrary 
ordering they have in the type list (the datatype number).
Gabriele
19-Apr-2010
[16384]
Henrik: it's the same as SORT
Pekr
19-Apr-2010
[16385x2]
would it be possible to have more operators? In !Mikrotik group, 
I am trying do some stuff for MT routerOS API. Thanks to Anton, I 
can now proceed. When "studying" Python code, I found segment like:

elif l < 0x4000:
     l |= 0x8000


1) they can directly compare 'l of integer type to binary value. 
But I might make wrong conclusion here. But 'l is really result of 
len('string here') operation. But - even if so, I don't miss such 
an automation, as I can always write if l = to-integer #{8000}


2) Second line is more interesting - I did not find precisely |=, 
but found e.g. /= ... and it translates like do some operation, and 
assign. In this regard, the only comparable operator of REBOL is 
++ or --, but in R2 this is just mezzanine, in R3 native. But if 
I am right, their code:

l |=0x8000

R3:
l: (to-binary l) or #{8000}


So, if I would assume 'l being of correct type already (binary here), 
would it be possible to have?:

l |= #{8000}


Hmm, that is not probably compatible with REBOL parser and REBOL's 
assigment operator, which is :
If I am completly wrong, then please ignore me, I am not that much 
into the language design stuff :-) But why don't we at least have 
& for 'and, and | for 'or shortcuts?
Steeve
19-Apr-2010
[16387x2]
What's the gain ?
Anyway, you can create your own alias.
In R3 >
|: :or
&: :and

In R2 >
alias 'and "&"
alias 'or "|"
Pekr
19-Apr-2010
[16389x2]
you could use the same (R3 way) aliasing aproach in R2 too, no?
& and | are easy to add. The gain is to have apply & modify operation. 
Maybe there is no gain ... Simply instead of a = a + 10, you can 
write in python a += 10 in Python.