Page 1 of 1

Different IP address representation variants

Posted: Fri Jun 10, 2016 11:44 pm
by Lunasole
The following shows variations of how IP address can be represented. They working in browsers and not only, many utils and other stuff recognizes such forms correctly (for example, windows ping utility)
Might be useful to pass primitive filters, if you going to post link to something which is blocked by ISP or some site^^

PS. As side-effect this example shows that PB lacks unsigned variables and Octal() function :)

Code: Select all

EnableExplicit

Structure IP4Adress
   StructureUnion
      IP.q         ; integer representation of IP. quad because of lack of unsigned long :/
      IP_byte.a [4]   ; separated bytes of IP4 adress
   EndStructureUnion
EndStructure

; converting decimal to octal. source: https://rosettacode.org/wiki/Count_in_octal#PureBasic
; RETURN:   string representing number in octal system
Procedure.s octal(n.q)
   Static Dim digits(20)
   Protected i, j, result.s
   For i = 0 To 20
      digits(i) = n % 8
      n / 8
      If n < 1
         For j = i To 0 Step -1
            result + Str(digits(j))
         Next 
         Break
      EndIf
   Next 
   ProcedureReturn result  
EndProcedure


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Define A.IP4Adress
Define S$


; some google IP - 216.58.214.206
; in memory it is stored in reversed mode
a\IP_byte[3] = 216
a\IP_byte[2] = 58
a\IP_byte[1] = 214
a\IP_byte[0] = 206

; dump variations to _test.html file
CreateFile(0, "_test.html", #PB_UTF8)
WriteStringN(0, "<html>")
WriteStringN(0, "<strong>#IP address variants:</strong><br><br>")

WriteStringN(0, ~"<a href=\"http://0x" + Hex(a\IP) + ~"\">HEX form</a><br>")
WriteStringN(0, ~"<a href=\"http://" + "0x" + Hex(a\IP_byte[3]) +
                ".0x" + Hex(a\IP_byte[2]) + 
                ".0x" + Hex(a\IP_byte[1]) +
                ".0x" + Hex(a\IP_byte[0]) + ~"\">HEX form #2</a><br>")

WriteStringN(0, ~"<a href=\"http://0" + octal(a\IP) + ~"\">Octal form</a><br>")

WriteStringN(0, ~"<a href=\"http://" + "0" + octal(a\IP_byte[3]) + 
                ".0" + octal(a\IP_byte[2]) + 
                ".0" + octal(a\IP_byte[1]) + 
                ".0" + octal(a\IP_byte[0]) + ~"\">Octal form #2</a><br>")

; also they can be mixed, like dec.dec.hex.oct

WriteStringN(0, ~"<a href=\"http://" + a\IP + ~"\">DEC form</a><br>")

WriteStringN(0, ~"<br><a href=\"http://" + a\IP_byte[3] + "." + a\IP_byte[2] + "." + a\IP_byte[1] + "." + a\IP_byte[0] + ~"\">[regular IP string]</a>")
WriteStringN(0, "<br><br><br>*see page source for details")
WriteStringN(0, "</html>")
CloseFile(0)

; shellexecute
RunProgram("_test.html")

Re: Different IP address representation variants

Posted: Sat Jun 11, 2016 12:17 am
by DontTalkToMe
Doesn't work ! All links just point to the same site even if they are clearly different ! :lol:

The alternative representations are nice, didn't know all these were supported.

Maybe it's not mandatory to use a quad in your union, you can make it work with .l if you want, I think.

Hex(a\IP, #PB_Long) instead of Hex(a\IP)

a\IP & $FFFFFFFF instead of a\IP

but sure, in the end you are just moving conversions around.


Unsigned would be nice, but we know it's not gonna happen :cry:

Re: Different IP address representation variants

Posted: Sat Jun 11, 2016 1:12 am
by Lunasole
DontTalkToMe wrote:Doesn't work ! All links just point to the same site even if they are clearly different ! :lol:
Well I mean't bypass on text-filtering level, not if IP itself filtered ^^
The links are different while stored, but browser(or other program) parses them and converts to regular IP.
DontTalkToMe wrote: Maybe it's not mandatory to use a quad in your union, you can make it work with .l if you want, I think.
That's possible of course, but looks dirty as for me. For example such behavior can cause problems:

Code: Select all

Define A.l = -500000
Debug A & $FFFFFFFF ; not working
Debug Str(A & $FFFFFFFF)

Re: Different IP address representation variants

Posted: Sat Jun 11, 2016 2:20 am
by Keya
DontTalkToMe wrote:Unsigned would be nice, but we know it's not gonna happen :cry:
join me in walking on eggshells, lol. I too used Quad for IP addresses to accomodate lack of unsigned dword (if i only have 1 feature request dword is it!) - perhaps we could get away with just using Long combined with some bit-tricks but I prefer to play it safe, albeit at the expense of some efficiency - it's just far too easy to get incorrect results from Long unless the dword bit-tricks are 100% implemented correctly and in full. :( ps. no IPv6? :)

Re: Different IP address representation variants

Posted: Sat Jun 11, 2016 10:39 am
by DontTalkToMe
Lunasole wrote: Well I mean't bypass on text-filtering level, not if IP itself filtered ^^
The links are different while stored, but browser(or other program) parses them and converts to regular IP.
I'm sorry, I was trying to make a joke by playing dumb, it's all my fault :oops:
Lunasole wrote: That's possible of course, but looks dirty as for me.
I agree, every time I have to do those things it's a pain to look at it.

@keya, yes you have to pay a lot of attention, and it's easy (I think) to miss something and introduce bugs. :cry: