Anyone ever tried, or perhaps know of a good way to retrieve your networks WAN IP address? I'm trying to create a program that is able to retrieve your machines LAN IP address, as well as your machines WAN IP address i.e. - the address you appear to be coming from to the rest of the e-world.

Anyway, i'm pretty sure it would be pretty complicated, and would probably require some sort of third party intervention to relay that data back in from the outside. I was just curious. Any help would be great.
Posted on 2004-05-18 12:40:01 by Nokturnal
Work on all W9X, NTX systems. Just a quick snip from a working program's error output routine. You will need to define your own data. Uncomment the first three bytes of ascii translation.

Regards, P1 :cool:

;Computer Name
mov dwLength,sizeof szComputerName
invoke RtlZeroMemory,OFFSET szComputerName,SIZEOF szComputerName
invoke GetComputerName, ADDR szComputerName, ADDR dwLength
.if eax == TRUE
.else
invoke GetLastError
invoke dwtoa, eax, addr Number
invoke lstrcat, addr szComputerName, addr szErrorComputer
invoke lstrcat, addr szComputerName, addr Number
.endif
;Moved for Format reasons.
;invoke lstrcat, addr szErrorMessage, addr szComputerName
;invoke lstrcat, addr szErrorMessage, addr szFieldSep

;MyIP
invoke WSAStartup,101h,addr wsadata
.if eax!=0
;invoke MessageBox,0,addr szStartupError,ADDR szMsgTitle,MB_OK
invoke lstrcat, addr szErrorMessage, addr szErrorIP
.else
invoke gethostbyname,addr szComputerName ;Windows always knows it's own IP address.
.if eax==0
; .if eax!=0 ;Use to trouble shoot IP address translation to ascii.
;invoke WSAGetLastError
;invoke dwtoa, eax, addr szNumber ;Preserve error number for output.
;invoke lstrcat, addr szHostNameError, addr szNumber
;invoke MessageBox,0,addr szHostNameError,ADDR szMsgTitle,MB_OK
invoke lstrcat, addr szErrorMessage, addr szErrorIP
.else
mov eax,
mov eax,
mov eax,
;Temporary
;mov eax, 0564A8C0h ;IP on local LAN
mov ebx,eax
;and eax,0FFh
;invoke dwtoa, eax, addr szNumber ;Preserve error number for output.
;invoke lstrcat, addr szIPaddress, addr szNumber
;invoke lstrcat, addr szIPaddress, addr szPeriod
ror ebx,8
;mov eax,ebx
;and eax,0FFh
;invoke dwtoa, eax, addr szNumber ;Preserve error number for output.
;invoke lstrcat, addr szIPaddress, addr szNumber
;invoke lstrcat, addr szIPaddress, addr szPeriod
ror ebx,8
;mov eax,ebx
;and eax,0FFh
;invoke dwtoa, eax, addr szNumber ;Preserve error number for output.
;invoke lstrcat, addr szIPaddress, addr szNumber
;invoke lstrcat, addr szIPaddress, addr szPeriod
ror ebx,8
mov eax,ebx
and eax,0FFh
invoke dwtoa, eax, addr Number ;Preserve error number for output.
invoke lstrcat, addr szIPaddress, addr Number
invoke szRight, addr szIPaddress, addr Number, 3
invoke lstrcat, addr szErrorMessage, addr Number
;invoke MessageBox,0,addr szIPaddress,ADDR szMsgTitle,MB_OK
.endif
invoke WSACleanup
.endif
Posted on 2004-05-18 14:06:46 by Pone
For me that code gets 10.1.3.6 i.e. my LAN ip: unless i am wrong, Nokturnal meant the external IP.
Posted on 2004-05-20 10:56:20 by stormix
Yep, stormix. I'm trying to retrieve the IP addressed to my gateway via my ISP.
Posted on 2004-05-20 15:14:21 by Nokturnal
Originally posted by Nokturnal
Anyone ever tried, or perhaps know of a good way to retrieve your networks WAN IP address? I'm trying to create a program that is able to retrieve your machines LAN IP address, as well as your machines WAN IP address i.e. - the address you appear to be coming from to the rest of the e-world.
It was not until the very end, that he made himself clear about what he wanted. My bad for not reading the complete post to the end.

