raw 128bit IPv6 address from GetClientIP()

Just starting out? Need help? Post your questions and find answers here.
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

raw 128bit IPv6 address from GetClientIP()

Post by Keya »

hello! I would like to read/save the raw 128-bit IPv6 address, so rather than converting it to a string with IPString() and back to raw Im assuming we can just read from the memory of the return value of GetClientIP() ? before we FreeIP() it

I modified the Echo client/server example that comes with PB and its (IPv6) working fine when I use "::1" localhost, but I can't get it to connect to any Internet sites for some reason - im guessing this is a problem with my system and not a bug, but can others please test?

Also if you could copy-and-paste the ShowMemoryViewer result along with an nslookup of whatever address it connected to that'd be great thankyou very much! hopefully from the memory dump the IP address location and byte order etc will be obvious :) (i cant work it out from the ::1 localhost as its almost all nulls)

Code: Select all

If InitNetwork() = 0
  MessageRequester("Error", "Can't initialize the network !", 0):  End
EndIf

Port = 80
addr.s = "www.ipv6ready.org" ; "www.google.com"  "ipv6.google.com" "2a00:1450:4001:815::200e"

ConnectionID = OpenNetworkConnection(addr, Port,  #PB_Network_TCP | #PB_Network_IPv6)
If ConnectionID
  MessageRequester("PureBasic - Client", "Client connected to server...", 0)
  IP = GetClientIP(ConnectionID)
  ShowMemoryViewer(@IP,24)   ;16 bytes IPv6 + 8 in case theres a ptr
  MessageRequester("IP","Showing IP memory (but am not sure if the first field is a ptr to the data)")
  CloseNetworkConnection(ConnectionID)
  FreeIP(IP) ;required when GetClientIP is ipv6
Else
  MessageRequester("PureBasic - Client", "Can't find the server (Is it launched ?).", 0)
EndIf
Fred
Administrator
Administrator
Posts: 18244
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: raw 128bit IPv6 address from GetClientIP()

Post by Fred »

Does your ISP support ipv6 ?
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: raw 128bit IPv6 address from GetClientIP()

Post by Keya »

Ahhh, no I don't think so!! (i enquired a couple years ago and they didn't then) thanks, i shouldve thought of that :)
It's not a major problem for me though because i can still test locally, but just in regards to reading the IP address I can't quite figure it out because the memory is too tricky to work out when the address is 95% nulls lol. So if somebody could show me their ShowMemoryViewer dump using an internet-based address thatd be much appreciated!
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: raw 128bit IPv6 address from GetClientIP()

Post by Keya »

I was finally able to find some valid/non-"::1" IPv6 addresses in a VM, so i was then able to confirm my suspicion/hope that to read the raw 128bit IPv6 address we just read 16 bytes from IP (before calling FreeIP(IP)), nice and easy like i hoped :)

Code: Select all

Structure IPv6addr
  StructureUnion
    octet.a[16]
    octetq.q[2]
  EndStructureUnion
EndStructure
 
InitNetwork()
If ExamineIPAddresses(#PB_Network_IPv6)   ;enumerate local IPv6 addresses
  Repeat
    IP = NextIPAddress()
    If IP
      Define IPv6.IPv6addr
      IPv6\octetq[0] = PeekQ(IP)
      IPv6\octetq[1] = PeekQ(IP+8)
      ShowMemoryViewer(IPv6, 16)
      MessageRequester("Next IP", IPString(IP, #PB_Network_IPv6))      
      FreeIP(IP)
    EndIf
  Until IP = 0
EndIf
its stored in the same left-to-right (big endian) byte order just like IPv4.
For example, "::1" is "0000:0000:0000:0000:0000:0000:0000:0001", with the 01 byte right-most.
It would be a nice timesaver if the PB documentation could mention these things :)
Post Reply