ip, mask and broadcast address...

Share your advanced PureBasic knowledge/code with the community.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

ip, mask and broadcast address...

Post by DoubleDutch »

Here is code to get ip, mask and broadcast for connected network interfaces (on Windows):

Code: Select all


Structure IP_ADDR_STRING
	pNext.l
	IpAddress.b[16]
	IpMask.b[16]
	Context.l
EndStructure

Structure IP_ADAPTER_INFO
	Next.l
	ComboIndex.l
	AdapterName.b[260]	; MAX_ADAPTER_NAME_LENGTH + 4
	Description.b[132]	; MAX_ADAPTER_DESCRIPTION_LENGTH + 4
	AdressLength.l
	Address.b[8]				; MAX_ADAPTER_ADDRESS_LENGTH
	Index.l
	Type.l
	DhcpEnabled.l
	CurrentIpAddressPTR.l
	IpAddressList.IP_ADDR_STRING
	GatewayList.IP_ADDR_STRING
	DhcpServer.IP_ADDR_STRING
	HaveWins.l
	PrimaryWinsServer.IP_ADDR_STRING
	SecondaryWinsServer.IP_ADDR_STRING
	LeaseObtained.l
	LeaseExpires.l
EndStructure

Procedure ConvertIPAddress(string$)
   ProcedureReturn MakeIPAddress(Val(StringField(string$,1,".")),Val(StringField(string$,2,".")),Val(StringField(string$,3,".")),Val(StringField(string$,4,"."))) 
EndProcedure

memlength.l=0
If GetAdaptersInfo_(0,@memlength)=#ERROR_BUFFER_OVERFLOW
	membuffer=AllocateMemory(memlength)
	If membuffer
		If GetAdaptersInfo_(membuffer,@memlength)=#ERROR_SUCCESS
			adapters=memlength/SizeOf(IP_ADAPTER_INFO)
			For loop=0 To adapters-1
				ip=ConvertIPAddress(PeekS(membuffer+(loop*SizeOf(IP_ADAPTER_INFO))+OffsetOf(IP_ADAPTER_INFO\IpAddressList)+OffsetOf(IP_ADDR_STRING\IpAddress)))
				If ip
					description$=PeekS(membuffer+(loop*SizeOf(IP_ADAPTER_INFO))+OffsetOf(IP_ADAPTER_INFO\Description))
					mask=ConvertIPAddress(PeekS(membuffer+(loop*SizeOf(IP_ADAPTER_INFO))+OffsetOf(IP_ADAPTER_INFO\IpAddressList)+OffsetOf(IP_ADDR_STRING\IpMask)))
					broadcast=(mask!$ffffffff)|ip
					Debug("description: "+description$+"  ip: "+IPString(ip)+"  mask: "+IPString(mask)+"  broadcast: "+IPString(broadcast))
				EndIf				
			Next
		EndIf
		FreeMemory(membuffer)
	EndIf
EndIf 
It would be better if this was built-in to the Network lib though...
Last edited by DoubleDutch on Fri Aug 26, 2011 1:53 pm, edited 2 times in total.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Re: ip, mask and broadcast address...

Post by jpd »

Hi DoubleDutch,

this tip not works..
structure IP_ADAPTER_INFO and the procedure ConvertIPAddress are not present.

thanks
jpd
PB 5.10 Windows 7 x64 SP1
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: ip, mask and broadcast address...

Post by SFSxOI »

I agree with DoubleDutch, something like this needs to be included. Thanks for posting this DoubleDutch :)

@jpd
Here is a version that gets network/listen address, and broadcast address. Not the DoubleDutch example, but something I had laying around if you need something now.

Code: Select all

#MIB_IPADDR_PRIMARY = $0001
#MIB_IPADDR_DYNAMIC = $0004
#MIB_IPADDR_DISCONNECTED = $0008
#MIB_IPADDR_DELETED = $0040
#MIB_IPADDR_TRANSIENT = $0080

Structure MIB_IPADDRROW
   dwAddr.l
   dwIndex.l
   dwMask.l
   dwBCastAddr.l
   dwReasmSize.l
   unused1.w
   wType.w
EndStructure

Structure MIB_IPADDRTABLE
   dwNumEntries.l
   *ipAddrTable.MIB_IPADDRROW
