MakeIPAddressFromString()

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

MakeIPAddressFromString()

Post by infratec »

Hi,

since it was needed:

Code: Select all

;http://www.purebasic.fr/english/viewtopic.php?f=12&t=66279&p=492103


CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf


Procedure.i MakeIPAddressFromString(String$)
  
  Protected.i Result, Dot, Colon, Count
  Protected Rev$, IPv4$, Replace$
  
  
  If FindString(String$, ".")
    Dot = #True
  EndIf
  
  If FindString(String$, ":")
    Colon = #True
  EndIf
  
  If Dot And Not Colon
    Result = MakeIPAddress(Val(StringField(String$, 1, ".")), Val(StringField(String$, 2, ".")), Val(StringField(String$, 3, ".")), Val(StringField(String$, 4, ".")))
  Else
    
    If Dot
      Rev$ = ReverseString(String$)
      IPv4$ = ReverseString(Left(Rev$, FindString(Rev$, ":") - 1))
      String$ = RemoveString(String$, IPv4$)
      
      String$ + LTrim(Hex(Val(StringField(IPv4$, 1, "."))) + RSet(Hex(Val(StringField(IPv4$, 2, "."))), 2, "0"), "0") + ":"
      String$ + LTrim(Hex(Val(StringField(IPv4$, 3, "."))) + RSet(Hex(Val(StringField(IPv4$, 4, "."))), 2, "0"), "0")
    EndIf
    
    Count = CountString(String$, ":")
    If Count < 7
      Count = 8 - Count
      While Count
        Replace$ + ":0"
        Count - 1
      Wend
      Replace$ + ":"
      String$ = ReplaceString(String$, "::", Replace$)
      If Left(String$, 1) = ":"
        String$ = "0" + String$
      EndIf
    EndIf
    
    Result = MakeIPAddress(Val("$" + StringField(String$, 1, ":")), Val("$" + StringField(String$, 2, ":")), Val("$" + StringField(String$, 3, ":")), Val("$" + StringField(String$, 4, ":")), Val("$" + StringField(String$, 5, ":")), Val("$" + StringField(String$, 6, ":")), Val("$" + StringField(String$, 7, ":")), Val("$" + StringField(String$, 8, ":")))
  EndIf
  
  ProcedureReturn Result
  
EndProcedure


CompilerIf #PB_Compiler_IsMainFile
  
  Define IP.i
  
  InitNetwork()
  
  IP = MakeIPAddressFromString("127.0.0.1")
  Debug IPString(IP, #PB_Network_IPv4)
  
  IP = MakeIPAddressFromString("fe80::4910:6451:3c95:5533")
  Debug IPString(IP, #PB_Network_IPv6)
  FreeIP(IP)
  
  IP = MakeIPAddressFromString("::ffff:7f00:1")
  Debug IPString(IP, #PB_Network_IPv6)
  FreeIP(IP)
  
  IP = MakeIPAddressFromString("::ffff:127.0.0.1")
  Debug IPString(IP, #PB_Network_IPv6)
  FreeIP(IP)
  
CompilerEndIf
Maybe it is not complete and I did no error checks.

Bernd
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: MakeIPAddressFromString()

Post by infratec »

:oops:

Fixed a bug in IPv6:

MakeIPAddress() was only called when the address included a ::
User avatar
le_magn
Enthusiast
Enthusiast
Posts: 281
Joined: Wed Aug 24, 2005 12:11 pm
Location: Italia

Re: MakeIPAddressFromString()

Post by le_magn »

Thank you !!!!
Image
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

Re: MakeIPAddressFromString()

Post by Mesa »

Make a try with regex.

Code: Select all

EnableExplicit

Procedure FindIPAddress(Text$, ipv=4)
  Protected ipv$, ipv4$, ipv6$, ipv46$
  Protected IPRegex, NbResults, i
  Dim Result$(0)
  
  ; ============= REGEX =======================================================================================================
  ;All regex found somewhere on the web
  
  ;ipv4
  ipv4$="(?:(?:0|1[\d]{0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)\.){3}(?:0|1[\d]{0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)"
  ; This regexp accepts 1.2.3.4 but Not 001.002.003.004 which is invalid in IPv4 addresses used
  ; in URLs And email addresses, which forbids leading zeroes. Extra leading zeroes would transform 
  ; the apparent IPv4 address into a domain name
  
  ;ipv6 several regex
  ipv6$="((([0-9A-Fa-f]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[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:)))(%.+)?\s*$"
  ; ============= END REGEX =======================================================================================================
  
  ;Select ipv4 or ipv6 or both 
  Select ipv
    Case 4
      ipv$=ipv4$
    Case 6
      ipv$=ipv6$ 
    Case 46
      ipv$=ipv46$
    Default
      Debug "unknown"
      End
  EndSelect
  
  ; Try to create regex
  IPRegex=CreateRegularExpression(#PB_Any,ipv$ )
  
  
  If  IPRegex
    ; Try to find IP 
    NbResults = ExtractRegularExpression(IPRegex, Text$, result$())
    
    ; How many IP found
    Debug "Nb matchs found: " + NbResults
    
    ; Display IPs
    For i = 0 To NbResults - 1
      Debug Result$(i)
    Next
    
  Else
    MessageRequester("Error", RegularExpressionError())
  EndIf
EndProcedure


FindIPAddress("127.0.0.1", 4)
FindIPAddress("2001:41D0:1:2E4e::1", 6)
FindIPAddress("127.0.0.1", 46) ; oops, sorry there is a bug
FindIPAddress("2001:41D0:1:2E4e::1", 46)
FindIPAddress("2001:41D0:1:2E4e::1", 66)

;TODO Replace: 4,6 and 46 by #ipv4, #ipv6 and #ipv4|#ipv6

M.
Post Reply