Share your advanced PureBasic knowledge/code with the community.
DoubleDutch
Addict
Posts: 3220 Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:
Post
by DoubleDutch » Sun Aug 21, 2011 1:28 pm
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.
jpd
Enthusiast
Posts: 167 Joined: Fri May 21, 2004 3:31 pm
Post
by jpd » Mon Aug 22, 2011 9:33 am
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
Posts: 2970 Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....
Post
by SFSxOI » Mon Aug 22, 2011 12:55 pm
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
Posts: 167 Joined: Fri May 21, 2004 3:31 pm
Post
by jpd » Mon Aug 22, 2011 2:23 pm
Hi SFSxOI,
thank you for the example posted.
This works well!
PB 5.10 Windows 7 x64 SP1
DoubleDutch
Addict
Posts: 3220 Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:
Post
by DoubleDutch » Mon Aug 22, 2011 7:42 pm
jpd: Sorry - I've edited the post - it is included now.
yrreti
Enthusiast
Posts: 546 Joined: Tue Oct 31, 2006 4:34 am
Post
by yrreti » Tue Aug 23, 2011 4:30 am
Thanks for sharing your tip DoubleDutch, but
it won't work, because the procedure ConvertIPAddress is still missing.
thanks
HeX0R
Addict
Posts: 1205 Joined: Mon Sep 20, 2004 7:12 am
Location: Hell
Post
by HeX0R » Fri Aug 26, 2011 12:13 pm
DoubleDutch
Addict
Posts: 3220 Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:
Post
by DoubleDutch » Fri Aug 26, 2011 12:29 pm
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
jpd
Enthusiast
Posts: 167 Joined: Fri May 21, 2004 3:31 pm
Post
by jpd » Fri Aug 26, 2011 1:09 pm
Hi DoubleDutch,
add the procedure on the first post then is your "Tricks 'n' Tips" Perfect!
Thanks
jpd
PB 5.10 Windows 7 x64 SP1
DoubleDutch
Addict
Posts: 3220 Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:
Post
by DoubleDutch » Fri Aug 26, 2011 1:54 pm
Done, thanks.
yrreti
Enthusiast
Posts: 546 Joined: Tue Oct 31, 2006 4:34 am
Post
by yrreti » Fri Aug 26, 2011 3:10 pm
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!
DoubleDutch
Addict
Posts: 3220 Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:
Post
by DoubleDutch » Fri Aug 26, 2011 3:50 pm
No problem. Thanks for the thanks!