View Full Version : NetMask question
Michael Clease
25-10-2013, 00:35
My knowledge of networking is quite limited and I wanted to know the netmask for a project I am working on so I came up with the script below.
Can I have some feedback on this, is it correct ?
I removed the code be it was rubbish.
Michael, I am getting this error-- see attached image.
Petr Schreiber
25-10-2013, 09:18
I think the error can be fixed by renaming mynetmask to mynet at line 10.
Mike - I can't confirm whether it is correct or not, my knowledge of networking is as good as my knowledge of quantum mechanics...
Petr
Michael Clease
25-10-2013, 09:38
you are correct, it was a last minute rename of a variable without running the script.
I fixed it now.
It is a nonsense answer I need to look for an api solution but its evading me at the moment, i'm sure its something to do with an ip class somewhere.
ErosOlmi
25-10-2013, 23:19
Michael,
please get attached new thinBasic_iNet.dll that will be present in next release. Copy it into \thinBasic\Lib\ directory replacing your current one.
I've added a couple of new functions that hopefully will solve the problem:
INet_GetInterfaces returns the number of network interfaces present in your machine
iNet_GetSubNet(index) will return the subnet mask of a particulat interface number
Example:
Uses "inet"Uses "Console"
Long lCount
PrintL "Number of interfaces present:", INET_GetInterfaces
PrintL "--------------------------------------"
For lCount = 1 To INET_GetInterfaces
PrintL lCount, INET_GetIp(lCount), inet_GetSubNet(lCount)
Next
PrintL "---Press a key to finish--------------"
WaitKey
Let me know if it works
Ciao
Eros
Michael Clease
27-10-2013, 23:33
I will test the code tomorrow, I can see some enhancements that could be added but I will let you know if I have been blown over to France by these winds.
Michael Clease
29-10-2013, 15:39
Thanks Eros it worked very well, below is what I had found out (with help from PB forums)
Uses "CONSOLE"
'// Definitions and structures used by getnetworkparams and getadaptersinfo apis
%MAX_ADAPTER_DESCRIPTION_LENGTH = 128 ' // arb.
%MAX_ADAPTER_NAME_LENGTH = 256 ' // arb.
%MAX_ADAPTER_ADDRESS_LENGTH = 8 ' // arb.
%ERROR_BUFFER_OVERFLOW = 111
%ERROR_SUCCESS = 0
Type IP_ADDRESS_STRING
sString As String * 4 * 4 ' char String[4 * 4]
End Type
Type IP_MASK_STRING
sString As String * 4 * 4 ' char String[4 * 4]
End Type
'//
'// IP_ADDR_STRING - store an IP address with its corresponding subnet mask,
'// both as dotted decimal strings
'//
Type IP_ADDR_STRING
pNext As IP_ADDR_STRING Ptr ' struct _IP_ADDR_STRING* Next
IpAddress As IP_ADDRESS_STRING ' IP_ADDRESS_STRING IpAddress
IpMask As IP_MASK_STRING ' IP_MASK_STRING IpMask
Context As DWord ' DWORD Context
End Type
'//
'// ADAPTER_INFO - per-adapter information. All IP addresses are stored as
'// strings
'//
Type tIP_ADAPTER_INFO
pNext As tIP_ADAPTER_INFO Ptr
ComboIndex As DWord
AdapterName As String * (%MAX_ADAPTER_NAME_LENGTH + 4)
Description As String * (%MAX_ADAPTER_DESCRIPTION_LENGTH + 4)
AddressLength As DWord
bAddress(%MAX_ADAPTER_ADDRESS_LENGTH - 1) As Byte
Index As DWord
uType As DWord
DhcpEnabled As DWord
CurrentIpAddress As IP_ADDR_STRING Ptr
IpAddressList As IP_ADDR_STRING
GatewayList As IP_ADDR_STRING
DhcpServer As IP_ADDR_STRING
HaveWins As Long
PrimaryWinsServer As IP_ADDR_STRING
SecondaryWinsServer As IP_ADDR_STRING
LeaseObtained As Long
LeaseExpires As Long
End Type
Declare Function GetAdaptersInfo Lib "iphlpapi.dll" Alias "GetAdaptersInfo" ( _
ByRef pAdapterInfo As tIP_ADAPTER_INFO, _ ' __out PIP_ADAPTER_INFO pAdapterInfo
ByRef pOutBufLen As DWord _ ' __in_out PULONG pOutBufLen
) As DWord ' DWORD
Dim ADAPT As String
Dim Adapters(1) As tIP_ADAPTER_INFO
Dim Counter As Long
adapt = getdg(Adapters)
For Counter = 1 To UBound(Adapters)
PrintL "Adapter"+Counter+" Adapter Name = "+Trim$(Adapters(Counter).AdapterName, $NUL)
PrintL "Adapter"+Counter+" Description = "+Trim$(Adapters(Counter).Description, $NUL)
PrintL "Adapter"+Counter+" Gateway address = "+Trim$(Adapters(Counter).Gatewaylist.Ipaddress.sString, $NUL)
PrintL "Adapter"+Counter+" IP address = "+Trim$(Adapters(Counter).IpAddressList.Ipaddress.sString, $NUL)
PrintL "Adapter"+Counter+" Netmask = "+Trim$(Adapters(Counter).IpAddressList.IPmask.sString, $NUL)+$CRLF
Next
PrintL "---Press a key to finish--------------"
WaitKey
Function GETDG(ByRef AdapterArray) As String
Local cbRequired As DWord
Local AdapterCount As DWord' Value = 1
If GetAdaptersInfo(ByVal 0, cbRequired)=%ERROR_BUFFER_OVERFLOW Then
AdapterCount = cbRequired \ SizeOf(tIP_ADAPTER_INFO)
ReDim AdapterArray(AdapterCount)
If GetAdaptersInfo(ByVal VarPtr(AdapterArray(1)), cbRequired) = %ERROR_SUCCESS Then
End If
End If
End Function
'--------------------------------------