Page 1 of 1

ip, mask and broadcast address...

Posted: Sun Aug 21, 2011 1:28 pm
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...

Re: ip, mask and broadcast address...

Posted: Mon Aug 22, 2011 9:33 am
by jpd
Hi DoubleDutch,

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

thanks
jpd

Re: ip, mask and broadcast address...

Posted: Mon Aug 22, 2011 12:55 pm
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()

Re: ip, mask and broadcast address...

Posted: Mon Aug 22, 2011 2:23 pm
by jpd
Hi SFSxOI,
thank you for the example posted.
This works well!

Re: ip, mask and broadcast address...

Posted: Mon Aug 22, 2011 7:42 pm
by DoubleDutch
jpd: Sorry - I've edited the post - it is included now.

Re: ip, mask and broadcast address...

Posted: Tue Aug 23, 2011 4:30 am
by yrreti
Thanks for sharing your tip DoubleDutch, but
it won't work, because the procedure ConvertIPAddress is still missing.

thanks

Re: ip, mask and broadcast address...

Posted: Fri Aug 26, 2011 12:13 pm
by HeX0R

Re: ip, mask and broadcast address...

Posted: Fri Aug 26, 2011 12:29 pm
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

Re: ip, mask and broadcast address...

Posted: Fri Aug 26, 2011 1:09 pm
by jpd
Hi DoubleDutch,

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

Thanks
jpd

Re: ip, mask and broadcast address...

Posted: Fri Aug 26, 2011 1:54 pm
by DoubleDutch
Done, thanks. :)

Re: ip, mask and broadcast address...

Posted: Fri Aug 26, 2011 3:10 pm
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

Re: ip, mask and broadcast address...

Posted: Fri Aug 26, 2011 3:50 pm
by DoubleDutch
No problem. Thanks for the thanks! :)