Page 1 of 1

IP Code

Posted: Fri Jan 15, 2010 12:04 am
by Stroggos
I'm making small chat client for a few friends and was wondering if there is a way of getting their IP's without them having to type it manually?

Re: IP Code

Posted: Fri Jan 15, 2010 1:34 am
by SFSxOI

Code: Select all

Structure CmdP_STARTUPINFO 
  cb.l 
  lpReserved.l 
  lpDesktop.l 
  lpTitle.l 
  dwX.l 
  dwY.l 
  dwXSize.l 
  dwYSize.l 
  dwXCountChars.l 
  dwYCountChars.l 
  dwFillAttribute.l 
  dwFlags.l 
  wShowWindow.w 
  cbReserved2.w 
  lpReserved2.l 
  hStdInput.l 
  hStdOutput.l 
  hStdError.l 
EndStructure

Global proc.PROCESS_INFORMATION, start.CmdP_STARTUPINFO, sa.SECURITY_ATTRIBUTES, hReadP.l, hWriteP.l, lngBytesread.l, strBuff.s=Space(256)

Procedure.s Rd_cmd(comnd_send.s)
sa\nLength =SizeOf(SECURITY_ATTRIBUTES)
sa\bInheritHandle = 1 
sa\lpSecurityDescriptor = 0 
ret = CreatePipe_(@hReadP, @hWriteP, @sa, 0) 

  If ret = 0 
    MessageRequester("Information", "CreateP failed. Error: ",0) 
    End 
  EndIf 

start\cb = SizeOf(CmdP_STARTUPINFO) 
start\dwFlags = #STARTF_USESHOWWINDOW | #STARTF_USESTDHANDLES 
start\hStdOutput = hWriteP 
start\hStdError = hWriteP 

ret = CreateProcess_(0, comnd_send, sa, sa, 1, #NORMAL_PRIORITY_CLASS, 0, 0, @start, @proc) 

  If ret <> 1 
    MessageRequester("Information","File Or command not found", 0) 
    End 
  EndIf 

ret = CloseHandle_(hWriteP) 

mOutputs.s = "" 

  While ret<>0 
    ret = ReadFile_(hReadP, strBuff, 255, @lngBytesread, 0) 
      If lngBytesread>0 
        mOutputs = mOutputs + Left(strBuff, lngBytesread) 
      EndIf 
  Wend 

ret = CloseHandle_(proc\hProcess) 
ret = CloseHandle_(proc\hThread) 
ret = CloseHandle_(hReadP) 

ProcedureReturn mOutputs
EndProcedure

If CreateFile(0, "IP.txt")
WriteString(0, Rd_cmd("ipconfig"))
CloseFile(0)
EndIf

Delay(10)
RunProgram("IP.txt")

Re: IP Code

Posted: Fri Jan 15, 2010 8:07 am
by Nituvious
This is what I used for an IP Validation system, it's since been abandoned.

Code: Select all

InitNetwork()
UserIPViewer$="http://h3x0r.ath.cx/Sonstiges/ShowMyIp12.php"
UserIP$ = StringField(GetHTTPHeader(UserIPViewer$), 2, Chr(34))
Debug UserIP$

Re: IP Code

Posted: Fri Jan 15, 2010 10:28 am
by Stroggos
Thanks to you both. You have been most helpfull

Re: IP Code

Posted: Fri Jan 15, 2010 11:12 am
by srod
@SFSxOI : nice code. However that is returning the internal IP address assigned by my router and not the external one. Any way of modifying your code? Nituvious' code gives the correct external IP address.

Re: IP Code

Posted: Fri Jan 15, 2010 1:14 pm
by blueznl
Your PC can not know its public IP adress if it is behind a NAT, as it cannot know how the nat translates the adress. The only option is to find a public machine on the internet which returns the IP adress to your program.

Re: IP Code

Posted: Fri Jan 15, 2010 1:19 pm
by srod
blueznl wrote:Your PC can not know its public IP adress if it is behind a NAT, as it cannot know how the nat translates the adress. The only option is to find a public machine on the internet which returns the IP adress to your program.
I understand; I figured that was probably the case. :)

Re: IP Code

