News:

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

@Hutch regarding thread - How to find IP address

Started by dicky96, January 08, 2006, 10:44:56 PM

Previous topic - Next topic

dicky96

Hi Hutch

Re - Post - How to find my IP address.

You have removed (and locked) my thread regards how to find my own IP address

I'm sorry I have to start a new thread over this issue - you say I need to take this matter up with the person who is running the server but I do not understand why you have deleted my post. 

In this instance the person running the server is ME.  This server only allows my clients access if thier IP address is on the allowed list. I did not write the server application, but I do have a good understanding of how it operates.

In some cases my clients have problems accessing data from my server becauseof the security method it uses, and one of the common reasons is because thier DNS name is not current (ie pointing to a old (defunct) IP address).

I am writing a diagnostic program to help clients fix problems for themselves.

I do not understand why this would be against the rules of this forum - I have read (and re-read) the rules you posted in reply to my request and still do not understand why this question is "illegal"

Hutch, please understand I am not in anyway trying to challenge your authority.  I only come here seeking for genuine help.  Please could you reconsider my request, or at least explain why it is wrong for me to ask this question.

dicky

hutch--

Dicky,

Its not a personal matter but we have learnt the hard way to shoot first and ask questions later. Make sense of what you are doing so the moderators understand it properly and if its OK, it can go back.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dicky96

OK Hutch, in simple terms this is what I am trying to do -

1. Look up a DNS name and resolve it to it's IP.
2. Get my own IP address from within a winsock application.
3. Compare these two IP addresses to see if they are the same.


I did a search on this topic and found a pretty comprehensive tutorial at http://www.tangentsoft.net/wskfaq/ which has the following to say on the issue of resolving your own IP from within winsock:


------------------------------------------------------------------------------------------------------------
3.3 - How do I get my IP address from within a Winsock program?
There are three methods, which each have advantages and disadvantages:

The simplest method is to call getsockname() on a connected socket. If you don't have a connected socket, this method will either fail or will return useless or redundant information.

To get your address without opening a socket first, do a gethostbyname() on the value gethostname() returns. This will return a list of all the host's interfaces, as shown in this example. (See the example page for problems with the method.)

The third method only works on Winsock 2. The new WSAIoctl() API supports the SIO_GET_INTERFACE_LIST option, and one of the bits of information returned is the addresses of each of the network interfaces in the system. [C++ Example] (Again, see the example page for caveats.)
--------------------------------------------------------------------------------------------------------------------

I'm attempting to use method 1 (as it claims it is the simplest method  :toothy ) and I do have a connected socket to a remote server on the net.

Here is the snippet of code I have written in my attempt to achieve this...

.data
NoIpNameIP      dd ?
ActualIP         dd ?
MySockIP         sockaddr_in <>
UserIP         db 256 dup (?)   <-This string has been initialised to hold users dns name xyz.no-ip.com
WsNameLen      dd 16

WsCheckUserIp:
   invoke gethostbyname, addr UserIP       ;resolve No-Ip name into IP address
   .if eax!=NULL
      mov eax,[eax+12]                     ;move the value of h_list member into eax
      mov eax,[eax]                        ;copy the pointer to the actual IP address into eax
      mov eax,[eax]              ;copy users NO-IP name's resolved IP address into eax
      mov [NoIpNameIP],eax       ;Store the NO-IP IP address
      invoke getsockname, hSock,addr MySockIP,addr WsNameLen
         .if eax==0
          lea eax, MySockIP   ;point eax to start of sockaddr struct returned by getsockname
            mov eax,[eax+12]                     
            mov eax,[eax]                        
            mov eax,[eax]                    
            mov [ActualIP],eax          ;Store the Actual IP address
            mov eax,0            ;all went OK so return 0 :)
            ret
         .else
            mov eax,2   ;error code - can't resolve users current internet IP
            ret   
         .endif
   .else
      mov eax,1            ;error code - can't resolve No-Ip name
      ret
   .endif   