EndStructure

Procedure BroadListen()

dwResult.i
dwRetVal.i
TableSize.i = 0
*TableBroadcastAddr.MIB_IPADDRTABLE = AllocateMemory(SizeOf(MIB_IPADDRTABLE))

dwResult = GetIpAddrTable_(*TableBroadcastAddr, @TableSize, #True)

If dwResult = #ERROR_INSUFFICIENT_BUFFER
  *pIPAddrTable = ReAllocateMemory(*TableBroadcastAddr, TableSize)
  dwRetVal = GetIpAddrTable_(*TableBroadcastAddr, @TableSize, #True) ; call it the second time to get the info
  If dwRetVal = #NO_ERROR
    *ipinfo.MIB_IPADDRROW = @*TableBroadcastAddr\ipAddrTable
    For loop = 0 To *TableBroadcastAddr\dwNumEntries - 1
      interfaceindex.i = *ipinfo\dwIndex
      If interfaceindex > 0 
        ipaddr = *ipinfo\dwAddr
        subnetmask = *ipinfo\dwMask
        
        ip_addr$ = PeekS(inet_ntoa_(ipaddr))
        Debug "Interface Index : " + Str(interfaceindex) + " IP address : " + ip_addr$
        
        subnet_mask$ = PeekS(inet_ntoa_(subnetmask))
        Debug "Interface Index : " + Str(interfaceindex) + " Subnet Mask  : " + subnet_mask$
        
        network_listen_addr$ = PeekS(inet_ntoa_(ipaddr & subnetmask))
        Debug "Interface Index : " + Str(interfaceindex) + " Network/Listen IP address : " + network_listen_addr$
        
; Note - don't use the dwBCastAddr member with GetIpAddrTable as per MSDN at http://msdn.microsoft.com/en-us/library/aa366845(VS.85).aspx The proper value for this member is not returned by the GetIpAddrTable function.

        broadcast$ = PeekS(inet_ntoa_((subnetmask!$ffffffff)|ipaddr))
        Debug "Interface Index : " + Str(interfaceindex) + " Broadcast IP address : " + broadcast$
                
        Debug "**************************************************************"
        
        
      EndIf
      *ipinfo = *ipinfo + SizeOf(MIB_IPADDRROW)
    Next
  EndIf
EndIf

EndProcedure

BroadListen()
Last edited by SFSxOI on Mon Aug 22, 2011 2:48 pm, edited 1 time in total.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Re: ip, mask and broadcast address...

Post by jpd »

Hi SFSxOI,
thank you for the example posted.
This works well!
PB 5.10 Windows 7 x64 SP1
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: ip, mask and broadcast address...

Post by DoubleDutch »

jpd: Sorry - I've edited the post - it is included now.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: ip, mask and broadcast address...

Post by yrreti »

Thanks for sharing your tip DoubleDutch, but
it won't work, because the procedure ConvertIPAddress is still missing.

thanks
User avatar
HeX0R
Addict
Addict
Posts: 1205
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: ip, mask and broadcast address...

Post by HeX0R »

User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: ip, mask and broadcast address...

Post by DoubleDutch »

Code: Select all

Procedure ConvertIPAddress(string$)
	ProcedureReturn MakeIPAddress(Val(StringField(string$,1,".")),Val(StringField(string$,2,".")),Val(StringField(string$,3,".")),Val(StringField(string$,4,"."))) 
EndProcedure
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Re: ip, mask and broadcast address...

Post by jpd »

Hi DoubleDutch,

add the procedure on the first post then is your "Tricks 'n' Tips" Perfect! :wink:

Thanks
jpd
PB 5.10 Windows 7 x64 SP1
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: ip, mask and broadcast address...

Post by DoubleDutch »

Done, thanks. :)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: ip, mask and broadcast address...

Post by yrreti »

Thanks DoubleDutch for adding the missing Procedure, and for sharing your code example.
It's always good to learn different programing techniques.


Thanks HeXOR. Nice job!
Postby HeX0R » Fri Aug 26, 2011 6:13 am
You can also try this code:
http://www.purebasic.fr/german/viewtopi ... =8&t=21140
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: ip, mask and broadcast address...

Post by DoubleDutch »

No problem. Thanks for the thanks! :)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Post Reply