IP ADDRESS & IN_ADDR Structure (heeeelp please) (OpenInsight 32-Bit)
At 11 NOV 2009 03:41:01PM Dale Jessop wrote:
Hi all,
I have some code which used to get the DNS name from an ip address, which I've modified so it will also get the DNS name of the local machine in which OI is running. However, whilst I have the HOSENT structure (from which I get the fully DNS name) I want to get the actual IP Address as well but that requires me to call inet_ntoa passing in a structure called in_addr.
Can anyone decipher this…
typedef struct in_addr {
union {struct {u_char s_b1,s_b2,s_b3,s_b4;} S_un_b;struct {u_short s_w1,s_w2;} S_un_w;u_long S_addr;} S_un;}IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR;
At 21 AUG 2012 11:12AM Jared Bratu wrote:
There are several different methods to get the address of the local workstation. I found some online examples that read the registry to retrieve the IP address. It has been adapted to check for multiple network interfaces and also differentiate between static and DHCP addresses. I hope this points you in the right direction. It hasn't been extensively tested.
0001 Function GetStationIPAddresses(void)
0002 Declare Function OleCreateInstance, OleCallMethod
0003
0004 MAX_NIC_CARDS = 50 ;*Maximum number of network interfaces to check
0005 AvailableNics = "" ;*Temporary GUID storage for available network interfaces
0006 IPAddressesOnSystem = "" ;*Available static and dhcp IP addresses from available network interfaces
0007
0008 oSh = OleCreateInstance("WScript.Shell")
0009
0010 *Loop through looking for network cards
0011 For i = 1 To MAX_NIC_CARDS
0012
0013 tmpRegBranch = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\" : i : "\ServiceName"
0014 retVal = OleCallMethod(oSh, "RegRead", tmpRegBranch)
0015
0016 *If not empty then add GUID to check for IP address
0017 If retVal <> "" Then
0018 AvailableNics←1> = retVal
0019 End
0020
0021 Next
0022
0023 AvailableNicsCount = DCOUNT(AvailableNics, @FM)
0024
0025 *Loop through all the nic GUID's to check for DHCP and static IP addresses
0026 For i = 1 To AvailableNicsCount
0027
0028 *Check for static address
0029 tmpRegBranch="HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\" : AvailableNics<i> : "\IPAddress"
0030 retVal = OleCallMethod(oSh, "RegRead", tmpRegBranch)
0031 If retVal <> "" Then
0032 IPAddressesOnSystem←1> = retVal
0033 End
0034
0035 *Check for DHCP address
0036 tmpRegBranch="HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\" : AvailableNics<i> : "\DhcpIPAddress"
0037 retVal = OleCallMethod(oSh, "RegRead", tmpRegBranch)
0038 If retVal <> "" Then
0039 IPAddressesOnSystem←1> = retVal
0040 End
0041
0042 Next
0043
0044 Debug
0045 *IPAddressesOnSystem now contains all IP addresses on this workstation
At 12 NOV 2009 05:15AM Dale Jessop wrote:
Hey Jared Thanks!!!
you put me on the right direction. I've imprvoed the code a little so it pulls out all of the IP addresses, including the IP address assigned wireless adapters.
Cheers Dale.
Function GetStationIPAddresses(void)
Declare Function OleCreateInstance, OleCallMethodDeclare function RegOpenKeyEx, RegQueryValueEx, RegCloseKeyAvailableNics="/* equates for the base registry keys */equ HKEY_CLASSES_ROOT$ to 0x80000000equ HKEY_CURRENT_USER$ to 0x80000001equ HKEY_LOCAL_MACHINE$ to 0x80000002equ HKEY_USERS$ to 0x80000003equ HKEY_PERFORMANCE_DATA$ to 0x80000004equ HKEY_CURRENT_CONFIG$ to 0x80000005equ HKEY_DYN_DATA$ to 0x80000006equ KEY_QUERY_VALUE$ to 0x0001equ ERROR_SUCCESS to 0x0000options=0samDesired=KEY_QUERY_VALUE$KeyHandle=0Hkey=HKEY_LOCAL_MACHINE$SubKey=SYSTEM\CurrentControlSet\Services\TCPIP\Linkage":\00\stat=0null='LockVariable KeyHandle as Longstat=RegOpenKeyEx(Hkey, SubKey, options, samDesired, KeyHandle)If Stat=ERROR_SUCCESS ThenQueueName=str(\00\, 512)Reg_SZ=1cbBuf=512Key=Bind":\00\x=RegQueryValueEx(KeyHandle, Key, 0, Reg_SZ, QueueName, cbBuf)QueueName=trim(QueueName)swap "\Device\" with @fm in QueueNameswap " " with "" in QueueNameswap \00\ with "" in QueueNamenoGuids=count(QueueName,@fm)+(QueueName ne "")for lop=noGuids to 1 step -1if (QueueName1,1={" AND QueueName-1,1=}") elseQueueName=delete(QueueName,lop,0,0)endnext lopAvailableNics=QueueNameendrv=RegCloseKey( KeyHandle)MAX_NIC_CARDS=50 ; * Maximum number of network interfaces to checkIPAddressesOnSystem=" ; * Available static and dhcp IP addresses from available network interfacesoSh=OleCreateInstance("WScript.Shell")
Loop through looking for network cardsFor i=1 To MAX_NIC_CARDStmpRegBranch=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\" : i : "\ServiceName"retVal=OleCallMethod(oSh, "RegRead", tmpRegBranch)
If not empty then add GUID to check for IP addressIf retVal "" Thenlocate retval in AvailableNics using @fm setting pos elseAvailableNics=retValendEndNextAvailableNicsCount=DCOUNT(AvailableNics, @FM)
Loop through all the nic GUID's to check for DHCP and static IP addressesFor i=1 To AvailableNicsCount
Check for static addresstmpRegBranch=HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\" : AvailableNics[i] : "\IPAddress"ipAddr=OleCallMethod(oSh, "RegRead", tmpRegBranch)If ipAddr "" Thengosub addIPAddrEnd
Check for DHCP addresstmpRegBranch=HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\" : AvailableNics[i] : "\DhcpIPAddress"ipAddr=OleCallMethod(oSh, "RegRead", tmpRegBranch)If ipAddr "" Thengosub addIPAddrEndNextreturnaddIpAddr:
locate ipAddr in IPAddressesOnSystem using @fm setting pos elseIPAddressesOnSystem=ipAddrendreturn
At 12 NOV 2009 08:58AM Bob Carten wrote:
This is a good job for WMI ( Windows management Interface )
WMI often has a way to answer questions about properties of your pc or domain, such as this one, or 'what groups is this user a member of?'. WMI is Microsoft's attempt to hide the complexity of the api calls and registry hives. WMI is difficult to learn, but easy to copy I use it because it is easy to find examples, Most examples do not translate well to Basic+. A workaround is to convert the script to a VbScript function, embed it / eval it from Basic+. The example below shows how to do this. It is a good technique to have in your back pocket.
0001 Function Get_IpAddresses(void) 0002 /* 0003 ** WMI Scripting Example 0004 ** lifted from http://msdn.microsoft.com/en-us/library/aa394595%28VS.85%29.aspx 0005 ** 0006 ** Note: WMI collections / Arrays can be difficult to translate to Basic+, 0007 ** So used embedded vbscript instead 0008 ** 0009 ** rjc 12Nov2009 0010 */ 0011 0012 Declare Function rti_Ole_GetObject 0013 Equ crlf$ To \0D0A\ 0014 0015 script = 'function GetAddresses()' 0016 script<-1> = 'set oList=CreateObject("Scripting.Dictionary")' 0017 script<-1> = 'strComputer=."' 0018 script<-1> = 'Set objWMIService=GetObject( _ ' 0019 script<-1> = ' "winmgmts:\\" & strComputer & "\root\cimv2")' 0020 script<-1> = 'Set IPConfigSet=objWMIService.ExecQuery _' 0021 script<-1> = ' ("Select IPAddress from Win32_NetworkAdapterConfiguration ")' 0022 script<-1> = '' 0023 script<-1> = 'For Each IPConfig in IPConfigSet' 0024 script<-1> = ' If Not IsNull(IPConfig.IPAddress) Then ' 0025 script<-1> = ' For i=LBound(IPConfig.IPAddress) _' 0026 script<-1> = ' to UBound(IPConfig.IPAddress)' 0027 script<-1> = ' oList.Add IPConfig.IPAddress(i), "" ' 0028 script<-1> = ' Next' 0029 script<-1> = ' End If' 0030 script<-1> = 'Next' 0031 script<-1> = 'addresses=oList.Keys()' 0032 script<-1> = 'GetAddresses=join(addresses, ",")' 0033 script<-1> = 'End Function' 0034 0035 Swap @fm With crlf$ In script 0036 0037 wsh = OLECreateInstance("MSScriptControl.ScriptControl") 0038 wsh->Language = "vbScript" 0039 x = wsh->AddCode(script) 0040 0041 expression = 'GetAddresses()' 0042 ans = wsh->Eval(expression) 0043 Convert ',' To @vm In ans 0044 Return ans 0045
At 12 NOV 2009 10:49AM Dale Jessop wrote:
Hi Bob,
Thanks! that is a neat trick, will try and remember that!
Dale.
At 12 NOV 2009 01:29PM John Bouley wrote:
Bob,
Thanks for the trick. I wonder how long it will take Microsoft to plug that hole?
![]()
John
At 12 NOV 2009 05:22PM Bob Carten wrote:
It's not a hole, it's intended behavior for the scripting control.
The scripting host is flexible and really powerful. With power comes responsibility The problems with the scripting host arise when developers expose the host to unsafe scripts, as Microsoft did with office, the browser and the Windows shell.