Back to the original request. Most LANs do NAT IP translation and the client machine will never know the external IP address it's using. Upon multiple requests, different IPs can be used. The best way I know of is to issue a http request to a known website that will echo the IP address, parse it out of the response. Which is next to useless, because most Network Administrators & Firewalls will not allow an inbound TCP/UDP connection to the made to a user's machine. And it's not usually re-useable through the Firewall/Proxy.

Curious minds want to know, what is the goal your trying to achieve?

Regards, P1 :cool:
Posted on 2004-05-20 17:23:32 by Pone
He made his intentions clear in the very first line of text.

No, there isn't really any way to do this... you can try things like using SNMP to your router, but that's in no way a generic approach. grc.com's "agent" reports local IP addresses, and other people rely on PHP scripts on web servers. If you're designing some application, the best you can do is ask the user for the external IP, if needed.
Posted on 2004-05-20 19:25:58 by f0dder
dyndns (http://www.dyndns.org) runs a service, but it's really for its clients.

http://checkip.dyndns.org/
http://checkip.dyndns.org:8245/ <- avoids proxies
Posted on 2004-05-20 19:29:25 by stormix
hi,

I had the same problem before.
but after some research there is no programatical way to do so,
so you have to ask server that you send your request to send you your ip. He sees you external IP. so you can know your ISP IP address you where given.

There are servers that have do some services that when you send to it a request it replies with your IP. Known as mirrors. I don't know any server that do so.

http://www.whatismyipaddress.com/

see this link, and try to parse your IP from it.
so you got your external IP.

that's All,
Amr Turk
Posted on 2004-05-21 06:52:06 by amr
Try this one better and try, easier to parse your IP address.

http://checkip.dyndns.org/


Amr Turk
Posted on 2004-05-21 06:55:10 by amr
Hi,

you could use UPNP to retrieve your WAN IP address if your router supports UPNP.
If you launch a sniffer and starts MSN Messenger you can see in the trace that microsoft
is using this technique to retrieve your external IP.

/Hawk
Posted on 2004-05-21 07:05:21 by Hawk
Originally posted by f0dder He made his intentions clear in the very first line of text.
As you know, all information is relative. Intent of the transmitter, to the perception of the receiver.

I worked on campus of a state university, with a DHCP setup to lease IP addresses to the workstations on the WAN, where the IPs were the same as the Class B license held by the university for Internet access.

Understanding comes from the fact, that we may not know enough of the other person's experience, to validate the truth of it, until we experience it ourselves.

I'm glad for you, that it was clear to you. I hope that you may find kindness and understanding from others, in the things that are not clear to you, when you are trying to seek clarity.

Regards, P1 :cool:
Posted on 2004-05-21 11:28:13 by Pone
you could use UPNP to retrieve your WAN IP address if your router supports UPNP.


Sorry, but how can u explain in more details. I'm not so good in networks. so need more details.

Amr Turk
Posted on 2004-05-22 10:19:33 by amr
Pone, you talk too much
Posted on 2004-05-22 11:15:51 by comrade
this is the quick and dirty way i did it to retrieve the outter wanip , i also used dyndns.org cuz i figure theyll be around for a long time .. my code is a little sloppy but it works so i cant complain



; basically what i want to do is connect to [url]http://checkip.dyndns.org/[/url] and get the html source of the page
; into a string buffer which would look like
; <html><head><title>Current IP Check</title></head><body>Current IP Address: 68.9.70.154</body></html>
; i want to parse off the beginning junk up to the : in the string so it will look like
; 68.9.70.154</body></html>
; then i want to parse the </body></html> so i have the remaining ip address in the string
; colon db ":",0
; mov al,colon / cmp al,[esi]

.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

include \masm32\include\wsock32.inc
includelib \masm32\lib\wsock32.lib
includelib \masm32\lib\wininet.lib
include \masm32\include\wininet.inc

Download PROTO :DWORD
Stripper PROTO :DWORD, :DWORD
.data
url1 db "http://checkip.dyndns.org/",0
szIP db "Your WAN IP",0
.data?
buffer db 256 dup(?)
htmlBuffer db 256 dup(?)
.code
Start:

invoke Download,addr url1
invoke Stripper, ADDR htmlBuffer, ADDR buffer ; stripper ,the string to strip,the finished string
invoke MessageBox,0,addr buffer,addr szIP,0
invoke ExitProcess,0

Download PROC lpszURL:DWORD
local hInternet:DWORD
local hURL:DWORD
local hFile:DWORD
;local htmlBuffer[1024]:BYTE
local BufferLen:DWORD
local BytesWrite:DWORD
jmp @F
lpszAgent DB "Mozilla",0
@@:
invoke InternetOpen, addr lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0
mov hInternet, eax
invoke InternetOpenUrl, hInternet, lpszURL, 0, 0, 0, 0
mov hURL, eax
invoke InternetReadFile, hURL, addr htmlBuffer, sizeof htmlBuffer, addr BufferLen

invoke CloseHandle, hFile
invoke InternetCloseHandle, hURL
cmp eax, 0
jz done
invoke InternetCloseHandle, hInternet
xor eax, eax
inc eax
ret
done:
xor eax, eax
ret
Download ENDP

Stripper PROC uses esi edi ebx theString:DWORD, stripped:DWORD

mov esi, theString

@@:
cmp BYTE PTR [esi], 0
je _ERROR ;end of string and no :
cmp BYTE PTR [esi], ':'
je @f
inc esi
jmp @b ; if not found jump back up to @@
@@:
inc esi
inc esi ;one space before IP now esi should point to IP.

mov edi, stripped
@@:
lodsb
cmp al, 0
je NoMore
cmp al, '<'
je NoMore
stosb ; Store String Byte
jmp @b
_ERROR:
mov eax, -1 ;function returns -1 if no IP found
ret
NoMore:
inc edi
mov BYTE PTR [edi], 0;bug fix
mov eax, 0
ret
Stripper ENDP

end Start

:grin: :grin: :grin:
Posted on 2004-05-22 15:11:07 by illwill
Thanks, Illwill
but that what I have explained lately(without code), you supported my opinion with a working code. Very good.

But there still a problem, What is this UPNP? and how to use is to get external IP?

that's all,

Amr Turk
Posted on 2004-05-23 03:36:27 by amr


But there still a problem, What is this UPNP? and how to use is to get external IP?




UPNP=universal plug and play

Note: not all routers support UPNP and if they do you propably want to turn it off (for security reasons).

This is how it should work.

- Send a HTTP request to port 80 on the gateway:


POST /upnp/control3 HTTP/1.1
HOST: <Your gateway ip address>:80
CONTENT-LENGTH: 296
CONTENT-TYPE: text/xml; charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress"

<s:Envelope
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
</u:GetExternalIPAddress>
</s:Body>
</s:Envelope>

- If your gateway supports UPNP you would get a reply with some xml data.
in that data you will find a field with your WAN ip address.

<NewExternalIPAddress>xxx.xxx.xxx.xxx</NewExternalIPAddress>


/Hawk
Posted on 2004-05-23 07:39:28 by Hawk
Hawk, does not work for me, and I have it enabled. Are you sure its standard on all routers? I have D-Link DI-604 broadband router.
Posted on 2004-05-23 10:32:40 by comrade
It should work if your router supports upnp (D-link 604 does).
Maybe you missed something in your upnp request?
Here is the request that I used:

upnp_request db "POST /upnp/control3 HTTP/1.1",CR,LF
db "Host: 127.0.0.1:80",CR,LF
db "CONTENT-LENGTH: 296",CR,LF
db "CONTENT-TYPE: text/xml; charset=",34,"utf-8",34,CR,LF
db "SOAPACTION: ",34,"urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress",34,CR,LF,CR,LF
db "<s:Envelope",CR,LF
db "xmlns:s=",34,"http://schemas.xmlsoap.org/soap/envelope/",34,CR,LF
db "s:encodingStyle=",34,"http://schemas.xmlsoap.org/soap/encoding/",34,">",CR,LF
db "<s:Body>",CR,LF
db "<u:GetExternalIPAddress xmlns:u=",34,"urn:schemas-upnp-org:service:WANIPConnection:1",34,">",CR,LF
db "</u:GetExternalIPAddress>",CR,LF
db "</s:Body>",CR,LF
db "</s:Envelope>",CR,LF
db CR,LF

request_size equ $ - upnp_request

Send it to port 80 on your router and you should get the external ip back.

/Hawk
Posted on 2004-05-24 02:27:36 by Hawk
I modified Thomas blocking sockets client example from the Winsock Tutorial.
It now sends out the upnp request instead and prints the respons in the console.

syntax: wanipreq <your gateway ip-address>
eg. wanipreq 192.168.0.1

/Hawk
Posted on 2004-05-24 02:44:32 by Hawk



That is the response I have,

I have UPNP in my router I checked it out.

and please send the code with the executable,

And thanks for help,


Amr
Posted on 2004-05-24 12:54:06 by amr