News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Get local/private IP Address

Started by Farabi, July 07, 2010, 01:30:53 AM

Previous topic - Next topic

Farabi


include \masm32\include\iphlpapi.inc
includelib \masm32\lib\iphlpapi.lib
MIB_TCPROW STRUCT
  dwState DWORD ?
  dwLocalAddr DWORD ?
  dwLocalPort DWORD ?
  dwRemoteAddr DWORD ?
  dwRemotePort DWORD ?
MIB_TCPROW ENDS
.code
GetLocalAddress proc uses esi edi ebx lptzAddr:dword,nAddr:dword
LOCAL fakeBuffer:DWORD
LOCAL lnBuffer:DWORD
LOCAL tempBuffer[128]:BYTE
LOCAL sci:dword

; Get TCP table in fake buffer of 1 byte. This will cause a
; buffer overflow error, the lnBuffer var will be filled with
; the required amount of memory.

        ; Inspired from MadWizard tutorial
mov lnBuffer, 1
invoke GetTcpTable, addr fakeBuffer, addr lnBuffer, 1
; Allocate the amount of memory neeeded
invoke GlobalAlloc, GMEM_FIXED, lnBuffer
mov edi, eax

; Get TCP table again, but with a real buffer now
lea ecx, lnBuffer
invoke GetTcpTable, eax, ecx, 1

mov ebx, [edi] ; first dword is number of items in tcp table


; --- loop through all items (ebx), esi is counter ---
push edi
add edi, 4
xor esi, esi
.WHILE esi<ebx

mov eax,(MIB_TCPROW ptr [edi]).dwLocalPort
mov edx, eax
shr edx, 8
shl ax, 8   
or eax, edx
.if eax==139
invoke inet_ntoa,[edi].MIB_TCPROW.dwLocalAddr
push eax
invoke memfill, lptzAddr,nAddr,0
pop eax
invoke lstrcat,lptzAddr,eax
;invoke MessageBox,0,eax,0,0
jmp done
.endif

add edi, SIZEOF MIB_TCPROW
inc esi
.ENDW
done:
pop edi

; --- free mem ---
invoke GlobalFree, edi


ret
GetLocalAddress endp


Please use on your computer and tell me if it giving you a wrong result.

[edit]
Put on more parameter to prevent crash.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Queue

It works on the computer of mine I tested it on (Win98SE), however, I have two network adapters, and so it only got the results for my first one (which happens to not be the one connected to my network). Also, here's expanded code that is a full working test example:
include \masm32\include\masm32rt.inc
include \masm32\include\ws2_32.inc
includelib \masm32\include\ws2_32.lib

include \masm32\include\iphlpapi.inc
includelib \masm32\lib\iphlpapi.lib
MIB_TCPROW STRUCT
  dwState DWORD ?
  dwLocalAddr DWORD ?
  dwLocalPort DWORD ?
  dwRemoteAddr DWORD ?
  dwRemotePort DWORD ?
MIB_TCPROW ENDS
.code
GetLocalAddress proc uses esi edi ebx lptzAddr:dword
LOCAL fakeBuffer:DWORD
LOCAL lnBuffer:DWORD
LOCAL tempBuffer[128]:BYTE
LOCAL sci:dword

; Get TCP table in fake buffer of 1 byte. This will cause a
; buffer overflow error, the lnBuffer var will be filled with
; the required amount of memory.

        ; Inspired from MadWizard tutorial
mov lnBuffer, 1
invoke GetTcpTable, addr fakeBuffer, addr lnBuffer, 1
; Allocate the amount of memory neeeded
invoke GlobalAlloc, GMEM_FIXED, lnBuffer
mov edi, eax

; Get TCP table again, but with a real buffer now
lea ecx, lnBuffer
invoke GetTcpTable, eax, ecx, 1

mov ebx, [edi] ; first dword is number of items in tcp table


; --- loop through all items (ebx), esi is counter ---
push edi
add edi, 4
xor esi, esi
.WHILE esi<ebx

mov eax,(MIB_TCPROW ptr [edi]).dwLocalPort
mov edx, eax
shr edx, 8
shl ax, 8   
or eax, edx
.if eax==139
invoke inet_ntoa,[edi].MIB_TCPROW.dwLocalAddr
push eax
invoke memfill, lptzAddr,256,0
pop eax
invoke lstrcat,lptzAddr,eax
;invoke MessageBox,0,eax,0,0
jmp done
.endif

add edi, SIZEOF MIB_TCPROW
inc esi
.ENDW
done:
pop edi

; --- free mem ---
invoke GlobalFree, edi


ret
GetLocalAddress endp

GetLocalAddress PROTO :DWORD
.data?
buffer db 16 dup(?)
.code
start:
invoke GetLocalAddress,offset buffer
invoke MessageBox,0,offset buffer,0,0
invoke ExitProcess,0
end start

Queue

Edit - What's with the huge memfill? An IP address maxes out at 16 bytes long INCLUDING the null terminator. XXX.XXX.XXX.XXX = 15

oex

Carefull.... What about IP6?

Y2K10
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Queue

Can that function return an IPv6 address, or does it only return IPv4? If the former, simply increase the buffer to the longest possible IPv6 address; 256 just seems like overkill.

Queue

oex

I dont know but I guess it will do

http://en.wikipedia.org/wiki/IPv6

I *think* that should about be the IP limit though.... At least for 10 years.... One for every grain of sand in the Sahara I think :lol
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Farabi

Quote
Edit - What's with the huge memfill? An IP address maxes out at 16 bytes long INCLUDING the null terminator. XXX.XXX.XXX.XXX = 15

You know copy paste, just make sure all of the mem is filled to 0 when I used that on getting a text.  :green
Quote
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

Quote
It works on the computer of mine I tested it on (Win98SE), however, I have two network adapters, and so it only got the results for my first one (which happens to not be the one connected to my network). Also, here's expanded code that is a full working test example:

I only had one, so I never be able to test it.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

Quote from: Queue on July 07, 2010, 02:12:54 AM
Can that function return an IPv6 address, or does it only return IPv4? If the former, simply increase the buffer to the longest possible IPv6 address; 256 just seems like overkill.

Queue

I'll search info about IPv6, maybe I can get into it soon.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

I should know this before


GetLocalAddress proc uses esi edi ebx lptzAddr:dword
LOCAL fakeBuffer:DWORD
LOCAL lnBuffer:DWORD
LOCAL tempBuffer[128]:BYTE
LOCAL tempItem:LV_ITEM
LOCAL sci:dword
LOCAL wsadata:WSADATA

; Get TCP table in fake buffer of 1 byte. This will cause a
; buffer overflow error, the lnBuffer var will be filled with
; the required amount of memory.
invoke WSAStartup,202h,addr wsadata
.if eax!=0
xor eax,eax
dec eax
ret
.endif
invoke gethostbyname,0
.if eax==0
invoke PERR
ret
.endif
mov eax,[eax+12]
mov eax,[eax]
mov ecx,[eax]
invoke inet_ntoa,ecx
push eax
invoke memfill, lptzAddr,16,0
pop eax
invoke lstrcat,lptzAddr,eax

ret
GetLocalAddress endp
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"