On exit from this subroutine, my program then goes on to compare the two IP address NoIpNameIP and ActualIP.

What's happening in reality is that the above code exits with no errors (eax==0) but the two IP addresses do not match, though I know they should.  Having traced this part of my code with ollydbg I can clearly see that getsockname is returning my local ethernet IP 0xC0A80003 or in other words 192.168.0.3, and not my internet IP which I wanted.

I hope you can see now I have explained this better, that my program is quite benign which is why I couldn't understand that I had broken any forum rules.

I appreciate this is more a winsock issue than an asm issue but I was hoping I could get some advice on this board.

dicky

PS - there used to be another forum at http://win32asmboard.cjb.net/ that had a specific winsock asm section, but that link appears to be broken.  Has that forum moved?


hutch--

Dicky,

Try this URL but I don't think there is a winsock section there any more.

http://win.asmcommunity.net/board/index.php
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dicky96

Hutch

Thanks for the link, they do indeed still have a networking section  :U and thank you for allowing me to explain myself properly. 

Maybe someone here can help me solve the above problem too  :wink

QvasiModo

Method 1 won't work unless you have connected a socket to a remote host; connecting to yourself only returns 127.0.0.1. Also that will give you the IP address for a single interface.

Method 2 is much better, I think. Here's an link to a page with some sample .NET code (but it's easy to port to assembly).

http://www.codeproject.com/csharp/network.asp

Hope that helps! :U

P1

Clipped from working code:  ( uses MASM32.lib )
Look up the functions and do the variables the way that makes sense to you.
szErrorMessage is main output of my error routine capturing key pieces of information from my software about the PC it's running on.  Note: it only collects the last byte of the network ip address seeing it's running on a local LAN. 

Regards,  P1  :8)


szComputerName   db 100h dup(?)
szIPaddress           db 010h dup(0)
szNumber           db "                    ",0

;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 szNumber
    invoke lstrcat, addr szComputerName, addr szErrorComputer
    invoke lstrcat, addr szComputerName, addr szNumber
.endif

;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
    .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,[eax+12]
        mov eax,[eax]
        mov eax,[eax]
        ;Temporary
        ;mov eax, 000000000h  ;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 szNumber  ;Preserve error number for output.
        invoke lstrcat, addr szIPaddress, addr szNumber
        invoke lstrcat, addr szErrorMessage, addr szIPaddress
        ;invoke MessageBox,0,addr szIPaddress,ADDR szMsgTitle,MB_OK
    .endif
    invoke WSACleanup   
.endif

Tedd

A little app for looking up hostnames..

To find your own, simply lookup "localhost" -- this will give your hostname (host.domain.some.foo.com) and local ip address (usually 127.0.0.1)
If you then lookup the hostname, this will list all ip addresses it should resolve to.

(And it also works for other hostnames, eg. "microsoft.com", "masmforum.com", etc)

[attachment deleted by admin]
No snowflake in an avalanche feels responsible.

dicky96

@Qvasimodo
Like you say method 1 needs a connected socket to a remote host - and that is exactly what I have.  Well at least I think that is what I have.........

Prior to the above code, my application does this:

WsConnectSock:
   invoke gethostbyname, eax      ;On entry EAX points to Server Name
   mov eax,[eax+12]                      ;move the value of h_list member into eax
   mov eax,[eax]                            ;copy the pointer to the actual IP address into eax
   mov eax,[eax]                            ;copy IP address into eax
   mov sin.sin_addr,eax
   mov sin.sin_family, AF_INET
   invoke htons, [UserPortNo]              ;convert port number into network byte order
   mov sin.sin_port,ax                     
   invoke connect,hSock,addr sin,sizeof sin
   ret   


Now that routine runs correctly as far as I can tell -  I've used ollydbg and set some breakpoint and I have examined the sockaddr_in structure just prior the the connect call, and  can clearly see that the IP address in sin.sin_addr is indeed the correct one for my host, and the port number is also correct.  Immediately after the connect routine returns I have the following