Posted: Fri Jan 15, 2010 11:24 pm
by SFSxOI
srod wrote:@SFSxOI : nice code. However that is returning the internal IP address assigned by my router and not the external one. Any way of modifying your code? Nituvious' code gives the correct external IP address.
Well, Nituvious is getting his from an external source which means a dependance on the external source to tell you what you want to know. I haven't tried that with my code but its an idea and a good question. :) Just off the top of my head do this:

Code: Select all

replace the line: 
WriteString(0, Rd_cmd("ipconfig"))

with

WriteString(0, Rd_cmd("route print"))

you will get a whole bunch of info but one of those i think will be your external IP address. Maybe some sort of filtering to single it out?

Sort of interesting, now i want to play around some and figure something out.

Re: IP Code

Posted: Sat Jan 16, 2010 12:05 am
by UserOfPure
SFSxOI wrote:you will get a whole bunch of info but one of those i think will be your external IP address
Nope, not at all. :(

Re: IP Code

Posted: Sat Jan 16, 2010 12:31 am
by SFSxOI
OK, then its not "WriteString(0, Rd_cmd("route print"))"

I was just guessing anyway, i just got around to trying it also and your correct, so thats not it. I don't think you can do this via a command prompt, but I seem to remember something from a while back, around the win95 days, that by manipulating some commands you could do that but i just don't remember right now. Oh well.

EDIT: Oh wait, i remember now, it was telnet via command prompt. You could try to telnet (via cmd prompt) into some mail servers and when you try to connect they respond with the IP your connecting from which is the external IP address because thats what they would see. Dont know if it works anymore though, I would think not with all the focus on security.

Re: IP Code

Posted: Sun Jan 17, 2010 2:20 am
by idle
try this but you can't expect the host to keep this active

Code: Select all


Declare.s GetURL(mUrl.s)
  
Procedure.s GetURL(mUrl.s) 
  Static bonline.i,gServerID.i
  Protected mHost.s,mFile.s,Header.s,sOut.s,*buffer 
  
  *buffer = AllocateMemory(1200)
  
  i = FindString(mUrl,"/",1)
  If i 
    mFile = Right(mUrl,Len(mUrl)-i)
    mHost = Left(mUrl,i-1)
  Else
    mHost = mUrl
    mfile = mUrl
  EndIf
  
    
  If bOnline = 0
     gServerID = OpenNetworkConnection(mHost,80)
     
     If Not gServerID 
       MessageRequester("Network Problem","Can't establish connection")
     EndIf 
    
  EndIf 
     
    Timeout = ElapsedMilliseconds() + 5000
        
    If gServerID
       
      Header = "GET /"+ mFile + " HTTP/1.1"+#CRLF$
      Header + "Host: "+ mHost + #CRLF$+#CRLF$
            
      
      SendNetworkData(gServerID,@Header,Len(Header)) 
      
      Repeat 
        Delay(2) 
      Until NetworkClientEvent(gServerID) = #PB_NetworkEvent_Data And ElapsedMilliseconds() < Timeout
      
      If ElapsedMilliseconds() < Timeout  
          
       
        Repeat 
          lt = ReceiveNetworkData(gServerID,*Buffer,1200)
          sout + PeekS(*Buffer,lt)
        Until DataLength = 0
      EndIf 
     
      pos = FindString(Sout,#CRLF$ + #CRLF$,1)
      
      If pos <> 0 
       pos+3     
       sout = Right(Sout,Len(sout)-pos)  
     
      EndIf 
     
     CloseNetworkConnection(gServerID)
  
  EndIf  
  
  FreeMemory(*Buffer) 
    
  ProcedureReturn sOut
  
  
EndProcedure 

InitNetwork()
Debug GetUrl("ipecho.net/plain.php")



To host it h\your self just put this up on your website and save it as .php

Code: Select all

<?php
echo $_SERVER['HTTP_REFERER'];
?>

Re: IP Code

Posted: Sun Jan 17, 2010 4:45 pm
by SFSxOI
idle, your code drops the first number of the returned IP address. For example;

75.66.198.25 will come bck as 5.66.198.25 < the 7 is missing. I had to change this to get it to display correctly:
'

Code: Select all


If pos <> 0 
       pos+3     ; < changed from 4 to 3
       sout = Right(Sout,Len(sout)-pos)
      
      EndIf

Re: IP Code

Posted: Mon Jan 18, 2010 5:07 am
by idle
thanks edited post