IP Code

Just starting out? Need help? Post your questions and find answers here.
Stroggos
User
User
Posts: 12
Joined: Thu Apr 10, 2008 11:32 am
Location: Adelaide, Australia

IP Code

Post 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?
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: IP Code

Post 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")
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Nituvious
Addict
Addict
Posts: 1030
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: IP Code

Post 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$
▓▓▓▓▓▒▒▒▒▒░░░░░
Stroggos
User
User
Posts: 12
Joined: Thu Apr 10, 2008 11:32 am
Location: Adelaide, Australia

Re: IP Code

Post by Stroggos »

Thanks to you both. You have been most helpfull
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: IP Code

Post 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.
I may look like a mule, but I'm not a complete ass.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: IP Code

Post 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.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: IP Code

Post 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. :)
I may look like a mule, but I'm not a complete ass.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: IP Code

Post 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.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: IP Code

Post 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. :(
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: IP Code

Post 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.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
idle
Always Here
Always Here
Posts: 6108
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: IP Code

Post 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'];
?>
Last edited by idle on Mon Jan 18, 2010 5:07 am, edited 1 time in total.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: IP Code

Post 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
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
idle
Always Here
Always Here
Posts: 6108
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: IP Code

Post by idle »

thanks edited post
Post Reply