.if eax!=SOCKET_ERROR      ;Socket Connected to Primary Server OK
...............
...............
...............


I do not get a SOCKET_ERROR so can we all agree I do indeed have a successfully connected socket to a host server?


It is when I do a getsockname on this socket that Winsock returns my LAN IP address and not my Internet IP address.  Qvasi, according to what you are saying I am not doing anything wrong, so what is the problem? 

Is it something to do with the fact I am connected to the net via a router?

@everyone
Thanks for all the advice so far - I'll study that, but I'm still interested to know what is wrong with the method I am currently using.

dicky

P1

Quote from: dicky96 on January 09, 2006, 06:24:50 PMIt is when I do a getsockname on this socket that Winsock returns my LAN IP address and not my Internet IP address.  Qvasi, according to what you are saying I am not doing anything wrong, so what is the problem? 

Is it something to do with the fact I am connected to the net via a router?
Yes, you are running through a router/firewall with NAT on.  And some places have a Proxy Server setup to control internet access for users.

Is this a current project with IT?  As in Startup or Modifying an existing project?

Do you have access to the setup of the network equipment?

The other BBS has moved to here:  http://win.asmcommunity.net/board/index.php

Regards,  P1  :8)


dicky96

Hi PI
This is a project of my own, so I control the LAN/Router as it is my home LAN I am working on  :wink

I did just have a play around running ipconfig /all and it does appear my PCs can only see thier own LAN address, and not thier Internet IP.  Maybe this is why the methos I am using does not work.

Once I've got my software working it will be run on a vairiety for systems - most will use routers, but some will connect through a PC running ICS, and some will run the program on a PC connected directly to the net through a modem.  But all are Home LANS, this is just a hobbyist thing.  What I neeed is a method to determine my current internet IP in any of these situations.

If for instance I go to www.whatismyip.com then that does show me my Internet IP - all I need to figure out is how to get that internet IP  from within a Winsock program.  Or more likely I need someone else with more experience to figure out for me....  :wink

hope that helps

dicky

P1

dicky96,

You are going to run into a bunch of NAT issues because they change with each re-boot, depending on the LAN situation of your home network.

Which router/hub are you using?  Have you setup static NAT for your server yet?

Please bring us up to date on what you have going.  We might be able to suggest a better solution for your problem.

Regards,  P1  :8)


dicky96

OK guys
I've spent some time looking into this.

The "method 2" above that Qvasimodo said would be better, I think at least in principle (All though I have not really tested it out), would still return my LAN IP when I am accessing the net via a router

The little app that Tedd kindly posted is very similar to my own code in as much as it resolves DNS names to thier internet IP address - and that part of my program works

The code from P1 also - if I understand it properly - converts a DNS name to an internet IP address

I'm coming to the opinion that the only way to find out for sure what my current Internet IP is from within winsock is for me to write an external server (or something similar) that kinda "echo's back" my internet IP when I connect.  I'm beginning to think that only something external to my LAN can see what my Internet IP is.

Unless some guru's on here (and I am sure there are many) can educate me otherwise of course.

dicky

dicky96


dicky96

OK what I have "going" is this

First I have a server that allows clients to access it if thier IP address is on the allowed list.  This is how the security method for this server is implemented. I didn't write this server software, but I do have a good idea how it works.

Because some clients have dynamic IPs the server actually maintains a list of DNS names.  These names are resolved to thier IP (gethostbyname) every n minutes. Therefore when a client connects they can gain access - as long as thier dns name was current when the server last refreshed it'sclient list

Everything works OK as long as clients keep thier DNS names up to date...  but when we consider how dumb the average human is, and then allow for the fact that 50% are dumber than that..... well you can see where ithis is going   :bg

Also things sometimes other things go wrong on the server side.

What I am working on is a diagnostic program that clients can run that will do a series of tests - one of these tests is to check they have kept thier dns name current with thier IP.  Other tests involve sending good data packets to the server and checking the response against the expected reply, and so forth.

I hope that gives enough information so you could suggest any alternative strategies.

dicky